Skip to content

Commit 907391f

Browse files
cortinicometa-codesync[bot]
authored andcommitted
Add RCTDevSupportHttpHeaders singleton (#55586)
Summary: Pull Request resolved: #55586 Add a thread-safe singleton that holds custom HTTP headers to be applied to all iOS devsupport network requests. This mirrors the Android `DevSupportHttpClient` from D93481539. Uses a GCD concurrent queue with barrier writes for reader-writer synchronization. Changelog: [Internal] [Changed] - Reviewed By: cipolleschi Differential Revision: D93490954 fbshipit-source-id: addb2f11025a046eedd0e296a7c398eef7211dc4
1 parent ba4ed28 commit 907391f

2 files changed

Lines changed: 89 additions & 0 deletions

File tree

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#import <Foundation/Foundation.h>
9+
10+
/**
11+
* Thread-safe singleton that holds custom HTTP headers to be applied
12+
* to all devsupport network requests (bundle fetches, packager status
13+
* checks, inspector and HMR WebSocket connections).
14+
*/
15+
@interface RCTDevSupportHttpHeaders : NSObject
16+
17+
+ (instancetype)sharedInstance;
18+
19+
- (void)addRequestHeader:(NSString *)name value:(NSString *)value;
20+
- (void)removeRequestHeader:(NSString *)name;
21+
- (NSDictionary<NSString *, NSString *> *)allHeaders;
22+
- (void)applyHeadersToRequest:(NSMutableURLRequest *)request;
23+
24+
@end
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#import "RCTDevSupportHttpHeaders.h"
9+
10+
@implementation RCTDevSupportHttpHeaders {
11+
NSMutableDictionary<NSString *, NSString *> *_headers;
12+
dispatch_queue_t _queue;
13+
}
14+
15+
+ (instancetype)sharedInstance
16+
{
17+
static RCTDevSupportHttpHeaders *sharedInstance;
18+
static dispatch_once_t onceToken;
19+
dispatch_once(&onceToken, ^{
20+
sharedInstance = [[RCTDevSupportHttpHeaders alloc] init];
21+
});
22+
return sharedInstance;
23+
}
24+
25+
- (instancetype)init
26+
{
27+
if (self = [super init]) {
28+
_headers = [NSMutableDictionary new];
29+
_queue = dispatch_queue_create("com.facebook.react.RCTDevSupportHttpHeaders", DISPATCH_QUEUE_SERIAL);
30+
}
31+
return self;
32+
}
33+
34+
- (void)addRequestHeader:(NSString *)name value:(NSString *)value
35+
{
36+
dispatch_sync(_queue, ^{
37+
self->_headers[name] = value;
38+
});
39+
}
40+
41+
- (void)removeRequestHeader:(NSString *)name
42+
{
43+
dispatch_sync(_queue, ^{
44+
[self->_headers removeObjectForKey:name];
45+
});
46+
}
47+
48+
- (NSDictionary<NSString *, NSString *> *)allHeaders
49+
{
50+
__block NSDictionary<NSString *, NSString *> *snapshot;
51+
dispatch_sync(_queue, ^{
52+
snapshot = [self->_headers copy];
53+
});
54+
return snapshot;
55+
}
56+
57+
- (void)applyHeadersToRequest:(NSMutableURLRequest *)request
58+
{
59+
NSDictionary<NSString *, NSString *> *headers = [self allHeaders];
60+
[headers enumerateKeysAndObjectsUsingBlock:^(NSString *headerName, NSString *headerValue, BOOL *stop) {
61+
[request setValue:headerValue forHTTPHeaderField:headerName];
62+
}];
63+
}
64+
65+
@end

0 commit comments

Comments
 (0)