Skip to content

Commit 0c7d780

Browse files
cipolleschimeta-codesync[bot]
authored andcommitted
Add RCTHTTPRequestInterceptor for selective HTTP header injection (#55869)
Summary: Pull Request resolved: #55869 ## Summary: Replace the blind application of dev headers on all HTTP requests with an interceptor pattern. A custom `RCTHTTPRequestInterceptor` block can be registered via `RCTSetCustomHTTPRequestInterceptor` to inspect and modify HTTP requests before they are sent. The interceptor returns a modified request to override, or nil to use the original request unchanged. This preserves the shared NSURLSession configuration (including meta-auth mTLS) while allowing selective per-request header injection. - Declare `RCTHTTPRequestInterceptor` typedef and `RCTSetCustomHTTPRequestInterceptor` in RCTHTTPRequestHandler.h - Implement the interceptor hook in RCTHTTPRequestHandler.mm - Remove the previous blind `RCTDevSupportHttpHeaders` application ## Changelog: [IOS][CHANGED] - Add RCTHTTPRequestInterceptor for selective HTTP request modification ## Test Plan: Verified that the RCTHTTPRequestInterceptor block is called when registered, and that returning nil leaves the original request unchanged. When no interceptor is registered, behavior is unchanged from before. ## Facebook: Applied from nest/mobile/apps/atod-sample/patches/react-native+0.83.1+006+atod-ios-http-handler.patch Reviewed By: cortinico Differential Revision: D95037920 fbshipit-source-id: ccd2c99c38eecb7fb5b074e7fce6f5abc3b6b3a1
1 parent f3aa8c2 commit 0c7d780

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

packages/react-native/Libraries/Network/RCTHTTPRequestHandler.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@ typedef NSURLSessionConfiguration * (^NSURLSessionConfigurationProvider)(void);
1414
* app.
1515
*/
1616
RCT_EXTERN void RCTSetCustomNSURLSessionConfigurationProvider(NSURLSessionConfigurationProvider /*provider*/);
17+
18+
typedef NSURLRequest *_Nullable (^RCTHTTPRequestInterceptor)(NSURLRequest *request);
19+
/**
20+
* The block provided via this function can inspect/modify HTTP requests before
21+
* they are sent. Return a modified request to override, or nil to use the
22+
* original request unchanged.
23+
*/
24+
RCT_EXTERN void RCTSetCustomHTTPRequestInterceptor(RCTHTTPRequestInterceptor /*interceptor*/);
25+
1726
/**
1827
* This is the default RCTURLRequestHandler implementation for HTTP requests.
1928
*/

packages/react-native/Libraries/Network/RCTHTTPRequestHandler.mm

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ void RCTSetCustomNSURLSessionConfigurationProvider(NSURLSessionConfigurationProv
2525
urlSessionConfigurationProvider = provider;
2626
}
2727

28+
static RCTHTTPRequestInterceptor httpRequestInterceptor;
29+
30+
void RCTSetCustomHTTPRequestInterceptor(RCTHTTPRequestInterceptor interceptor)
31+
{
32+
httpRequestInterceptor = interceptor;
33+
}
34+
2835
@implementation RCTHTTPRequestHandler {
2936
NSMapTable *_delegates;
3037
NSURLSession *_session;
@@ -99,7 +106,14 @@ - (NSURLSessionDataTask *)sendRequest:(NSURLRequest *)request withDelegate:(id<R
99106
valueOptions:NSPointerFunctionsStrongMemory
100107
capacity:0];
101108
}
102-
NSURLSessionDataTask *task = [_session dataTaskWithRequest:request];
109+
NSURLRequest *finalRequest = request;
110+
if (httpRequestInterceptor != nullptr) {
111+
NSURLRequest *intercepted = httpRequestInterceptor(request);
112+
if (intercepted != nil) {
113+
finalRequest = intercepted;
114+
}
115+
}
116+
NSURLSessionDataTask *task = [_session dataTaskWithRequest:finalRequest];
103117
[_delegates setObject:delegate forKey:task];
104118
[task resume];
105119
return task;

0 commit comments

Comments
 (0)