Skip to content

add HttpURLConnection proxy #302

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion library/src/main/java/com/danikula/videocache/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.danikula.videocache.sourcestorage.SourceInfoStorage;

import java.io.File;
import java.net.Proxy;

/**
* Configuration for proxy cache.
Expand All @@ -19,13 +20,15 @@ class Config {
public final DiskUsage diskUsage;
public final SourceInfoStorage sourceInfoStorage;
public final HeaderInjector headerInjector;
public final Proxy proxy;

Config(File cacheRoot, FileNameGenerator fileNameGenerator, DiskUsage diskUsage, SourceInfoStorage sourceInfoStorage, HeaderInjector headerInjector) {
Config(File cacheRoot, FileNameGenerator fileNameGenerator, DiskUsage diskUsage, SourceInfoStorage sourceInfoStorage, HeaderInjector headerInjector, Proxy proxy) {
this.cacheRoot = cacheRoot;
this.fileNameGenerator = fileNameGenerator;
this.diskUsage = diskUsage;
this.sourceInfoStorage = sourceInfoStorage;
this.headerInjector = headerInjector;
this.proxy = proxy;
}

File generateCacheFile(String url) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Proxy;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
Expand Down Expand Up @@ -353,6 +354,7 @@ public static final class Builder {
private DiskUsage diskUsage;
private SourceInfoStorage sourceInfoStorage;
private HeaderInjector headerInjector;
private Proxy proxy;

public Builder(Context context) {
this.sourceInfoStorage = SourceInfoStorageFactory.newSourceInfoStorage(context);
Expand Down Expand Up @@ -441,6 +443,12 @@ public Builder headerInjector(HeaderInjector headerInjector) {
return this;
}


public Builder proxy(Proxy proxy) {
this.proxy = checkNotNull(proxy);
return this;
}

/**
* Builds new instance of {@link HttpProxyCacheServer}.
*
Expand All @@ -452,7 +460,7 @@ public HttpProxyCacheServer build() {
}

private Config buildConfig() {
return new Config(cacheRoot, fileNameGenerator, diskUsage, sourceInfoStorage, headerInjector);
return new Config(cacheRoot, fileNameGenerator, diskUsage, sourceInfoStorage, headerInjector, proxy);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public int getClientsCount() {
}

private HttpProxyCache newHttpProxyCache() throws ProxyCacheException {
HttpUrlSource source = new HttpUrlSource(url, config.sourceInfoStorage, config.headerInjector);
HttpUrlSource source = new HttpUrlSource(url, config.sourceInfoStorage, config.headerInjector, config.proxy);
FileCache cache = new FileCache(config.generateCacheFile(url), config.diskUsage);
HttpProxyCache httpProxyCache = new HttpProxyCache(source, cache);
httpProxyCache.registerCacheListener(uiCacheListener);
Expand Down
11 changes: 8 additions & 3 deletions library/src/main/java/com/danikula/videocache/HttpUrlSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.io.InputStream;
import java.io.InterruptedIOException;
import java.net.HttpURLConnection;
import java.net.Proxy;
import java.net.URL;
import java.util.Map;

Expand All @@ -38,6 +39,7 @@ public class HttpUrlSource implements Source {
private static final int MAX_REDIRECTS = 5;
private final SourceInfoStorage sourceInfoStorage;
private final HeaderInjector headerInjector;
public final Proxy proxy;
private SourceInfo sourceInfo;
private HttpURLConnection connection;
private InputStream inputStream;
Expand All @@ -47,12 +49,13 @@ public HttpUrlSource(String url) {
}

public HttpUrlSource(String url, SourceInfoStorage sourceInfoStorage) {
this(url, sourceInfoStorage, new EmptyHeadersInjector());
this(url, sourceInfoStorage, new EmptyHeadersInjector(),null);
}

public HttpUrlSource(String url, SourceInfoStorage sourceInfoStorage, HeaderInjector headerInjector) {
public HttpUrlSource(String url, SourceInfoStorage sourceInfoStorage, HeaderInjector headerInjector, Proxy proxy) {
this.sourceInfoStorage = checkNotNull(sourceInfoStorage);
this.headerInjector = checkNotNull(headerInjector);
this.proxy = proxy;
SourceInfo sourceInfo = sourceInfoStorage.get(url);
this.sourceInfo = sourceInfo != null ? sourceInfo :
new SourceInfo(url, Integer.MIN_VALUE, ProxyCacheUtils.getSupposablyMime(url));
Expand All @@ -62,6 +65,7 @@ public HttpUrlSource(HttpUrlSource source) {
this.sourceInfo = source.sourceInfo;
this.sourceInfoStorage = source.sourceInfoStorage;
this.headerInjector = source.headerInjector;
this.proxy = null;
}

@Override
Expand Down Expand Up @@ -159,7 +163,8 @@ private HttpURLConnection openConnection(long offset, int timeout) throws IOExce
String url = this.sourceInfo.url;
do {
LOG.debug("Open connection " + (offset > 0 ? " with offset " + offset : "") + " to " + url);
connection = (HttpURLConnection) new URL(url).openConnection();
if (proxy == null)connection = (HttpURLConnection) new URL(url).openConnection();
else connection = (HttpURLConnection) new URL(url).openConnection(proxy);
injectCustomHeaders(connection, url);
if (offset > 0) {
connection.setRequestProperty("Range", "bytes=" + offset + "-");
Expand Down