From 65b3dd2643c1c5ede087e6b9707621adc04b01a8 Mon Sep 17 00:00:00 2001 From: Alex Hunt Date: Mon, 23 Nov 2026 16:37:20 +0000 Subject: [PATCH] [NetworkReporter][iOS] Support redirects in RCTInspectorNetworkReporter --- .../Network/RCTInspectorNetworkReporter.h | 12 ++++++++++ .../Network/RCTInspectorNetworkReporter.mm | 23 ++++++++++++++++++- .../api-snapshots/ReactAppleDebugCxx.api | 1 + .../api-snapshots/ReactAppleFrameworksCxx.api | 1 + .../api-snapshots/ReactAppleNewarchCxx.api | 1 + .../api-snapshots/ReactAppleReleaseCxx.api | 1 + 6 files changed, 38 insertions(+), 1 deletion(-) diff --git a/packages/react-native/Libraries/Network/RCTInspectorNetworkReporter.h b/packages/react-native/Libraries/Network/RCTInspectorNetworkReporter.h index d3ea35f66e13..383273f2d9b4 100644 --- a/packages/react-native/Libraries/Network/RCTInspectorNetworkReporter.h +++ b/packages/react-native/Libraries/Network/RCTInspectorNetworkReporter.h @@ -34,6 +34,18 @@ request:(NSURLRequest *)request encodedDataLength:(int)encodedDataLength; +/** + * Report a network request that is about to be sent after following a + * redirect, under the same `requestId` as the request that was redirected. + * `encodedDataLength` is the size of `redirectResponse`. + * + * - Corresponds to `Network.requestWillBeSent` with `redirectResponse` in CDP. + */ ++ (void)reportRequestStart:(NSString *)requestId + request:(NSURLRequest *)request + encodedDataLength:(int)encodedDataLength + redirectResponse:(NSHTTPURLResponse *)redirectResponse; + /** * Report timestamp for sending the network request, and (in a debug build) * provide final headers to be reported via CDP. diff --git a/packages/react-native/Libraries/Network/RCTInspectorNetworkReporter.mm b/packages/react-native/Libraries/Network/RCTInspectorNetworkReporter.mm index 50eb801950da..8cfd1d0212c0 100644 --- a/packages/react-native/Libraries/Network/RCTInspectorNetworkReporter.mm +++ b/packages/react-native/Libraries/Network/RCTInspectorNetworkReporter.mm @@ -85,6 +85,14 @@ @implementation RCTInspectorNetworkReporter { + (void)reportRequestStart:(NSString *)requestId request:(NSURLRequest *)request encodedDataLength:(int)encodedDataLength +{ + [self reportRequestStart:requestId request:request encodedDataLength:encodedDataLength redirectResponse:nil]; +} + ++ (void)reportRequestStart:(NSString *)requestId + request:(NSURLRequest *)request + encodedDataLength:(int)encodedDataLength + redirectResponse:(NSHTTPURLResponse *)redirectResponse { RequestInfo requestInfo; requestInfo.url = [request.URL absoluteString].UTF8String; @@ -95,7 +103,20 @@ + (void)reportRequestStart:(NSString *)requestId requestInfo.httpBody = convertRequestBodyToStringTruncated(request); #endif - NetworkReporter::getInstance().reportRequestStart(requestId.UTF8String, requestInfo, encodedDataLength, std::nullopt); + std::optional redirectResponseInfo; + if (redirectResponse != nil) { + ResponseInfo responseInfo; + responseInfo.url = redirectResponse.URL.absoluteString.UTF8String; + responseInfo.statusCode = static_cast(redirectResponse.statusCode); +#ifdef REACT_NATIVE_DEBUGGER_ENABLED + responseInfo.headers = + convertNSDictionaryToHeaders((NSDictionary *)redirectResponse.allHeaderFields); +#endif + redirectResponseInfo = std::move(responseInfo); + } + + NetworkReporter::getInstance().reportRequestStart( + requestId.UTF8String, requestInfo, encodedDataLength, redirectResponseInfo); } + (void)reportConnectionTiming:(NSString *)requestId request:(NSURLRequest *)request diff --git a/scripts/cxx-api/api-snapshots/ReactAppleDebugCxx.api b/scripts/cxx-api/api-snapshots/ReactAppleDebugCxx.api index 5733ae36e244..039c38331268 100644 --- a/scripts/cxx-api/api-snapshots/ReactAppleDebugCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactAppleDebugCxx.api @@ -1194,6 +1194,7 @@ interface RCTInspectorNetworkReporter : public NSObject { public virtual static void reportDataReceived:data:(NSString* requestId, NSData* data); public virtual static void reportRequestFailed:cancelled:(NSString* requestId, BOOL cancelled); public virtual static void reportRequestStart:request:encodedDataLength:(NSString* requestId, NSURLRequest* request, int encodedDataLength); + public virtual static void reportRequestStart:request:encodedDataLength:redirectResponse:(NSString* requestId, NSURLRequest* request, int encodedDataLength, NSHTTPURLResponse* redirectResponse); public virtual static void reportResponseEnd:encodedDataLength:(NSString* requestId, int encodedDataLength); public virtual static void reportResponseStart:response:statusCode:headers:(NSString* requestId, NSURLResponse* response, int statusCode, NSDictionary* headers); } diff --git a/scripts/cxx-api/api-snapshots/ReactAppleFrameworksCxx.api b/scripts/cxx-api/api-snapshots/ReactAppleFrameworksCxx.api index af897d88f1b6..6485ca36f74d 100644 --- a/scripts/cxx-api/api-snapshots/ReactAppleFrameworksCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactAppleFrameworksCxx.api @@ -1217,6 +1217,7 @@ interface RCTInspectorNetworkReporter : public NSObject { public virtual static void reportDataReceived:data:(NSString* requestId, NSData* data); public virtual static void reportRequestFailed:cancelled:(NSString* requestId, BOOL cancelled); public virtual static void reportRequestStart:request:encodedDataLength:(NSString* requestId, NSURLRequest* request, int encodedDataLength); + public virtual static void reportRequestStart:request:encodedDataLength:redirectResponse:(NSString* requestId, NSURLRequest* request, int encodedDataLength, NSHTTPURLResponse* redirectResponse); public virtual static void reportResponseEnd:encodedDataLength:(NSString* requestId, int encodedDataLength); public virtual static void reportResponseStart:response:statusCode:headers:(NSString* requestId, NSURLResponse* response, int statusCode, NSDictionary* headers); } diff --git a/scripts/cxx-api/api-snapshots/ReactAppleNewarchCxx.api b/scripts/cxx-api/api-snapshots/ReactAppleNewarchCxx.api index 70d6e5c30013..b39b21693d72 100644 --- a/scripts/cxx-api/api-snapshots/ReactAppleNewarchCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactAppleNewarchCxx.api @@ -1194,6 +1194,7 @@ interface RCTInspectorNetworkReporter : public NSObject { public virtual static void reportDataReceived:data:(NSString* requestId, NSData* data); public virtual static void reportRequestFailed:cancelled:(NSString* requestId, BOOL cancelled); public virtual static void reportRequestStart:request:encodedDataLength:(NSString* requestId, NSURLRequest* request, int encodedDataLength); + public virtual static void reportRequestStart:request:encodedDataLength:redirectResponse:(NSString* requestId, NSURLRequest* request, int encodedDataLength, NSHTTPURLResponse* redirectResponse); public virtual static void reportResponseEnd:encodedDataLength:(NSString* requestId, int encodedDataLength); public virtual static void reportResponseStart:response:statusCode:headers:(NSString* requestId, NSURLResponse* response, int statusCode, NSDictionary* headers); } diff --git a/scripts/cxx-api/api-snapshots/ReactAppleReleaseCxx.api b/scripts/cxx-api/api-snapshots/ReactAppleReleaseCxx.api index 4accfd8303a1..921f8d5543dd 100644 --- a/scripts/cxx-api/api-snapshots/ReactAppleReleaseCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactAppleReleaseCxx.api @@ -1194,6 +1194,7 @@ interface RCTInspectorNetworkReporter : public NSObject { public virtual static void reportDataReceived:data:(NSString* requestId, NSData* data); public virtual static void reportRequestFailed:cancelled:(NSString* requestId, BOOL cancelled); public virtual static void reportRequestStart:request:encodedDataLength:(NSString* requestId, NSURLRequest* request, int encodedDataLength); + public virtual static void reportRequestStart:request:encodedDataLength:redirectResponse:(NSString* requestId, NSURLRequest* request, int encodedDataLength, NSHTTPURLResponse* redirectResponse); public virtual static void reportResponseEnd:encodedDataLength:(NSString* requestId, int encodedDataLength); public virtual static void reportResponseStart:response:statusCode:headers:(NSString* requestId, NSURLResponse* response, int statusCode, NSDictionary* headers); }