Skip to content

Commit

Permalink
fix(ios): websocket module support extra headers (Tencent#4069) (Tenc…
Browse files Browse the repository at this point in the history
  • Loading branch information
wwwcg committed Oct 23, 2024
1 parent 01c7627 commit 01d9201
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
3 changes: 3 additions & 0 deletions framework/ios/debug/websocket/HippySRWebSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ extern NSString *const HippySRHTTPResponseErrorKey;

// Some helper constructors.
- (instancetype)initWithURL:(NSURL *)url protocols:(NSArray<NSString *> *)protocols;
- (instancetype)initWithURL:(NSURL *)url
extraHeaders:(NSDictionary *)extraHeaders
protocols:(NSArray<NSString *> *)protocols;
- (instancetype)initWithURL:(NSURL *)url;

// Delegate queue will be dispatch_main_queue by default.
Expand Down
18 changes: 15 additions & 3 deletions framework/ios/debug/websocket/HippySRWebSocket.m
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,9 @@ - (instancetype)initWithURLRequest:(NSURLRequest *)request
- (instancetype)initWithURL:(NSURL *)URL
{ return [self initWithURL:URL protocols:nil]; }

- (instancetype)initWithURL:(NSURL *)URL protocols:(NSArray<NSString *> *)protocols
{
- (instancetype)initWithURL:(NSURL *)URL
extraHeaders:(NSDictionary *)extraHeaders
protocols:(NSArray<NSString *> *)protocols {
NSMutableURLRequest *request;
if (URL) {
// Build a mutable request so we can fill the cookie header.
Expand All @@ -293,11 +294,22 @@ - (instancetype)initWithURL:(NSURL *)URL protocols:(NSArray<NSString *> *)protoc

// Load and set the cookie header.
NSArray<NSHTTPCookie *> *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:components.URL];
[request setAllHTTPHeaderFields:[NSHTTPCookie requestHeaderFieldsWithCookies:cookies]];
NSDictionary *cookieHeaders = [NSHTTPCookie requestHeaderFieldsWithCookies:cookies];

// Set extraHeaders passed from the js
NSMutableDictionary *allHeaders = cookieHeaders.mutableCopy ?: [NSMutableDictionary dictionary];
if (extraHeaders) {
[allHeaders addEntriesFromDictionary:extraHeaders];
}
[request setAllHTTPHeaderFields:allHeaders];
}
return [self initWithURLRequest:request protocols:protocols];
}

- (instancetype)initWithURL:(NSURL *)URL protocols:(NSArray<NSString *> *)protocols {
return [self initWithURL:URL extraHeaders:nil protocols:protocols];
}

- (void)_HippySR_commonInit
{
NSString *scheme = _url.scheme.lowercaseString;
Expand Down
16 changes: 12 additions & 4 deletions framework/ios/debug/websocket/HippyWebSocketManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
#import "HippyDefines.h"

static NSUInteger socketIndex = 0;
static NSString *const kHippyWebSocketUrlParamKey = @"url";
static NSString *const kHippyWebSocketHeaderParamKey = @"headers";
static NSString *const kHippySecWebSocketProtocolKey = @"Sec-WebSocket-Protocol";

#pragma mark - HippyWebSocketManager

Expand Down Expand Up @@ -62,11 +65,16 @@ - (void)invalidate {
}

HIPPY_EXPORT_METHOD(connect:(NSDictionary *)params resolver:(HippyPromiseResolveBlock)resolve rejecter:(HippyPromiseRejectBlock)reject) {
NSDictionary *headers = params[@"headers"];
NSString *url = params[@"url"];
NSString *protocols = headers[@"Sec-WebSocket-Protocol"];
NSDictionary *headers = params[kHippyWebSocketHeaderParamKey];
NSString *url = params[kHippyWebSocketUrlParamKey];
NSString *protocols = headers[kHippySecWebSocketProtocolKey];
// prepare extra headers
NSMutableDictionary *extraHeaders = headers.mutableCopy;
[extraHeaders removeObjectForKey:kHippySecWebSocketProtocolKey];
NSArray<NSString *> *protocolArray = [protocols componentsSeparatedByString:@","];
HippySRWebSocket *socket = [[HippySRWebSocket alloc] initWithURL:[NSURL URLWithString:url] protocols:protocolArray];
HippySRWebSocket *socket = [[HippySRWebSocket alloc] initWithURL:[NSURL URLWithString:url]
extraHeaders:extraHeaders
protocols:protocolArray];
socket.delegate = self;
socket.socketID = socketIndex++;
NSNumber *socketId = @(socket.socketID);
Expand Down

0 comments on commit 01d9201

Please sign in to comment.