NSURLProtocol - handle https request

纵饮孤独 提交于 2020-01-05 12:28:06

问题


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.


回答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.




回答2:


"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.)



来源:https://stackoverflow.com/questions/28499690/nsurlprotocol-handle-https-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!