Skip to content

Commit

Permalink
Add ability to set custom downloader HTTP headers (fix #171)
Browse files Browse the repository at this point in the history
  • Loading branch information
Olivier Poitrey committed Feb 16, 2013
1 parent 5f53560 commit 6f198ab
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
1 change: 1 addition & 0 deletions Examples/SDWebImage Demo/MasterViewController.m
Expand Up @@ -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;
}

Expand Down
15 changes: 15 additions & 0 deletions SDWebImage/SDWebImageDownloader.h
Expand Up @@ -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
*
Expand Down
21 changes: 20 additions & 1 deletion SDWebImage/SDWebImageDownloader.m
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand All @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit 6f198ab

Please sign in to comment.