Skip to content

Commit

Permalink
#20: allow to inject header from client side. TODO: tests are needed
Browse files Browse the repository at this point in the history
  • Loading branch information
lucas34 authored and danikula committed Apr 26, 2017
1 parent 89ab593 commit 5f52f62
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 2 deletions.
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 @@ -2,6 +2,7 @@

import com.danikula.videocache.file.DiskUsage;
import com.danikula.videocache.file.FileNameGenerator;
import com.danikula.videocache.headers.HeaderInjector;
import com.danikula.videocache.sourcestorage.SourceInfoStorage;

import java.io.File;
Expand All @@ -17,12 +18,14 @@ class Config {
public final FileNameGenerator fileNameGenerator;
public final DiskUsage diskUsage;
public final SourceInfoStorage sourceInfoStorage;
public final HeaderInjector headerInjector;

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

File generateCacheFile(String url) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.text.TextUtils;

import com.danikula.videocache.file.FileCache;
import com.danikula.videocache.headers.HeaderInjector;

import java.io.BufferedOutputStream;
import java.io.IOException;
Expand All @@ -24,6 +25,7 @@ class HttpProxyCache extends ProxyCache {
private final HttpUrlSource source;
private final FileCache cache;
private CacheListener listener;
private HeaderInjector injector;

public HttpProxyCache(HttpUrlSource source, FileCache cache) {
super(source, cache);
Expand All @@ -35,6 +37,10 @@ public void registerCacheListener(CacheListener cacheListener) {
this.listener = cacheListener;
}

public void registerHeaderInjector(HeaderInjector headerInjector) {
this.injector = headerInjector;
}

public void processRequest(GetRequest request, Socket socket) throws IOException, ProxyCacheException {
OutputStream out = new BufferedOutputStream(socket.getOutputStream());
String responseHeaders = newResponseHeaders(request);
Expand Down Expand Up @@ -85,6 +91,7 @@ private void responseWithCache(OutputStream out, long offset) throws ProxyCacheE

private void responseWithoutCache(OutputStream out, long offset) throws ProxyCacheException, IOException {
HttpUrlSource newSourceNoCache = new HttpUrlSource(this.source);
newSourceNoCache.setHeaderInjector(injector);
try {
newSourceNoCache.open((int) offset);
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
Expand All @@ -109,4 +116,5 @@ protected void onCachePercentsAvailableChanged(int percents) {
listener.onCacheAvailable(cache.file, source.getUrl(), percents);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import com.danikula.videocache.file.Md5FileNameGenerator;
import com.danikula.videocache.file.TotalCountLruDiskUsage;
import com.danikula.videocache.file.TotalSizeLruDiskUsage;
import com.danikula.videocache.headers.EmptyHeadersInjector;
import com.danikula.videocache.headers.HeaderInjector;
import com.danikula.videocache.sourcestorage.SourceInfoStorage;
import com.danikula.videocache.sourcestorage.SourceInfoStorageFactory;

Expand Down Expand Up @@ -350,12 +352,14 @@ public static final class Builder {
private FileNameGenerator fileNameGenerator;
private DiskUsage diskUsage;
private SourceInfoStorage sourceInfoStorage;
private HeaderInjector headerInjector;

public Builder(Context context) {
this.sourceInfoStorage = SourceInfoStorageFactory.newSourceInfoStorage(context);
this.cacheRoot = StorageUtils.getIndividualCacheDirectory(context);
this.diskUsage = new TotalSizeLruDiskUsage(DEFAULT_MAX_SIZE);
this.fileNameGenerator = new Md5FileNameGenerator();
this.headerInjector = new EmptyHeadersInjector();
}

/**
Expand Down Expand Up @@ -426,6 +430,17 @@ public Builder diskUsage(DiskUsage diskUsage) {
return this;
}

/**
* Add headers along the request to the server
*
* @param headerInjector to inject header base on url
* @return a builder
*/
public Builder headerInjector(HeaderInjector headerInjector) {
this.headerInjector = checkNotNull(headerInjector);
return this;
}

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

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

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ private HttpProxyCache newHttpProxyCache() throws ProxyCacheException {
FileCache cache = new FileCache(config.generateCacheFile(url), config.diskUsage);
HttpProxyCache httpProxyCache = new HttpProxyCache(source, cache);
httpProxyCache.registerCacheListener(uiCacheListener);
httpProxyCache.registerHeaderInjector(config.headerInjector);
return httpProxyCache;
}

Expand Down
16 changes: 16 additions & 0 deletions library/src/main/java/com/danikula/videocache/HttpUrlSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import android.text.TextUtils;

import com.danikula.videocache.headers.EmptyHeadersInjector;
import com.danikula.videocache.headers.HeaderInjector;
import com.danikula.videocache.sourcestorage.SourceInfoStorage;
import com.danikula.videocache.sourcestorage.SourceInfoStorageFactory;

Expand All @@ -14,6 +16,7 @@
import java.io.InterruptedIOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;

import static com.danikula.videocache.Preconditions.checkNotNull;
import static com.danikula.videocache.ProxyCacheUtils.DEFAULT_BUFFER_SIZE;
Expand All @@ -37,6 +40,7 @@ public class HttpUrlSource implements Source {
private SourceInfo sourceInfo;
private HttpURLConnection connection;
private InputStream inputStream;
private HeaderInjector headerInjector = new EmptyHeadersInjector();

public HttpUrlSource(String url) {
this(url, SourceInfoStorageFactory.newEmptySourceInfoStorage());
Expand All @@ -54,6 +58,10 @@ public HttpUrlSource(HttpUrlSource source) {
this.sourceInfoStorage = source.sourceInfoStorage;
}

public void setHeaderInjector(HeaderInjector injector) {
this.headerInjector = injector;
}

@Override
public synchronized long length() throws ProxyCacheException {
if (sourceInfo.length == Integer.MIN_VALUE) {
Expand Down Expand Up @@ -150,6 +158,14 @@ private HttpURLConnection openConnection(long offset, int timeout) throws IOExce
do {
LOG.debug("Open connection " + (offset > 0 ? " with offset " + offset : "") + " to " + url);
connection = (HttpURLConnection) new URL(url).openConnection();

Map<String, String> extraHeaders = headerInjector.loadFromUrl(url);
if(extraHeaders != null && !extraHeaders.isEmpty()) {
for (Map.Entry<String, String> header : extraHeaders.entrySet()) {
connection.setRequestProperty(header.getKey(), header.getValue());
}
}

if (offset > 0) {
connection.setRequestProperty("Range", "bytes=" + offset + "-");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* Copyright
*/
package com.danikula.videocache.headers;

import java.util.HashMap;
import java.util.Map;

/**
* Created with IntelliJ
* Created by lucas
* Date 26/03/15
*/
public class EmptyHeadersInjector implements HeaderInjector {

@Override
public Map<String, String> loadFromUrl(String url) {
return new HashMap<>();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.danikula.videocache.headers;

import java.util.Map;

/**
* Created with IntelliJ
* Created by lucas
* Date 26/03/15
*/

public interface HeaderInjector {

Map<String, String> loadFromUrl(String url);

}

0 comments on commit 5f52f62

Please sign in to comment.