|
| 1 | +/* |
| 2 | + * Copyright 2014-2026 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. |
| 3 | + */ |
| 4 | + |
| 5 | +package io.ktor.client.engine.curl |
| 6 | + |
| 7 | +import io.ktor.client.engine.* |
| 8 | +import io.ktor.client.engine.curl.internal.* |
| 9 | +import io.ktor.client.plugins.* |
| 10 | +import io.ktor.client.plugins.sse.* |
| 11 | +import io.ktor.client.plugins.websocket.* |
| 12 | +import io.ktor.client.request.* |
| 13 | +import io.ktor.client.utils.* |
| 14 | +import io.ktor.http.* |
| 15 | +import io.ktor.http.cio.* |
| 16 | +import io.ktor.util.date.* |
| 17 | +import io.ktor.utils.io.* |
| 18 | + |
| 19 | +internal class CurlClientEngine( |
| 20 | + override val config: CurlClientEngineConfig |
| 21 | +) : HttpClientEngineBase("ktor-curl") { |
| 22 | + |
| 23 | + override val supportedCapabilities = setOf(HttpTimeoutCapability, WebSocketCapability, SSECapability) |
| 24 | + |
| 25 | + private val curlProcessor = CurlProcessor(coroutineContext) |
| 26 | + |
| 27 | + @OptIn(InternalAPI::class) |
| 28 | + override suspend fun execute(data: HttpRequestData): HttpResponseData { |
| 29 | + val callContext = callContext() |
| 30 | + |
| 31 | + val requestTime = GMTDate() |
| 32 | + |
| 33 | + val curlRequest = data.toCurlRequest(config) |
| 34 | + val responseData = curlProcessor.executeRequest(curlRequest) |
| 35 | + |
| 36 | + return with(responseData) { |
| 37 | + val headerBytes = ByteReadChannel(headersBytes).apply { |
| 38 | + // Skip status line |
| 39 | + readLineStrict() |
| 40 | + } |
| 41 | + val rawHeaders = parseHeaders(headerBytes) |
| 42 | + val headers = rawHeaders |
| 43 | + .toBuilder().apply { |
| 44 | + dropCompressionHeaders(data.method, data.attributes) |
| 45 | + }.build() |
| 46 | + |
| 47 | + rawHeaders.release() |
| 48 | + |
| 49 | + val status = HttpStatusCode.fromValue(status) |
| 50 | + |
| 51 | + val responseBody: Any = if (data.isUpgradeRequest()) { |
| 52 | + val wsConfig = data.attributes[WEBSOCKETS_KEY] |
| 53 | + val websocket = responseBody as CurlWebSocketResponseBody |
| 54 | + CurlWebSocketSession( |
| 55 | + websocket, |
| 56 | + callContext, |
| 57 | + wsConfig.channelsConfig.outgoing, |
| 58 | + curlProcessor, |
| 59 | + ) |
| 60 | + } else { |
| 61 | + val httpResponse = responseBody as CurlHttpResponseBody |
| 62 | + data.attributes.getOrNull(ResponseAdapterAttributeKey) |
| 63 | + ?.adapt(data, status, headers, httpResponse.bodyChannel, data.body, callContext) |
| 64 | + ?: httpResponse.bodyChannel |
| 65 | + } |
| 66 | + |
| 67 | + HttpResponseData( |
| 68 | + status, |
| 69 | + requestTime, |
| 70 | + headers, |
| 71 | + version.fromCurl(), |
| 72 | + responseBody, |
| 73 | + callContext |
| 74 | + ) |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + override fun close() { |
| 79 | + super.close() |
| 80 | + curlProcessor.close() |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +@Deprecated("This exception will be removed in a future release in favor of a better error handling.") |
| 85 | +public class CurlIllegalStateException(cause: String) : IllegalStateException(cause) |
| 86 | + |
| 87 | +@Deprecated("This exception will be removed in a future release in favor of a better error handling.") |
| 88 | +public class CurlRuntimeException(cause: String) : RuntimeException(cause) |
0 commit comments