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 eabdf61d..67f17992 100644 --- a/SDWebImage/Core/SDWebImageDownloaderRequestModifier.h +++ b/SDWebImage/Core/SDWebImageDownloaderRequestModifier.h @@ -29,7 +29,41 @@ 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 + +/** +A convenient request modifier to provide the HTTP request including HTTP Method, Headers and Body. +*/ +@interface SDWebImageDownloaderRequestModifier (Conveniences) + +/// 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 block API instead. +- (nonnull instancetype)initWithHeaders:(nullable NSDictionary *)headers; + +/// 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 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 22d044af..7b81a40c 100644 --- a/SDWebImage/Core/SDWebImageDownloaderRequestModifier.m +++ b/SDWebImage/Core/SDWebImageDownloaderRequestModifier.m @@ -37,3 +37,35 @@ } @end + +@implementation SDWebImageDownloaderRequestModifier (Conveniences) + +- (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)initWithBody:(NSData *)body { + return [self initWithMethod:nil headers:nil body:body]; +} + +- (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 a8bcc0b5..8c530688 100644 --- a/SDWebImage/Core/SDWebImageDownloaderResponseModifier.h +++ b/SDWebImage/Core/SDWebImageDownloaderResponseModifier.h @@ -29,7 +29,41 @@ 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 + +/** +A convenient response modifier to provide the HTTP response including HTTP Status Code, Version and Headers. +*/ +@interface SDWebImageDownloaderResponseModifier (Conveniences) + +/// 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 block API instead. +- (nonnull instancetype)initWithHeaders:(nullable NSDictionary *)headers; + +/// 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 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 0894b953..6acf02a2 100644 --- a/SDWebImage/Core/SDWebImageDownloaderResponseModifier.m +++ b/SDWebImage/Core/SDWebImageDownloaderResponseModifier.m @@ -38,3 +38,36 @@ } @end + +@implementation SDWebImageDownloaderResponseModifier (Conveniences) + +- (instancetype)initWithStatusCode:(NSInteger)statusCode { + return [self initWithStatusCode:statusCode version:nil headers:nil]; +} + +- (instancetype)initWithVersion:(NSString *)version { + return [self initWithStatusCode:200 version:version headers:nil]; +} + +- (instancetype)initWithHeaders:(NSDictionary *)headers { + return [self initWithStatusCode:200 version:nil headers:headers]; +} + +- (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 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";