From 6379dfc58ba9e7a48515d50016314fe9395f3360 Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Fri, 24 Apr 2020 12:04:54 +0800 Subject: [PATCH 1/5] Add the convenient request/response modifier, which provide HTTP header directly, don't need to repeat the code --- .../SDWebImageDownloaderRequestModifier.h | 21 ++++++++++ .../SDWebImageDownloaderRequestModifier.m | 38 ++++++++++++++++++ .../SDWebImageDownloaderResponseModifier.h | 21 ++++++++++ .../SDWebImageDownloaderResponseModifier.m | 40 +++++++++++++++++++ 4 files changed, 120 insertions(+) diff --git a/SDWebImage/Core/SDWebImageDownloaderRequestModifier.h b/SDWebImage/Core/SDWebImageDownloaderRequestModifier.h index eabdf61d..016ef309 100644 --- a/SDWebImage/Core/SDWebImageDownloaderRequestModifier.h +++ b/SDWebImage/Core/SDWebImageDownloaderRequestModifier.h @@ -33,3 +33,24 @@ typedef NSURLRequest * _Nullable (^SDWebImageDownloaderRequestModifierBlock)(NSU + (nonnull instancetype)requestModifierWithBlock:(nonnull SDWebImageDownloaderRequestModifierBlock)block; @end + +/** + A convenient request modifier to provide the HTTP request including HTTP method, headers and body. + */ +@interface SDWebImageDownloaderHTTPRequestModifier : NSObject + +/// 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 +- (nonnull instancetype)initWithMethod:(nullable NSString *)method headers:(nullable NSDictionary *)headers body:(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 SDWebImageDownloader global configuration. +/// @param body HTTP Body +/// @note This is for convenience, if you need code to control the logic, use `SDWebImageDownloaderRequestModifier` instead ++ (nonnull instancetype)requestModifierWithMethod:(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 22d044af..f80db2e9 100644 --- a/SDWebImage/Core/SDWebImageDownloaderRequestModifier.m +++ b/SDWebImage/Core/SDWebImageDownloaderRequestModifier.m @@ -37,3 +37,41 @@ } @end + +@interface SDWebImageDownloaderHTTPRequestModifier () + +@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 headers:(NSDictionary *)headers body:(NSData *)body { + self = [super init]; + if (self) { + _method = [method copy]; + _headers = [headers copy]; + _body = [body copy]; + } + return self; +} + ++ (instancetype)requestModifierWithMethod:(NSString *)method headers:(NSDictionary *)headers body:(NSData *)body { + SDWebImageDownloaderHTTPRequestModifier *requestModifier = [[SDWebImageDownloaderHTTPRequestModifier alloc] initWithMethod:method headers:headers body:body]; + return requestModifier; +} + +- (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]; +} + +@end diff --git a/SDWebImage/Core/SDWebImageDownloaderResponseModifier.h b/SDWebImage/Core/SDWebImageDownloaderResponseModifier.h index a8bcc0b5..60b46020 100644 --- a/SDWebImage/Core/SDWebImageDownloaderResponseModifier.h +++ b/SDWebImage/Core/SDWebImageDownloaderResponseModifier.h @@ -33,3 +33,24 @@ typedef NSURLResponse * _Nullable (^SDWebImageDownloaderResponseModifierBlock)(N + (nonnull instancetype)responseModifierWithBlock:(nonnull SDWebImageDownloaderResponseModifierBlock)block; @end + +/** + A convenient response modifier to provide the HTTP response including HTTP method, headers and body. + */ +@interface SDWebImageDownloaderHTTPResponseModifier : NSObject + +/// 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 +/// @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; + +/// Create the response modifier with HTTP Version, Status Code and Headers +/// @param version HTTP Version, nil means "HTTP/1.1". See available value in CFNetwork like `kCFHTTPVersion1_1`. +/// @param statusCode HTTP Status Code +/// @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)responseModifierWithVersion:(nullable NSString *)version statusCode:(NSInteger)statusCode headers:(nullable NSDictionary *)headers; + +@end diff --git a/SDWebImage/Core/SDWebImageDownloaderResponseModifier.m b/SDWebImage/Core/SDWebImageDownloaderResponseModifier.m index 0894b953..7388b83a 100644 --- a/SDWebImage/Core/SDWebImageDownloaderResponseModifier.m +++ b/SDWebImage/Core/SDWebImageDownloaderResponseModifier.m @@ -38,3 +38,43 @@ } @end + +@interface SDWebImageDownloaderHTTPResponseModifier () + +@property (nonatomic, copy, nullable) NSString *version; +@property (nonatomic) NSInteger statusCode; +@property (nonatomic, copy, nullable) NSDictionary *headers; + +@end + +@implementation SDWebImageDownloaderHTTPResponseModifier + +- (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; +} + ++ (instancetype)responseModifierWithVersion:(NSString *)version statusCode:(NSInteger)statusCode headers:(NSDictionary *)headers { + SDWebImageDownloaderHTTPResponseModifier *responseModifier = [[SDWebImageDownloaderHTTPResponseModifier alloc] initWithVersion:version statusCode:statusCode headers:headers]; + return responseModifier; +} + +- (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 ?: (__bridge NSString *)kCFHTTPVersion1_1 headerFields:[mutableHeaders copy]]; + return httpResponse; +} + +@end From 484f6f422c8f3e2b480ef42d65b30cacac0cab23 Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Fri, 24 Apr 2020 12:19:51 +0800 Subject: [PATCH 2/5] Add the only headers arg convenience initializer --- .../Core/SDWebImageDownloaderRequestModifier.h | 14 ++++++-------- .../Core/SDWebImageDownloaderRequestModifier.m | 9 ++++----- .../Core/SDWebImageDownloaderResponseModifier.h | 14 ++++++-------- .../Core/SDWebImageDownloaderResponseModifier.m | 9 ++++----- 4 files changed, 20 insertions(+), 26 deletions(-) diff --git a/SDWebImage/Core/SDWebImageDownloaderRequestModifier.h b/SDWebImage/Core/SDWebImageDownloaderRequestModifier.h index 016ef309..8928cf76 100644 --- a/SDWebImage/Core/SDWebImageDownloaderRequestModifier.h +++ b/SDWebImage/Core/SDWebImageDownloaderRequestModifier.h @@ -39,18 +39,16 @@ typedef NSURLRequest * _Nullable (^SDWebImageDownloaderRequestModifierBlock)(NSU */ @interface SDWebImageDownloaderHTTPRequestModifier : NSObject +/// 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 +- (nonnull instancetype)initWithHeaders:(nullable NSDictionary *)headers; + /// 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 -- (nonnull instancetype)initWithMethod:(nullable NSString *)method headers:(nullable NSDictionary *)headers body:(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 SDWebImageDownloader global configuration. -/// @param body HTTP Body -/// @note This is for convenience, if you need code to control the logic, use `SDWebImageDownloaderRequestModifier` instead -+ (nonnull instancetype)requestModifierWithMethod:(nullable NSString *)method headers:(nullable NSDictionary *)headers body:(nullable NSData *)body; +- (nonnull instancetype)initWithMethod:(nullable NSString *)method headers:(nullable NSDictionary *)headers body:(nullable NSData *)body NS_DESIGNATED_INITIALIZER; @end diff --git a/SDWebImage/Core/SDWebImageDownloaderRequestModifier.m b/SDWebImage/Core/SDWebImageDownloaderRequestModifier.m index f80db2e9..8ce2c893 100644 --- a/SDWebImage/Core/SDWebImageDownloaderRequestModifier.m +++ b/SDWebImage/Core/SDWebImageDownloaderRequestModifier.m @@ -48,6 +48,10 @@ @implementation SDWebImageDownloaderHTTPRequestModifier +- (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) { @@ -58,11 +62,6 @@ return self; } -+ (instancetype)requestModifierWithMethod:(NSString *)method headers:(NSDictionary *)headers body:(NSData *)body { - SDWebImageDownloaderHTTPRequestModifier *requestModifier = [[SDWebImageDownloaderHTTPRequestModifier alloc] initWithMethod:method headers:headers body:body]; - return requestModifier; -} - - (NSURLRequest *)modifiedRequestWithRequest:(NSURLRequest *)request { NSMutableURLRequest *mutableRequest = [request mutableCopy]; mutableRequest.HTTPMethod = self.method; diff --git a/SDWebImage/Core/SDWebImageDownloaderResponseModifier.h b/SDWebImage/Core/SDWebImageDownloaderResponseModifier.h index 60b46020..1032f464 100644 --- a/SDWebImage/Core/SDWebImageDownloaderResponseModifier.h +++ b/SDWebImage/Core/SDWebImageDownloaderResponseModifier.h @@ -39,18 +39,16 @@ typedef NSURLResponse * _Nullable (^SDWebImageDownloaderResponseModifierBlock)(N */ @interface SDWebImageDownloaderHTTPResponseModifier : NSObject +/// 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 +- (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 /// @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; - -/// Create the response modifier with HTTP Version, Status Code and Headers -/// @param version HTTP Version, nil means "HTTP/1.1". See available value in CFNetwork like `kCFHTTPVersion1_1`. -/// @param statusCode HTTP Status Code -/// @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)responseModifierWithVersion:(nullable NSString *)version statusCode:(NSInteger)statusCode headers:(nullable NSDictionary *)headers; +- (nonnull instancetype)initWithVersion:(nullable NSString *)version statusCode:(NSInteger)statusCode headers:(nullable NSDictionary *)headers NS_DESIGNATED_INITIALIZER; @end diff --git a/SDWebImage/Core/SDWebImageDownloaderResponseModifier.m b/SDWebImage/Core/SDWebImageDownloaderResponseModifier.m index 7388b83a..e38f5d41 100644 --- a/SDWebImage/Core/SDWebImageDownloaderResponseModifier.m +++ b/SDWebImage/Core/SDWebImageDownloaderResponseModifier.m @@ -49,6 +49,10 @@ @implementation SDWebImageDownloaderHTTPResponseModifier +- (instancetype)initWithHeaders:(NSDictionary *)headers { + return [self initWithVersion:nil statusCode:200 headers:headers]; +} + - (instancetype)initWithVersion:(NSString *)version statusCode:(NSInteger)statusCode headers:(NSDictionary *)headers { self = [super init]; if (self) { @@ -59,11 +63,6 @@ return self; } -+ (instancetype)responseModifierWithVersion:(NSString *)version statusCode:(NSInteger)statusCode headers:(NSDictionary *)headers { - SDWebImageDownloaderHTTPResponseModifier *responseModifier = [[SDWebImageDownloaderHTTPResponseModifier alloc] initWithVersion:version statusCode:statusCode headers:headers]; - return responseModifier; -} - - (NSURLResponse *)modifiedResponseWithResponse:(NSURLResponse *)response { if (![response isKindOfClass:NSHTTPURLResponse.class]) { return response; From fb517edf3770fd20b69db51e0b8be94b1d84e3c8 Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Fri, 24 Apr 2020 18:13:26 +0800 Subject: [PATCH 3/5] Use the String instead of CFNetwork symbol, seems not visible on watchOS ;) --- SDWebImage/Core/SDWebImageDownloaderDecryptor.h | 5 +++++ SDWebImage/Core/SDWebImageDownloaderRequestModifier.h | 7 ++++++- SDWebImage/Core/SDWebImageDownloaderResponseModifier.h | 7 ++++++- SDWebImage/Core/SDWebImageDownloaderResponseModifier.m | 2 +- 4 files changed, 18 insertions(+), 3 deletions(-) diff --git a/SDWebImage/Core/SDWebImageDownloaderDecryptor.h b/SDWebImage/Core/SDWebImageDownloaderDecryptor.h index 184d4f85..682bc933 100644 --- a/SDWebImage/Core/SDWebImageDownloaderDecryptor.h +++ b/SDWebImage/Core/SDWebImageDownloaderDecryptor.h @@ -30,7 +30,12 @@ A downloader response modifier class with block. */ @interface SDWebImageDownloaderDecryptor : NSObject +/// Create the data decryptor with block +/// @param block A block to control decrypt logic - (nonnull instancetype)initWithBlock:(nonnull SDWebImageDownloaderDecryptorBlock)block; + +/// Create the data decryptor with block +/// @param block A block to control decrypt logic + (nonnull instancetype)decryptorWithBlock:(nonnull SDWebImageDownloaderDecryptorBlock)block; @end diff --git a/SDWebImage/Core/SDWebImageDownloaderRequestModifier.h b/SDWebImage/Core/SDWebImageDownloaderRequestModifier.h index 8928cf76..eff38844 100644 --- a/SDWebImage/Core/SDWebImageDownloaderRequestModifier.h +++ b/SDWebImage/Core/SDWebImageDownloaderRequestModifier.h @@ -29,7 +29,12 @@ typedef NSURLRequest * _Nullable (^SDWebImageDownloaderRequestModifierBlock)(NSU */ @interface SDWebImageDownloaderRequestModifier : NSObject +/// Create the request modifier with block +/// @param block A block to control modifier logic - (nonnull instancetype)initWithBlock:(nonnull SDWebImageDownloaderRequestModifierBlock)block; + +/// Create the request modifier with block +/// @param block A block to control modifier logic + (nonnull instancetype)requestModifierWithBlock:(nonnull SDWebImageDownloaderRequestModifierBlock)block; @end @@ -49,6 +54,6 @@ typedef NSURLRequest * _Nullable (^SDWebImageDownloaderRequestModifierBlock)(NSU /// @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 -- (nonnull instancetype)initWithMethod:(nullable NSString *)method headers:(nullable NSDictionary *)headers body:(nullable NSData *)body NS_DESIGNATED_INITIALIZER; +- (nonnull instancetype)initWithMethod:(nullable NSString *)method headers:(nullable NSDictionary *)headers body:(nullable NSData *)body; @end diff --git a/SDWebImage/Core/SDWebImageDownloaderResponseModifier.h b/SDWebImage/Core/SDWebImageDownloaderResponseModifier.h index 1032f464..6ddf6563 100644 --- a/SDWebImage/Core/SDWebImageDownloaderResponseModifier.h +++ b/SDWebImage/Core/SDWebImageDownloaderResponseModifier.h @@ -29,7 +29,12 @@ typedef NSURLResponse * _Nullable (^SDWebImageDownloaderResponseModifierBlock)(N */ @interface SDWebImageDownloaderResponseModifier : NSObject +/// Create the response modifier with block +/// @param block A block to control modifier logic - (nonnull instancetype)initWithBlock:(nonnull SDWebImageDownloaderResponseModifierBlock)block; + +/// Create the response modifier with block +/// @param block A block to control modifier logic + (nonnull instancetype)responseModifierWithBlock:(nonnull SDWebImageDownloaderResponseModifierBlock)block; @end @@ -49,6 +54,6 @@ typedef NSURLResponse * _Nullable (^SDWebImageDownloaderResponseModifierBlock)(N /// @param statusCode HTTP Status Code /// @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 NS_DESIGNATED_INITIALIZER; +- (nonnull instancetype)initWithVersion:(nullable NSString *)version statusCode:(NSInteger)statusCode headers:(nullable NSDictionary *)headers; @end diff --git a/SDWebImage/Core/SDWebImageDownloaderResponseModifier.m b/SDWebImage/Core/SDWebImageDownloaderResponseModifier.m index e38f5d41..c072082e 100644 --- a/SDWebImage/Core/SDWebImageDownloaderResponseModifier.m +++ b/SDWebImage/Core/SDWebImageDownloaderResponseModifier.m @@ -72,7 +72,7 @@ NSString *value = self.headers[header]; mutableHeaders[header] = value; } - NSHTTPURLResponse *httpResponse = [[NSHTTPURLResponse alloc] initWithURL:response.URL statusCode:self.statusCode HTTPVersion:self.version ?: (__bridge NSString *)kCFHTTPVersion1_1 headerFields:[mutableHeaders copy]]; + NSHTTPURLResponse *httpResponse = [[NSHTTPURLResponse alloc] initWithURL:response.URL statusCode:self.statusCode HTTPVersion:self.version ?: @"HTTP/1.1" headerFields:[mutableHeaders copy]]; return httpResponse; } From 783353bfe9af2f6ec69f17c1ea572ee7e2ad1d14 Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Fri, 24 Apr 2020 18:54:16 +0800 Subject: [PATCH 4/5] Update the convienient API using the Category, actually most use case is HTTP request, data and file URL are rare --- .../SDWebImageDownloaderRequestModifier.h | 26 +++++++--- .../SDWebImageDownloaderRequestModifier.m | 45 +++++++--------- .../SDWebImageDownloaderResponseModifier.h | 28 ++++++---- .../SDWebImageDownloaderResponseModifier.m | 52 ++++++++----------- 4 files changed, 80 insertions(+), 71 deletions(-) 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 From cc3f03dfa4c07fce74f894e6bd509c438da24e2e Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Mon, 27 Apr 2020 12:10:56 +0800 Subject: [PATCH 5/5] Add the test case about conveniences modifier, rename the category name --- .../Core/SDWebImageDownloaderRequestModifier.h | 2 +- .../Core/SDWebImageDownloaderRequestModifier.m | 2 +- .../SDWebImageDownloaderResponseModifier.h | 2 +- .../SDWebImageDownloaderResponseModifier.m | 2 +- Tests/Tests/SDWebImageDownloaderTests.m | 18 ++++++++++++++++-- 5 files changed, 20 insertions(+), 6 deletions(-) diff --git a/SDWebImage/Core/SDWebImageDownloaderRequestModifier.h b/SDWebImage/Core/SDWebImageDownloaderRequestModifier.h index 0533b4e5..67f17992 100644 --- a/SDWebImage/Core/SDWebImageDownloaderRequestModifier.h +++ b/SDWebImage/Core/SDWebImageDownloaderRequestModifier.h @@ -42,7 +42,7 @@ typedef NSURLRequest * _Nullable (^SDWebImageDownloaderRequestModifierBlock)(NSU /** A convenient request modifier to provide the HTTP request including HTTP Method, Headers and Body. */ -@interface SDWebImageDownloaderRequestModifier (HTTPConveniences) +@interface SDWebImageDownloaderRequestModifier (Conveniences) /// Create the request modifier with HTTP Method. /// @param method HTTP Method, nil means to GET. diff --git a/SDWebImage/Core/SDWebImageDownloaderRequestModifier.m b/SDWebImage/Core/SDWebImageDownloaderRequestModifier.m index da36dadd..7b81a40c 100644 --- a/SDWebImage/Core/SDWebImageDownloaderRequestModifier.m +++ b/SDWebImage/Core/SDWebImageDownloaderRequestModifier.m @@ -38,7 +38,7 @@ @end -@implementation SDWebImageDownloaderRequestModifier (HTTPConveniences) +@implementation SDWebImageDownloaderRequestModifier (Conveniences) - (instancetype)initWithMethod:(NSString *)method { return [self initWithMethod:method headers:nil body:nil]; diff --git a/SDWebImage/Core/SDWebImageDownloaderResponseModifier.h b/SDWebImage/Core/SDWebImageDownloaderResponseModifier.h index e2a036e5..8c530688 100644 --- a/SDWebImage/Core/SDWebImageDownloaderResponseModifier.h +++ b/SDWebImage/Core/SDWebImageDownloaderResponseModifier.h @@ -42,7 +42,7 @@ typedef NSURLResponse * _Nullable (^SDWebImageDownloaderResponseModifierBlock)(N /** A convenient response modifier to provide the HTTP response including HTTP Status Code, Version and Headers. */ -@interface SDWebImageDownloaderResponseModifier (HTTPConveniences) +@interface SDWebImageDownloaderResponseModifier (Conveniences) /// Create the response modifier with HTTP Status code. /// @param statusCode HTTP Status Code. diff --git a/SDWebImage/Core/SDWebImageDownloaderResponseModifier.m b/SDWebImage/Core/SDWebImageDownloaderResponseModifier.m index 309ae984..6acf02a2 100644 --- a/SDWebImage/Core/SDWebImageDownloaderResponseModifier.m +++ b/SDWebImage/Core/SDWebImageDownloaderResponseModifier.m @@ -39,7 +39,7 @@ @end -@implementation SDWebImageDownloaderResponseModifier (HTTPConveniences) +@implementation SDWebImageDownloaderResponseModifier (Conveniences) - (instancetype)initWithStatusCode:(NSInteger)statusCode { return [self initWithStatusCode:statusCode version:nil headers:nil]; diff --git a/Tests/Tests/SDWebImageDownloaderTests.m b/Tests/Tests/SDWebImageDownloaderTests.m index 4cd877c1..5d2c52ce 100644 --- a/Tests/Tests/SDWebImageDownloaderTests.m +++ b/Tests/Tests/SDWebImageDownloaderTests.m @@ -502,7 +502,14 @@ - (void)test23ThatDownloadRequestModifierWorks { XCTestExpectation *expectation = [self expectationWithDescription:@"Download request modifier not works"]; SDWebImageDownloader *downloader = [[SDWebImageDownloader alloc] init]; - SDWebImageDownloaderRequestModifier *requestModifier = [SDWebImageDownloaderRequestModifier requestModifierWithBlock:^NSURLRequest * _Nullable(NSURLRequest * _Nonnull request) { + + // Test conveniences modifier + SDWebImageDownloaderRequestModifier *requestModifier = [[SDWebImageDownloaderRequestModifier alloc] initWithHeaders:@{@"Biz" : @"Bazz"}]; + NSURLRequest *testRequest = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:kTestJPEGURL]]; + testRequest = [requestModifier modifiedRequestWithRequest:testRequest]; + expect(testRequest.allHTTPHeaderFields).equal(@{@"Biz" : @"Bazz"}); + + requestModifier = [SDWebImageDownloaderRequestModifier requestModifierWithBlock:^NSURLRequest * _Nullable(NSURLRequest * _Nonnull request) { if ([request.URL.absoluteString isEqualToString:kTestPNGURL]) { // Test that return a modified request NSMutableURLRequest *mutableRequest = [request mutableCopy]; @@ -550,8 +557,15 @@ SDWebImageDownloader *downloader = [[SDWebImageDownloader alloc] init]; + // Test conveniences modifier + SDWebImageDownloaderResponseModifier *responseModifier = [[SDWebImageDownloaderResponseModifier alloc] initWithHeaders:@{@"Biz" : @"Bazz"}]; + NSURLResponse *testResponse = [[NSHTTPURLResponse alloc] initWithURL:[NSURL URLWithString:kTestPNGURL] statusCode:404 HTTPVersion:@"HTTP/1.1" headerFields:nil]; + testResponse = [responseModifier modifiedResponseWithResponse:testResponse]; + expect(((NSHTTPURLResponse *)testResponse).allHeaderFields).equal(@{@"Biz" : @"Bazz"}); + expect(((NSHTTPURLResponse *)testResponse).statusCode).equal(200); + // 1. Test webURL to response custom status code and header - SDWebImageDownloaderResponseModifier *responseModifier = [SDWebImageDownloaderResponseModifier responseModifierWithBlock:^NSURLResponse * _Nullable(NSURLResponse * _Nonnull response) { + responseModifier = [SDWebImageDownloaderResponseModifier responseModifierWithBlock:^NSURLResponse * _Nullable(NSURLResponse * _Nonnull response) { NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response; NSMutableDictionary *mutableHeaderFields = [httpResponse.allHeaderFields mutableCopy]; mutableHeaderFields[@"Foo"] = @"Bar";