Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add handling for image returned with 404 http response (fixes #399) #396

Merged
merged 6 commits into from
Aug 24, 2017
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions Source/Classes/PINRemoteImageManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -791,13 +791,13 @@ - (void)downloadImageWithURL:(NSURL *)url
//stores the object in the caches
[self materializeAndCacheObject:data cacheInDisk:data additionalCost:0 url:url key:key options:options outImage:&image outAltRep:&alternativeRepresentation];
}

if (error == nil && image == nil && alternativeRepresentation == nil) {
remoteImageError = [NSError errorWithDomain:PINRemoteImageManagerErrorDomain
code:PINRemoteImageManagerErrorFailedToDecodeImage
userInfo:nil];
}

[self callCompletionsWithKey:key image:image alternativeRepresentation:alternativeRepresentation cached:NO response:response error:remoteImageError finalized:YES];
} withPriority:operationPriorityWithImageManagerPriority(priority)];
}];
Expand Down
12 changes: 10 additions & 2 deletions Source/Classes/PINURLSessionManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,10 @@ - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didComp
}

if (!error && [task.response isKindOfClass:[NSHTTPURLResponse class]]) {
NSInteger statusCode = [(NSHTTPURLResponse *)task.response statusCode];
if (statusCode >= 400) {
NSHTTPURLResponse *response = (NSHTTPURLResponse *)task.response;
NSInteger statusCode = [response statusCode];
BOOL recoverable = [self responseRecoverableFrom404:response];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mind adding a comment that says something along the lines of:
"If a 404 response contains an image, we treat it as a successful request and return the image"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added & thanks for approval.

if (statusCode >= 400 && recoverable == NO) {
error = [NSError errorWithDomain:PINURLErrorDomain
code:statusCode
userInfo:@{NSLocalizedDescriptionKey : @"HTTP Error Response."}];
Expand Down Expand Up @@ -214,6 +216,12 @@ - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didFini
[self storeTimeToFirstByte:[firstByte timeIntervalSinceDate:requestStart] forHost:task.originalRequest.URL.host];
}

- (BOOL)responseRecoverableFrom404:(NSHTTPURLResponse*)response
{
return response.statusCode == 404
&& [response.allHeaderFields[@"content-type"] rangeOfString:@"image"].location != NSNotFound;
}

/* We don't bother locking around the timeToFirstByteCache because NSCache itself is
threadsafe and we're not concerned about dropping or overwriting a result. */
- (void)storeTimeToFirstByte:(NSTimeInterval)timeToFirstByte forHost:(NSString *)host
Expand Down
22 changes: 22 additions & 0 deletions Tests/PINRemoteImageTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,14 @@ - (NSURL *)progressiveURL
return bigURLs;
}

- (NSURL *)imageFrom404URL
{
NSString *base64EncodedUrl = @"aHR0cHM6Ly9pLnl0aW1nLmNvbS92aS9PRzlRbW9jNGVZcy9tcWRlZmF1bHQuanBn";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cough

NSData *nsdataFromBase64String = [[NSData alloc] initWithBase64EncodedString:base64EncodedUrl options:0];
NSString *base64Decoded = [[NSString alloc] initWithData:nsdataFromBase64String encoding:NSUTF8StringEncoding];
return [[NSURL alloc] initWithString:base64Decoded];
}

#pragma mark - <PINURLSessionManagerDelegate>

- (void)didReceiveData:(NSData *)data forTask:(NSURLSessionTask *)task
Expand Down Expand Up @@ -390,6 +398,20 @@ - (void)testBase64
[self waitForExpectationsWithTimeout:[self timeoutTimeInterval] handler:nil];
}

- (void)testHTTPResponse404WithData
{
XCTestExpectation *expectation = [self expectationWithDescription:@"image from 404 response should set correctly"];
PINRemoteImageManager *manager = [[PINRemoteImageManager alloc] init];
NSURL *url = [self imageFrom404URL];
[manager downloadImageWithURL:url completion:^(PINRemoteImageManagerResult * _Nonnull result) {
XCTAssertNotNil(result.image);
[expectation fulfill];
}];
[self waitForExpectationsWithTimeout:[self timeoutTimeInterval] handler:^(NSError * _Nullable error) {
XCTAssertNil(error);
}];
}

- (void)testErrorOnNilURLDownload
{
#pragma clang diagnostic push
Expand Down