Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom NSURLProtocol slower after switching NSURLConnection to NSURLSession

I have a custom NSURLProtocol ("UrlProtocol") written to intercept requests from a UIWebView when navigating to specific websites, and apply an extra HTTP header before being sent. I followed https://www.raywenderlich.com/59982/nsurlprotocol-tutorial for a working class. My problem comes with switching from the deprecated NSURLConnection to NSURLSession: I tested an extremely simple one-file html page, which loaded successfully. However, slightly more complex sites with resources like js files, images, etc. will timeout, whereas using NSURLConnection the entire site will load within a few seconds.

I'll paste the original UrlProtocol using NSURLConnection, then the new class using NSURLSession. The original:

#import "UrlProtocol.h"
#import "Globals.h"

@implementation UrlProtocol

+ (BOOL)canInitWithRequest:(NSURLRequest *)request {
    if (![request.URL.absoluteString hasPrefix:@"http"]) return NO; //No need to intercept non-http requests

    if ([NSURLProtocol propertyForKey:@"handled" inRequest:request]) {
        return NO;
    }

    return YES;
}

+ (NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request {
    NSString* key = @"custom-auth-header";
    Globals* globals = [Globals getInstance];
    NSString* token = [globals token];

    NSMutableURLRequest *newRequest = [request mutableCopy]; //Create a mutable copy that can be modified
    [newRequest setValue:token forHTTPHeaderField:key];

    return [newRequest copy]; //return a non-mutable copy
}

+ (BOOL)requestIsCacheEquivalent:(NSURLRequest *)a toRequest:(NSURLRequest *)b {
    return [super requestIsCacheEquivalent:a toRequest:b];
}

- (void)startLoading {
    NSMutableURLRequest *newRequest = [self.request mutableCopy];
    [NSURLProtocol setProperty:@YES forKey:@"handled" inRequest:newRequest];

    self.connection = [NSURLConnection connectionWithRequest:newRequest delegate:self];
}

- (void)stopLoading {
    NSLog(@"stopLoading");
    [self.connection cancel];
    self.connection = 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];
}

@end

The new UrlProtocol using NSURLSessionDataTasks for every request:

#import "UrlProtocol.h"
#import "Globals.h"

@implementation UrlProtocol

+ (BOOL)canInitWithRequest:(NSURLRequest * _Nonnull) request {
    if (![request.URL.absoluteString hasPrefix:@"http"]) return NO; //No need to intercept non-http requests

    if ([NSURLProtocol propertyForKey:@"handled" inRequest:request]) {
        return NO;
    }

    return YES;
}

+ (NSURLRequest * _Nonnull)canonicalRequestForRequest:(NSURLRequest * _Nonnull)request {
    NSString* key = @"custom-auth-header";
    Globals* globals = [Globals getInstance];
    NSString* token = [globals token];

    NSMutableURLRequest *newRequest = [request mutableCopy]; //Create a mutable copy that can be modified
    [newRequest setValue:token forHTTPHeaderField:key];
    return [newRequest copy]; //return a non-mutable copy
}

+ (BOOL)requestIsCacheEquivalent:(NSURLRequest * _Nonnull)a toRequest:(NSURLRequest * _Nonnull)b {
    return [super requestIsCacheEquivalent:a toRequest:b];
}

- (void)startLoading {
    NSMutableURLRequest *newRequest = [self.request mutableCopy];
    [NSURLProtocol setProperty:@YES forKey:@"handled" inRequest:newRequest];
    [Globals setUrlSessionDelegate:self];
    Globals* globals = [Globals getInstance];
    self.dataTask = [globals.session dataTaskWithRequest:newRequest];
    [self.dataTask resume];
}

- (void)URLSession:(NSURLSession * _Nonnull)session dataTask:(NSURLSessionDataTask * _Nullable)dataTask didReceiveData:(NSData * _Nullable)data{
    [self.client URLProtocol:self didLoadData:data];
}

- (void)URLSession:(NSURLSession * _Nonnull)session dataTask:(NSURLSessionDataTask * _Nullable)dataTask didReceiveResponse:(NSURLResponse * _Nullable)response
                 completionHandler:(void (^ _Nullable)(NSURLSessionResponseDisposition))completionHandler{
    [self.client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed];
    completionHandler(NSURLSessionResponseAllow);
}

- (void)URLSession:(NSURLSession * _Nonnull)session task:(NSURLSessionTask * _Nonnull)task didCompleteWithError:(NSError * _Nullable)error{
    if (error){
        [self.client URLProtocol:self didFailWithError:error];
    } else {
        [self.client URLProtocolDidFinishLoading:self];
    }
}

- (void)URLSession:(NSURLSession * _Nonnull)session task:(NSURLSessionTask * _Nonnull)task willPerformHTTPRedirection:(NSHTTPURLResponse * _Nonnull)response 
                        newRequest:(NSURLRequest * _Nonnull)request completionHandler:(void (^ _Nonnull)(NSURLRequest * _Nullable))completionHandler {
    completionHandler(request);
}

- (void)stopLoading {
    [self.dataTask cancel];
    self.dataTask = nil;
}

@end

"Globals" is a singleton where I have initialized one NSURLSession meant to be used throughout the runtime of the app. It also contains the token I set as a custom HTTP header for all requests:

#import "Globals.h"
#import "UrlProtocol.h"

@implementation Globals
@synthesize token;
@synthesize session;

static Globals *instance = nil;

+(Globals*) getInstance
{
    @synchronized(self)
    {
        if (instance == nil)
        {
            instance = [Globals new];
        }
    }
    return instance;
}

//UrlProtocol class has no init method, so the NSURLSession delegate is being set on url load. We will ensure only one NSURLSession is created.
+(void) setUrlSessionDelegate:(UrlProtocol*) urlProtocol{
    Globals* globals = [Globals getInstance];
    if (!globals.session){
        globals.session = [NSURLSession sessionWithConfiguration:NSURLSessionConfiguration.defaultSessionConfiguration  delegate:urlProtocol delegateQueue:nil];
    }
}

@end
like image 904
Chicowitz Avatar asked Jan 05 '17 04:01

Chicowitz


1 Answers

Solved my problem by creating a new default NSURLSession for each NSURLSessionDataTask. Something was wrong with the way I was trying to share one NSURLSession for all my tasks. URLProtocol's startLoading method is now as follows:

- (void)startLoading {
    NSMutableURLRequest *newRequest = [self.request mutableCopy];
    [NSURLProtocol setProperty:@YES forKey:@"handled" inRequest:newRequest];
    NSURLSessionConfiguration* config = NSURLSessionConfiguration.defaultSessionConfiguration;
    NSURLSession* session = [NSURLSession sessionWithConfiguration:config  delegate:self delegateQueue:nil];
    self.dataTask = [session dataTaskWithRequest:newRequest];
    [self.dataTask resume];
}

The simple HTML page test before must have worked because only one task was needed to load the page

like image 193
Chicowitz Avatar answered Nov 08 '22 02:11

Chicowitz