From 24f0842ea087838c0de33059d2eee83baa2af928 Mon Sep 17 00:00:00 2001 From: Olivier Poitrey Date: Mon, 19 Mar 2012 19:12:14 +0100 Subject: [PATCH] Treat image URL with response status code >= 400 as errors (fix #79) --- SDWebImageDownloader.m | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/SDWebImageDownloader.m b/SDWebImageDownloader.m index 2dc493b5..7ee9c4c3 100644 --- a/SDWebImageDownloader.m +++ b/SDWebImageDownloader.m @@ -104,6 +104,27 @@ NSString *const SDWebImageDownloadStopNotification = @"SDWebImageDownloadStopNot #pragma mark NSURLConnection (delegate) +- (void)connection:(NSURLConnection *)aConnection didReceiveResponse:(NSURLResponse *)response +{ + if ([((NSHTTPURLResponse *)response) statusCode] >= 400) + { + [aConnection cancel]; + + [[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStopNotification object:nil]; + + if ([delegate respondsToSelector:@selector(imageDownloader:didFailWithError:)]) + { + NSError *error = [[NSError alloc] initWithDomain:NSURLErrorDomain + code:[((NSHTTPURLResponse *)response) statusCode] + userInfo:nil]; + [delegate performSelector:@selector(imageDownloader:didFailWithError:) withObject:self withObject:error]; + } + + self.connection = nil; + self.imageData = nil; + } +} + - (void)connection:(NSURLConnection *)aConnection didReceiveData:(NSData *)data { [imageData appendData:data];