From 33a4979d7c7613a4f5081490342c82a9d176094f Mon Sep 17 00:00:00 2001 From: nikita Date: Sat, 20 Feb 2021 09:47:43 +0300 Subject: [PATCH 1/6] proxy connect custom headers --- bom/pom.xml | 2 +- client/pom.xml | 2 +- .../netty/request/NettyRequestSender.java | 32 +++++++++++++------ .../asynchttpclient/proxy/ProxyServer.java | 24 ++++++++++++-- example/pom.xml | 2 +- extras/guava/pom.xml | 2 +- extras/jdeferred/pom.xml | 2 +- extras/pom.xml | 2 +- extras/registry/pom.xml | 2 +- extras/retrofit2/pom.xml | 2 +- extras/rxjava/pom.xml | 2 +- extras/rxjava2/pom.xml | 2 +- extras/simple/pom.xml | 2 +- extras/typesafeconfig/pom.xml | 2 +- netty-utils/pom.xml | 2 +- pom.xml | 2 +- 16 files changed, 58 insertions(+), 26 deletions(-) diff --git a/bom/pom.xml b/bom/pom.xml index a26d8ce0b7..a532c93d70 100644 --- a/bom/pom.xml +++ b/bom/pom.xml @@ -5,7 +5,7 @@ org.asynchttpclient async-http-client-project - 2.12.3-SNAPSHOT + 2.12.3-SNAPSHOT-fork async-http-client-bom diff --git a/client/pom.xml b/client/pom.xml index 4ab3de83ce..f0271ce6e0 100644 --- a/client/pom.xml +++ b/client/pom.xml @@ -2,7 +2,7 @@ org.asynchttpclient async-http-client-project - 2.12.3-SNAPSHOT + 2.12.3-SNAPSHOT-fork 4.0.0 async-http-client diff --git a/client/src/main/java/org/asynchttpclient/netty/request/NettyRequestSender.java b/client/src/main/java/org/asynchttpclient/netty/request/NettyRequestSender.java index 4fa0589a84..a2ccc3ff74 100755 --- a/client/src/main/java/org/asynchttpclient/netty/request/NettyRequestSender.java +++ b/client/src/main/java/org/asynchttpclient/netty/request/NettyRequestSender.java @@ -107,7 +107,8 @@ public ListenableFuture sendRequest(final Request request, return sendRequestWithCertainForceConnect(request, asyncHandler, future, proxyServer, true); } else { // CONNECT will depend if we can pool or connection or if we have to open a new one - return sendRequestThroughProxy(request, asyncHandler, future, proxyServer); + HttpHeaders customProxyHeaders = proxyServer.getCustomHeaders().apply(request); + return sendRequestThroughProxy(request, asyncHandler, future, proxyServer, customProxyHeaders); } } else { // no CONNECT for sure @@ -151,7 +152,8 @@ private ListenableFuture sendRequestWithCertainForceConnect(Request reque private ListenableFuture sendRequestThroughProxy(Request request, AsyncHandler asyncHandler, NettyResponseFuture future, - ProxyServer proxyServer) { + ProxyServer proxyServer, + HttpHeaders customHeaders) { NettyResponseFuture newFuture = null; for (int i = 0; i < 3; i++) { @@ -175,7 +177,7 @@ private ListenableFuture sendRequestThroughProxy(Request request, // couldn't poll an active channel newFuture = newNettyRequestAndResponseFuture(request, asyncHandler, future, proxyServer, true); - return sendRequestWithNewChannel(request, proxyServer, newFuture, asyncHandler); + return sendRequestWithNewChannel(request, proxyServer, newFuture, asyncHandler, customHeaders); } private NettyResponseFuture newNettyRequestAndResponseFuture(final Request request, @@ -271,7 +273,14 @@ private ListenableFuture sendRequestWithOpenChannel(NettyResponseFuture ListenableFuture sendRequestWithNewChannel(Request request, ProxyServer proxy, NettyResponseFuture future, - AsyncHandler asyncHandler) { + AsyncHandler asyncHandler){ + return sendRequestWithNewChannel(request, proxy, future, asyncHandler, null); + } + private ListenableFuture sendRequestWithNewChannel(Request request, + ProxyServer proxy, + NettyResponseFuture future, + AsyncHandler asyncHandler, + HttpHeaders customHeaders) { // some headers are only set when performing the first request HttpHeaders headers = future.getNettyRequest().getHttpRequest().headers(); @@ -279,6 +288,9 @@ private ListenableFuture sendRequestWithNewChannel(Request request, Realm proxyRealm = future.getProxyRealm(); requestFactory.addAuthorizationHeader(headers, perConnectionAuthorizationHeader(request, proxy, realm)); requestFactory.setProxyAuthorizationHeader(headers, perConnectionProxyAuthorizationHeader(request, proxyRealm)); + if(customHeaders!=null) { + headers.add(customHeaders); + } future.setInAuth(realm != null && realm.isUsePreemptiveAuth() && realm.getScheme() != AuthScheme.NTLM); future.setInProxyAuth( @@ -633,12 +645,12 @@ public void drainChannelAndExecuteNextRequest(final Channel channel, @Override public void call() { whenHandshaked.addListener(f -> { - if (f.isSuccess()) { - sendNextRequest(nextRequest, future); - } else { - future.abort(f.cause()); - } - } + if (f.isSuccess()) { + sendNextRequest(nextRequest, future); + } else { + future.abort(f.cause()); + } + } ); } }); diff --git a/client/src/main/java/org/asynchttpclient/proxy/ProxyServer.java b/client/src/main/java/org/asynchttpclient/proxy/ProxyServer.java index 13c33590be..611c02ecfb 100644 --- a/client/src/main/java/org/asynchttpclient/proxy/ProxyServer.java +++ b/client/src/main/java/org/asynchttpclient/proxy/ProxyServer.java @@ -16,11 +16,15 @@ */ package org.asynchttpclient.proxy; +import io.netty.handler.codec.http.HttpHeaders; import org.asynchttpclient.Realm; +import org.asynchttpclient.Request; import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.function.Function; +import java.util.function.Supplier; import static org.asynchttpclient.util.Assertions.assertNotNull; import static org.asynchttpclient.util.MiscUtils.isNonEmpty; @@ -36,15 +40,21 @@ public class ProxyServer { private final Realm realm; private final List nonProxyHosts; private final ProxyType proxyType; + private final Function customHeaders; public ProxyServer(String host, int port, int securedPort, Realm realm, List nonProxyHosts, - ProxyType proxyType) { + ProxyType proxyType, Function customHeaders) { this.host = host; this.port = port; this.securedPort = securedPort; this.realm = realm; this.nonProxyHosts = nonProxyHosts; this.proxyType = proxyType; + this.customHeaders = customHeaders; + } + public ProxyServer(String host, int port, int securedPort, Realm realm, List nonProxyHosts, + ProxyType proxyType) { + this(host, port, securedPort, realm, nonProxyHosts, proxyType, null); } public String getHost() { @@ -71,6 +81,10 @@ public ProxyType getProxyType() { return proxyType; } + public Function getCustomHeaders() { + return customHeaders; + } + /** * Checks whether proxy should be used according to nonProxyHosts settings of * it, or we want to go directly to target host. If null proxy is @@ -118,6 +132,7 @@ public static class Builder { private Realm realm; private List nonProxyHosts; private ProxyType proxyType; + private Function customHeaders; public Builder(String host, int port) { this.host = host; @@ -157,11 +172,16 @@ public Builder setProxyType(ProxyType proxyType) { return this; } + public Builder setCustomHeaders(Function customHeaders) { + this.customHeaders = customHeaders; + return this; + } + public ProxyServer build() { List nonProxyHosts = this.nonProxyHosts != null ? Collections.unmodifiableList(this.nonProxyHosts) : Collections.emptyList(); ProxyType proxyType = this.proxyType != null ? this.proxyType : ProxyType.HTTP; - return new ProxyServer(host, port, securedPort, realm, nonProxyHosts, proxyType); + return new ProxyServer(host, port, securedPort, realm, nonProxyHosts, proxyType, customHeaders); } } } diff --git a/example/pom.xml b/example/pom.xml index 2e9dbd07f5..dcc43fb280 100644 --- a/example/pom.xml +++ b/example/pom.xml @@ -2,7 +2,7 @@ org.asynchttpclient async-http-client-project - 2.12.3-SNAPSHOT + 2.12.3-SNAPSHOT-fork 4.0.0 async-http-client-example diff --git a/extras/guava/pom.xml b/extras/guava/pom.xml index ae6fa22e55..62f2897a3b 100644 --- a/extras/guava/pom.xml +++ b/extras/guava/pom.xml @@ -2,7 +2,7 @@ org.asynchttpclient async-http-client-extras-parent - 2.12.3-SNAPSHOT + 2.12.3-SNAPSHOT-fork 4.0.0 async-http-client-extras-guava diff --git a/extras/jdeferred/pom.xml b/extras/jdeferred/pom.xml index 16ccaff4f1..ecef25ff65 100644 --- a/extras/jdeferred/pom.xml +++ b/extras/jdeferred/pom.xml @@ -18,7 +18,7 @@ async-http-client-extras-parent org.asynchttpclient - 2.12.3-SNAPSHOT + 2.12.3-SNAPSHOT-fork async-http-client-extras-jdeferred Asynchronous Http Client JDeferred Extras diff --git a/extras/pom.xml b/extras/pom.xml index 099dfa82eb..6bec8619ae 100644 --- a/extras/pom.xml +++ b/extras/pom.xml @@ -2,7 +2,7 @@ org.asynchttpclient async-http-client-project - 2.12.3-SNAPSHOT + 2.12.3-SNAPSHOT-fork 4.0.0 async-http-client-extras-parent diff --git a/extras/registry/pom.xml b/extras/registry/pom.xml index 645c50fd28..21028590ff 100644 --- a/extras/registry/pom.xml +++ b/extras/registry/pom.xml @@ -2,7 +2,7 @@ org.asynchttpclient async-http-client-extras-parent - 2.12.3-SNAPSHOT + 2.12.3-SNAPSHOT-fork 4.0.0 async-http-client-extras-registry diff --git a/extras/retrofit2/pom.xml b/extras/retrofit2/pom.xml index d333159c54..d60292ce84 100644 --- a/extras/retrofit2/pom.xml +++ b/extras/retrofit2/pom.xml @@ -4,7 +4,7 @@ async-http-client-extras-parent org.asynchttpclient - 2.12.3-SNAPSHOT + 2.12.3-SNAPSHOT-fork async-http-client-extras-retrofit2 diff --git a/extras/rxjava/pom.xml b/extras/rxjava/pom.xml index b04fdae93a..1821f7b971 100644 --- a/extras/rxjava/pom.xml +++ b/extras/rxjava/pom.xml @@ -3,7 +3,7 @@ async-http-client-extras-parent org.asynchttpclient - 2.12.3-SNAPSHOT + 2.12.3-SNAPSHOT-fork async-http-client-extras-rxjava Asynchronous Http Client RxJava Extras diff --git a/extras/rxjava2/pom.xml b/extras/rxjava2/pom.xml index 7646e8fdd6..24f95f4d79 100644 --- a/extras/rxjava2/pom.xml +++ b/extras/rxjava2/pom.xml @@ -3,7 +3,7 @@ async-http-client-extras-parent org.asynchttpclient - 2.12.3-SNAPSHOT + 2.12.3-SNAPSHOT-fork async-http-client-extras-rxjava2 Asynchronous Http Client RxJava2 Extras diff --git a/extras/simple/pom.xml b/extras/simple/pom.xml index e901f9fb4e..263516be07 100644 --- a/extras/simple/pom.xml +++ b/extras/simple/pom.xml @@ -3,7 +3,7 @@ async-http-client-extras-parent org.asynchttpclient - 2.12.3-SNAPSHOT + 2.12.3-SNAPSHOT-fork async-http-client-extras-simple Asynchronous Http Simple Client diff --git a/extras/typesafeconfig/pom.xml b/extras/typesafeconfig/pom.xml index 40b40bac3e..e0c1b22f1e 100644 --- a/extras/typesafeconfig/pom.xml +++ b/extras/typesafeconfig/pom.xml @@ -4,7 +4,7 @@ async-http-client-extras-parent org.asynchttpclient - 2.12.3-SNAPSHOT + 2.12.3-SNAPSHOT-fork async-http-client-extras-typesafe-config diff --git a/netty-utils/pom.xml b/netty-utils/pom.xml index 920b5ede00..44f30c3b31 100644 --- a/netty-utils/pom.xml +++ b/netty-utils/pom.xml @@ -2,7 +2,7 @@ org.asynchttpclient async-http-client-project - 2.12.3-SNAPSHOT + 2.12.3-SNAPSHOT-fork 4.0.0 async-http-client-netty-utils diff --git a/pom.xml b/pom.xml index c20acb2339..7c3efb97a0 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ org.asynchttpclient async-http-client-project - 2.12.3-SNAPSHOT + 2.12.3-SNAPSHOT-fork pom Asynchronous Http Client Project From b50b63ca8f949e30e82540628c3d4f712bf90445 Mon Sep 17 00:00:00 2001 From: nikita Date: Sat, 20 Feb 2021 13:15:47 +0300 Subject: [PATCH 2/6] npe fix --- .../netty/request/NettyRequestSender.java | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/client/src/main/java/org/asynchttpclient/netty/request/NettyRequestSender.java b/client/src/main/java/org/asynchttpclient/netty/request/NettyRequestSender.java index a2ccc3ff74..0b7f850caa 100755 --- a/client/src/main/java/org/asynchttpclient/netty/request/NettyRequestSender.java +++ b/client/src/main/java/org/asynchttpclient/netty/request/NettyRequestSender.java @@ -107,7 +107,10 @@ public ListenableFuture sendRequest(final Request request, return sendRequestWithCertainForceConnect(request, asyncHandler, future, proxyServer, true); } else { // CONNECT will depend if we can pool or connection or if we have to open a new one - HttpHeaders customProxyHeaders = proxyServer.getCustomHeaders().apply(request); + HttpHeaders customProxyHeaders = null; + if(proxyServer.getCustomHeaders()!=null) { + customProxyHeaders = proxyServer.getCustomHeaders().apply(request); + } return sendRequestThroughProxy(request, asyncHandler, future, proxyServer, customProxyHeaders); } } else { @@ -284,13 +287,13 @@ private ListenableFuture sendRequestWithNewChannel(Request request, // some headers are only set when performing the first request HttpHeaders headers = future.getNettyRequest().getHttpRequest().headers(); + if(customHeaders!=null) { + headers.add(customHeaders); + } Realm realm = future.getRealm(); Realm proxyRealm = future.getProxyRealm(); requestFactory.addAuthorizationHeader(headers, perConnectionAuthorizationHeader(request, proxy, realm)); requestFactory.setProxyAuthorizationHeader(headers, perConnectionProxyAuthorizationHeader(request, proxyRealm)); - if(customHeaders!=null) { - headers.add(customHeaders); - } future.setInAuth(realm != null && realm.isUsePreemptiveAuth() && realm.getScheme() != AuthScheme.NTLM); future.setInProxyAuth( From 0a52ec4aabab2acaf992a3baa7d88bf47d34fd91 Mon Sep 17 00:00:00 2001 From: nikita Date: Sat, 20 Feb 2021 13:24:31 +0300 Subject: [PATCH 3/6] refactor --- .../netty/request/NettyRequestSender.java | 39 +++++++------------ .../asynchttpclient/proxy/ProxyServer.java | 2 +- 2 files changed, 16 insertions(+), 25 deletions(-) diff --git a/client/src/main/java/org/asynchttpclient/netty/request/NettyRequestSender.java b/client/src/main/java/org/asynchttpclient/netty/request/NettyRequestSender.java index 0b7f850caa..d4a9aaea93 100755 --- a/client/src/main/java/org/asynchttpclient/netty/request/NettyRequestSender.java +++ b/client/src/main/java/org/asynchttpclient/netty/request/NettyRequestSender.java @@ -107,11 +107,7 @@ public ListenableFuture sendRequest(final Request request, return sendRequestWithCertainForceConnect(request, asyncHandler, future, proxyServer, true); } else { // CONNECT will depend if we can pool or connection or if we have to open a new one - HttpHeaders customProxyHeaders = null; - if(proxyServer.getCustomHeaders()!=null) { - customProxyHeaders = proxyServer.getCustomHeaders().apply(request); - } - return sendRequestThroughProxy(request, asyncHandler, future, proxyServer, customProxyHeaders); + return sendRequestThroughProxy(request, asyncHandler, future, proxyServer); } } else { // no CONNECT for sure @@ -155,8 +151,7 @@ private ListenableFuture sendRequestWithCertainForceConnect(Request reque private ListenableFuture sendRequestThroughProxy(Request request, AsyncHandler asyncHandler, NettyResponseFuture future, - ProxyServer proxyServer, - HttpHeaders customHeaders) { + ProxyServer proxyServer) { NettyResponseFuture newFuture = null; for (int i = 0; i < 3; i++) { @@ -180,7 +175,7 @@ private ListenableFuture sendRequestThroughProxy(Request request, // couldn't poll an active channel newFuture = newNettyRequestAndResponseFuture(request, asyncHandler, future, proxyServer, true); - return sendRequestWithNewChannel(request, proxyServer, newFuture, asyncHandler, customHeaders); + return sendRequestWithNewChannel(request, proxyServer, newFuture, asyncHandler); } private NettyResponseFuture newNettyRequestAndResponseFuture(final Request request, @@ -276,19 +271,15 @@ private ListenableFuture sendRequestWithOpenChannel(NettyResponseFuture ListenableFuture sendRequestWithNewChannel(Request request, ProxyServer proxy, NettyResponseFuture future, - AsyncHandler asyncHandler){ - return sendRequestWithNewChannel(request, proxy, future, asyncHandler, null); - } - private ListenableFuture sendRequestWithNewChannel(Request request, - ProxyServer proxy, - NettyResponseFuture future, - AsyncHandler asyncHandler, - HttpHeaders customHeaders) { + AsyncHandler asyncHandler) { // some headers are only set when performing the first request HttpHeaders headers = future.getNettyRequest().getHttpRequest().headers(); - if(customHeaders!=null) { - headers.add(customHeaders); + if(proxy.getCustomHeaders()!=null) { + HttpHeaders customHeaders =proxy.getCustomHeaders().apply(request); + if(customHeaders!=null) { + headers.add(customHeaders); + } } Realm realm = future.getRealm(); Realm proxyRealm = future.getProxyRealm(); @@ -648,12 +639,12 @@ public void drainChannelAndExecuteNextRequest(final Channel channel, @Override public void call() { whenHandshaked.addListener(f -> { - if (f.isSuccess()) { - sendNextRequest(nextRequest, future); - } else { - future.abort(f.cause()); - } - } + if (f.isSuccess()) { + sendNextRequest(nextRequest, future); + } else { + future.abort(f.cause()); + } + } ); } }); diff --git a/client/src/main/java/org/asynchttpclient/proxy/ProxyServer.java b/client/src/main/java/org/asynchttpclient/proxy/ProxyServer.java index 611c02ecfb..e9f68be845 100644 --- a/client/src/main/java/org/asynchttpclient/proxy/ProxyServer.java +++ b/client/src/main/java/org/asynchttpclient/proxy/ProxyServer.java @@ -17,6 +17,7 @@ package org.asynchttpclient.proxy; import io.netty.handler.codec.http.HttpHeaders; + import org.asynchttpclient.Realm; import org.asynchttpclient.Request; @@ -24,7 +25,6 @@ import java.util.Collections; import java.util.List; import java.util.function.Function; -import java.util.function.Supplier; import static org.asynchttpclient.util.Assertions.assertNotNull; import static org.asynchttpclient.util.MiscUtils.isNonEmpty; From 55c885d9dcda1b7c67b78108b84914ab0de753e3 Mon Sep 17 00:00:00 2001 From: nikita Date: Sat, 20 Feb 2021 15:18:51 +0300 Subject: [PATCH 4/6] formatting fix + version fix --- bom/pom.xml | 2 +- client/pom.xml | 2 +- .../asynchttpclient/netty/request/NettyRequestSender.java | 6 +++--- .../main/java/org/asynchttpclient/proxy/ProxyServer.java | 1 + example/pom.xml | 2 +- extras/guava/pom.xml | 2 +- extras/jdeferred/pom.xml | 2 +- extras/pom.xml | 2 +- extras/registry/pom.xml | 2 +- extras/retrofit2/pom.xml | 2 +- extras/rxjava/pom.xml | 2 +- extras/rxjava2/pom.xml | 2 +- extras/simple/pom.xml | 2 +- extras/typesafeconfig/pom.xml | 2 +- netty-utils/pom.xml | 2 +- pom.xml | 2 +- 16 files changed, 18 insertions(+), 17 deletions(-) diff --git a/bom/pom.xml b/bom/pom.xml index a532c93d70..a26d8ce0b7 100644 --- a/bom/pom.xml +++ b/bom/pom.xml @@ -5,7 +5,7 @@ org.asynchttpclient async-http-client-project - 2.12.3-SNAPSHOT-fork + 2.12.3-SNAPSHOT async-http-client-bom diff --git a/client/pom.xml b/client/pom.xml index f0271ce6e0..4ab3de83ce 100644 --- a/client/pom.xml +++ b/client/pom.xml @@ -2,7 +2,7 @@ org.asynchttpclient async-http-client-project - 2.12.3-SNAPSHOT-fork + 2.12.3-SNAPSHOT 4.0.0 async-http-client diff --git a/client/src/main/java/org/asynchttpclient/netty/request/NettyRequestSender.java b/client/src/main/java/org/asynchttpclient/netty/request/NettyRequestSender.java index d4a9aaea93..e399af5362 100755 --- a/client/src/main/java/org/asynchttpclient/netty/request/NettyRequestSender.java +++ b/client/src/main/java/org/asynchttpclient/netty/request/NettyRequestSender.java @@ -275,9 +275,9 @@ private ListenableFuture sendRequestWithNewChannel(Request request, // some headers are only set when performing the first request HttpHeaders headers = future.getNettyRequest().getHttpRequest().headers(); - if(proxy.getCustomHeaders()!=null) { - HttpHeaders customHeaders =proxy.getCustomHeaders().apply(request); - if(customHeaders!=null) { + if(proxy.getCustomHeaders() != null) { + HttpHeaders customHeaders = proxy.getCustomHeaders().apply(request); + if(customHeaders != null) { headers.add(customHeaders); } } diff --git a/client/src/main/java/org/asynchttpclient/proxy/ProxyServer.java b/client/src/main/java/org/asynchttpclient/proxy/ProxyServer.java index e9f68be845..bdbc76db8e 100644 --- a/client/src/main/java/org/asynchttpclient/proxy/ProxyServer.java +++ b/client/src/main/java/org/asynchttpclient/proxy/ProxyServer.java @@ -52,6 +52,7 @@ public ProxyServer(String host, int port, int securedPort, Realm realm, List nonProxyHosts, ProxyType proxyType) { this(host, port, securedPort, realm, nonProxyHosts, proxyType, null); diff --git a/example/pom.xml b/example/pom.xml index dcc43fb280..2e9dbd07f5 100644 --- a/example/pom.xml +++ b/example/pom.xml @@ -2,7 +2,7 @@ org.asynchttpclient async-http-client-project - 2.12.3-SNAPSHOT-fork + 2.12.3-SNAPSHOT 4.0.0 async-http-client-example diff --git a/extras/guava/pom.xml b/extras/guava/pom.xml index 62f2897a3b..ae6fa22e55 100644 --- a/extras/guava/pom.xml +++ b/extras/guava/pom.xml @@ -2,7 +2,7 @@ org.asynchttpclient async-http-client-extras-parent - 2.12.3-SNAPSHOT-fork + 2.12.3-SNAPSHOT 4.0.0 async-http-client-extras-guava diff --git a/extras/jdeferred/pom.xml b/extras/jdeferred/pom.xml index ecef25ff65..16ccaff4f1 100644 --- a/extras/jdeferred/pom.xml +++ b/extras/jdeferred/pom.xml @@ -18,7 +18,7 @@ async-http-client-extras-parent org.asynchttpclient - 2.12.3-SNAPSHOT-fork + 2.12.3-SNAPSHOT async-http-client-extras-jdeferred Asynchronous Http Client JDeferred Extras diff --git a/extras/pom.xml b/extras/pom.xml index 6bec8619ae..099dfa82eb 100644 --- a/extras/pom.xml +++ b/extras/pom.xml @@ -2,7 +2,7 @@ org.asynchttpclient async-http-client-project - 2.12.3-SNAPSHOT-fork + 2.12.3-SNAPSHOT 4.0.0 async-http-client-extras-parent diff --git a/extras/registry/pom.xml b/extras/registry/pom.xml index 21028590ff..645c50fd28 100644 --- a/extras/registry/pom.xml +++ b/extras/registry/pom.xml @@ -2,7 +2,7 @@ org.asynchttpclient async-http-client-extras-parent - 2.12.3-SNAPSHOT-fork + 2.12.3-SNAPSHOT 4.0.0 async-http-client-extras-registry diff --git a/extras/retrofit2/pom.xml b/extras/retrofit2/pom.xml index d60292ce84..d333159c54 100644 --- a/extras/retrofit2/pom.xml +++ b/extras/retrofit2/pom.xml @@ -4,7 +4,7 @@ async-http-client-extras-parent org.asynchttpclient - 2.12.3-SNAPSHOT-fork + 2.12.3-SNAPSHOT async-http-client-extras-retrofit2 diff --git a/extras/rxjava/pom.xml b/extras/rxjava/pom.xml index 1821f7b971..b04fdae93a 100644 --- a/extras/rxjava/pom.xml +++ b/extras/rxjava/pom.xml @@ -3,7 +3,7 @@ async-http-client-extras-parent org.asynchttpclient - 2.12.3-SNAPSHOT-fork + 2.12.3-SNAPSHOT async-http-client-extras-rxjava Asynchronous Http Client RxJava Extras diff --git a/extras/rxjava2/pom.xml b/extras/rxjava2/pom.xml index 24f95f4d79..7646e8fdd6 100644 --- a/extras/rxjava2/pom.xml +++ b/extras/rxjava2/pom.xml @@ -3,7 +3,7 @@ async-http-client-extras-parent org.asynchttpclient - 2.12.3-SNAPSHOT-fork + 2.12.3-SNAPSHOT async-http-client-extras-rxjava2 Asynchronous Http Client RxJava2 Extras diff --git a/extras/simple/pom.xml b/extras/simple/pom.xml index 263516be07..e901f9fb4e 100644 --- a/extras/simple/pom.xml +++ b/extras/simple/pom.xml @@ -3,7 +3,7 @@ async-http-client-extras-parent org.asynchttpclient - 2.12.3-SNAPSHOT-fork + 2.12.3-SNAPSHOT async-http-client-extras-simple Asynchronous Http Simple Client diff --git a/extras/typesafeconfig/pom.xml b/extras/typesafeconfig/pom.xml index e0c1b22f1e..40b40bac3e 100644 --- a/extras/typesafeconfig/pom.xml +++ b/extras/typesafeconfig/pom.xml @@ -4,7 +4,7 @@ async-http-client-extras-parent org.asynchttpclient - 2.12.3-SNAPSHOT-fork + 2.12.3-SNAPSHOT async-http-client-extras-typesafe-config diff --git a/netty-utils/pom.xml b/netty-utils/pom.xml index 44f30c3b31..920b5ede00 100644 --- a/netty-utils/pom.xml +++ b/netty-utils/pom.xml @@ -2,7 +2,7 @@ org.asynchttpclient async-http-client-project - 2.12.3-SNAPSHOT-fork + 2.12.3-SNAPSHOT 4.0.0 async-http-client-netty-utils diff --git a/pom.xml b/pom.xml index 7c3efb97a0..c20acb2339 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ org.asynchttpclient async-http-client-project - 2.12.3-SNAPSHOT-fork + 2.12.3-SNAPSHOT pom Asynchronous Http Client Project From e3cf248a365b78bee9d70b91328aea39b8147850 Mon Sep 17 00:00:00 2001 From: nikita Date: Sat, 20 Feb 2021 17:29:05 +0300 Subject: [PATCH 5/6] npe fix --- .../org/asynchttpclient/netty/request/NettyRequestSender.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/src/main/java/org/asynchttpclient/netty/request/NettyRequestSender.java b/client/src/main/java/org/asynchttpclient/netty/request/NettyRequestSender.java index e399af5362..08b1b19ac3 100755 --- a/client/src/main/java/org/asynchttpclient/netty/request/NettyRequestSender.java +++ b/client/src/main/java/org/asynchttpclient/netty/request/NettyRequestSender.java @@ -275,7 +275,7 @@ private ListenableFuture sendRequestWithNewChannel(Request request, // some headers are only set when performing the first request HttpHeaders headers = future.getNettyRequest().getHttpRequest().headers(); - if(proxy.getCustomHeaders() != null) { + if(proxy != null && proxy.getCustomHeaders() != null ) { HttpHeaders customHeaders = proxy.getCustomHeaders().apply(request); if(customHeaders != null) { headers.add(customHeaders); From 191c032971ee5a9e017c91f91bb8bb92aa8d3d82 Mon Sep 17 00:00:00 2001 From: nikita Date: Wed, 24 Feb 2021 16:17:06 +0300 Subject: [PATCH 6/6] test added --- .../proxy/CustomHeaderProxyTest.java | 119 ++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 client/src/test/java/org/asynchttpclient/proxy/CustomHeaderProxyTest.java diff --git a/client/src/test/java/org/asynchttpclient/proxy/CustomHeaderProxyTest.java b/client/src/test/java/org/asynchttpclient/proxy/CustomHeaderProxyTest.java new file mode 100644 index 0000000000..37b8c0edd8 --- /dev/null +++ b/client/src/test/java/org/asynchttpclient/proxy/CustomHeaderProxyTest.java @@ -0,0 +1,119 @@ +/* + * Copyright (c) 2010-2012 Sonatype, Inc. All rights reserved. + * + * This program is licensed to you under the Apache License Version 2.0, + * and you may not use this file except in compliance with the Apache License Version 2.0. + * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the Apache License Version 2.0 is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. + */ +package org.asynchttpclient.proxy; + +import io.netty.handler.codec.http.DefaultHttpHeaders; +import org.asynchttpclient.AbstractBasicTest; +import org.asynchttpclient.AsyncHttpClient; +import org.asynchttpclient.AsyncHttpClientConfig; +import org.asynchttpclient.Response; +import org.asynchttpclient.request.body.generator.ByteArrayBodyGenerator; +import org.asynchttpclient.test.EchoHandler; +import org.asynchttpclient.util.HttpConstants; +import org.eclipse.jetty.proxy.ConnectHandler; +import org.eclipse.jetty.server.Request; +import org.eclipse.jetty.server.Server; +import org.eclipse.jetty.server.ServerConnector; +import org.eclipse.jetty.server.handler.AbstractHandler; +import org.testng.annotations.AfterClass; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; + +import static org.asynchttpclient.Dsl.*; +import static org.asynchttpclient.test.TestUtils.*; +import static org.testng.Assert.assertEquals; + +/** + * Proxy usage tests. + */ +public class CustomHeaderProxyTest extends AbstractBasicTest { + + private Server server2; + + private final String customHeaderName = "Custom-Header"; + private final String customHeaderValue = "Custom-Value"; + + public AbstractHandler configureHandler() throws Exception { + return new ProxyHandler(customHeaderName, customHeaderValue); + } + + @BeforeClass(alwaysRun = true) + public void setUpGlobal() throws Exception { + server = new Server(); + ServerConnector connector = addHttpConnector(server); + server.setHandler(configureHandler()); + server.start(); + port1 = connector.getLocalPort(); + + server2 = new Server(); + ServerConnector connector2 = addHttpsConnector(server2); + server2.setHandler(new EchoHandler()); + server2.start(); + port2 = connector2.getLocalPort(); + + logger.info("Local HTTP server started successfully"); + } + + @AfterClass(alwaysRun = true) + public void tearDownGlobal() throws Exception { + server.stop(); + server2.stop(); + } + + @Test + public void testHttpProxy() throws Exception { + AsyncHttpClientConfig config = config() + .setFollowRedirect(true) + .setProxyServer( + proxyServer("localhost", port1) + .setCustomHeaders((req) -> new DefaultHttpHeaders().add(customHeaderName, customHeaderValue)) + .build() + ) + .setUseInsecureTrustManager(true) + .build(); + try (AsyncHttpClient asyncHttpClient = asyncHttpClient(config)) { + Response r = asyncHttpClient.executeRequest(post(getTargetUrl2()).setBody(new ByteArrayBodyGenerator(LARGE_IMAGE_BYTES))).get(); + assertEquals(r.getStatusCode(), 200); + } + } + + public static class ProxyHandler extends ConnectHandler { + String customHeaderName; + String customHeaderValue; + + public ProxyHandler(String customHeaderName, String customHeaderValue) { + this.customHeaderName = customHeaderName; + this.customHeaderValue = customHeaderValue; + } + + @Override + public void handle(String s, Request r, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { + if (HttpConstants.Methods.CONNECT.equalsIgnoreCase(request.getMethod())) { + if (request.getHeader(customHeaderName).equals(customHeaderValue)) { + response.setStatus(HttpServletResponse.SC_OK); + super.handle(s, r, request, response); + } else { + response.setStatus(HttpServletResponse.SC_BAD_REQUEST); + r.setHandled(true); + } + } else { + super.handle(s, r, request, response); + } + } + } +}