mirror of
https://github.com/facebook/react-native.git
synced 2026-11-18 20:19:07 +00:00
Add custom request header support to DevSupportHttpClient (#55582)
Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/55582 Add addRequestHeader/removeRequestHeader methods to DevSupportHttpClient. These allow callers to register custom HTTP headers that will be included in all requests made through both the httpClient and websocketClient. Implemented via a shared OkHttp Interceptor that reads from a ConcurrentHashMap, so headers can be safely added/removed from any thread. Changelog: [Internal] Reviewed By: cipolleschi Differential Revision: D93481539 fbshipit-source-id: e8d6bc4c9d21c65a0f4d95065eaadff776933c55
This commit is contained in:
1 parent
0ebe6b6ecc
commit
99ee571d8f
1 file changed
+23
+23
@@ -9,7 +9,9 @@
|
||||
|
||||
package com.facebook.react.devsupport.inspector
|
||||
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
import java.util.concurrent.TimeUnit
|
||||
import okhttp3.Interceptor
|
||||
import okhttp3.OkHttpClient
|
||||
|
||||
/**
|
||||
@@ -17,9 +19,20 @@ import okhttp3.OkHttpClient
|
||||
* dispatcher across all dev support HTTP and WebSocket usage.
|
||||
*/
|
||||
internal object DevSupportHttpClient {
|
||||
private val customHeaders = ConcurrentHashMap<String, String>()
|
||||
|
||||
private val headerInterceptor = Interceptor { chain ->
|
||||
val builder = chain.request().newBuilder()
|
||||
for ((name, value) in customHeaders) {
|
||||
builder.header(name, value)
|
||||
}
|
||||
chain.proceed(builder.build())
|
||||
}
|
||||
|
||||
/** Client for HTTP requests: connect=5s, write=disabled, read=disabled. */
|
||||
val httpClient: OkHttpClient =
|
||||
OkHttpClient.Builder()
|
||||
.addInterceptor(headerInterceptor)
|
||||
.connectTimeout(5, TimeUnit.SECONDS)
|
||||
.writeTimeout(0, TimeUnit.MILLISECONDS)
|
||||
.readTimeout(0, TimeUnit.MINUTES)
|
||||
@@ -32,4 +45,14 @@ internal object DevSupportHttpClient {
|
||||
.connectTimeout(10, TimeUnit.SECONDS)
|
||||
.writeTimeout(10, TimeUnit.SECONDS)
|
||||
.build()
|
||||
|
||||
/** Add a custom header to be included in all requests made through both clients. */
|
||||
fun addRequestHeader(name: String, value: String) {
|
||||
customHeaders[name] = value
|
||||
}
|
||||
|
||||
/** Remove a previously added custom header. */
|
||||
fun removeRequestHeader(name: String) {
|
||||
customHeaders.remove(name)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user