Skip to content

Commit

Permalink
KTOR-6190 Handle NPE in JavaClientEngine body() call (#3728)
Browse files Browse the repository at this point in the history
(cherry picked from commit 3bc2917)
  • Loading branch information
e5l committed Aug 30, 2023
1 parent e637c4a commit ddc3ff3
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ public class JavaHttpEngine(override val config: JavaHttpConfig) : HttpClientEng
if (data.isUpgradeRequest()) {
engine.executeWebSocketRequest(callContext, data)
} else {
engine.executeHttpRequest(callContext, data)
engine.executeHttpRequest(callContext, data) ?: throw kotlinx.coroutines.CancellationException(
"Request was cancelled"
)
}
} catch (cause: Throwable) {
callContext.cancel(CancellationException("Failed to execute request", cause))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import kotlin.coroutines.*
internal suspend fun HttpClient.executeHttpRequest(
callContext: CoroutineContext,
requestData: HttpRequestData
): HttpResponseData {
): HttpResponseData? {
val httpRequest = requestData.convertToHttpRequest(callContext)
return try {
sendAsync(httpRequest, JavaHttpResponseBodyHandler(callContext)).await().body()
sendAsync(httpRequest, JavaHttpResponseBodyHandler(callContext))?.await()?.body()
} catch (cause: HttpConnectTimeoutException) {
throw ConnectTimeoutException(requestData, cause)
} catch (cause: HttpTimeoutException) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import io.ktor.client.*
import io.ktor.client.call.*
import io.ktor.client.plugins.*
import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.http.*
import kotlinx.coroutines.*
import java.net.*
import java.time.*
import java.util.concurrent.*
import kotlin.test.*
Expand All @@ -25,6 +27,21 @@ class JavaEngineTests {
}
}

@Test
fun testProxy() = runBlocking {

val client = HttpClient(Java) {
engine {
proxy = Proxy(Proxy.Type.HTTP, InetSocketAddress("localhost", 8082))
}
}

val body = client.get("http://127.0.0.1:8080/")
.bodyAsText()

assertEquals("proxy", body)
}

@Test
fun testThreadLeak() = runBlocking {
System.setProperty("jdk.internal.httpclient.selectorTimeout", "50")
Expand Down

0 comments on commit ddc3ff3

Please sign in to comment.