From cde5b80c2d3f02b715c1524be3b4ab13c2f89ede Mon Sep 17 00:00:00 2001 From: redkale Date: Tue, 17 Oct 2023 21:37:58 +0800 Subject: [PATCH] HttpSimpleClient --- .../redkale/net/http/HttpSimpleClient.java | 44 +++++++++++++++++-- 1 file changed, 40 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/redkale/net/http/HttpSimpleClient.java b/src/main/java/org/redkale/net/http/HttpSimpleClient.java index 3fa550b85..5bb5d2ebf 100644 --- a/src/main/java/org/redkale/net/http/HttpSimpleClient.java +++ b/src/main/java/org/redkale/net/http/HttpSimpleClient.java @@ -202,8 +202,9 @@ public class HttpSimpleClient { public CompletableFuture> sendAsync(String method, String url, Map headers, byte[] body, Convert convert, Type valueType) { final URI uri = URI.create(url); - final SocketAddress address = new InetSocketAddress(uri.getHost(), uri.getPort() > 0 ? uri.getPort() : (url.startsWith("https:") ? 443 : 80)); - return asyncGroup.createTCPClient(address, readTimeoutSeconds, writeTimeoutSeconds).thenCompose(conn -> { + final String host = uri.getHost(); + final int port = uri.getPort() > 0 ? uri.getPort() : (url.startsWith("https:") ? 443 : 80); + return createConnection(host, port).thenCompose(conn -> { final ByteArray array = new ByteArray(); int urlpos = url.indexOf("/", url.indexOf("//") + 3); array.put((method.toUpperCase() + " " + (urlpos > 0 ? url.substring(urlpos) : "/") + " HTTP/1.1\r\n" @@ -240,6 +241,11 @@ public class HttpSimpleClient { return future; }); } + + protected CompletableFuture createConnection(String host, int port) { + return asyncGroup.createTCPClient(new InetSocketAddress(host, port), readTimeoutSeconds, writeTimeoutSeconds).thenApply(conn -> new HttpConnection(conn)); + } + // // public static void main(String[] args) throws Throwable { // final AsyncIOGroup asyncGroup = new AsyncIOGroup(8192, 16); @@ -248,6 +254,36 @@ public class HttpSimpleClient { // HttpSimpleClient client = HttpSimpleClient.create(asyncGroup); // System.out.println(client.getAsync(url).join()); // } +// + protected static class HttpConnection { + + protected final AsyncConnection channel; + + public HttpConnection(AsyncConnection channel) { + this.channel = channel; + } + + public void dispose() { + this.channel.dispose(); + } + + public void setReadBuffer(ByteBuffer buffer) { + this.channel.setReadBuffer(buffer); + } + + public void offerReadBuffer(ByteBuffer buffer) { + this.channel.offerReadBuffer(buffer); + } + + public void read(CompletionHandler handler) { + this.channel.read(handler); + } + + public void write(ByteTuple array, CompletionHandler handler) { + this.channel.write(array, handler); + } + + } protected static class ClientReadCompletionHandler implements CompletionHandler { @@ -259,7 +295,7 @@ public class HttpSimpleClient { protected static final int READ_STATE_END = 4; - protected final AsyncConnection conn; + protected final HttpConnection conn; protected final ByteArray array; @@ -275,7 +311,7 @@ public class HttpSimpleClient { protected int contentLength = -1; - public ClientReadCompletionHandler(AsyncConnection conn, ByteArray array, Convert convert, Type valueType, CompletableFuture> future) { + public ClientReadCompletionHandler(HttpConnection conn, ByteArray array, Convert convert, Type valueType, CompletableFuture> future) { this.conn = conn; this.array = array; this.convert = convert;