Repository navigation
KTOR-6886: Android Http Client using Android's HttpEngine on API 34+ #4013
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Yuri Schimke (yschimke)
wants to merge
1
commit into
ktorio:main
Choose a base branch
from
yschimke:android14
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
...tor-client-android/jvm/src/io/ktor/client/engine/android/Android14URLConnectionFactory.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| /* | ||
| * Copyright 2014-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. | ||
| */ | ||
|
|
||
| package io.ktor.client.engine.android | ||
|
|
||
| import android.net.http.HttpEngine | ||
| import android.net.http.ProxyOptions | ||
| import android.util.Pair | ||
| import kotlinx.atomicfu.atomic | ||
| import java.net.HttpURLConnection | ||
| import java.net.InetSocketAddress | ||
| import java.net.Proxy | ||
| import java.net.URI | ||
|
|
||
| // @RequiresExtension(extension = Build.VERSION_CODES.S, version = 7) | ||
| internal class AndroidNetHttpEngineFactory(private val config: AndroidEngineConfig) : URLConnectionFactory { | ||
| private val engineDelegate = lazy { buildEngine() } | ||
| private val engine by engineDelegate | ||
| private val closed = atomic(false) | ||
|
|
||
| private fun buildEngine(): HttpEngine { | ||
| val ctx = checkNotNull(config.context) { | ||
| "AndroidEngineConfig.context must be set when using HttpEngine; prefer applicationContext." | ||
| } | ||
| return HttpEngine.Builder(ctx) | ||
| .apply { | ||
| config.proxy?.let { ProxyHelper.setupProxy(this, it) } | ||
| config.httpEngineConfig?.invoke(this) | ||
| } | ||
| .build() | ||
| } | ||
|
|
||
| override operator fun invoke(urlString: String): HttpURLConnection { | ||
| return engine.openConnection(URI.create(urlString).toURL()) as HttpURLConnection | ||
| } | ||
|
|
||
| override fun close() { | ||
| if (closed.compareAndSet(false, true) && engineDelegate.isInitialized()) { | ||
| engine.shutdown() | ||
| } | ||
| } | ||
|
|
||
| // @RequiresExtension(extension = Build.VERSION_CODES.S, version = 22) | ||
| private object ProxyHelper { | ||
| fun setupProxy(builder: HttpEngine.Builder, proxy: Proxy) { | ||
| val address = proxy.address() as? InetSocketAddress ?: return | ||
| if (proxy.type() != Proxy.Type.HTTP) return | ||
|
|
||
| val httpProxy = android.net.http.Proxy.createHttpProxy( | ||
| android.net.http.Proxy.SCHEME_HTTP, | ||
| address.hostString, | ||
| address.port, | ||
| { it.run() }, | ||
| object : android.net.http.Proxy.HttpConnectCallback { | ||
| override fun onBeforeRequest(request: android.net.http.Proxy.HttpConnectCallback.Request) { | ||
| request.use { it.proceed(emptyList()) } | ||
| } | ||
|
|
||
| override fun onResponseReceived( | ||
| responseHeaders: List<Pair<String, String>>, | ||
| statusCode: Int | ||
| ): Int = android.net.http.Proxy.HttpConnectCallback.RESPONSE_ACTION_PROCEED | ||
| } | ||
| ) | ||
| builder.setProxyOptions( | ||
| ProxyOptions.fromProxyList( | ||
| listOf(httpProxy), | ||
| ProxyOptions.ALL_PROXIES_FAILED_BEHAVIOR_DISALLOW_DIRECT | ||
| ) | ||
| ) | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
...-client/ktor-client-android/jvm/src/io/ktor/client/engine/android/URLConnectionFactory.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| /* | ||
| * Copyright 2014-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. | ||
| */ | ||
|
|
||
| package io.ktor.client.engine.android | ||
|
|
||
| import java.io.Closeable | ||
| import java.net.* | ||
|
|
||
| internal interface URLConnectionFactory : Closeable { | ||
| operator fun invoke(urlString: String): HttpURLConnection | ||
|
|
||
| override fun close() {} | ||
|
|
||
| class StandardURLConnectionFactory(val config: AndroidEngineConfig) : URLConnectionFactory { | ||
| override operator fun invoke(urlString: String): HttpURLConnection { | ||
| val url = URL(urlString) | ||
| val connection: URLConnection = config.proxy?.let { url.openConnection(it) } ?: url.openConnection() | ||
| return connection as HttpURLConnection | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.