diff --git a/packages/react-native/Libraries/Network/RCTInspectorNetworkReporter.h b/packages/react-native/Libraries/Network/RCTInspectorNetworkReporter.h index d3ea35f66e1..383273f2d9b 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 baa3d1b0ae1..53c91081d5e 100644 --- a/packages/react-native/Libraries/Network/RCTInspectorNetworkReporter.mm +++ b/packages/react-native/Libraries/Network/RCTInspectorNetworkReporter.mm @@ -81,6 +81,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; @@ -91,7 +99,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 d9b5033ad90..39d8e914501 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 3c64308d98f..9a0047775f4 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 280067df592..3dd65642f97 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 4d607c7ed5e..d48b82f43a8 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); }