From 597a70a8b667307d7bd2722c639bf5cd672a7260 Mon Sep 17 00:00:00 2001 From: Tony Li Date: Mon, 25 Nov 2013 14:21:42 +0800 Subject: [PATCH] Add HTTP header filter to pick headers for downloading request --- SDWebImage/SDWebImageDownloader.h | 8 ++++++++ SDWebImage/SDWebImageDownloader.m | 9 ++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/SDWebImage/SDWebImageDownloader.h b/SDWebImage/SDWebImageDownloader.h index 399db45a..93f2404b 100644 --- a/SDWebImage/SDWebImageDownloader.h +++ b/SDWebImage/SDWebImageDownloader.h @@ -58,6 +58,14 @@ typedef void(^SDWebImageDownloaderCompletedBlock)(UIImage *image, NSData *data, + (SDWebImageDownloader *)sharedDownloader; +/** + * Set filter to pick headers for downloading image HTTP request. + * + * This block will be invoked for each downloading image request, returned + * NSDictionary will be used as headers in corresponding HTTP request. + */ +@property (nonatomic, strong) NSDictionary *(^headersFilter)(NSURL *url, NSDictionary *headers); + /** * Set a value for a HTTP header to be appended to each download HTTP request. * diff --git a/SDWebImage/SDWebImageDownloader.m b/SDWebImage/SDWebImageDownloader.m index 063056fe..abdf3afa 100644 --- a/SDWebImage/SDWebImageDownloader.m +++ b/SDWebImage/SDWebImageDownloader.m @@ -120,7 +120,14 @@ static NSString *const kCompletedCallbackKey = @"completed"; NSMutableURLRequest *request = [NSMutableURLRequest.alloc initWithURL:url cachePolicy:(options & SDWebImageDownloaderUseNSURLCache ? NSURLRequestUseProtocolCachePolicy : NSURLRequestReloadIgnoringLocalCacheData) timeoutInterval:15]; request.HTTPShouldHandleCookies = NO; request.HTTPShouldUsePipelining = YES; - request.allHTTPHeaderFields = wself.HTTPHeaders; + if (wself.headersFilter) + { + request.allHTTPHeaderFields = wself.headersFilter(url, [wself.HTTPHeaders copy]); + } + else + { + request.allHTTPHeaderFields = wself.HTTPHeaders; + } operation = [SDWebImageDownloaderOperation.alloc initWithRequest:request options:options progress:^(NSUInteger receivedSize, long long expectedSize) { if (!wself) return;