Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#import <React/RCTConvert.h>
#import <React/RCTDefines.h>
#import <React/RCTDevSupportHttpHeaders.h>

#import <SocketRocket/SRWebSocket.h>

Expand Down Expand Up @@ -46,7 +47,9 @@ - (void)start
{
[self stop];
_stopped = NO;
_socket = [[SRWebSocket alloc] initWithURL:_url];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:_url];
[[RCTDevSupportHttpHeaders sharedInstance] applyHeadersToRequest:request];
_socket = [[SRWebSocket alloc] initWithURLRequest:request];
_socket.delegate = self;
[_socket setDelegateDispatchQueue:_delegateDispatchQueue];
[_socket open];
Expand Down
8 changes: 5 additions & 3 deletions packages/react-native/React/Base/RCTBundleURLProvider.mm
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#import "RCTConstants.h"
#import "RCTConvert.h"
#import "RCTDefines.h"
#import "RCTDevSupportHttpHeaders.h"
#import "RCTLog.h"

#import <jsinspector-modern/InspectorFlags.h>
Expand Down Expand Up @@ -93,9 +94,10 @@ + (BOOL)isPackagerRunning:(NSString *)hostPort scheme:(NSString *)scheme
NSURL *url = [serverRootWithHostPort(hostPort, scheme) URLByAppendingPathComponent:@"status"];

NSURLSession *session = [NSURLSession sharedSession];
NSURLRequest *request = [NSURLRequest requestWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:10];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:10];
[[RCTDevSupportHttpHeaders sharedInstance] applyHeadersToRequest:request];
__block NSURLResponse *response;
__block NSData *data;

Expand Down
24 changes: 24 additions & 0 deletions packages/react-native/React/Base/RCTDevSupportHttpHeaders.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

#import <Foundation/Foundation.h>

/**
* Thread-safe singleton that holds custom HTTP headers to be applied
* to all devsupport network requests (bundle fetches, packager status
* checks, inspector and HMR WebSocket connections).
*/
@interface RCTDevSupportHttpHeaders : NSObject

+ (instancetype)sharedInstance;

- (void)addRequestHeader:(NSString *)name value:(NSString *)value;
- (void)removeRequestHeader:(NSString *)name;
- (NSDictionary<NSString *, NSString *> *)allHeaders;
- (void)applyHeadersToRequest:(NSMutableURLRequest *)request;

@end
65 changes: 65 additions & 0 deletions packages/react-native/React/Base/RCTDevSupportHttpHeaders.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

#import "RCTDevSupportHttpHeaders.h"

@implementation RCTDevSupportHttpHeaders {
NSMutableDictionary<NSString *, NSString *> *_headers;
dispatch_queue_t _queue;
}

+ (instancetype)sharedInstance
{
static RCTDevSupportHttpHeaders *sharedInstance;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [[RCTDevSupportHttpHeaders alloc] init];
});
return sharedInstance;
}

- (instancetype)init
{
if (self = [super init]) {
_headers = [NSMutableDictionary new];
_queue = dispatch_queue_create("com.facebook.react.RCTDevSupportHttpHeaders", DISPATCH_QUEUE_CONCURRENT);
}
return self;
}

- (void)addRequestHeader:(NSString *)name value:(NSString *)value
{
dispatch_barrier_async(_queue, ^{
self->_headers[name] = value;
});
}

- (void)removeRequestHeader:(NSString *)name
{
dispatch_barrier_async(_queue, ^{
[self->_headers removeObjectForKey:name];
});
}

- (NSDictionary<NSString *, NSString *> *)allHeaders
{
__block NSDictionary<NSString *, NSString *> *snapshot;
dispatch_sync(_queue, ^{
snapshot = [self->_headers copy];
});
return snapshot;
}

- (void)applyHeadersToRequest:(NSMutableURLRequest *)request
{
NSDictionary<NSString *, NSString *> *headers = [self allHeaders];
for (NSString *name in headers) {
[request setValue:headers[name] forHTTPHeaderField:name];
}
}

@end
3 changes: 3 additions & 0 deletions packages/react-native/React/Base/RCTMultipartDataTask.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

#import "RCTMultipartDataTask.h"

#import "RCTDevSupportHttpHeaders.h"

@interface RCTMultipartDataTask () <NSURLSessionDataDelegate, NSURLSessionDataDelegate>

@end
Expand Down Expand Up @@ -40,6 +42,7 @@ - (void)startTask
delegateQueue:nil];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:_url];
[request addValue:@"multipart/mixed" forHTTPHeaderField:@"Accept"];
[[RCTDevSupportHttpHeaders sharedInstance] applyHeadersToRequest:request];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request];
[dataTask resume];
[session finishTasksAndInvalidate];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#import <React/RCTCxxInspectorPackagerConnection.h>
#import <React/RCTDefines.h>
#import <React/RCTDevSupportHttpHeaders.h>

#import <CommonCrypto/CommonCrypto.h>
#import <jsinspector-modern/InspectorFlags.h>
Expand Down Expand Up @@ -154,6 +155,7 @@ + (void)openDebugger:(NSURL *)bundleURL withErrorMessage:(NSString *)errorMessag
escapedInspectorDeviceId]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[[RCTDevSupportHttpHeaders sharedInstance] applyHeadersToRequest:request];

[[[NSURLSession sharedSession]
dataTaskWithRequest:request
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

#import "RCTInspectorNetworkHelper.h"
#import <React/RCTDevSupportHttpHeaders.h>
#import <React/RCTLog.h>

using ListenerBlock = void (^)(RCTInspectorNetworkListener *);
Expand Down Expand Up @@ -47,6 +48,7 @@ - (void)loadNetworkResourceWithParams:(const RCTInspectorLoadNetworkResourceRequ

NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
[urlRequest setHTTPMethod:@"GET"];
[[RCTDevSupportHttpHeaders sharedInstance] applyHeadersToRequest:urlRequest];
NSURLSessionDataTask *dataTask = [self.session dataTaskWithRequest:urlRequest];
__weak NSURLSessionDataTask *weakDataTask = dataTask;

Expand Down
Loading