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

fix(ios): websocket module support extra headers #4069

Merged
merged 1 commit into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions ios/sdk/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 ios/sdk/debug/websocket/HippySRWebSocket.m
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,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 @@ -295,11 +296,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 ios/sdk/debug/websocket/HippyWebSocketManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
#import "HippySRWebSocket.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
Loading