mirror of
https://github.com/facebook/react-native.git
synced 2026-11-18 20:19:07 +00:00
Re-implement request body previews on Android (#55456)
Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/55456 **Context** Follow-up to D89373824, which addressed a runtime Android crash with `FormData` uploads (https://github.com/facebook/react-native/issues/54881) but introduced a "[Preview unavailable]" fallback for the `ProgressRequestBody` case. **This diff** Reintroduce request body previews under this case. Protects agains the original crash by: - Checking `body.isOneShot()` to prevent disallowed double-reads of stream bodies. - [Pre-existing] Preserves `BuildConfig.DEBUG` guard added in D89377163. Changelog: [Android][Added] - React Native DevTools: Restore request payload previews on Android Reviewed By: vzaidman Differential Revision: D89381124 fbshipit-source-id: ae6ce006285b4bcd7bbb09bb4413d119f18a8630
This commit is contained in:
1 parent
0d40f2a178
commit
cc44e96e0b
3 files changed
+38
-11
No files matched your search
+34
@@ -16,13 +16,18 @@ import com.facebook.react.bridge.ReactApplicationContext
|
||||
import com.facebook.react.bridge.WritableMap
|
||||
import com.facebook.react.bridge.buildReadableArray
|
||||
import com.facebook.react.internal.featureflags.ReactNativeFeatureFlags
|
||||
import java.io.IOException
|
||||
import java.net.SocketTimeoutException
|
||||
import okhttp3.Headers
|
||||
import okhttp3.RequestBody
|
||||
import okio.Buffer
|
||||
|
||||
/**
|
||||
* Utility class for reporting network lifecycle events to JavaScript and InspectorNetworkReporter.
|
||||
*/
|
||||
internal object NetworkEventUtil {
|
||||
private const val MAX_BODY_PREVIEW_SIZE = 512 * 1024 // 512KB
|
||||
|
||||
@JvmStatic
|
||||
fun onCreateRequest(
|
||||
devToolsRequestId: String,
|
||||
@@ -243,4 +248,33 @@ internal object NetworkEventUtil {
|
||||
}
|
||||
return responseHeaders
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
fun getRequestBodyPreview(requestBody: RequestBody?): String? {
|
||||
if (requestBody == null) {
|
||||
return null
|
||||
}
|
||||
|
||||
// Unwrap ProgressRequestBody
|
||||
val body = (requestBody as? ProgressRequestBody)?.innerBody() ?: requestBody
|
||||
|
||||
if (body.isOneShot()) {
|
||||
// Fallback - body cannot be read twice
|
||||
return "[Preview unavailable]"
|
||||
}
|
||||
|
||||
return try {
|
||||
val buffer = Buffer()
|
||||
body.writeTo(buffer)
|
||||
|
||||
val size = buffer.size()
|
||||
if (size <= MAX_BODY_PREVIEW_SIZE) {
|
||||
buffer.readUtf8()
|
||||
} else {
|
||||
buffer.readUtf8(MAX_BODY_PREVIEW_SIZE.toLong()) + "... (truncated, ${size} bytes total)"
|
||||
}
|
||||
} catch (e: IOException) {
|
||||
"[Preview unavailable]"
|
||||
}
|
||||
}
|
||||
}
|
||||
+2
-6
@@ -610,12 +610,8 @@ public class NetworkingModule(
|
||||
request.url().toString(),
|
||||
request.method(),
|
||||
NetworkEventUtil.okHttpHeadersToMap(request.headers()),
|
||||
if (ReactBuildConfig.DEBUG) {
|
||||
// Debug build: Include request body for preview in DevTools
|
||||
(request.body() as? ProgressRequestBody)?.getBodyPreview() ?: request.body()?.toString()
|
||||
} else {
|
||||
null
|
||||
},
|
||||
if (ReactBuildConfig.DEBUG) NetworkEventUtil.getRequestBodyPreview(request.body())
|
||||
else null,
|
||||
request.body()?.contentLength() ?: 0,
|
||||
)
|
||||
|
||||
|
||||
+2
-5
@@ -50,6 +50,8 @@ internal class ProgressRequestBody(
|
||||
sinkWrapper.flush()
|
||||
}
|
||||
|
||||
fun innerBody(): RequestBody = requestBody
|
||||
|
||||
private fun outputStreamSink(sink: BufferedSink): Sink {
|
||||
return Okio.sink(
|
||||
object : FilterOutputStream(sink.outputStream()) {
|
||||
@@ -78,9 +80,4 @@ internal class ProgressRequestBody(
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
fun getBodyPreview(): String {
|
||||
// TODO: Safely implement request body previews
|
||||
return "[Preview unavailable]"
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user