From 6f198abd1b2c129ae104b70cea485a495fc773f7 Mon Sep 17 00:00:00 2001 From: Olivier Poitrey Date: Sat, 16 Feb 2013 23:33:41 +0100 Subject: [PATCH] Add ability to set custom downloader HTTP headers (fix #171) --- .../SDWebImage Demo/MasterViewController.m | 1 + SDWebImage/SDWebImageDownloader.h | 15 +++++++++++++ SDWebImage/SDWebImageDownloader.m | 21 ++++++++++++++++++- 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/Examples/SDWebImage Demo/MasterViewController.m b/Examples/SDWebImage Demo/MasterViewController.m index 70d04e216..b3624ba81 100644 --- a/Examples/SDWebImage Demo/MasterViewController.m +++ b/Examples/SDWebImage Demo/MasterViewController.m @@ -332,6 +332,7 @@ - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil @"http://static2.dmcdn.net/static/video/269/938/51839962:jpeg_preview_small.jpg?20121105214014", nil]; } + [SDWebImageManager.sharedManager.imageDownloader setValue:@"SDWebImage Demo" forHTTPHeaderField:@"AppName"]; return self; } diff --git a/SDWebImage/SDWebImageDownloader.h b/SDWebImage/SDWebImageDownloader.h index 001735fa6..dce4841e9 100644 --- a/SDWebImage/SDWebImageDownloader.h +++ b/SDWebImage/SDWebImageDownloader.h @@ -31,6 +31,21 @@ typedef void(^SDWebImageDownloaderCompletedBlock)(UIImage *image, NSData *data, + (SDWebImageDownloader *)sharedDownloader; +/** + * Set a value for a HTTP header to be appended to each download HTTP request. + * + * @param value The value for the header field. Use `nil` value to remove the header. + * @param field The name of the header field to set. + */ +- (void)setValue:(NSString *)value forHTTPHeaderField:(NSString *)field; + +/** + * Returns the value of the specified HTTP header field. + * + * @return The value associated with the header field field, or `nil` if there is no corresponding header field. + */ +- (NSString *)valueForHTTPHeaderField:(NSString *)field; + /** * Creates a SDWebImageDownloader async downloader instance with a given URL * diff --git a/SDWebImage/SDWebImageDownloader.m b/SDWebImage/SDWebImageDownloader.m index ef039b9ef..b52d38c52 100644 --- a/SDWebImage/SDWebImageDownloader.m +++ b/SDWebImage/SDWebImageDownloader.m @@ -20,6 +20,7 @@ @interface SDWebImageDownloader () @property (strong, nonatomic) NSOperationQueue *downloadQueue; @property (strong, nonatomic) NSMutableDictionary *URLCallbacks; +@property (strong, nonatomic) NSMutableDictionary *HTTPHeaders; // This queue is used to serialize the handling of the network responses of all the download operation in a single queue @property (SDDispatchQueueSetterSementics, nonatomic) dispatch_queue_t workingQueue; @property (SDDispatchQueueSetterSementics, nonatomic) dispatch_queue_t barrierQueue; @@ -68,6 +69,7 @@ - (id)init _downloadQueue = NSOperationQueue.new; _downloadQueue.maxConcurrentOperationCount = 2; _URLCallbacks = NSMutableDictionary.new; + _HTTPHeaders = [NSMutableDictionary dictionaryWithObject:@"image/*" forKey:@"Accept"]; _workingQueue = dispatch_queue_create("com.hackemist.SDWebImageDownloader", DISPATCH_QUEUE_SERIAL); _barrierQueue = dispatch_queue_create("com.hackemist.SDWebImageDownloaderBarrierQueue", DISPATCH_QUEUE_CONCURRENT); } @@ -81,6 +83,23 @@ - (void)dealloc SDDispatchQueueRelease(_barrierQueue); } +- (void)setValue:(NSString *)value forHTTPHeaderField:(NSString *)field +{ + if (value) + { + self.HTTPHeaders[field] = value; + } + else + { + [self.HTTPHeaders removeObjectForKey:field]; + } +} + +- (NSString *)valueForHTTPHeaderField:(NSString *)field +{ + return self.HTTPHeaders[field]; +} + - (void)setMaxConcurrentDownloads:(NSInteger)maxConcurrentDownloads { _downloadQueue.maxConcurrentOperationCount = maxConcurrentDownloads; @@ -102,7 +121,7 @@ - (NSInteger)maxConcurrentDownloads NSMutableURLRequest *request = [NSMutableURLRequest.alloc initWithURL:url cachePolicy:NSURLCacheStorageNotAllowed timeoutInterval:15]; request.HTTPShouldHandleCookies = NO; request.HTTPShouldUsePipelining = YES; - [request addValue:@"image/*" forHTTPHeaderField:@"Accept"]; + request.allHTTPHeaderFields = wself.HTTPHeaders; operation = [SDWebImageDownloaderOperation.alloc initWithRequest:request queue:wself.workingQueue options:options progress:^(NSUInteger receivedSize, long long expectedSize) { if (!wself) return;