Skip to content

Commit

Permalink
Implements for HtmlUnitDriver a support of SOCKS proxy and bypass pro…
Browse files Browse the repository at this point in the history
…xy addresses.

What's done:
* support of SOCKS proxy address.
* support of bypass proxy hosts (noProxy) for HTTP and SOCKS proxies.
* tests

Now you could specify proxy settings using capabilities,
Proxy object or directly calling methods for existed driver.

Be note that bypass proxy hosts should be a string of hosts delimited by comma
and each host needs to be Java regex pattern.
  • Loading branch information
alex-savchuk committed Sep 7, 2013
1 parent 0aa9068 commit 11f3c0e
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 18 deletions.
144 changes: 126 additions & 18 deletions java/client/src/org/openqa/selenium/htmlunit/HtmlUnitDriver.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -202,23 +202,7 @@ public HtmlUnitDriver(Capabilities capabilities) {

setJavascriptEnabled(capabilities.isJavascriptEnabled());

if (capabilities.getCapability(CapabilityType.PROXY) != null) {
Proxy proxy = Proxies.extractProxy(capabilities);
String fullProxy = proxy.getHttpProxy();
String pacfile = proxy.getProxyAutoconfigUrl();
if (fullProxy != null) {
int index = fullProxy.indexOf(":");
if (index != -1) {
String host = fullProxy.substring(0, index);
int port = Integer.parseInt(fullProxy.substring(index + 1));
setProxy(host, port);
} else {
setProxy(fullProxy, 0);
}
} else if(pacfile != null && !pacfile.equals("")) {
setAutoProxy(pacfile);
}
}
setProxySettings(Proxies.extractProxy(capabilities));
}

// Package visibility for testing
Expand Down Expand Up @@ -317,11 +301,135 @@ protected WebClient modifyWebClient(WebClient client) {
return client;
}

/**
* Set proxy for WebClient using Proxy.
*
* @param proxy The proxy preferences.
*/
public void setProxySettings(Proxy proxy) {
if (proxy == null || proxy.getProxyType() == Proxy.ProxyType.UNSPECIFIED) {
return;
}

switch (proxy.getProxyType()) {
case MANUAL:

ArrayList<String> noProxyHosts = new ArrayList<String>();
String noProxy = proxy.getNoProxy();
if (noProxy != null && !noProxy.equals("")) {
String[] hosts = noProxy.split(",");
for (int i = 0; i < hosts.length; i++) {
if (hosts[i].trim().length() > 0) {
noProxyHosts.add(hosts[i].trim());
}
}
}

String httpProxy = proxy.getHttpProxy();
if (httpProxy != null && !httpProxy.equals("")) {
String host = httpProxy;
int port = 0;

int index = httpProxy.indexOf(":");
if (index != -1) {
host = httpProxy.substring(0, index);
port = Integer.parseInt(httpProxy.substring(index + 1));
}

setHTTPProxy(host, port, noProxyHosts);
}

String socksProxy = proxy.getSocksProxy();
if (socksProxy != null && !socksProxy.equals("")) {
String host = socksProxy;
int port = 0;

int index = socksProxy.indexOf(":");
if (index != -1) {
host = socksProxy.substring(0, index);
port = Integer.parseInt(socksProxy.substring(index + 1));
}

setSocksProxy(host, port, noProxyHosts);
}

// sslProxy is not supported/implemented
// ftpProxy is not supported/implemented

break;
case PAC:
String pac = proxy.getProxyAutoconfigUrl();
if (pac != null && !pac.equals("")) {
setAutoProxy(pac);
}
break;
}
}

/**
* Sets HTTP proxy for WebClient
*
* @param host The hostname of HTTP proxy
* @param port The port of HTTP proxy, 0 means HTTP proxy w/o port
*/
public void setProxy(String host, int port) {
proxyConfig = new ProxyConfig(host, port);
setHTTPProxy(host, port, null);
}

/**
* Sets HTTP proxy for WebClient with bypass proxy hosts
*
* @param host The hostname of HTTP proxy
* @param port The port of HTTP proxy, 0 means HTTP proxy w/o port
* @param noProxyHosts The list of hosts which need to bypass HTTP proxy
*/
public void setHTTPProxy(String host, int port, ArrayList<String> noProxyHosts) {
proxyConfig = new ProxyConfig();
proxyConfig.setProxyHost(host);
proxyConfig.setProxyPort(port);
if (noProxyHosts != null && noProxyHosts.size() > 0) {
for (String noProxyHost : noProxyHosts) {
proxyConfig.addHostsToProxyBypass(noProxyHost);
}
}
getWebClient().getOptions().setProxyConfig(proxyConfig);
}

/**
* Sets SOCKS proxy for WebClient
*
* @param host The hostname of SOCKS proxy
* @param port The port of SOCKS proxy, 0 means HTTP proxy w/o port
*/
public void setSocksProxy(String host, int port) {
setSocksProxy(host, port, null);
}

/**
* Sets SOCKS proxy for WebClient with bypass proxy hosts
*
* @param host The hostname of SOCKS proxy
* @param port The port of SOCKS proxy, 0 means HTTP proxy w/o port
* @param noProxyHosts The list of hosts which need to bypass SOCKS proxy
*/
public void setSocksProxy(String host, int port, ArrayList<String> noProxyHosts) {
proxyConfig = new ProxyConfig();
proxyConfig.setProxyHost(host);
proxyConfig.setProxyPort(port);
proxyConfig.setSocksProxy(true);
if (noProxyHosts != null && noProxyHosts.size() > 0) {
for (String noProxyHost : noProxyHosts) {
proxyConfig.addHostsToProxyBypass(noProxyHost);
}
}
getWebClient().getOptions().setProxyConfig(proxyConfig);
}

/**
* Sets Proxy Autoconfiguration URL for WebClient
*
* @param autoProxyUrl The Proxy Autoconfiguration URL
*/
public void setAutoProxy(String autoProxyUrl) {
proxyConfig = new ProxyConfig();
proxyConfig.setProxyAutoConfigUrl(autoProxyUrl);
Expand Down
1 change: 1 addition & 0 deletions java/client/test/org/openqa/selenium/htmlunit/HtmlUnitSpecificTests.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
@SuiteClasses({
HtmlUnitCapabilitiesTest.class,
InputKeysContainerTest.class,
HtmlUnitProxyTest.class,
ToStringTest.class
})
public class HtmlUnitSpecificTests {
Expand Down

0 comments on commit 11f3c0e

Please sign in to comment.