Ktor HTTP client core library providing asynchronous HTTP client functionality for multiplatform applications with macOS ARM64 support.
—
HTTP engine abstraction providing platform-specific implementations and configuration options for connection management, timeouts, and networking behavior across different platforms.
Core engine interface providing platform-specific HTTP implementations.
/**
* HTTP client engine interface for platform-specific implementations
*/
interface HttpClientEngine : CoroutineScope, Closeable {
/** Engine configuration */
val config: HttpClientEngineConfig
/** Coroutine dispatcher for engine operations */
val dispatcher: CoroutineDispatcher
/** Set of capabilities supported by this engine */
val supportedCapabilities: Set<HttpClientEngineCapability<*>>
/** Execute HTTP request and return response data */
suspend fun execute(data: HttpRequestData): HttpResponseData
/** Install engine into HTTP client */
fun install(client: HttpClient)
/** Close engine and release resources */
override fun close()
}Base configuration class for HTTP client engines.
/**
* Base configuration for HTTP client engines
*/
open class HttpClientEngineConfig {
/** Custom coroutine dispatcher for engine operations */
var dispatcher: CoroutineDispatcher? = null
/** Enable HTTP pipelining (where supported) */
var pipelining: Boolean = false
/** Proxy configuration */
var proxy: ProxyConfig? = null
/** Local address to bind connections */
var localAddress: SocketAddress? = null
/** Configure proxy from URL string */
fun proxy(url: String) {
proxy = ProxyConfig.parseUrl(url)
}
}Interface for creating HTTP client engines.
/**
* Factory interface for creating HTTP client engines
* @param T Engine configuration type
*/
interface HttpClientEngineFactory<out T : HttpClientEngineConfig> {
/** Create engine instance with configuration */
fun create(block: T.() -> Unit = {}): HttpClientEngine
}System for declaring and checking engine capabilities.
/**
* Marker for HTTP client engine capabilities
* @param T Capability configuration type
*/
class HttpClientEngineCapability<T : Any>(private val key: String) {
override fun toString(): String = "Capability[$key]"
}
/** Check if client supports specific capability */
fun HttpClient.isSupported(capability: HttpClientEngineCapability<*>): Boolean/**
* Proxy configuration for HTTP requests
*/
class ProxyConfig(
/** Proxy type (HTTP or SOCKS) */
val type: ProxyType,
/** Proxy server address */
val address: SocketAddress
) {
companion object {
/** Parse proxy configuration from URL */
fun parseUrl(url: String): ProxyConfig
}
}
/**
* Proxy protocol types
*/
enum class ProxyType {
/** HTTP proxy */
HTTP,
/** SOCKS proxy */
SOCKS
}Usage Examples:
import io.ktor.client.*
import io.ktor.client.engine.*
// HTTP proxy configuration
val httpClient = HttpClient {
engine {
proxy = ProxyConfig(
type = ProxyType.HTTP,
address = InetSocketAddress("proxy.example.com", 8080)
)
}
}
// Proxy from URL string
val proxyClient = HttpClient {
engine {
proxy("http://proxy.example.com:8080")
}
}
// SOCKS proxy
val socksClient = HttpClient {
engine {
proxy = ProxyConfig(
type = ProxyType.SOCKS,
address = InetSocketAddress("socks.example.com", 1080)
)
}
}Usage Examples:
import io.ktor.client.*
import io.ktor.client.engine.cio.*
import kotlinx.coroutines.Dispatchers
// Basic engine configuration
val client = HttpClient(CIO) {
engine {
// Enable HTTP pipelining
pipelining = true
// Use custom dispatcher
dispatcher = Dispatchers.IO
// Set local address
localAddress = InetSocketAddress("192.168.1.100", 0)
// Configure proxy
proxy("http://proxy.company.com:8080")
}
}
// Platform-specific configuration
val platformClient = HttpClient {
engine {
// Configure based on platform capabilities
if (isJvmPlatform()) {
// JVM-specific settings
pipelining = true
} else if (isNativePlatform()) {
// Native-specific settings
dispatcher = Dispatchers.Default
}
}
}
// Check engine capabilities
if (client.isSupported(HttpTimeout)) {
println("Engine supports timeout configuration")
}
if (client.isSupported(WebSocketCapability)) {
println("Engine supports WebSockets")
}Different platforms provide different HTTP engine implementations:
JVM Engines:
Native Engines:
JavaScript Engine:
/**
* Platform-specific engine creation examples
*/
// JVM - Apache engine
val apacheClient = HttpClient(Apache) {
engine {
socketTimeout = 10_000
connectTimeout = 10_000
connectionRequestTimeout = 20_000
}
}
// JVM - OkHttp engine
val okHttpClient = HttpClient(OkHttp) {
engine {
config {
retryOnConnectionFailure(true)
connectTimeout(10, TimeUnit.SECONDS)
}
}
}
// Native - CIO engine
val nativeClient = HttpClient(CIO) {
engine {
maxConnectionsCount = 1000
endpoint {
maxConnectionsPerRoute = 100
pipelineMaxSize = 20
keepAliveTime = 5000
}
}
}
// JavaScript - default engine
val jsClient = HttpClient(Js) {
engine {
// JS-specific configuration
}
}/**
* Advanced connection configuration options
*/
class EngineConnectionConfig {
/** Maximum number of connections */
var maxConnectionsCount: Int = 1000
/** Maximum connections per route */
var maxConnectionsPerRoute: Int = 100
/** Connection keep-alive time */
var keepAliveTime: Long = 5000
/** Connection idle timeout */
var idleTimeout: Long = 30000
/** Enable connection pooling */
var connectionPooling: Boolean = true
}/**
* SSL/TLS configuration for secure connections
*/
class SSLConfig {
/** Trust all certificates (development only) */
var trustAllCertificates: Boolean = false
/** Custom certificate validation */
var certificateValidator: ((X509Certificate) -> Boolean)? = null
/** SSL context configuration */
var sslContext: SSLContext? = null
/** Hostname verification */
var hostnameVerifier: HostnameVerifier? = null
}Usage Examples:
import io.ktor.client.*
import io.ktor.client.engine.cio.*
// Advanced connection configuration
val advancedClient = HttpClient(CIO) {
engine {
maxConnectionsCount = 1000
endpoint {
maxConnectionsPerRoute = 100
pipelineMaxSize = 20
keepAliveTime = 5000
connectTimeout = 10000
connectAttempts = 3
}
https {
// SSL configuration
serverName = "api.example.com"
cipherSuites = CIOCipherSuites.SupportedSuites
trustManager = customTrustManager
}
}
}
// Development SSL configuration (trust all certificates)
val devClient = HttpClient(CIO) {
engine {
https {
trustManager = X509TrustManager.createTrustAllManager()
}
}
}interface HttpClientEngineCapability<T : Any>
class ClientEngineClosedException(
cause: Throwable? = null
) : IllegalStateException("Client engine is already closed.", cause)
data class HttpRequestData(
val url: Url,
val method: HttpMethod,
val headers: Headers,
val body: OutgoingContent,
val executionContext: Job,
val attributes: Attributes
)
data class HttpResponseData(
val statusCode: HttpStatusCode,
val requestTime: GMTDate,
val headers: Headers,
val version: HttpProtocolVersion,
val body: Any,
val callContext: CoroutineContext
)data class SocketAddress(
val hostname: String,
val port: Int
)
class InetSocketAddress(
hostname: String,
port: Int
) : SocketAddress(hostname, port) {
constructor(port: Int) : this("0.0.0.0", port)
}
class ConnectTimeoutException(
message: String,
cause: Throwable? = null
) : IOException(message, cause)
class SocketTimeoutException(
message: String,
cause: Throwable? = null
) : IOException(message, cause)Install with Tessl CLI
npx tessl i tessl/maven-io-ktor--ktor-client-core-macosarm64