diff --git a/SDWebImage/Core/SDWebImageDownloaderRequestModifier.h b/SDWebImage/Core/SDWebImageDownloaderRequestModifier.h index eff38844..0533b4e5 100644 --- a/SDWebImage/Core/SDWebImageDownloaderRequestModifier.h +++ b/SDWebImage/Core/SDWebImageDownloaderRequestModifier.h @@ -40,20 +40,30 @@ typedef NSURLRequest * _Nullable (^SDWebImageDownloaderRequestModifierBlock)(NSU @end /** - A convenient request modifier to provide the HTTP request including HTTP method, headers and body. - */ -@interface SDWebImageDownloaderHTTPRequestModifier : NSObject +A convenient request modifier to provide the HTTP request including HTTP Method, Headers and Body. +*/ +@interface SDWebImageDownloaderRequestModifier (HTTPConveniences) -/// Create the request modifier with HTTP Headers +/// Create the request modifier with HTTP Method. +/// @param method HTTP Method, nil means to GET. +/// @note This is for convenience, if you need code to control the logic, use block API instead. +- (nonnull instancetype)initWithMethod:(nullable NSString *)method; + +/// Create the request modifier with HTTP Headers. /// @param headers HTTP Headers. Case insensitive according to HTTP/1.1(HTTP/2) standard. The headers will overide the same fileds from original request. -/// @note This is for convenience, if you need code to control the logic, use `SDWebImageDownloaderRequestModifier` instead +/// @note This is for convenience, if you need code to control the logic, use block API instead. - (nonnull instancetype)initWithHeaders:(nullable NSDictionary *)headers; -/// Create the request modifier with HTTP Method, Headers and Body +/// Create the request modifier with HTTP Body. +/// @param body HTTP Body. +/// @note This is for convenience, if you need code to control the logic, use block API instead. +- (nonnull instancetype)initWithBody:(nullable NSData *)body; + +/// Create the request modifier with HTTP Method, Headers and Body. /// @param method HTTP Method, nil means to GET. /// @param headers HTTP Headers. Case insensitive according to HTTP/1.1(HTTP/2) standard. The headers will overide the same fileds from original request. -/// @param body HTTP Body -/// @note This is for convenience, if you need code to control the logic, use `SDWebImageDownloaderRequestModifier` instead +/// @param body HTTP Body. +/// @note This is for convenience, if you need code to control the logic, use block API instead. - (nonnull instancetype)initWithMethod:(nullable NSString *)method headers:(nullable NSDictionary *)headers body:(nullable NSData *)body; @end diff --git a/SDWebImage/Core/SDWebImageDownloaderRequestModifier.m b/SDWebImage/Core/SDWebImageDownloaderRequestModifier.m index 8ce2c893..da36dadd 100644 --- a/SDWebImage/Core/SDWebImageDownloaderRequestModifier.m +++ b/SDWebImage/Core/SDWebImageDownloaderRequestModifier.m @@ -38,39 +38,34 @@ @end -@interface SDWebImageDownloaderHTTPRequestModifier () +@implementation SDWebImageDownloaderRequestModifier (HTTPConveniences) -@property (nonatomic, copy, nullable) NSString *method; -@property (nonatomic, copy, nullable) NSDictionary *headers; -@property (nonatomic, copy, nullable) NSData *body; - -@end - -@implementation SDWebImageDownloaderHTTPRequestModifier +- (instancetype)initWithMethod:(NSString *)method { + return [self initWithMethod:method headers:nil body:nil]; +} - (instancetype)initWithHeaders:(NSDictionary *)headers { return [self initWithMethod:nil headers:headers body:nil]; } -- (instancetype)initWithMethod:(NSString *)method headers:(NSDictionary *)headers body:(NSData *)body { - self = [super init]; - if (self) { - _method = [method copy]; - _headers = [headers copy]; - _body = [body copy]; - } - return self; +- (instancetype)initWithBody:(NSData *)body { + return [self initWithMethod:nil headers:nil body:body]; } -- (NSURLRequest *)modifiedRequestWithRequest:(NSURLRequest *)request { - NSMutableURLRequest *mutableRequest = [request mutableCopy]; - mutableRequest.HTTPMethod = self.method; - mutableRequest.HTTPBody = self.body; - for (NSString *header in self.headers) { - NSString *value = self.headers[header]; - [mutableRequest setValue:value forHTTPHeaderField:header]; - } - return [mutableRequest copy]; +- (instancetype)initWithMethod:(NSString *)method headers:(NSDictionary *)headers body:(NSData *)body { + method = [method copy]; + headers = [headers copy]; + body = [body copy]; + return [self initWithBlock:^NSURLRequest * _Nullable(NSURLRequest * _Nonnull request) { + NSMutableURLRequest *mutableRequest = [request mutableCopy]; + mutableRequest.HTTPMethod = method; + mutableRequest.HTTPBody = body; + for (NSString *header in headers) { + NSString *value = headers[header]; + [mutableRequest setValue:value forHTTPHeaderField:header]; + } + return [mutableRequest copy]; + }]; } @end diff --git a/SDWebImage/Core/SDWebImageDownloaderResponseModifier.h b/SDWebImage/Core/SDWebImageDownloaderResponseModifier.h index 6ddf6563..e2a036e5 100644 --- a/SDWebImage/Core/SDWebImageDownloaderResponseModifier.h +++ b/SDWebImage/Core/SDWebImageDownloaderResponseModifier.h @@ -40,20 +40,30 @@ typedef NSURLResponse * _Nullable (^SDWebImageDownloaderResponseModifierBlock)(N @end /** - A convenient response modifier to provide the HTTP response including HTTP method, headers and body. - */ -@interface SDWebImageDownloaderHTTPResponseModifier : NSObject +A convenient response modifier to provide the HTTP response including HTTP Status Code, Version and Headers. +*/ +@interface SDWebImageDownloaderResponseModifier (HTTPConveniences) + +/// Create the response modifier with HTTP Status code. +/// @param statusCode HTTP Status Code. +/// @note This is for convenience, if you need code to control the logic, use block API instead. +- (nonnull instancetype)initWithStatusCode:(NSInteger)statusCode; + +/// Create the response modifier with HTTP Version. Status code defaults to 200. +/// @param version HTTP Version, nil means "HTTP/1.1". +/// @note This is for convenience, if you need code to control the logic, use block API instead. +- (nonnull instancetype)initWithVersion:(nullable NSString *)version; /// Create the response modifier with HTTP Headers. Status code defaults to 200. /// @param headers HTTP Headers. Case insensitive according to HTTP/1.1(HTTP/2) standard. The headers will overide the same fileds from original response. -/// @note This is for convenience, if you need code to control the logic, use `SDWebImageDownloaderResponseModifier` instead +/// @note This is for convenience, if you need code to control the logic, use block API instead. - (nonnull instancetype)initWithHeaders:(nullable NSDictionary *)headers; -/// Create the response modifier with HTTP Version, Status Code and Headers -/// @param version HTTP Version, nil means "HTTP/1.1" -/// @param statusCode HTTP Status Code +/// Create the response modifier with HTTP Status Code, Version and Headers. +/// @param statusCode HTTP Status Code. +/// @param version HTTP Version, nil means "HTTP/1.1". /// @param headers HTTP Headers. Case insensitive according to HTTP/1.1(HTTP/2) standard. The headers will overide the same fileds from original response. -/// @note This is for convenience, if you need code to control the logic, use `SDWebImageDownloaderResponseModifier` instead -- (nonnull instancetype)initWithVersion:(nullable NSString *)version statusCode:(NSInteger)statusCode headers:(nullable NSDictionary *)headers; +/// @note This is for convenience, if you need code to control the logic, use block API instead. +- (nonnull instancetype)initWithStatusCode:(NSInteger)statusCode version:(nullable NSString *)version headers:(nullable NSDictionary *)headers; @end diff --git a/SDWebImage/Core/SDWebImageDownloaderResponseModifier.m b/SDWebImage/Core/SDWebImageDownloaderResponseModifier.m index c072082e..309ae984 100644 --- a/SDWebImage/Core/SDWebImageDownloaderResponseModifier.m +++ b/SDWebImage/Core/SDWebImageDownloaderResponseModifier.m @@ -39,41 +39,35 @@ @end -@interface SDWebImageDownloaderHTTPResponseModifier () +@implementation SDWebImageDownloaderResponseModifier (HTTPConveniences) -@property (nonatomic, copy, nullable) NSString *version; -@property (nonatomic) NSInteger statusCode; -@property (nonatomic, copy, nullable) NSDictionary *headers; +- (instancetype)initWithStatusCode:(NSInteger)statusCode { + return [self initWithStatusCode:statusCode version:nil headers:nil]; +} -@end - -@implementation SDWebImageDownloaderHTTPResponseModifier +- (instancetype)initWithVersion:(NSString *)version { + return [self initWithStatusCode:200 version:version headers:nil]; +} - (instancetype)initWithHeaders:(NSDictionary *)headers { - return [self initWithVersion:nil statusCode:200 headers:headers]; + return [self initWithStatusCode:200 version:nil headers:headers]; } -- (instancetype)initWithVersion:(NSString *)version statusCode:(NSInteger)statusCode headers:(NSDictionary *)headers { - self = [super init]; - if (self) { - _version = [version copy]; - _statusCode = statusCode; - _headers = [headers copy]; - } - return self; -} - -- (NSURLResponse *)modifiedResponseWithResponse:(NSURLResponse *)response { - if (![response isKindOfClass:NSHTTPURLResponse.class]) { - return response; - } - NSMutableDictionary *mutableHeaders = [((NSHTTPURLResponse *)response).allHeaderFields mutableCopy]; - for (NSString *header in self.headers) { - NSString *value = self.headers[header]; - mutableHeaders[header] = value; - } - NSHTTPURLResponse *httpResponse = [[NSHTTPURLResponse alloc] initWithURL:response.URL statusCode:self.statusCode HTTPVersion:self.version ?: @"HTTP/1.1" headerFields:[mutableHeaders copy]]; - return httpResponse; +- (instancetype)initWithStatusCode:(NSInteger)statusCode version:(NSString *)version headers:(NSDictionary *)headers { + version = version ? [version copy] : @"HTTP/1.1"; + headers = [headers copy]; + return [self initWithBlock:^NSURLResponse * _Nullable(NSURLResponse * _Nonnull response) { + if (![response isKindOfClass:NSHTTPURLResponse.class]) { + return response; + } + NSMutableDictionary *mutableHeaders = [((NSHTTPURLResponse *)response).allHeaderFields mutableCopy]; + for (NSString *header in headers) { + NSString *value = headers[header]; + mutableHeaders[header] = value; + } + NSHTTPURLResponse *httpResponse = [[NSHTTPURLResponse alloc] initWithURL:response.URL statusCode:statusCode HTTPVersion:version headerFields:[mutableHeaders copy]]; + return httpResponse; + }]; } @end