Created
October 9, 2015 21:02
-
-
Save quellish/d5454671e011b7fd2945 to your computer and use it in GitHub Desktop.
Retry-After iOS workaround
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
id retryAfterValue = [[(NSHTTPURLResponse *)response allHeaderFields] valueForKey:@"Retry-After"]; | |
if (retryAfterValue != nil){ | |
NSMutableDictionary *editedHeaders = [NSMutableDictionary dictionaryWithDictionary:[(NSHTTPURLResponse *)response allHeaderFields]]; | |
// Max-age should be calculated based on the retry-after value, which can be an integer or a string date | |
NSString *maxAgeString = [NSString stringWithFormat:@"max-age=%@" , retryAfterValue]; | |
[editedHeaders setValue:maxAgeString forKey:@"Cache-Control"]; | |
// Retry-after header must be removed | |
[editedHeaders setValue:nil forKey:@"Retry-After"]; | |
NSHTTPURLResponse *editedResponse = [[NSHTTPURLResponse alloc] initWithURL:[response URL] statusCode:[(NSHTTPURLResponse *)response statusCode] HTTPVersion:(NSString *)kCFHTTPVersion1_1 headerFields:[editedHeaders copy]]; | |
NSCachedURLResponse *cachedResponse = [[NSCachedURLResponse alloc] initWithResponse:editedResponse data:data]; | |
[[NSURLCache sharedURLCache] storeCachedResponse:cachedResponse forRequest:[self request]]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment