Skip to content

Commit

Permalink
Add to string (#5)
Browse files Browse the repository at this point in the history
* Added toString() to most Selectors as requested by #34

* Corrected wrong interpretation of the return value of the FindProxyForURL function (value with the proxy type 'HTTP').

* add CHANGELOG entry

Co-authored-by: Per Abich <[email protected]>
Co-authored-by: SUGIMOTO Kei <[email protected]>
  • Loading branch information
3 people authored Jun 8, 2020
1 parent 6a679d6 commit f34df91
Show file tree
Hide file tree
Showing 11 changed files with 92 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ This file contains the change log.
* Fixed issue #10: IE fixed socks proxy parsed as http. Thanks to OnixGH!
* Fixed issue #50: NullPointerException in Proxy-vole 1.0.5
* Fixed issue #53: Added support for DHCP resolution to WpadProxySearchStrategy. Thanks to RocusHalbasch!
* Fixed issue #34: Feature Request: useful toString

## 1.0.5
* Fixed issue #33: Problems with new Proxy-Vole 1.0.4. Thanks to gschnepp!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ public List<Proxy> select(URI uri) {
return ProxyUtil.noProxyList();
}

@Override
public String toString() {
return "NoProxySelector{}";
}
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,10 @@ public List<Proxy> select(URI uri) {
return this.proxyList;
}

@Override
public String toString() {
return "FixedProxySelector{" +
"proxyList=" + proxyList +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,17 @@ public enum CacheScope {
CACHE_SCOPE_URL
}

@Override
public String toString() {
return "BufferedProxySelector{" +
"delegate=" + delegate +
", cache=" + cache +
", maxSize=" + maxSize +
", ttl=" + ttl +
", cacheScope=" + cacheScope +
'}';
}

private ProxySelector delegate;

private ConcurrentHashMap<String, CacheEntry> cache;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,11 @@ public int size() {
return this.selectors.size();
}

@Override
public String toString() {
return "ProtocolDispatchSelector{" +
"selectors=" + selectors +
", fallbackSelector=" + fallbackSelector +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,12 @@ final void setRetryAfterMs(long retryAfterMs) {
this.retryAfterMs = retryAfterMs;
}

@Override
public String toString() {
return "ProxyListFallbackSelector{" +
"delegate=" + delegate +
", failedDelayCache=" + failedDelayCache +
", retryAfterMs=" + retryAfterMs +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -155,21 +155,20 @@ private List<Proxy> findProxy(URI uri) {
************************************************************************/

private Proxy buildProxyFromPacResult(String pacResult) {
if (pacResult.trim().length() < 6) {
return Proxy.NO_PROXY;
}
String proxyDef = pacResult.trim();
if (proxyDef.toUpperCase().startsWith(PAC_DIRECT)) {
return Proxy.NO_PROXY;
}
String[] words = pacResult.trim().split("\\s+");

if (words.length == 0) return Proxy.NO_PROXY;
if (words.length == 1) return Proxy.NO_PROXY;

String proxyType = words[0];
String host = concat(words,1);

// Check proxy type.
Proxy.Type type = Proxy.Type.HTTP;
if (proxyDef.toUpperCase().startsWith(PAC_SOCKS)) {
if (proxyType.toUpperCase().startsWith(PAC_SOCKS)) {
type = Proxy.Type.SOCKS;
}

String host = proxyDef.substring(6);
Integer port = ProxyUtil.DEFAULT_PROXY_PORT;

// Split port from host
Expand All @@ -183,5 +182,12 @@ private Proxy buildProxyFromPacResult(String pacResult) {
SocketAddress adr = InetSocketAddress.createUnresolved(host, port);
return new Proxy(type, adr);
}
private static String concat(String[] strings, int startIndex){
StringBuilder b = new StringBuilder();
for (int i = startIndex; i < strings.length; i++) {
b.append(strings[i]);
}
return b.toString();

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
this.delegate.connectFailed(uri, sa, ioe);
}

@Override
public String toString() {
return "ProxyBypassListSelector{" +
"delegate=" + delegate +
", whiteListFilter=" + whiteListFilter +
'}';
}

/*************************************************************************
* select
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,11 @@ public List<Proxy> select(URI uri) {
return ProxyUtil.noProxyList();
}

@Override
public String toString() {
return "UseProxyWhiteListSelector{" +
"delegate=" + delegate +
", whiteListFilter=" + whiteListFilter +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,27 @@ public void testScriptMuliProxy() throws ProxyException, MalformedURLException {
assertEquals(new Proxy(Type.HTTP, InetSocketAddress.createUnresolved("my-proxy4.com", 80)), result.get(3));
}

/*************************************************************************
* Test method
*
* @throws ProxyException
* on proxy detection error.
* @throws MalformedURLException
* on URL erros
************************************************************************/
@Test
public void testScriptProxyTypes() throws ProxyException, MalformedURLException {
PacProxySelector pacProxySelector = new PacProxySelector(new UrlPacScriptSource(toUrl("testProxyTypes.pac")));
List<Proxy> result = pacProxySelector.select(TestUtil.HTTP_TEST_URI);
assertEquals(6, result.size());
assertEquals(new Proxy(Type.HTTP, InetSocketAddress.createUnresolved("my-proxy.com", 80)), result.get(0));
assertEquals(new Proxy(Type.HTTP, InetSocketAddress.createUnresolved("my-proxy2.com", 80)), result.get(1));
assertEquals(new Proxy(Type.HTTP, InetSocketAddress.createUnresolved("my-proxy3.com", 486)), result.get(2));
assertEquals(new Proxy(Type.SOCKS, InetSocketAddress.createUnresolved("my-proxy4.com", 80)), result.get(3));
assertEquals(new Proxy(Type.SOCKS, InetSocketAddress.createUnresolved("my-proxy5.com", 80)), result.get(4));
assertEquals(new Proxy(Type.SOCKS, InetSocketAddress.createUnresolved("my-proxy6.com", 80)), result.get(5));
}

/*************************************************************************
* Test method for the override local IP feature.
*
Expand Down
4 changes: 4 additions & 0 deletions src/test/resources/pac/testProxyTypes.pac
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
function FindProxyForURL(url, host)
{
return "PROXY my-proxy.com:80 ; HTTP my-proxy2.com: 80; HTTPS my-proxy3.com :486; SOCKS my-proxy4.com:80; SOCKS4 my-proxy5.com:80; SOCKS5 my-proxy6.com:80;";
}

0 comments on commit f34df91

Please sign in to comment.