From 58158b4662ff2d01bd664201ebfa4039d597677f Mon Sep 17 00:00:00 2001 From: tursom Date: Sun, 10 Apr 2022 17:25:15 +0800 Subject: [PATCH] update --- .../web/client/netty/NettyHttpClient.kt | 3 +- .../web/client/netty/NettyHttpRequest.kt | 35 +++++++++++++++++-- .../web/client/netty/NettyHttpClientTest.kt | 4 +-- 3 files changed, 34 insertions(+), 8 deletions(-) diff --git a/ts-web/ts-web-netty-client/src/main/kotlin/cn/tursom/web/client/netty/NettyHttpClient.kt b/ts-web/ts-web-netty-client/src/main/kotlin/cn/tursom/web/client/netty/NettyHttpClient.kt index 6a68f08..4b909d0 100644 --- a/ts-web/ts-web-netty-client/src/main/kotlin/cn/tursom/web/client/netty/NettyHttpClient.kt +++ b/ts-web/ts-web-netty-client/src/main/kotlin/cn/tursom/web/client/netty/NettyHttpClient.kt @@ -17,9 +17,8 @@ open class NettyHttpClient : HttpClient { uri.port } val pool = HttpConnectionPool.poolOf(uri.host, port, uri.scheme == "https") - val request = NettyHttpRequest(pool) + val request = NettyHttpRequest(pool, uri.scheme, uri.host, uri.port, uri.path) request.method = method - request.path = uri.path return request } } diff --git a/ts-web/ts-web-netty-client/src/main/kotlin/cn/tursom/web/client/netty/NettyHttpRequest.kt b/ts-web/ts-web-netty-client/src/main/kotlin/cn/tursom/web/client/netty/NettyHttpRequest.kt index 47907b4..a260292 100644 --- a/ts-web/ts-web-netty-client/src/main/kotlin/cn/tursom/web/client/netty/NettyHttpRequest.kt +++ b/ts-web/ts-web-netty-client/src/main/kotlin/cn/tursom/web/client/netty/NettyHttpRequest.kt @@ -2,6 +2,7 @@ package cn.tursom.web.client.netty import cn.tursom.core.buffer.ByteBuffer import cn.tursom.core.buffer.impl.NettyByteBuffer +import cn.tursom.core.toStartWith import cn.tursom.log.impl.Slf4jImpl import cn.tursom.web.client.HttpRequest import cn.tursom.web.client.HttpResponse @@ -13,9 +14,20 @@ import io.netty.handler.codec.http.HttpVersion class NettyHttpRequest( private val pool: HttpConnectionPool, + val scheme: String, + val host: String, + val port: Int, + path: String, ) : HttpRequest { companion object : Slf4jImpl() + private val portStr: String + get() = if (port <= 0) { + "" + } else { + ":$port" + } + private var request: FullHttpRequest = DefaultFullHttpRequest(HttpVersion.HTTP_1_0, HttpMethod.GET, "") override var version: String @@ -29,11 +41,12 @@ class NettyHttpRequest( set(value) { request.method = HttpMethod.valueOf(value) } - override var path: String - get() = request.uri() + + override var path: String = path.toStartWith('/') set(value) { - request.uri = value + field = value.toStartWith('/') } + override val params = HashMap>().withDefault { ArrayList() } override fun addParam(key: String, value: String) { @@ -58,6 +71,22 @@ class NettyHttpRequest( } override suspend fun send(): HttpResponse { + request.uri = buildString { + append("$scheme://$host$portStr$path") + append(buildString { + params.forEach { (key, params) -> + if (isEmpty()) { + append("?") + } + params.forEach { param -> + if (isNotEmpty()) { + append('&') + } + append("$key=$param") + } + } + }) + } val receiveChannel = pool.useConnection { it.request(request) } diff --git a/ts-web/ts-web-netty-client/src/test/kotlin/cn/tursom/web/client/netty/NettyHttpClientTest.kt b/ts-web/ts-web-netty-client/src/test/kotlin/cn/tursom/web/client/netty/NettyHttpClientTest.kt index f7eefca..fd6ea6d 100644 --- a/ts-web/ts-web-netty-client/src/test/kotlin/cn/tursom/web/client/netty/NettyHttpClientTest.kt +++ b/ts-web/ts-web-netty-client/src/test/kotlin/cn/tursom/web/client/netty/NettyHttpClientTest.kt @@ -13,10 +13,8 @@ internal class NettyHttpClientTest { runBlocking { val request = client.request("GET", "https://cdn.segmentfault.com/r-e032f7ee/umi.js") .addHeader("accept-encoding", "br") - request.path = "https://cdn.segmentfault.com/r-e032f7ee/umi.js" val response = request.send() - println(response.body - .string()) + println(response.body.string()) } } } \ No newline at end of file