Skip to content

Instantly share code, notes, and snippets.

@quellish
Created October 9, 2015 21:02
Show Gist options
  • Save quellish/d5454671e011b7fd2945 to your computer and use it in GitHub Desktop.
Save quellish/d5454671e011b7fd2945 to your computer and use it in GitHub Desktop.
Retry-After iOS workaround
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