Skip to content

Instantly share code, notes, and snippets.

@pita5
Created October 16, 2012 12:28
Show Gist options
  • Save pita5/3898957 to your computer and use it in GitHub Desktop.
Save pita5/3898957 to your computer and use it in GitHub Desktop.
- (void)_checkCacheOrFail:(RNCachedData *)cache
{
if (cache)
{
NSHTTPURLResponse* response = (NSHTTPURLResponse *)[cache response];
NSURLRequest* redirectRequest = [cache redirectRequest];
if (redirectRequest)
{
[[self client] URLProtocol:self wasRedirectedToRequest:redirectRequest redirectResponse:response];
}
else
{
NSLog(@"Returning cached response for %@", [[self request] URL]);
NSData *data = [[cache data] copy];
NSMutableDictionary* responses = [NSMutableDictionary dictionaryWithDictionary:[response allHeaderFields]];
[responses setValue:@"" forKey:@"csm-did-cache"];
NSHTTPURLResponse* cachedResponse = [[NSHTTPURLResponse alloc] initWithURL:response.URL statusCode:response.statusCode HTTPVersion:@"HTTP/1.1" headerFields:responses];
// we handle caching ourselves.
[[self client] URLProtocol:self didReceiveResponse:cachedResponse cacheStoragePolicy:NSURLCacheStorageNotAllowed];
[[self client] URLProtocol:self didLoadData:data];
[[self client] URLProtocolDidFinishLoading:self];
}
}
else
{
[[self client] URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorCannotConnectToHost userInfo:nil]];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment