Add the convenient request/response modifier, which provide HTTP header directly, don't need to repeat the code

This commit is contained in:
DreamPiggy 2020-04-24 12:04:54 +08:00
parent 11754268f9
commit 6379dfc58b
4 changed files with 120 additions and 0 deletions

View File

@ -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 <SDWebImageDownloaderRequestModifier>
/// 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<NSString *, NSString *> *)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<NSString *, NSString *> *)headers body:(nullable NSData *)body;
@end

View File

@ -37,3 +37,41 @@
}
@end
@interface SDWebImageDownloaderHTTPRequestModifier ()
@property (nonatomic, copy, nullable) NSString *method;
@property (nonatomic, copy, nullable) NSDictionary<NSString *,NSString *> *headers;
@property (nonatomic, copy, nullable) NSData *body;
@end
@implementation SDWebImageDownloaderHTTPRequestModifier
- (instancetype)initWithMethod:(NSString *)method headers:(NSDictionary<NSString *,NSString *> *)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<NSString *,NSString *> *)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

View File

@ -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 <SDWebImageDownloaderResponseModifier>
/// 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<NSString *, NSString *> *)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<NSString *, NSString *> *)headers;
@end

View File

@ -38,3 +38,43 @@
}
@end
@interface SDWebImageDownloaderHTTPResponseModifier ()
@property (nonatomic, copy, nullable) NSString *version;
@property (nonatomic) NSInteger statusCode;
@property (nonatomic, copy, nullable) NSDictionary<NSString *,NSString *> *headers;
@end
@implementation SDWebImageDownloaderHTTPResponseModifier
- (instancetype)initWithVersion:(NSString *)version statusCode:(NSInteger)statusCode headers:(NSDictionary<NSString *,NSString *> *)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<NSString *,NSString *> *)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