Skip to content

Commit 99ee571

Browse files
cortinicometa-codesync[bot]
authored andcommitted
Add custom request header support to DevSupportHttpClient (facebook#55582)
Summary: Pull Request resolved: facebook#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
1 parent 0ebe6b6 commit 99ee571

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/devsupport/inspector/DevSupportHttpClient.kt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,30 @@
99

1010
package com.facebook.react.devsupport.inspector
1111

12+
import java.util.concurrent.ConcurrentHashMap
1213
import java.util.concurrent.TimeUnit
14+
import okhttp3.Interceptor
1315
import okhttp3.OkHttpClient
1416

1517
/**
1618
* Shared [OkHttpClient] instances for devsupport networking. Uses a single connection pool and
1719
* dispatcher across all dev support HTTP and WebSocket usage.
1820
*/
1921
internal object DevSupportHttpClient {
22+
private val customHeaders = ConcurrentHashMap<String, String>()
23+
24+
private val headerInterceptor = Interceptor { chain ->
25+
val builder = chain.request().newBuilder()
26+
for ((name, value) in customHeaders) {
27+
builder.header(name, value)
28+
}
29+
chain.proceed(builder.build())
30+
}
31+
2032
/** Client for HTTP requests: connect=5s, write=disabled, read=disabled. */
2133
val httpClient: OkHttpClient =
2234
OkHttpClient.Builder()
35+
.addInterceptor(headerInterceptor)
2336
.connectTimeout(5, TimeUnit.SECONDS)
2437
.writeTimeout(0, TimeUnit.MILLISECONDS)
2538
.readTimeout(0, TimeUnit.MINUTES)
@@ -32,4 +45,14 @@ internal object DevSupportHttpClient {
3245
.connectTimeout(10, TimeUnit.SECONDS)
3346
.writeTimeout(10, TimeUnit.SECONDS)
3447
.build()
48+
49+
/** Add a custom header to be included in all requests made through both clients. */
50+
fun addRequestHeader(name: String, value: String) {
51+
customHeaders[name] = value
52+
}
53+
54+
/** Remove a previously added custom header. */
55+
fun removeRequestHeader(name: String) {
56+
customHeaders.remove(name)
57+
}
3558
}

0 commit comments

Comments
 (0)