objective-cuiwebviewnsurlprotocol

UIWebView not loading contents of redirected page when using custom NSURLProtocol


I have a UIWebView and a custom NSURLProtocol class registered to proxy HTTP requests. I have a problem loading github.com on it. If I browse to https://github.com then it loads the page and its contents just fine. However, if I browse to http://github.com, it loads the HTML correctly but it doesn't load the images or CSS. Here's how it looks like when I load the https version:

HTTPS GitHub

Here's how it looks like when I load the http version:

HTTP GitHub

Here's the code for the view controller that I used to reproduce this problem:

@interface ViewController ()
{
    UIWebView *aWebView;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [NSURLProtocol registerClass:[WebViewProxyURLProtocol class]];

    aWebView = [[UIWebView alloc] initWithFrame:self.view.frame];
    aWebView.delegate = self;
    [self.view addSubview:aWebView];

    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://github.com"]];
    [aWebView loadRequest:request];
}

@end

Here's the NSURLProtocol implementation:

@interface WebViewProxyURLProtocol : NSURLProtocol <NSURLConnectionDataDelegate>

@end

@implementation WebViewProxyURLProtocol {
    NSMutableURLRequest* correctedRequest;
}

+ (BOOL)canInitWithRequest:(NSURLRequest *)request {
    return ([[request allHTTPHeaderFields] objectForKey:@"X-WebViewProxy"] == nil);
}

+ (NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request {
    return request;
}

+ (BOOL)requestIsCacheEquivalent:(NSURLRequest *)a toRequest:(NSURLRequest *)b {
    return NO;
}

- (id)initWithRequest:(NSURLRequest *)request cachedResponse:(NSCachedURLResponse *)cachedResponse client:(id<NSURLProtocolClient>)client {
    if (self = [super initWithRequest:request cachedResponse:cachedResponse client:client]) {
        // Add header to prevent loop in proxy
        correctedRequest = request.mutableCopy;
        [correctedRequest addValue:@"True" forHTTPHeaderField:@"X-WebViewProxy"];
    }
    return self;
}

- (void)startLoading {
    [NSURLConnection connectionWithRequest:correctedRequest delegate:self];
}

- (void)stopLoading {
    correctedRequest = nil;
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    [self.client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [self.client URLProtocol:self didLoadData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    [self.client URLProtocolDidFinishLoading:self];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    [self.client URLProtocol:self didFailWithError:error];
}

- (NSURLRequest *)connection:(NSURLConnection *)connection
             willSendRequest:(NSURLRequest *)request
            redirectResponse:(NSURLResponse *)redirectResponse
{
    return request;
}

- (NSCachedURLResponse *)connection:(NSURLConnection *)connection
                  willCacheResponse:(NSCachedURLResponse *)cachedResponse
{
    return nil;
}

@end

If I disable the custom NSURLProtocol class, it works fine. I used Charles to inspect the HTTP requests and responses and they look identical with and without NSURLProtocol.

So the question is: why does UIWebView not request the contents of the web page when using NSURLConnection to get the page data?


Solution

  • I figured this out eventually. When I get a redirect response (HTTP 3xx) in connection:didReceiveResponse: I had to call [self.client URLProtocol:self wasRedirectedToRequest:redirectRequest redirectResponse:response].