Skip to content

Commit

Permalink
Merged branch 'cowwoc-jetty-10.0.x-8723-provide-a-thread-safe-way-to-…
Browse files Browse the repository at this point in the history
…modify-proxies-at-runtime' into 'jetty-10.0.x'.
  • Loading branch information
sbordet committed Oct 25, 2022
2 parents a6afbaf + 5973ce2 commit 94c6e63
Show file tree
Hide file tree
Showing 18 changed files with 83 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void startServer() throws Exception
server.start();

URI uri = server.getURI();
client.getProxyConfiguration().getProxies().add(new HttpProxy("localhost", uri.getPort()));
client.getProxyConfiguration().addProxy(new HttpProxy("localhost", uri.getPort()));
}

@AfterEach
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,7 @@ public void proxy() throws Exception

// Add the new proxy to the list of proxies already registered.
ProxyConfiguration proxyConfig = httpClient.getProxyConfiguration();
proxyConfig.getProxies().add(proxy);
proxyConfig.addProxy(proxy);

ContentResponse response = httpClient.GET("http://domain.com/path");
// end::proxy[]
Expand All @@ -684,7 +684,7 @@ public void proxyAuthentication() throws Exception
// Proxy configuration.
ProxyConfiguration proxyConfig = httpClient.getProxyConfiguration();
HttpProxy proxy = new HttpProxy("proxy.net", 8080);
proxyConfig.getProxies().add(proxy);
proxyConfig.addProxy(proxy);

ContentResponse response = httpClient.newRequest(serverURI).send();
// end::proxyAuthentication[]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void startWithHttpClient() throws Exception
// Instantiate and configure HttpClient.
HttpClient httpClient = new HttpClient();
// For example, configure a proxy.
httpClient.getProxyConfiguration().getProxies().add(new HttpProxy("localhost", 8888));
httpClient.getProxyConfiguration().addProxy(new HttpProxy("localhost", 8888));

// Instantiate WebSocketClient, passing HttpClient to the constructor.
WebSocketClient webSocketClient = new WebSocketClient(httpClient);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@
package org.eclipse.jetty.client;

import java.net.URI;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;

import org.eclipse.jetty.http.HttpScheme;
import org.eclipse.jetty.io.ClientConnectionFactory;
import org.eclipse.jetty.util.BlockingArrayQueue;
import org.eclipse.jetty.util.HostPort;
import org.eclipse.jetty.util.ssl.SslContextFactory;

