Limit request body size for Network.requestWillBeSent (#51026)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/51026

Performance optimisation — truncate request body content (sent as `Request.postData` over CDP) at 1MB.

Changelog: [Internal]

Reviewed By: vzaidman

Differential Revision: D71905868

fbshipit-source-id: ba82b4937902e057d4694bf4f000eb77ee37aa8a
This commit is contained in:
Alex Hunt authored and Facebook GitHub Bot committed 2025-05-01 21:46:36 -07:00
1 parent 04f0e66a6b
commit e6870c68f9
1 file changed
+17 -1
@@ -23,6 +23,22 @@ Headers convertNSDictionaryToHeaders(const NSDictionary<NSString *, NSString *>
return responseHeaders;
}
std::string convertRequestBodyToStringTruncated(NSURLRequest *request)
{
const NSUInteger maxBodySize = 1024 * 1024; // 1MB
auto bodyLength = request.HTTPBody.length;
auto bytesToRead = std::min(bodyLength, maxBodySize);
auto body = std::string((const char *)request.HTTPBody.bytes, bytesToRead);
if (bytesToRead < bodyLength) {
body +=
"\n... [truncated, showing " + std::to_string(bytesToRead) + " of " + std::to_string(bodyLength) + " bytes]";
}
return body;
}
} // namespace
#endif
@@ -39,7 +55,7 @@ Headers convertNSDictionaryToHeaders(const NSDictionary<NSString *, NSString *>
#ifdef REACT_NATIVE_DEBUGGER_ENABLED
// Debug build: Process additional request info for CDP reporting
requestInfo.headers = convertNSDictionaryToHeaders(request.allHTTPHeaderFields);
requestInfo.httpBody = std::string((const char *)request.HTTPBody.bytes, request.HTTPBody.length);
requestInfo.httpBody = convertRequestBodyToStringTruncated(request);
#endif
NetworkReporter::getInstance().reportRequestStart(