Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import okhttp3.HttpUrl
import okhttp3.MediaType
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.OkHttpClient
import okhttp3.dnsoverhttps.internal.DnsOverHttpsTransport
import okhttp3.dnsoverhttps.internal.DnsOverHttpsQuery
import okhttp3.internal.dns.StateMachineDnsCall
import okhttp3.internal.dns.execute
import okhttp3.internal.publicsuffix.PublicSuffixDatabase
Expand All @@ -46,8 +46,8 @@ class DnsOverHttps internal constructor(
@get:JvmName("resolvePrivateAddresses") val resolvePrivateAddresses: Boolean,
@get:JvmName("resolvePublicAddresses") val resolvePublicAddresses: Boolean,
) : Dns {
private val transport =
DnsOverHttpsTransport(
private val queryFactory =
DnsOverHttpsQuery.Factory(
client = client,
dnsUrl = url,
post = post,
Expand All @@ -56,7 +56,7 @@ class DnsOverHttps internal constructor(
override fun newCall(request: Dns.Request): Dns.Call =
StateMachineDnsCall(
request = request,
transport = transport,
queryFactory = queryFactory,
canceledException = validate(request.hostname),
includeIPv6 = includeIPv6,
includeServiceMetadata = includeServiceMetadata,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
@file:Suppress("ktlint:standard:filename")

package okhttp3.dnsoverhttps.internal

import java.io.IOException
Expand All @@ -25,60 +27,24 @@ import okhttp3.Protocol
import okhttp3.Request
import okhttp3.RequestBody
import okhttp3.Response
import okhttp3.dnsoverhttps.DnsOverHttps
import okhttp3.dnsoverhttps.DnsOverHttps.Companion.DNS_MESSAGE
import okhttp3.dnsoverhttps.DnsOverHttps.Companion.MAX_RESPONSE_SIZE
import okhttp3.internal.OkHttpInternalApi
import okhttp3.internal.dns.DnsMessage
import okhttp3.internal.dns.DnsMessageReader
import okhttp3.internal.dns.DnsMessageWriter
import okhttp3.internal.dns.DnsQuery
import okhttp3.internal.dns.Question
import okhttp3.internal.dns.StateMachineDnsCall
import okhttp3.internal.platform.Platform
import okio.Buffer
import okio.BufferedSink

@OkHttpInternalApi
internal class DnsOverHttpsTransport(
private val client: OkHttpClient,
private val dnsUrl: HttpUrl,
private val post: Boolean,
) : StateMachineDnsCall.Transport<Call> {
override fun newQuery(question: Question): Call {
val dnsMessage = DnsMessage.query(question)
return client.newCall(
request =
Request
.Builder()
.header("Accept", DnsOverHttps.DNS_MESSAGE.toString())
.apply {
if (post) {
url(dnsUrl)
cacheUrlOverride(
dnsUrl
.newBuilder()
.addQueryParameter("hostname", question.name)
.addQueryParameter("type", question.type.toString())
.build(),
)
post(QueryRequestBody(dnsMessage))
} else {
val requestUrl =
dnsUrl
.newBuilder()
.addQueryParameter("dns", dnsMessage.asQueryParameter())
.build()
url(requestUrl)
}
}.build(),
)
}

override fun enqueue(
query: Call,
callback: StateMachineDnsCall.Transport.Callback<Call>,
) {
query.enqueue(
internal class DnsOverHttpsQuery(
val call: Call,
) : DnsQuery {
override fun enqueue(callback: DnsQuery.Callback) {
call.enqueue(
object : Callback {
override fun onFailure(
call: Call,
Expand All @@ -104,8 +70,47 @@ internal class DnsOverHttpsTransport(
)
}

override fun cancel(query: Call) {
query.cancel()
override fun cancel() {
call.cancel()
}

class Factory(
private val client: OkHttpClient,
private val dnsUrl: HttpUrl,
private val post: Boolean,
) : DnsQuery.Factory {
override fun newQuery(question: Question): DnsQuery {
val dnsMessage = DnsMessage.query(question)
return DnsOverHttpsQuery(
call =
client.newCall(
request =
Request
.Builder()
.header("Accept", DNS_MESSAGE.toString())
.apply {
if (post) {
url(dnsUrl)
cacheUrlOverride(
dnsUrl
.newBuilder()
.addQueryParameter("hostname", question.name)
.addQueryParameter("type", question.type.toString())
.build(),
)
post(QueryRequestBody(dnsMessage))
} else {
val requestUrl =
dnsUrl
.newBuilder()
.addQueryParameter("dns", dnsMessage.asQueryParameter())
.build()
url(requestUrl)
}
}.build(),
),
)
}
}
}

Expand Down
26 changes: 5 additions & 21 deletions okhttp-testing-support/src/main/kotlin/okhttp3/FakeDns.kt
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,11 @@ class FakeDns(
}
}

return dnsResponse(request, answers)
return DnsMessage.response(
id = request.id,
questions = request.questions,
answers = answers,
)
}

private fun ResourceRecord.matches(question: Question): Boolean {
Expand Down Expand Up @@ -323,23 +327,3 @@ class FakeDns(
) : Request
}
}

fun dnsResponse(
request: DnsMessage,
answers: List<ResourceRecord>,
): DnsMessage {
// QR = 1 (Response)
// OPCODE = 0 (standard query)
// RD = 1 (Recursion Desired)
// RA = 1 (Recursion Available)
// RCODE = 0 (success)
// QR OPCODE AA TC RD RA Z RCODE
val flags = 0b1___0000__0__0__1__1_000__0000

return DnsMessage(
id = request.id,
flags = flags,
questions = request.questions,
answers = answers,
)
}
Loading
Loading