22
33import android .text .TextUtils ;
44import android .util .Log ;
5+
56import java .io .BufferedInputStream ;
67import java .io .IOException ;
78import java .io .InputStream ;
1112
1213import static com .danikula .videocache .ProxyCacheUtils .DEFAULT_BUFFER_SIZE ;
1314import static com .danikula .videocache .ProxyCacheUtils .LOG_TAG ;
15+ import static java .net .HttpURLConnection .HTTP_MOVED_PERM ;
16+ import static java .net .HttpURLConnection .HTTP_MOVED_TEMP ;
1417import static java .net .HttpURLConnection .HTTP_OK ;
1518import static java .net .HttpURLConnection .HTTP_PARTIAL ;
19+ import static java .net .HttpURLConnection .HTTP_SEE_OTHER ;
1620
1721/**
1822 * {@link Source} that uses http resource as source for {@link ProxyCache}.
2125 */
2226public class HttpUrlSource implements Source {
2327
24- public String url ;
28+ private static final int MAX_REDIRECTS = 5 ;
29+ public final String url ;
2530 private HttpURLConnection connection ;
2631 private InputStream inputStream ;
2732 private volatile int available = Integer .MIN_VALUE ;
@@ -47,33 +52,10 @@ public synchronized int available() throws ProxyCacheException {
4752 @ Override
4853 public void open (int offset ) throws ProxyCacheException {
4954 try {
50- boolean isRedirected ;
51- int redirectCount = 0 ;
52- int responseCode ;
53- do {
54- Log .d (ProxyCacheUtils .LOG_TAG , "Open connection " + (offset > 0 ? " with offset " + offset : "" ) + " to " + url );
55- connection = (HttpURLConnection ) new URL (url ).openConnection ();
56- if (offset > 0 ) {
57- connection .setRequestProperty ("Range" , "bytes=" + offset + "-" );
58- }
59- responseCode = connection .getResponseCode ();
60- if ((responseCode == HttpURLConnection .HTTP_MOVED_PERM
61- || responseCode == HttpURLConnection .HTTP_MOVED_TEMP
62- || responseCode == HttpURLConnection .HTTP_SEE_OTHER )) {
63- url = connection .getHeaderField ("Location" );
64- isRedirected = true ;
65- redirectCount ++;
66- } else {
67- isRedirected = false ;
68- }
69- if (redirectCount > ProxyCacheUtils .MAX_REDIRECTS ) {
70- throw new ProxyCacheException ("Too many redirects" );
71- }
72- } while (isRedirected );
73-
55+ connection = openConnection (offset , "GET" , -1 );
7456 mime = connection .getContentType ();
7557 inputStream = new BufferedInputStream (connection .getInputStream (), DEFAULT_BUFFER_SIZE );
76- available = readSourceAvailableBytes (connection , offset , responseCode );
58+ available = readSourceAvailableBytes (connection , offset , connection . getResponseCode () );
7759 } catch (IOException e ) {
7860 throw new ProxyCacheException ("Error opening connection for " + url + " with offset " + offset , e );
7961 }
@@ -111,28 +93,7 @@ private void fetchContentInfo() throws ProxyCacheException {
11193 HttpURLConnection urlConnection = null ;
11294 InputStream inputStream = null ;
11395 try {
114- boolean isRedirected ;
115- int redirectCount = 0 ;
116- do {
117- urlConnection = (HttpURLConnection ) new URL (url ).openConnection ();
118- urlConnection .setConnectTimeout (10000 );
119- urlConnection .setReadTimeout (10000 );
120- urlConnection .setRequestMethod ("HEAD" );
121- int responseCode = urlConnection .getResponseCode ();
122- if ((responseCode == HttpURLConnection .HTTP_MOVED_PERM
123- || responseCode == HttpURLConnection .HTTP_MOVED_TEMP
124- || responseCode == HttpURLConnection .HTTP_SEE_OTHER )) {
125- url = urlConnection .getHeaderField ("Location" );
126- isRedirected = true ;
127- redirectCount ++;
128- } else {
129- isRedirected = false ;
130- }
131- if (redirectCount > ProxyCacheUtils .MAX_REDIRECTS ) {
132- throw new ProxyCacheException ("Too many redirects" );
133- }
134- } while (isRedirected );
135-
96+ urlConnection = openConnection (0 , "HEAD" , 10000 );
13697 available = urlConnection .getContentLength ();
13798 mime = urlConnection .getContentType ();
13899 inputStream = urlConnection .getInputStream ();
@@ -147,13 +108,47 @@ private void fetchContentInfo() throws ProxyCacheException {
147108 }
148109 }
149110
111+ private HttpURLConnection openConnection (int offset , String method , int timeout ) throws IOException , ProxyCacheException {
112+ HttpURLConnection connection ;
113+ boolean redirected ;
114+ int redirectCount = 0 ;
115+ String url = this .url ;
116+ do {
117+ Log .d (LOG_TAG , "Open connection " + (offset > 0 ? " with offset " + offset : "" ) + " to " + url );
118+ connection = (HttpURLConnection ) new URL (url ).openConnection ();
119+ connection .setRequestMethod (method );
120+ if (offset > 0 ) {
121+ connection .setRequestProperty ("Range" , "bytes=" + offset + "-" );
122+ }
123+ if (timeout > 0 ) {
124+ connection .setConnectTimeout (timeout );
125+ connection .setReadTimeout (timeout );
126+ }
127+ int code = connection .getResponseCode ();
128+ redirected = code == HTTP_MOVED_PERM || code == HTTP_MOVED_TEMP || code == HTTP_SEE_OTHER ;
129+ if (redirected ) {
130+ url = connection .getHeaderField ("Location" );
131+ redirectCount ++;
132+ connection .disconnect ();
133+ }
134+ if (redirectCount > MAX_REDIRECTS ) {
135+ throw new ProxyCacheException ("Too many redirects: " + redirectCount );
136+ }
137+ } while (redirected );
138+ return connection ;
139+ }
140+
150141 public synchronized String getMime () throws ProxyCacheException {
151142 if (TextUtils .isEmpty (mime )) {
152143 fetchContentInfo ();
153144 }
154145 return mime ;
155146 }
156147
148+ public String getUrl () {
149+ return url ;
150+ }
151+
157152 @ Override
158153 public String toString () {
159154 return "HttpUrlSource{url='" + url + "}" ;
0 commit comments