From 6379dfc58ba9e7a48515d50016314fe9395f3360 Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Fri, 24 Apr 2020 12:04:54 +0800 Subject: [PATCH] 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