Skip to content

Commit

Permalink
HttpClient does not use HTTP proxy when client options ssl=true overr…
Browse files Browse the repository at this point in the history
…idden by request options ssl=false - fixes #1944
  • Loading branch information
vietj committed Apr 17, 2017
1 parent 0f07ecc commit 0050e99
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
12 changes: 6 additions & 6 deletions src/main/java/io/vertx/core/http/impl/HttpClientImpl.java
Expand Up @@ -46,7 +46,6 @@

import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Arrays;
import java.util.Base64;
Expand Down Expand Up @@ -109,7 +108,7 @@ public class HttpClientImpl implements HttpClient, MetricsProvider {
private final ContextImpl creatingContext;
private final ConnectionManager connectionManager;
private final Closeable closeHook;
private final boolean useProxy;
private final ProxyType proxyType;
private final SSLHelper sslHelper;
private volatile boolean closed;
private volatile Function<HttpClientResponse, Future<HttpClientRequest>> redirectHandler = DEFAULT_HANDLER;
Expand Down Expand Up @@ -149,8 +148,7 @@ public HttpClientImpl(VertxInternal vertx, HttpClientOptions options) {
}
HttpClientMetrics metrics = vertx.metricsSPI().createMetrics(this, options);
connectionManager = new ConnectionManager(this, metrics);
ProxyOptions proxyOptions = options.getProxyOptions();
useProxy = !options.isSsl() && proxyOptions != null && proxyOptions.getType() == ProxyType.HTTP;
proxyType = options.getProxyOptions() != null ? options.getProxyOptions().getType() : null;
}

@Override
Expand Down Expand Up @@ -945,6 +943,8 @@ private HttpClientRequest createRequest(HttpMethod method, String host, int port
Objects.requireNonNull(relativeURI, "no null relativeURI accepted");
checkClosed();
HttpClientRequest req;
boolean useSSL = ssl != null ? ssl : options.isSsl();
boolean useProxy = !useSSL && proxyType == ProxyType.HTTP;
if (useProxy) {
relativeURI = "http://" + host + (port != 80 ? ":" + port : "") + relativeURI;
ProxyOptions proxyOptions = options.getProxyOptions();
Expand All @@ -955,11 +955,11 @@ private HttpClientRequest createRequest(HttpMethod method, String host, int port
headers.add("Proxy-Authorization", "Basic " + Base64.getEncoder()
.encodeToString((proxyOptions.getUsername() + ":" + proxyOptions.getPassword()).getBytes()));
}
req = new HttpClientRequestImpl(this, ssl != null ? ssl : options.isSsl(), method, proxyOptions.getHost(), proxyOptions.getPort(),
req = new HttpClientRequestImpl(this, useSSL, method, proxyOptions.getHost(), proxyOptions.getPort(),
relativeURI, vertx);
req.setHost(host + (port != 80 ? ":" + port : ""));
} else {
req = new HttpClientRequestImpl(this, ssl != null ? ssl : options.isSsl(), method, host, port, relativeURI, vertx);
req = new HttpClientRequestImpl(this, useSSL, method, host, port, relativeURI, vertx);
}
if (headers != null) {
req.headers().setAll(headers);
Expand Down
20 changes: 17 additions & 3 deletions src/test/java/io/vertx/test/core/Http1xTest.java
Expand Up @@ -36,6 +36,7 @@
import io.vertx.core.http.HttpServerRequest;
import io.vertx.core.http.HttpServerResponse;
import io.vertx.core.http.HttpVersion;
import io.vertx.core.http.RequestOptions;
import io.vertx.core.http.impl.HttpClientRequestImpl;
import io.vertx.core.impl.ConcurrentHashSet;
import io.vertx.core.impl.ContextImpl;
Expand Down Expand Up @@ -2705,22 +2706,35 @@ public void testServerConnectionExceptionHandler() throws Exception {
@Test
public void testHttpProxyRequest() throws Exception {
startProxy(null, ProxyType.HTTP);

client.close();
client = vertx.createHttpClient(new HttpClientOptions()
.setProxyOptions(new ProxyOptions().setType(ProxyType.HTTP).setHost("localhost").setPort(proxy.getPort())));
testHttpProxyRequest2(client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/"));
}

@Test
public void testHttpProxyRequestOverrideClientSsl() throws Exception {
startProxy(null, ProxyType.HTTP);
client.close();
client = vertx.createHttpClient(new HttpClientOptions()
.setSsl(true).setProxyOptions(new ProxyOptions().setType(ProxyType.HTTP).setHost("localhost").setPort(proxy.getPort())));
testHttpProxyRequest2(client.get(new RequestOptions().setSsl(false).setHost("localhost").setPort(8080)));
}

private void testHttpProxyRequest2(HttpClientRequest clientReq) throws Exception {
server.requestHandler(req -> {
req.response().end();
});

server.listen(onSuccess(s -> {
client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/", resp -> {
clientReq.handler(resp -> {
assertEquals(200, resp.statusCode());
assertNotNull("request did not go through proxy", proxy.getLastUri());
assertEquals("Host header doesn't contain target host", "localhost:8080", proxy.getLastRequestHeaders().get("Host"));
testComplete();
}).exceptionHandler(th -> fail(th)).end();
});
clientReq.exceptionHandler(this::fail);
clientReq.end();
}));
await();
}
Expand Down

0 comments on commit 0050e99

Please sign in to comment.