22
33import android .text .TextUtils ;
44
5+ import com .danikula .videocache .headers .EmptyHeadersInjector ;
6+ import com .danikula .videocache .headers .HeaderInjector ;
57import com .danikula .videocache .sourcestorage .SourceInfoStorage ;
68import com .danikula .videocache .sourcestorage .SourceInfoStorageFactory ;
79
1416import java .io .InterruptedIOException ;
1517import java .net .HttpURLConnection ;
1618import java .net .URL ;
19+ import java .util .Map ;
1720
1821import static com .danikula .videocache .Preconditions .checkNotNull ;
1922import static com .danikula .videocache .ProxyCacheUtils .DEFAULT_BUFFER_SIZE ;
@@ -34,6 +37,7 @@ public class HttpUrlSource implements Source {
3437
3538 private static final int MAX_REDIRECTS = 5 ;
3639 private final SourceInfoStorage sourceInfoStorage ;
40+ private final HeaderInjector headerInjector ;
3741 private SourceInfo sourceInfo ;
3842 private HttpURLConnection connection ;
3943 private InputStream inputStream ;
@@ -43,7 +47,12 @@ public HttpUrlSource(String url) {
4347 }
4448
4549 public HttpUrlSource (String url , SourceInfoStorage sourceInfoStorage ) {
50+ this (url , sourceInfoStorage , new EmptyHeadersInjector ());
51+ }
52+
53+ public HttpUrlSource (String url , SourceInfoStorage sourceInfoStorage , HeaderInjector headerInjector ) {
4654 this .sourceInfoStorage = checkNotNull (sourceInfoStorage );
55+ this .headerInjector = checkNotNull (headerInjector );
4756 SourceInfo sourceInfo = sourceInfoStorage .get (url );
4857 this .sourceInfo = sourceInfo != null ? sourceInfo :
4958 new SourceInfo (url , Integer .MIN_VALUE , ProxyCacheUtils .getSupposablyMime (url ));
@@ -52,6 +61,7 @@ public HttpUrlSource(String url, SourceInfoStorage sourceInfoStorage) {
5261 public HttpUrlSource (HttpUrlSource source ) {
5362 this .sourceInfo = source .sourceInfo ;
5463 this .sourceInfoStorage = source .sourceInfoStorage ;
64+ this .headerInjector = source .headerInjector ;
5565 }
5666
5767 @ Override
@@ -150,6 +160,7 @@ private HttpURLConnection openConnection(long offset, int timeout) throws IOExce
150160 do {
151161 LOG .debug ("Open connection " + (offset > 0 ? " with offset " + offset : "" ) + " to " + url );
152162 connection = (HttpURLConnection ) new URL (url ).openConnection ();
163+ injectCustomHeaders (connection , url );
153164 if (offset > 0 ) {
154165 connection .setRequestProperty ("Range" , "bytes=" + offset + "-" );
155166 }
@@ -171,6 +182,13 @@ private HttpURLConnection openConnection(long offset, int timeout) throws IOExce
171182 return connection ;
172183 }
173184
185+ private void injectCustomHeaders (HttpURLConnection connection , String url ) {
186+ Map <String , String > extraHeaders = headerInjector .addHeaders (url );
187+ for (Map .Entry <String , String > header : extraHeaders .entrySet ()) {
188+ connection .setRequestProperty (header .getKey (), header .getValue ());
189+ }
190+ }
191+
174192 public synchronized String getMime () throws ProxyCacheException {
175193 if (TextUtils .isEmpty (sourceInfo .mime )) {
176194 fetchContentInfo ();
0 commit comments