Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Copy request cookies from store #6

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 76 additions & 39 deletions src/ios/WebviewProxy.m
Original file line number Diff line number Diff line change
Expand Up @@ -45,51 +45,88 @@ - (BOOL) overrideSchemeTask: (id <WKURLSchemeTask>)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"];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the late review.

So this basically sets cookies to the request before sending them? In theory they should already be set by WebKit automatically because they are in the cookie store used by the session that is doing the request.

From my experience this is not really necessary and strangely I did not encouter the issues discussed in #3. Maybe something different is the cause of this issue, but this needs further investigation.


// 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;
}

Expand Down