Skip to content
Closed
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
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_SERIAL);
}
return self;
}

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

- (void)removeRequestHeader:(NSString *)name
{
dispatch_sync(_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];
[headers enumerateKeysAndObjectsUsingBlock:^(NSString *headerName, NSString *headerValue, BOOL *stop) {
[request setValue:headerValue forHTTPHeaderField:headerName];
}];
}

@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
Loading