Skip to content

Commit 724f559

Browse files
fru1tworldbjhham
authored andcommitted
KTOR-9554 Add dns config to OkHttp engine
1 parent 000b416 commit 724f559

4 files changed

Lines changed: 55 additions & 0 deletions

File tree

ktor-client/ktor-client-okhttp/api/ktor-client-okhttp.api

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@ public final class io/ktor/client/engine/okhttp/OkHttpConfig : io/ktor/client/en
1212
public final fun addNetworkInterceptor (Lokhttp3/Interceptor;)V
1313
public final fun config (Lkotlin/jvm/functions/Function1;)V
1414
public final fun getClientCacheSize ()I
15+
public final fun getDns ()Lokhttp3/Dns;
1516
public final fun getDuplexStreamingEnabled ()Z
1617
public final fun getPreconfigured ()Lokhttp3/OkHttpClient;
1718
public final fun getWebSocketFactory ()Lokhttp3/WebSocket$Factory;
1819
public final fun setClientCacheSize (I)V
20+
public final fun setDns (Lokhttp3/Dns;)V
1921
public final fun setDuplexStreamingEnabled (Z)V
2022
public final fun setPreconfigured (Lokhttp3/OkHttpClient;)V
2123
public final fun setWebSocketFactory (Lokhttp3/WebSocket$Factory;)V

ktor-client/ktor-client-okhttp/jvm/src/io/ktor/client/engine/okhttp/OkHttpConfig.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,24 @@ public class OkHttpConfig : HttpClientEngineConfig() {
5555
*/
5656
public var duplexStreamingEnabled: Boolean = false
5757

58+
/**
59+
* Specifies the [Dns] resolver used by the [OkHttpClient] to look up IP addresses for hostnames.
60+
* When `null`, OkHttp's default [Dns.SYSTEM] resolver is used.
61+
*
62+
* Set this to inject a custom resolver, for example to enable DNS-over-HTTPS or
63+
* to override host resolution in tests:
64+
* ```kotlin
65+
* install(OkHttp) {
66+
* engine {
67+
* dns = Dns { hostname -> listOf(InetAddress.getByName("127.0.0.1")) }
68+
* }
69+
* }
70+
* ```
71+
*
72+
* [Report a problem](https://ktor.io/feedback/?fqname=io.ktor.client.engine.okhttp.OkHttpConfig.dns)
73+
*/
74+
public var dns: Dns? = null
75+
5876
/**
5977
* Configures [OkHttpClient] using [OkHttpClient.Builder].
6078
*

ktor-client/ktor-client-okhttp/jvm/src/io/ktor/client/engine/okhttp/OkHttpEngine.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ public class OkHttpEngine(override val config: OkHttpConfig) : HttpClientEngineB
152152
}
153153
builder.apply(config.config)
154154
config.proxy?.let { builder.proxy(it) }
155+
config.dns?.let { builder.dns(it) }
155156
timeoutExtension?.let {
156157
builder.setupTimeoutAttributes(it)
157158
}

ktor-client/ktor-client-okhttp/jvm/test/io/ktor/client/engine/okhttp/OkHttpEngineTest.kt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
package io.ktor.client.engine.okhttp
66

77
import okhttp3.Dispatcher
8+
import okhttp3.Dns
89
import okhttp3.OkHttpClient
10+
import java.net.InetAddress
911
import kotlin.test.Test
1012
import kotlin.test.assertSame
1113

@@ -26,4 +28,36 @@ class OkHttpEngineTest {
2628

2729
assertSame(dispatcher, client.dispatcher)
2830
}
31+
32+
@Test
33+
fun `dns config is applied to OkHttpClient`() {
34+
val customDns = Dns { _ -> listOf(InetAddress.getByName("127.0.0.1")) }
35+
36+
val engine = OkHttpEngine(OkHttpConfig().apply { dns = customDns })
37+
try {
38+
val cacheField = engine.javaClass.getDeclaredField("clientCache").apply { isAccessible = true }
39+
val clientCache = cacheField.get(engine) as Map<*, *>
40+
41+
val client = clientCache[null] as OkHttpClient
42+
43+
assertSame(customDns, client.dns)
44+
} finally {
45+
engine.close()
46+
}
47+
}
48+
49+
@Test
50+
fun `default dns is preserved when dns config is not set`() {
51+
val engine = OkHttpEngine(OkHttpConfig())
52+
try {
53+
val cacheField = engine.javaClass.getDeclaredField("clientCache").apply { isAccessible = true }
54+
val clientCache = cacheField.get(engine) as Map<*, *>
55+
56+
val client = clientCache[null] as OkHttpClient
57+
58+
assertSame(Dns.SYSTEM, client.dns)
59+
} finally {
60+
engine.close()
61+
}
62+
}
2963
}

0 commit comments

Comments
 (0)