Expand All @@ -30,23 +31,50 @@
* Applications add subclasses of {@link Proxy} to this configuration via:
* <pre>
* ProxyConfiguration proxyConfig = httpClient.getProxyConfiguration();
* proxyConfig.getProxies().add(new HttpProxy(proxyHost, 8080));
* proxyConfig.addProxy(new HttpProxy(proxyHost, 8080));
* </pre>
*
* @see HttpClient#getProxyConfiguration()
*/
public class ProxyConfiguration
{
private final List<Proxy> proxies = new ArrayList<>();
private final List<Proxy> proxies = new BlockingArrayQueue<>();

/**
* @deprecated use {@link #addProxy(Proxy)} and {@link #removeProxy(Proxy)} instead
* @return the forward proxies to use
*/
@Deprecated(forRemoval = true)
public List<Proxy> getProxies()
{
return proxies;
}

/**
* Adds a proxy.
*
* @param proxy a proxy
* @throws NullPointerException if {@code proxy} is null
*/
public void addProxy(Proxy proxy)
{
proxies.add(Objects.requireNonNull(proxy));
}

/**
* Removes a proxy.
*
* @param proxy a proxy
* @return true if a match is found
*/
public boolean removeProxy(Proxy proxy)
{
return proxies.remove(proxy);
}

public Proxy match(Origin origin)
{
for (Proxy proxy : getProxies())
for (Proxy proxy : proxies)
{
if (proxy.matches(origin))
return proxy;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public void handle(String target, org.eclipse.jetty.server.Request baseRequest,
// Setup the custom proxy
int proxyPort = connector.getLocalPort();
int serverPort = proxyPort + 1; // Any port will do for these tests - just not the same as the proxy
client.getProxyConfiguration().getProxies().add(new CAFEBABEProxy(new Origin.Address("localhost", proxyPort), false));
client.getProxyConfiguration().addProxy(new CAFEBABEProxy(new Origin.Address("localhost", proxyPort), false));

ContentResponse response = client.newRequest(serverHost, serverPort)
.timeout(5, TimeUnit.SECONDS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ protected void service(String target, Request jettyRequest, HttpServletRequest r

int proxyPort = connector.getLocalPort();
int serverPort = proxyPort + 1; // Any port will do.
client.getProxyConfiguration().getProxies().add(new HttpProxy("localhost", proxyPort));
client.getProxyConfiguration().addProxy(new HttpProxy("localhost", proxyPort));

// We are simulating to be a HttpClient inside a proxy.
// The server is configured with the PROXY protocol to know the socket address of clients.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ else if (serverHost.equals(request.getServerName()))

int proxyPort = connector.getLocalPort();
int serverPort = proxyPort + 1; // Any port will do for these tests - just not the same as the proxy
client.getProxyConfiguration().getProxies().add(new HttpProxy("localhost", proxyPort));
client.getProxyConfiguration().addProxy(new HttpProxy("localhost", proxyPort));

ContentResponse response = client.newRequest(serverHost, serverPort)
.scheme(scenario.getScheme())
Expand Down Expand Up @@ -108,7 +108,7 @@ public void handle(String target, org.eclipse.jetty.server.Request baseRequest,
String proxyHost = "localhost";
int proxyPort = connector.getLocalPort();
int serverPort = proxyPort + 1; // Any port will do for these tests - just not the same as the proxy
client.getProxyConfiguration().getProxies().add(new HttpProxy(proxyHost, proxyPort));
client.getProxyConfiguration().addProxy(new HttpProxy(proxyHost, proxyPort));

ContentResponse response1 = client.newRequest(serverHost, serverPort)
.scheme(scenario.getScheme())
Expand Down Expand Up @@ -202,7 +202,7 @@ else if (target.startsWith("/server"))
});

int proxyPort = connector.getLocalPort();
client.getProxyConfiguration().getProxies().add(new HttpProxy(proxyHost, proxyPort));
client.getProxyConfiguration().addProxy(new HttpProxy(proxyHost, proxyPort));

ContentResponse response1 = client.newRequest(serverHost, serverPort)
.scheme(scenario.getScheme())
Expand Down Expand Up @@ -290,7 +290,7 @@ public void handle(String target, org.eclipse.jetty.server.Request baseRequest,
client.getAuthenticationStore().addAuthentication(new BasicAuthentication(proxyURI, proxyRealm, "proxyUser", "proxyPassword"));
URI serverURI = URI.create(scenario.getScheme() + "://" + serverHost + ":" + serverPort);
client.getAuthenticationStore().addAuthentication(new BasicAuthentication(serverURI, serverRealm, "serverUser", "serverPassword"));
client.getProxyConfiguration().getProxies().add(new HttpProxy(proxyHost, proxyPort));
client.getProxyConfiguration().addProxy(new HttpProxy(proxyHost, proxyPort));
final AtomicInteger requests = new AtomicInteger();
client.getRequestListeners().add(new Request.Listener.Adapter()
{
Expand Down Expand Up @@ -361,7 +361,7 @@ public void handle(String target, org.eclipse.jetty.server.Request baseRequest,
int serverPort = proxyPort + 1;
URI proxyURI = URI.create(scenario.getScheme() + "://" + proxyHost + ":" + proxyPort);
client.getAuthenticationStore().addAuthentication(new BasicAuthentication(proxyURI, proxyRealm, "proxyUser", "proxyPassword"));
client.getProxyConfiguration().getProxies().add(new HttpProxy(proxyHost, proxyPort));
client.getProxyConfiguration().addProxy(new HttpProxy(proxyHost, proxyPort));
final AtomicInteger requests = new AtomicInteger();
client.getRequestListeners().add(new Request.Listener.Adapter()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public void dispose() throws Exception
public void testSocks4Proxy() throws Exception
{
int proxyPort = proxy.socket().getLocalPort();
client.getProxyConfiguration().getProxies().add(new Socks4Proxy("localhost", proxyPort));
client.getProxyConfiguration().addProxy(new Socks4Proxy("localhost", proxyPort));

CountDownLatch latch = new CountDownLatch(1);

Expand Down Expand Up @@ -139,7 +139,7 @@ public void testSocks4Proxy() throws Exception
public void testSocks4ProxyWithSplitResponse() throws Exception
{
int proxyPort = proxy.socket().getLocalPort();
client.getProxyConfiguration().getProxies().add(new Socks4Proxy("localhost", proxyPort));
client.getProxyConfiguration().addProxy(new Socks4Proxy("localhost", proxyPort));

CountDownLatch latch = new CountDownLatch(1);

Expand Down Expand Up @@ -215,7 +215,7 @@ public void testSocks4ProxyWithTLSServer() throws Exception
// The hostname must be that of the server, not of the proxy.
ssl.setHostnameVerifier((hostname, session) -> serverHost.equals(hostname));
});
client.getProxyConfiguration().getProxies().add(new Socks4Proxy(proxyHost, proxyPort));
client.getProxyConfiguration().addProxy(new Socks4Proxy(proxyHost, proxyPort));

CountDownLatch latch = new CountDownLatch(1);
client.newRequest(serverHost, serverPort)
Expand Down Expand Up @@ -283,7 +283,7 @@ public void testRequestTimeoutWhenSocksProxyDoesNotRespond() throws Exception
{
String proxyHost = "localhost";
int proxyPort = proxy.socket().getLocalPort();
client.getProxyConfiguration().getProxies().add(new Socks4Proxy(proxyHost, proxyPort));
client.getProxyConfiguration().addProxy(new Socks4Proxy(proxyHost, proxyPort));

long timeout = 1000;
Request request = client.newRequest("localhost", proxyPort + 1)
Expand All @@ -305,7 +305,7 @@ public void testIdleTimeoutWhenSocksProxyDoesNotRespond() throws Exception
{
String proxyHost = "localhost";
int proxyPort = proxy.socket().getLocalPort();
client.getProxyConfiguration().getProxies().add(new Socks4Proxy(proxyHost, proxyPort));
client.getProxyConfiguration().addProxy(new Socks4Proxy(proxyHost, proxyPort));
long idleTimeout = 1000;
client.setIdleTimeout(idleTimeout);

Expand All @@ -327,7 +327,7 @@ public void testSocksProxyClosesConnectionImmediately() throws Exception
{
String proxyHost = "localhost";
int proxyPort = proxy.socket().getLocalPort();
client.getProxyConfiguration().getProxies().add(new Socks4Proxy(proxyHost, proxyPort));
client.getProxyConfiguration().addProxy(new Socks4Proxy(proxyHost, proxyPort));

Request request = client.newRequest("localhost", proxyPort + 1);
FutureResponseListener listener = new FutureResponseListener(request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ protected void service(String target, Request jettyRequest, HttpServletRequest r
});

int proxyPort = connector.getLocalPort();
client.getProxyConfiguration().getProxies().add(new HttpProxy(new Origin.Address("localhost", proxyPort), false, new Origin.Protocol(List.of("h2c"), false)));
client.getProxyConfiguration().addProxy(new HttpProxy(new Origin.Address("localhost", proxyPort), false, new Origin.Protocol(List.of("h2c"), false)));

int serverPort = proxyPort + 1; // Any port will do, just not the same as the proxy.
ContentResponse response = client.newRequest("localhost", serverPort)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ private void startClient() throws Exception
clientPool.setName("client");
client = new HttpClient();
client.setExecutor(clientPool);
client.getProxyConfiguration().getProxies().add(new HttpProxy("localhost", proxyConnector.getLocalPort()));
client.getProxyConfiguration().addProxy(new HttpProxy("localhost", proxyConnector.getLocalPort()));
client.start();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ public void onFillable()
ClientConnector clientConnector = new ClientConnector();
clientConnector.setSslContextFactory(clientTLS);
HttpClient httpClient = new HttpClient(new HttpClientTransportOverHTTP(clientConnector));
httpClient.getProxyConfiguration().getProxies().add(newHttpProxy());
httpClient.getProxyConfiguration().addProxy(newHttpProxy());
httpClient.start();

try
Expand Down Expand Up @@ -253,7 +253,7 @@ protected void addProxyHeaders(HttpServletRequest clientRequest, Request proxyRe
});

HttpClient httpClient = new HttpClient();
httpClient.getProxyConfiguration().getProxies().add(newHttpProxy());
httpClient.getProxyConfiguration().addProxy(newHttpProxy());
httpClient.start();

ContentResponse response = httpClient.newRequest("[::1]", serverConnector.getLocalPort())
Expand Down Expand Up @@ -292,7 +292,7 @@ protected void addProxyHeaders(HttpServletRequest clientRequest, Request proxyRe
});

HttpClient httpClient = new HttpClient();
httpClient.getProxyConfiguration().getProxies().add(newHttpProxy());
httpClient.getProxyConfiguration().addProxy(newHttpProxy());
httpClient.start();

ContentResponse response = httpClient.newRequest("[::1]", serverConnector.getLocalPort())
Expand Down
Loading

0 comments on commit 94c6e63

Please sign in to comment.