Added Cookie Header to XML and Websocket request
Summary: Continuation of Pull Request #7167 https://github.com/facebook/react-native/pull/7167 Needed to clean my repository. So created this Pull Request Closes https://github.com/facebook/react-native/pull/10575 Differential Revision: D4955291 Pulled By: shergin fbshipit-source-id: 94b9a086b7cf70ee6cc152d0b1a36c260140450e
This commit is contained in:
parent
bea7659762
commit
047961fbf7
|
@ -63,6 +63,9 @@ RCT_EXPORT_MODULE()
|
|||
callbackQueue.maxConcurrentOperationCount = 1;
|
||||
callbackQueue.underlyingQueue = [[_bridge networking] methodQueue];
|
||||
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
|
||||
[configuration setHTTPShouldSetCookies:YES];
|
||||
[configuration setHTTPCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways];
|
||||
[configuration setHTTPCookieStorage:[NSHTTPCookieStorage sharedHTTPCookieStorage]];
|
||||
_session = [NSURLSession sessionWithConfiguration:configuration
|
||||
delegate:self
|
||||
delegateQueue:callbackQueue];
|
||||
|
|
|
@ -228,7 +228,19 @@ RCT_EXPORT_MODULE()
|
|||
NSURL *URL = [RCTConvert NSURL:query[@"url"]]; // this is marked as nullable in JS, but should not be null
|
||||
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
|
||||
request.HTTPMethod = [RCTConvert NSString:RCTNilIfNull(query[@"method"])].uppercaseString ?: @"GET";
|
||||
request.allHTTPHeaderFields = [self stripNullsInRequestHeaders:[RCTConvert NSDictionary:query[@"headers"]]];
|
||||
|
||||
// Load and set the cookie header.
|
||||
NSArray<NSHTTPCookie *> *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:URL];
|
||||
request.allHTTPHeaderFields = [NSHTTPCookie requestHeaderFieldsWithCookies:cookies];
|
||||
|
||||
// Set supplied headers.
|
||||
NSDictionary *headers = [RCTConvert NSDictionary:query[@"headers"]];
|
||||
[headers enumerateKeysAndObjectsUsingBlock:^(NSString *key, id value, BOOL *stop) {
|
||||
if (value) {
|
||||
[request addValue:[RCTConvert NSString:value] forHTTPHeaderField:key];
|
||||
}
|
||||
}];
|
||||
|
||||
request.timeoutInterval = [RCTConvert NSTimeInterval:query[@"timeout"]];
|
||||
request.HTTPShouldHandleCookies = [RCTConvert BOOL:query[@"withCredentials"]];
|
||||
NSDictionary<NSString *, id> *data = [RCTConvert NSDictionary:RCTNilIfNull(query[@"data"])];
|
||||
|
|
|
@ -60,6 +60,20 @@ RCT_EXPORT_MODULE()
|
|||
RCT_EXPORT_METHOD(connect:(NSURL *)URL protocols:(NSArray *)protocols headers:(NSDictionary *)headers socketID:(nonnull NSNumber *)socketID)
|
||||
{
|
||||
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
|
||||
|
||||
// We load cookies from sharedHTTPCookieStorage (shared with XHR and
|
||||
// fetch). To get secure cookies for wss URLs, replace wss with https
|
||||
// in the URL.
|
||||
NSURLComponents *components = [NSURLComponents componentsWithURL:URL resolvingAgainstBaseURL:true];
|
||||
if ([components.scheme.lowercaseString isEqualToString:@"wss"]) {
|
||||
components.scheme = @"https";
|
||||
}
|
||||
|
||||
// Load and set the cookie header.
|
||||
NSArray<NSHTTPCookie *> *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:components.URL];
|
||||
request.allHTTPHeaderFields = [NSHTTPCookie requestHeaderFieldsWithCookies:cookies];
|
||||
|
||||
// Load supplied headers
|
||||
[headers enumerateKeysAndObjectsUsingBlock:^(NSString *key, id value, BOOL *stop) {
|
||||
[request addValue:[RCTConvert NSString:value] forHTTPHeaderField:key];
|
||||
}];
|
||||
|
|
Loading…
Reference in New Issue