304 http status code handling

This commit is contained in:
Serg Krivoblotsky 2014-10-31 23:30:27 +02:00
parent 1a3ccc2e8d
commit 3d94e34415
1 changed files with 13 additions and 3 deletions

View File

@ -195,7 +195,9 @@
#pragma mark NSURLConnection (delegate) #pragma mark NSURLConnection (delegate)
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
if (![response respondsToSelector:@selector(statusCode)] || [((NSHTTPURLResponse *)response) statusCode] < 400) {
//'304 Not Modified' is an exceptional one
if ((![response respondsToSelector:@selector(statusCode)] || [((NSHTTPURLResponse *)response) statusCode] < 400) && [((NSHTTPURLResponse *)response) statusCode] != 304) {
NSInteger expected = response.expectedContentLength > 0 ? (NSInteger)response.expectedContentLength : 0; NSInteger expected = response.expectedContentLength > 0 ? (NSInteger)response.expectedContentLength : 0;
self.expectedSize = expected; self.expectedSize = expected;
if (self.progressBlock) { if (self.progressBlock) {
@ -205,8 +207,16 @@
self.imageData = [[NSMutableData alloc] initWithCapacity:expected]; self.imageData = [[NSMutableData alloc] initWithCapacity:expected];
} }
else { else {
[self.connection cancel]; NSUInteger code = [((NSHTTPURLResponse *)response) statusCode];
//This is the case when server returns '304 Not Modified'. It means that remote image is not changed.
//In case of 304 we need just cancel the operation and return cached image from the cache.
if (code == 304) {
[self cancelInternal];
} else {
[self.connection cancel];
}
[[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStopNotification object:nil]; [[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStopNotification object:nil];
if (self.completedBlock) { if (self.completedBlock) {