mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-04-27 19:25:11 +08:00
Added an optional options parameter for WebSockets
Summary: This enables overriding origin, and other request headers. Similar to the https://github.com/websockets/ws api. Closes https://github.com/facebook/react-native/pull/4629 Reviewed By: svcscm Differential Revision: D2839951 Pulled By: mkonicek fb-gh-sync-id: 3578af4343f90572b8851ff28342a05945498ef6
This commit is contained in:
committed by
facebook-github-bot-4
parent
15f806957f
commit
9b87e6c860
@@ -234,6 +234,7 @@ typedef void (^data_callback)(RCTSRWebSocket *webSocket, NSData *data);
|
||||
__strong RCTSRWebSocket *_selfRetain;
|
||||
|
||||
NSArray<NSString *> *_requestedProtocols;
|
||||
NSDictionary<NSString *, NSString *> *_requestedOptions;
|
||||
RCTSRIOConsumerPool *_consumerPool;
|
||||
}
|
||||
|
||||
@@ -244,7 +245,7 @@ static __strong NSData *CRLFCRLF;
|
||||
CRLFCRLF = [[NSData alloc] initWithBytes:"\r\n\r\n" length:4];
|
||||
}
|
||||
|
||||
- (instancetype)initWithURLRequest:(NSURLRequest *)request protocols:(NSArray<NSString *> *)protocols;
|
||||
- (instancetype)initWithURLRequest:(NSURLRequest *)request protocols:(NSArray<NSString *> *)protocols options:(NSDictionary<NSString *, NSString *> *)options
|
||||
{
|
||||
RCTAssertParam(request);
|
||||
|
||||
@@ -253,6 +254,7 @@ static __strong NSData *CRLFCRLF;
|
||||
_urlRequest = request;
|
||||
|
||||
_requestedProtocols = [protocols copy];
|
||||
_requestedOptions = [options copy];
|
||||
|
||||
[self _RCTSR_commonInit];
|
||||
}
|
||||
@@ -263,18 +265,23 @@ RCT_NOT_IMPLEMENTED(- (instancetype)init)
|
||||
|
||||
- (instancetype)initWithURLRequest:(NSURLRequest *)request;
|
||||
{
|
||||
return [self initWithURLRequest:request protocols:nil];
|
||||
return [self initWithURLRequest:request protocols:nil options: nil];
|
||||
}
|
||||
|
||||
- (instancetype)initWithURL:(NSURL *)URL;
|
||||
{
|
||||
return [self initWithURL:URL protocols:nil];
|
||||
return [self initWithURL:URL protocols:nil options:nil];
|
||||
}
|
||||
|
||||
- (instancetype)initWithURL:(NSURL *)URL protocols:(NSArray<NSString *> *)protocols;
|
||||
{
|
||||
return [self initWithURL:URL protocols:protocols options:nil];
|
||||
}
|
||||
|
||||
- (instancetype)initWithURL:(NSURL *)URL protocols:(NSArray<NSString *> *)protocols options:(NSDictionary<NSString *, id> *)options
|
||||
{
|
||||
NSURLRequest *request = URL ? [NSURLRequest requestWithURL:URL] : nil;
|
||||
return [self initWithURLRequest:request protocols:protocols];
|
||||
return [self initWithURLRequest:request protocols:protocols options:options];
|
||||
}
|
||||
|
||||
- (void)_RCTSR_commonInit;
|
||||
@@ -465,12 +472,12 @@ RCT_NOT_IMPLEMENTED(- (instancetype)init)
|
||||
CFHTTPMessageSetHeaderFieldValue(request, CFSTR("Sec-WebSocket-Key"), (__bridge CFStringRef)_secKey);
|
||||
CFHTTPMessageSetHeaderFieldValue(request, CFSTR("Sec-WebSocket-Version"), (__bridge CFStringRef)[NSString stringWithFormat:@"%ld", (long)_webSocketVersion]);
|
||||
|
||||
CFHTTPMessageSetHeaderFieldValue(request, CFSTR("Origin"), (__bridge CFStringRef)_url.RCTSR_origin);
|
||||
|
||||
if (_requestedProtocols) {
|
||||
CFHTTPMessageSetHeaderFieldValue(request, CFSTR("Sec-WebSocket-Protocol"), (__bridge CFStringRef)[_requestedProtocols componentsJoinedByString:@", "]);
|
||||
}
|
||||
|
||||
CFHTTPMessageSetHeaderFieldValue(request, CFSTR("Origin"), (__bridge CFStringRef)(_requestedOptions[@"origin"] ?: _url.RCTSR_origin));
|
||||
|
||||
[_urlRequest.allHTTPHeaderFields enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
|
||||
CFHTTPMessageSetHeaderFieldValue(request, (__bridge CFStringRef)key, (__bridge CFStringRef)obj);
|
||||
}];
|
||||
|
||||
Reference in New Issue
Block a user