-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🎉 fix #28: ignore system proxy for localhost requests
- Loading branch information
Showing
7 changed files
with
191 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
library/src/main/java/com/danikula/videocache/IgnoreHostProxySelector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package com.danikula.videocache; | ||
|
||
import java.io.IOException; | ||
import java.net.Proxy; | ||
import java.net.ProxySelector; | ||
import java.net.SocketAddress; | ||
import java.net.URI; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import static com.danikula.videocache.Preconditions.checkNotNull; | ||
|
||
/** | ||
* {@link ProxySelector} that ignore system default proxies for concrete host. | ||
* <p> | ||
* It is important to <a href="https://github.com/danikula/AndroidVideoCache/issues/28">ignore system proxy</a> for localhost connection. | ||
* | ||
* @author Alexey Danilov (danikula@gmail.com). | ||
*/ | ||
class IgnoreHostProxySelector extends ProxySelector { | ||
|
||
private static final List<Proxy> NO_PROXY_LIST = Arrays.asList(Proxy.NO_PROXY); | ||
|
||
private final ProxySelector defaultProxySelector; | ||
private final String hostToIgnore; | ||
private final int portToIgnore; | ||
|
||
IgnoreHostProxySelector(ProxySelector defaultProxySelector, String hostToIgnore, int portToIgnore) { | ||
this.defaultProxySelector = checkNotNull(defaultProxySelector); | ||
this.hostToIgnore = checkNotNull(hostToIgnore); | ||
this.portToIgnore = portToIgnore; | ||
} | ||
|
||
static void install(String hostToIgnore, int portToIgnore) { | ||
ProxySelector defaultProxySelector = ProxySelector.getDefault(); | ||
ProxySelector ignoreHostProxySelector = new IgnoreHostProxySelector(defaultProxySelector, hostToIgnore, portToIgnore); | ||
ProxySelector.setDefault(ignoreHostProxySelector); | ||
} | ||
|
||
@Override | ||
public List<Proxy> select(URI uri) { | ||
boolean ignored = hostToIgnore.equals(uri.getHost()) && portToIgnore == uri.getPort(); | ||
List<Proxy> proxies = ignored ? NO_PROXY_LIST : defaultProxySelector.select(uri); | ||
return proxies; | ||
} | ||
|
||
@Override | ||
public void connectFailed(URI uri, SocketAddress address, IOException failure) { | ||
defaultProxySelector.connectFailed(uri, address, failure); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
test/src/test/java/com/danikula/videocache/ProxySelectorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.danikula.videocache; | ||
|
||
import com.google.common.collect.Lists; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.mockito.Mockito; | ||
|
||
import java.net.InetSocketAddress; | ||
import java.net.Proxy; | ||
import java.net.ProxySelector; | ||
import java.net.URI; | ||
import java.util.List; | ||
|
||
import static com.danikula.videocache.support.ProxyCacheTestUtils.resetSystemProxy; | ||
import static org.fest.assertions.api.Assertions.assertThat; | ||
import static org.mockito.Mockito.when; | ||
|
||
/** | ||
* Tests {@link IgnoreHostProxySelector}. | ||
* | ||
* @author Alexey Danilov (danikula@gmail.com). | ||
*/ | ||
public class ProxySelectorTest extends BaseTest { | ||
|
||
@Before | ||
public void setup() throws Exception { | ||
resetSystemProxy(); | ||
} | ||
|
||
@Test // https://github.com/danikula/AndroidVideoCache/issues/28 | ||
public void testIgnoring() throws Exception { | ||
InetSocketAddress proxyAddress = new InetSocketAddress("proxy.com", 80); | ||
Proxy systemProxy = new Proxy(Proxy.Type.HTTP, proxyAddress); | ||
ProxySelector mockedProxySelector = Mockito.mock(ProxySelector.class); | ||
when(mockedProxySelector.select(Mockito.<URI>any())).thenReturn(Lists.newArrayList(systemProxy)); | ||
ProxySelector.setDefault(mockedProxySelector); | ||
|
||
IgnoreHostProxySelector.install("localhost", 42); | ||
|
||
ProxySelector proxySelector = ProxySelector.getDefault(); | ||
List<Proxy> githubProxies = proxySelector.select(new URI("http://github.com")); | ||
assertThat(githubProxies).hasSize(1); | ||
assertThat(githubProxies.get(0).address()).isEqualTo(proxyAddress); | ||
|
||
List<Proxy> localhostProxies = proxySelector.select(new URI("http://localhost:42")); | ||
assertThat(localhostProxies).hasSize(1); | ||
assertThat(localhostProxies.get(0)).isEqualTo(Proxy.NO_PROXY); | ||
|
||
List<Proxy> localhostPort69Proxies = proxySelector.select(new URI("http://localhost:69")); | ||
assertThat(localhostPort69Proxies).hasSize(1); | ||
assertThat(localhostPort69Proxies.get(0).address()).isEqualTo(proxyAddress); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters