Skip to content

Commit b596c9c

Browse files
committed
Merge pull request SDWebImage#942 from Krivoblotsky/master
'304 Not Modified' HTTP status code handling.
2 parents 7f39e5e + 3d94e34 commit b596c9c

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

SDWebImage/SDWebImageDownloaderOperation.m

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,9 @@ - (BOOL)isConcurrent {
195195
#pragma mark NSURLConnection (delegate)
196196

197197
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
198-
if (![response respondsToSelector:@selector(statusCode)] || [((NSHTTPURLResponse *)response) statusCode] < 400) {
198+
199+
//'304 Not Modified' is an exceptional one
200+
if ((![response respondsToSelector:@selector(statusCode)] || [((NSHTTPURLResponse *)response) statusCode] < 400) && [((NSHTTPURLResponse *)response) statusCode] != 304) {
199201
NSInteger expected = response.expectedContentLength > 0 ? (NSInteger)response.expectedContentLength : 0;
200202
self.expectedSize = expected;
201203
if (self.progressBlock) {
@@ -205,8 +207,16 @@ - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLRespon
205207
self.imageData = [[NSMutableData alloc] initWithCapacity:expected];
206208
}
207209
else {
208-
[self.connection cancel];
209-
210+
NSUInteger code = [((NSHTTPURLResponse *)response) statusCode];
211+
212+
//This is the case when server returns '304 Not Modified'. It means that remote image is not changed.
213+
//In case of 304 we need just cancel the operation and return cached image from the cache.
214+
if (code == 304) {
215+
[self cancelInternal];
216+
} else {
217+
[self.connection cancel];
218+
}
219+
210220
[[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStopNotification object:nil];
211221

212222
if (self.completedBlock) {

0 commit comments

Comments
 (0)