Skip to content

Commit

Permalink
[java] Allow setting proxy for all http client instances (#12940)
Browse files Browse the repository at this point in the history
he default AsyncHttpClient used in the previous version of Selenium allowed the use of a global proxy set via the system properties. https://github.com/SeleniumHQ/selenium/blob/selenium-4.13.0/java/src/org/openqa/selenium/remote/http/netty/NettyClient.java#L88C3-L88C3

Extending the same functionality for the current default JDK 11 Http Client.

However, we have the option to use client config to set proxy in the RemoteWebDriverBuilder. It is used by the underlying HttpClient of RemoteWebDriver only. The other HttpClients created along the flow, do not use it. Example:
https://github.com/SeleniumHQ/selenium/blob/trunk/java/src/org/openqa/selenium/devtools/CdpEndpointFinder.java#L48

Similarly, for the Grid, we create different instances of HttpClient to be used in different classes which cannot be configured to use the proxy.
  • Loading branch information
pujagani authored Oct 17, 2023
1 parent ae0d894 commit e8c3e9d
Showing 1 changed file with 27 additions and 19 deletions.
46 changes: 27 additions & 19 deletions java/src/org/openqa/selenium/remote/http/jdk/JdkHttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.io.IOException;
import java.io.UncheckedIOException;
import java.net.Authenticator;
import java.net.InetSocketAddress;
import java.net.PasswordAuthentication;
import java.net.ProtocolException;
import java.net.Proxy;
Expand All @@ -45,6 +46,7 @@
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Supplier;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand Down Expand Up @@ -123,28 +125,34 @@ protected PasswordAuthentication getPasswordAuthentication() {
builder = builder.authenticator(authenticator);
}

Proxy proxy = config.proxy();
if (proxy != null) {
ProxySelector proxySelector =
new ProxySelector() {
@Override
public List<Proxy> select(URI uri) {
if (proxy == null) {
return List.of();
}
if (uri.getScheme().toLowerCase().startsWith("http")) {
return List.of(proxy);
}
String proxyHost = System.getProperty("http.proxyHost");
String proxyPort = System.getProperty("http.proxyPort");

Proxy proxy =
(proxyHost != null && proxyPort != null)
? new Proxy(
Proxy.Type.HTTP, new InetSocketAddress(proxyHost, Integer.parseInt(proxyPort)))
: config.proxy();

ProxySelector proxySelector =
new ProxySelector() {
@Override
public List<Proxy> select(URI uri) {
if (proxy == null) {
return List.of();
}

@Override
public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
// Do nothing
if (uri.getScheme().toLowerCase().startsWith("http")) {
return List.of(proxy);
}
};
builder = builder.proxy(proxySelector);
}
return List.of();
}

@Override
public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
// Do nothing
}
};
builder = builder.proxy(proxySelector);

SSLContext sslContext = config.sslContext();
if (sslContext != null) {
Expand Down

0 comments on commit e8c3e9d

Please sign in to comment.