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

Explicitely only cache GET requests #26

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions RNCachingURLProtocol.m
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,9 @@ @implementation RNCachingURLProtocol

+ (BOOL)canInitWithRequest:(NSURLRequest *)request
{
// only handle http requests we haven't marked with our header.
if ([[[request URL] scheme] isEqualToString:@"http"] &&
// only handle http GET requests we haven't marked with our header.
if ([request.HTTPMethod isEqualToString:@"GET"] &&
[[[request URL] scheme] isEqualToString:@"http"] &&
([request valueForHTTPHeaderField:RNCachingURLHeader] == nil)) {
return YES;
}
Expand All @@ -85,7 +86,7 @@ - (NSString *)cachePathForRequest:(NSURLRequest *)aRequest
- (void)startLoading
{
if (![self useCache]) {
NSMutableURLRequest *connectionRequest =
NSMutableURLRequest *connectionRequest =
#if WORKAROUND_MUTABLE_COPY_LEAK
[[self request] mutableCopyWorkaround];
#else
Expand All @@ -106,7 +107,7 @@ - (void)startLoading
if (redirectRequest) {
[[self client] URLProtocol:self wasRedirectedToRequest:redirectRequest redirectResponse:response];
} else {

[[self client] URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed]; // we handle caching ourselves.
[[self client] URLProtocol:self didLoadData:data];
[[self client] URLProtocolDidFinishLoading:self];
Expand Down Expand Up @@ -138,7 +139,7 @@ - (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSUR
// We need to remove our header so we know to handle this request and cache it.
// There are 3 requests in flight: the outside request, which we handled, the internal request,
// which we marked with our header, and the redirectableRequest, which we're modifying here.
// The redirectable request will cause a new outside request from the NSURLProtocolClient, which
// The redirectable request will cause a new outside request from the NSURLProtocolClient, which
// must not be marked with our header.
[redirectableRequest setValue:nil forHTTPHeaderField:RNCachingURLHeader];

Expand Down Expand Up @@ -190,7 +191,7 @@ - (void)connectionDidFinishLoading:(NSURLConnection *)connection
[self setResponse:nil];
}

- (BOOL) useCache
- (BOOL) useCache
{
BOOL reachable = (BOOL) [[Reachability reachabilityWithHostName:[[[self request] URL] host]] currentReachabilityStatus] != NotReachable;
return !reachable;
Expand Down