From 7ad9f0a6d944b15af2a37d7c1e853318f80d1858 Mon Sep 17 00:00:00 2001 From: Matt Steele Date: Wed, 3 Mar 2021 14:11:07 -0600 Subject: [PATCH] Copy request cookies from store --- src/ios/WebviewProxy.m | 115 +++++++++++++++++++++++++++-------------- 1 file changed, 76 insertions(+), 39 deletions(-) diff --git a/src/ios/WebviewProxy.m b/src/ios/WebviewProxy.m index a222219..26509f9 100644 --- a/src/ios/WebviewProxy.m +++ b/src/ios/WebviewProxy.m @@ -45,51 +45,88 @@ - (BOOL) overrideSchemeTask: (id )urlSchemeTask { [request setHTTPShouldHandleCookies:YES]; [request setTimeoutInterval:1800]; - [[[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { - if(error && (self.stoppedTasks == nil || ![self.stoppedTasks containsObject:urlSchemeTask])) { - @try { - NSLog(@"WebviewProxy error: %@", error); - [urlSchemeTask didFailWithError:error]; - return; - } @catch (NSException *exception) { - NSLog(@"WebViewProxy send error exception: %@", exception.debugDescription); + NSString *validDomain = request.URL.host; + const BOOL requestIsSecure = [request.URL.scheme isEqualToString:@"https"]; + + NSMutableArray *array = [NSMutableArray array]; + + // https://stackoverflow.com/a/32845148 + [cookieStore getAllCookies:^(NSArray* cookies) { + // case 1: didn't call this completionHandler + for (NSHTTPCookie *cookie in cookies) { + NSLog(@"Proxy checking cookie %@", cookie.name); + // Don't even bother with values containing a `'` + if ([cookie.name rangeOfString:@"'"].location != NSNotFound) { + NSLog(@"Skipping %@ because it contains a '", cookie.properties); + continue; } + + // Is the cookie for current domain? + if (![validDomain hasSuffix:cookie.domain]) { + NSLog(@"Skipping %@ (because not %@)", cookie.properties, validDomain); + continue; + } + + // Are we secure only? + if (cookie.secure && !requestIsSecure) { + NSLog(@"Skipping %@ (because %@ not secure)", cookie.properties, request.URL.absoluteString); + continue; + } + + NSString *value = [NSString stringWithFormat:@"%@=%@", cookie.name, cookie.value]; + NSLog(@"Proxy Adding cookie: %@", cookie.name); + [array addObject:value]; } + NSString *header = [array componentsJoinedByString:@";"]; + NSLog(@"Proxy Setting cookie: %@", header); + [request setValue:header forHTTPHeaderField:@"Cookie"]; - // set cookies to WKWebView - NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response; - if(httpResponse) { - NSArray* cookies = [NSHTTPCookie cookiesWithResponseHeaderFields:[httpResponse allHeaderFields] forURL:response.URL]; - [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookies:cookies forURL:httpResponse.URL mainDocumentURL:nil]; - cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]; + [[[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { + if(error && (self.stoppedTasks == nil || ![self.stoppedTasks containsObject:urlSchemeTask])) { + @try { + NSLog(@"WebviewProxy error: %@", error); + [urlSchemeTask didFailWithError:error]; + return; + } @catch (NSException *exception) { + NSLog(@"WebViewProxy send error exception: %@", exception.debugDescription); + } + } - for (NSHTTPCookie* c in cookies) - { - dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){ - //running in background thread is necessary because setCookie otherwise fails - dispatch_async(dispatch_get_main_queue(), ^(void){ - [cookieStore setCookie:c completionHandler:nil]; + // set cookies to WKWebView + NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response; + if(httpResponse) { + NSArray* cookies = [NSHTTPCookie cookiesWithResponseHeaderFields:[httpResponse allHeaderFields] forURL:response.URL]; + [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookies:cookies forURL:httpResponse.URL mainDocumentURL:nil]; + cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]; + + for (NSHTTPCookie* c in cookies) + { + dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){ + //running in background thread is necessary because setCookie otherwise fails + dispatch_async(dispatch_get_main_queue(), ^(void){ + [cookieStore setCookie:c completionHandler:nil]; + }); }); - }); - }; - } - - // Do not use urlSchemeTask if it has been closed in stopURLSchemeTask. Otherwise the app will crash. - @try { - if(self.stoppedTasks == nil || ![self.stoppedTasks containsObject:urlSchemeTask]) { - [urlSchemeTask didReceiveResponse:response]; - [urlSchemeTask didReceiveData:data]; - [urlSchemeTask didFinish]; - } else { - NSLog(@"Task stopped %@", startPath); + }; } - } @catch (NSException *exception) { - NSLog(@"WebViewProxy send response exception: %@", exception.debugDescription); - } @finally { - // Cleanup - [self.stoppedTasks removeObject:urlSchemeTask]; - } - }] resume]; + + // Do not use urlSchemeTask if it has been closed in stopURLSchemeTask. Otherwise the app will crash. + @try { + if(self.stoppedTasks == nil || ![self.stoppedTasks containsObject:urlSchemeTask]) { + [urlSchemeTask didReceiveResponse:response]; + [urlSchemeTask didReceiveData:data]; + [urlSchemeTask didFinish]; + } else { + NSLog(@"Task stopped %@", startPath); + } + } @catch (NSException *exception) { + NSLog(@"WebViewProxy send response exception: %@", exception.debugDescription); + } @finally { + // Cleanup + [self.stoppedTasks removeObject:urlSchemeTask]; + } + }] resume]; + }]; return YES; }