1

This is canInitWithRequest in NSURLProtocol:

+ (BOOL)canInitWithRequest:(NSURLRequest *)request
{
  // only handle http requests we haven't marked with our header.
  if ([[[request URL] scheme] isEqualToString:@"http"] &&
      ([request valueForHTTPHeaderField:RNCachingURLHeader] == nil)) {
    return YES;
  }
  return NO;
}  

But I want NSURLProtocol to allow https requests as well.
I tried this but all requests fail when called :

+ (BOOL)canInitWithRequest:(NSURLRequest *)request
{
  // only handle http requests we haven't marked with our header.
  if (([[[request URL] scheme] isEqualToString:@"http"] || [[[request URL] scheme] isEqualToString:@"https"]) &&
      ([request valueForHTTPHeaderField:RNCachingURLHeader] == nil)) {
    return YES;
  }
  return NO;

}

One example is of a login request which has https scheme. In the above function, YES is returned but when the login request is called, I get this error in log :

Error Domain=NSURLErrorDomain Code=-1004 "Could not connect to the server."

However if I remove check of https in canInitWithRequest, I get no error.
I am using RNCachingURLProtocol to cache the requests.
How may I achieve this.

Nitish
  • 13,845
  • 28
  • 135
  • 263
  • 1
    Check which one of your test in the `if` is FALSE. I tried your code, and it passed both the `isEqualToString:` test, but I don't have the `RNCachingURLHeader` value to test. – Larme Feb 13 '15 at 12:40
  • @Larne : RNCachingURLHeader = @"X-RNCache" – Nitish Feb 13 '15 at 12:44
  • I can't reproduce your issue. I tried, but https requests had the same behavior in your `canInitWithRequest:` that the http ones. In `canInitWithRequest:`: logs `[[request URL] scheme]` & `[request valueForHTTPHeaderField:RNCachingURLHeader]`. Also, check if there could be a case sensitive issue (your comparing to `lowercase` the `scheme`). – Larme Feb 13 '15 at 13:38

2 Answers2

1

Just for testing and comprehension refactor your code:

+ (BOOL)canInitWithRequest:(NSURLRequest *)request
{
    NSString *scheme = [[[request URL] scheme] lowercaseString];
    BOOL supportedScheme = [scheme isEqualToString:@"http"] || [scheme isEqualToString:@"https"];

    NSString *header = [request valueForHTTPHeaderField:RNCachingURLHeader];
    BOOL validHeader = header == nil;

    BOOL canInit = supportedScheme && validHeader;

    return canInit;
}

Note the lowercaseString, that is defensive coding.

zaph
  • 111,848
  • 21
  • 189
  • 228
  • This does not seem to answer the question. – Jeffrey Bosboom Feb 16 '15 at 16:39
  • @JeffreyBosboom Right and it does not claim to. There is no other option that displays formatting. Note the part of the question: `However if I remove check of https in canInitWithRequest, I get no error` and that indicates that there is an error in this or at a minimum some confusion. In the OP's question there is really no way to test what the individual portions provide and which one, if any, contribute to the failure. This coding style may help the OP find his problem. – zaph Feb 16 '15 at 16:58
  • 1
    So this should be a comment? Not an answer? – JumpingJezza Feb 17 '15 at 02:11
0

"Could not connect to the server" means that you're offline, but the URL you requested was not found in the cache.

Look in startLoading. Check the absolute string of the URL. Make sure that the one you cached is precisely the same as the one you're now requesting. If you put any changeable data in your URL, then it won't match.

I just noticed that a lot of little problems I thought I'd fixed in this code were actually pending pull requests. So I've merged most of those now. In particular, my way of hashing URLs was very prone to collisions. That's fixed. And the updated code adds support for non-http. (But your error still suggests an URL mismatch.)

Rob Napier
  • 286,113
  • 34
  • 456
  • 610
  • initialize function only registers for http in the updated code on git. [self setSupportedSchemes:[NSSet setWithObject:@"http"]]; – Nitish Feb 19 '15 at 07:44
  • That's just the default. You can set the property to include https without hacking the code. – Rob Napier Feb 19 '15 at 12:05
  • I tried to set https for supported schemes : [self setSupportedSchemes:[NSSet setWithObjects:@"http", @"https", nil]]; in initialize function. But still the https requests are not getting called. So I can't even check if there is a url mismatch because startLoading is not called for https. – Nitish Mar 10 '15 at 10:24