mirror of
https://github.com/square/okhttp.git
synced 2025-08-07 12:42:57 +03:00
Idiomatic Kotlin in DnsOverHttps.kt
- define the following vals in constructor instead of passing `builder: Builder`. - `@get:JvmName(...)` val - `client: OkHttpClient` - `url: HttpUrl` - `includeIPv6: Boolean` - `post: Boolean` - `resolvePrivateAddresses: Boolean` - `resolvePublicAddresses: Boolean` - `private` val - `systemDns: Dns` - `bootstrapDnsHosts: List<InetAddress>?` - add `@Deprecated(...)` to the following functions. - `fun client(): OkHttpClient` - `fun url(): HttpUrl` - `fun includeIPv6(): Boolean` - `fun post(): Boolean` - `fun resolvePrivateAddresses(): Boolean` - `fun resolvePublicAddresses(): Boolean`
This commit is contained in:
@@ -50,26 +50,59 @@ import java.util.concurrent.CountDownLatch
|
|||||||
*
|
*
|
||||||
* [doh_spec]: https://tools.ietf.org/html/draft-ietf-doh-dns-over-https-13
|
* [doh_spec]: https://tools.ietf.org/html/draft-ietf-doh-dns-over-https-13
|
||||||
*/
|
*/
|
||||||
class DnsOverHttps internal constructor(builder: Builder) : Dns {
|
class DnsOverHttps internal constructor(
|
||||||
private val client: OkHttpClient =
|
@get:JvmName("client") val client: OkHttpClient,
|
||||||
builder.client?.newBuilder()?.dns(buildBootstrapClient(builder))?.build()
|
@get:JvmName("url") val url: HttpUrl,
|
||||||
?: throw NullPointerException("client not set")
|
@get:JvmName("includeIPv6") val includeIPv6: Boolean,
|
||||||
private val url: HttpUrl = builder.url ?: throw NullPointerException("url not set")
|
@get:JvmName("post") val post: Boolean,
|
||||||
private val includeIPv6: Boolean = builder.includeIPv6
|
@get:JvmName("resolvePrivateAddresses") val resolvePrivateAddresses: Boolean,
|
||||||
private val post: Boolean = builder.post
|
@get:JvmName("resolvePublicAddresses") val resolvePublicAddresses: Boolean,
|
||||||
private val resolvePrivateAddresses: Boolean = builder.resolvePrivateAddresses
|
private val systemDns: Dns,
|
||||||
private val resolvePublicAddresses: Boolean = builder.resolvePublicAddresses
|
private val bootstrapDnsHosts: List<InetAddress>?
|
||||||
|
) : Dns {
|
||||||
|
|
||||||
|
@JvmName("-deprecated_url")
|
||||||
|
@Deprecated(
|
||||||
|
message = "moved to val",
|
||||||
|
replaceWith = ReplaceWith(expression = "url"),
|
||||||
|
level = DeprecationLevel.WARNING)
|
||||||
fun url(): HttpUrl = url
|
fun url(): HttpUrl = url
|
||||||
|
|
||||||
|
@JvmName("-deprecated_post")
|
||||||
|
@Deprecated(
|
||||||
|
message = "moved to val",
|
||||||
|
replaceWith = ReplaceWith(expression = "post"),
|
||||||
|
level = DeprecationLevel.WARNING)
|
||||||
fun post(): Boolean = post
|
fun post(): Boolean = post
|
||||||
|
|
||||||
|
@JvmName("-deprecated_includeIPv6")
|
||||||
|
@Deprecated(
|
||||||
|
message = "moved to val",
|
||||||
|
replaceWith = ReplaceWith(expression = "includeIPv6"),
|
||||||
|
level = DeprecationLevel.WARNING)
|
||||||
fun includeIPv6(): Boolean = includeIPv6
|
fun includeIPv6(): Boolean = includeIPv6
|
||||||
|
|
||||||
fun client(): OkHttpClient = client
|
@JvmName("-deprecated_client")
|
||||||
|
@Deprecated(
|
||||||
|
message = "moved to val",
|
||||||
|
replaceWith = ReplaceWith(expression = "client"),
|
||||||
|
level = DeprecationLevel.WARNING)
|
||||||
|
fun client(): OkHttpClient = client.newBuilder()
|
||||||
|
.dns(buildBootstrapClient(bootstrapDnsHosts, url, systemDns))
|
||||||
|
.build()
|
||||||
|
|
||||||
|
@JvmName("-deprecated_resolvePrivateAddresses")
|
||||||
|
@Deprecated(
|
||||||
|
message = "moved to val",
|
||||||
|
replaceWith = ReplaceWith(expression = "resolvePrivateAddresses"),
|
||||||
|
level = DeprecationLevel.WARNING)
|
||||||
fun resolvePrivateAddresses(): Boolean = resolvePrivateAddresses
|
fun resolvePrivateAddresses(): Boolean = resolvePrivateAddresses
|
||||||
|
|
||||||
|
@JvmName("-deprecated_resolvePublicAddresses")
|
||||||
|
@Deprecated(
|
||||||
|
message = "moved to val",
|
||||||
|
replaceWith = ReplaceWith(expression = "resolvePublicAddresses"),
|
||||||
|
level = DeprecationLevel.WARNING)
|
||||||
fun resolvePublicAddresses(): Boolean = resolvePublicAddresses
|
fun resolvePublicAddresses(): Boolean = resolvePublicAddresses
|
||||||
|
|
||||||
@Throws(UnknownHostException::class)
|
@Throws(UnknownHostException::class)
|
||||||
@@ -263,7 +296,18 @@ class DnsOverHttps internal constructor(builder: Builder) : Dns {
|
|||||||
internal var resolvePrivateAddresses = false
|
internal var resolvePrivateAddresses = false
|
||||||
internal var resolvePublicAddresses = true
|
internal var resolvePublicAddresses = true
|
||||||
|
|
||||||
fun build(): DnsOverHttps = DnsOverHttps(this)
|
fun build(): DnsOverHttps {
|
||||||
|
return DnsOverHttps(
|
||||||
|
checkNotNull(client) { "client not set" },
|
||||||
|
checkNotNull(url) { "url not set" },
|
||||||
|
includeIPv6,
|
||||||
|
post,
|
||||||
|
resolvePrivateAddresses,
|
||||||
|
resolvePublicAddresses,
|
||||||
|
systemDns,
|
||||||
|
bootstrapDnsHosts
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
fun client(client: OkHttpClient) = apply {
|
fun client(client: OkHttpClient) = apply {
|
||||||
this.client = client
|
this.client = client
|
||||||
@@ -305,13 +349,15 @@ class DnsOverHttps internal constructor(builder: Builder) : Dns {
|
|||||||
val DNS_MESSAGE: MediaType = MediaType.get("application/dns-message")
|
val DNS_MESSAGE: MediaType = MediaType.get("application/dns-message")
|
||||||
const val MAX_RESPONSE_SIZE = 64 * 1024
|
const val MAX_RESPONSE_SIZE = 64 * 1024
|
||||||
|
|
||||||
private fun buildBootstrapClient(builder: Builder): Dns {
|
private fun buildBootstrapClient(
|
||||||
val hosts = builder.bootstrapDnsHosts
|
bootstrapDnsHosts: List<InetAddress>?,
|
||||||
|
url: HttpUrl,
|
||||||
return if (hosts != null) {
|
systemDns: Dns
|
||||||
BootstrapDns(builder.url!!.host, hosts)
|
): Dns {
|
||||||
|
return if (bootstrapDnsHosts != null) {
|
||||||
|
BootstrapDns(url.host, bootstrapDnsHosts)
|
||||||
} else {
|
} else {
|
||||||
builder.systemDns
|
systemDns
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user