Skip to content

Commit

Permalink
Merge pull request #1498 from ratpack/http-client-proxy
Browse files Browse the repository at this point in the history
Http client proxy
  • Loading branch information
johnrengelman committed Jan 10, 2020
2 parents 1a4b802 + d433325 commit d2cd2fd
Show file tree
Hide file tree
Showing 17 changed files with 410 additions and 20 deletions.
1 change: 1 addition & 0 deletions ratpack-core/ratpack-core.gradle
Expand Up @@ -35,6 +35,7 @@ dependencies {
compile project(":ratpack-exec")
compile "io.netty:netty-codec-http:$commonVersions.netty"
compile "io.netty:netty-handler:$commonVersions.netty"
compile "io.netty:netty-handler-proxy:$commonVersions.netty"
compile 'com.sun.activation:javax.activation:1.2.0'

compile "com.github.ben-manes.caffeine:caffeine:${commonVersions.caffeine}"
Expand Down
Expand Up @@ -166,6 +166,13 @@ default Promise<ReceivedResponse> get(URI uri) {
*/
int getMaxResponseChunkSize();

/**
* The configured proxy instance for the client.
* @return The configure proxy instance for the client
* @since 1.8.0
*/
Proxy getProxy();

/**
* Closes any pooled connections.
*
Expand Down
10 changes: 10 additions & 0 deletions ratpack-core/src/main/java/ratpack/http/client/HttpClientSpec.java
Expand Up @@ -185,4 +185,14 @@ public interface HttpClientSpec {
* @since 1.6
*/
HttpClientSpec enableMetricsCollection(boolean enableMetricsCollection);

/**
* Configure a HTTP proxy for outgoing calls from this client.
*
* @param proxy the proxy configuration
* @return {@code this}
* @since 1.8.0
*/
HttpClientSpec proxy(Action<? super ProxySpec> proxy);

}
51 changes: 51 additions & 0 deletions ratpack-core/src/main/java/ratpack/http/client/Proxy.java
@@ -0,0 +1,51 @@
/*
* Copyright 2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package ratpack.http.client;

import java.util.Collection;

/**
* Configuration data for an HTTP proxy to utilize for outbound requests using {@link HttpClient}.
*
* @since 1.8.0
*/
public interface Proxy {

/**
* The host that proxied requests will be sent.
*
* @return The host that proxied requests will be sent.
*/
String getHost();

/**
* The port on the proxy where proxied requests will be sent.
*
* @return The port on the proxy where proxied requests will be sent.
*/
int getPort();

/**
* A collection of patterns which if any or matched, the outgoing request will bypass the HTTP proxy.
* <p>
* The format of the values in this list follow the standard
* <a href="https://docs.oracle.com/javase/8/docs/technotes/guides/net/proxies.html">Java Networking and Proxies standards</a>.
*
* @return A collection of patterns which if any or matched, the outgoing request will bypass the HTTP proxy.
*/
Collection<String> getNonProxyHosts();
}
52 changes: 52 additions & 0 deletions ratpack-core/src/main/java/ratpack/http/client/ProxySpec.java
@@ -0,0 +1,52 @@
/*
* Copyright 2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package ratpack.http.client;

import java.util.Collection;

/**
* Class for specifying configuration for an HTTP proxy to utilize for outgoing requests using {@link HttpClient}.
*
* @since 1.8.0
*/
public interface ProxySpec {

/**
* Configure the host that will proxy outbound HTTP requests.
* @param host the host name for the HTTP proxy
* @return {@code this}
*/
ProxySpec host(String host);

/**
* Configure the port on the proxy to will outbound HTTP requests will be sent.
* @param port the port for the HTTP proxy
* @return {@code this}
*/
ProxySpec port(int port);

/**
* Configure a collection of patterns for which if any are matched, the outbound request will bypass the HTTP proxy.
* <p>
* The format of the values in this list follow the standard
* <a href="https://docs.oracle.com/javase/8/docs/technotes/guides/net/proxies.html">Java Networking and Proxies standards</a>
*
* @param nonProxyHosts a list of patterns to match the destination host
* @return {@code this}
*/
ProxySpec nonProxyHosts(Collection<String> nonProxyHosts);
}
Expand Up @@ -29,13 +29,9 @@
import ratpack.exec.Promise;
import ratpack.exec.internal.ExecControllerInternal;
import ratpack.func.Action;
import ratpack.http.client.HttpClient;
import ratpack.http.client.HttpClientSpec;
import ratpack.http.client.HttpResponse;
import ratpack.http.client.ReceivedResponse;
import ratpack.http.client.RequestSpec;
import ratpack.http.client.StreamedResponse;
import ratpack.http.client.*;
import ratpack.server.ServerConfig;
import ratpack.util.Exceptions;
import ratpack.util.internal.TransportDetector;

import java.net.URI;
Expand Down Expand Up @@ -81,23 +77,33 @@ protected ChannelPool newPool(HttpChannelKey key) {
};

private final DefaultHttpClient.Spec spec;
private final ProxyInternal proxy;

private DefaultHttpClient(DefaultHttpClient.Spec spec) {
this.spec = spec;

if (spec.proxy != null) {
DefaultProxy.Spec proxySpec = new DefaultProxy.Spec();
Exceptions.uncheck(() -> spec.proxy.execute(proxySpec));
this.proxy = new DefaultProxy(proxySpec);
} else {
proxy = null;
}
}

private InstrumentedChannelPoolHandler getPoolingHandler(HttpChannelKey key) {

if (spec.enableMetricsCollection) {
return new InstrumentedFixedChannelPoolHandler(key, getPoolSize(), getIdleTimeout());
return new InstrumentedFixedChannelPoolHandler(key, getPoolSize(), getIdleTimeout(), proxy);
}
return new NoopFixedChannelPoolHandler(key, getIdleTimeout());
return new NoopFixedChannelPoolHandler(key, getIdleTimeout(), proxy);
}

private InstrumentedChannelPoolHandler getSimpleHandler(HttpChannelKey key) {
if (spec.enableMetricsCollection) {
return new InstrumentedSimpleChannelPoolHandler(key);
return new InstrumentedSimpleChannelPoolHandler(key, proxy);
}
return new NoopSimpleChannelPoolHandler(key);
return new NoopSimpleChannelPoolHandler(key, proxy);
}

@Override
Expand Down Expand Up @@ -155,6 +161,11 @@ public Duration getConnectTimeout() {
return spec.connectTimeout;
}

@Override
public Proxy getProxy() {
return proxy;
}

@Override
public void close() {
channelPoolMap.close();
Expand Down Expand Up @@ -192,6 +203,7 @@ private static class Spec implements HttpClientSpec {
private Action<? super HttpResponse> responseInterceptor = Action.noop();
private Action<? super Throwable> errorInterceptor = Action.noop();
private boolean enableMetricsCollection;
private Action<? super ProxySpec> proxy;

private Spec() {
}
Expand All @@ -208,6 +220,7 @@ private Spec(Spec spec) {
this.requestInterceptor = spec.requestInterceptor;
this.responseInterceptor = spec.responseInterceptor;
this.enableMetricsCollection = spec.enableMetricsCollection;
this.proxy = spec.proxy;
}

@Override
Expand Down Expand Up @@ -287,6 +300,12 @@ public HttpClientSpec enableMetricsCollection(boolean enableMetricsCollection) {
this.enableMetricsCollection = enableMetricsCollection;
return this;
}

@Override
public HttpClientSpec proxy(Action<? super ProxySpec> proxy) {
this.proxy = proxy;
return this;
}
}

@Override
Expand Down
@@ -0,0 +1,118 @@
/*
* Copyright 2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package ratpack.http.client.internal;

import ratpack.http.client.ProxySpec;

import java.util.Collection;
import java.util.Collections;

public class DefaultProxy implements ProxyInternal {

private final Spec spec;

@Override
public String getHost() {
return spec.host;
}

@Override
public int getPort() {
return spec.port;
}

@Override
public Collection<String> getNonProxyHosts() {
return spec.nonProxyHosts;
}

@Override
public boolean shouldProxy(String host) {
return !isIgnoredForHost(host);
}

// Captured from https://github.com/AsyncHttpClient/async-http-client/blob/758dcf214bf0ec08142ba234a3967d98a3dc60ef/client/src/main/java/org/asynchttpclient/proxy/ProxyServer.java
/**
* Checks whether proxy should be used according to nonProxyHosts settings of
* it, or we want to go directly to target host. If <code>null</code> proxy is
* passed in, this method returns true -- since there is NO proxy, we should
* avoid to use it. Simple hostname pattern matching using "*" are supported,
* but only as prefixes.
*
* @param hostname the hostname
* @return true if we have to ignore proxy use (obeying non-proxy hosts
* settings), false otherwise.
* @see <a href=
* "https://docs.oracle.com/javase/8/docs/api/java/net/doc-files/net-properties.html">Networking
* Properties</a>
*/
public boolean isIgnoredForHost(String hostname) {
if (spec.nonProxyHosts != null && spec.nonProxyHosts.size() > 0) {
for (String nonProxyHost : spec.nonProxyHosts) {
if (matchNonProxyHost(hostname, nonProxyHost)) {
return true;
}
}
}

return false;
}

// Captured from https://github.com/AsyncHttpClient/async-http-client/blob/758dcf214bf0ec08142ba234a3967d98a3dc60ef/client/src/main/java/org/asynchttpclient/proxy/ProxyServer.java
private boolean matchNonProxyHost(String targetHost, String nonProxyHost) {

if (nonProxyHost.length() > 1) {
if (nonProxyHost.charAt(0) == '*') {
return targetHost.regionMatches(true, targetHost.length() - nonProxyHost.length() + 1, nonProxyHost, 1,
nonProxyHost.length() - 1);
} else if (nonProxyHost.charAt(nonProxyHost.length() - 1) == '*') {
return targetHost.regionMatches(true, 0, nonProxyHost, 0, nonProxyHost.length() - 1);
}
}

return nonProxyHost.equalsIgnoreCase(targetHost);
}

public DefaultProxy(Spec spec) {
this.spec = spec;
}

public static class Spec implements ProxySpec {

private String host;
private int port;
private Collection<String> nonProxyHosts = Collections.emptyList();

@Override
public ProxySpec host(String host) {
this.host = host;
return this;
}

@Override
public ProxySpec port(int port) {
this.port = port;
return this;
}

@Override
public ProxySpec nonProxyHosts(Collection<String> nonProxyHosts) {
this.nonProxyHosts = nonProxyHosts;
return this;
}
}
}
Expand Up @@ -26,8 +26,8 @@ public class InstrumentedFixedChannelPoolHandler extends NoopFixedChannelPoolHan
private final LongAdder activeConnectionCount;
private final int maxConnectionCount;

public InstrumentedFixedChannelPoolHandler(HttpChannelKey channelKey, int poolSize, Duration idleTimeout) {
super(channelKey, idleTimeout);
public InstrumentedFixedChannelPoolHandler(HttpChannelKey channelKey, int poolSize, Duration idleTimeout, ProxyInternal proxy) {
super(channelKey, idleTimeout, proxy);
this.activeConnectionCount = new LongAdder();
this.maxConnectionCount = poolSize;
}
Expand Down
Expand Up @@ -24,8 +24,8 @@ public class InstrumentedSimpleChannelPoolHandler extends NoopSimpleChannelPoolH

private final LongAdder activeConnectionCount;

public InstrumentedSimpleChannelPoolHandler(HttpChannelKey channelKey) {
super(channelKey);
public InstrumentedSimpleChannelPoolHandler(HttpChannelKey channelKey, ProxyInternal proxy) {
super(channelKey, proxy);
this.activeConnectionCount = new LongAdder();
}

Expand Down

0 comments on commit d2cd2fd

Please sign in to comment.