@@ -218,10 +218,6 @@ private static WebResourceResponse createWebResourceResponse(String mimeType, St
218
218
* @return a response if the request URL had a matching handler, null if no handler was found.
219
219
*/
220
220
public WebResourceResponse shouldInterceptRequest (Uri uri , WebResourceRequest request ) {
221
- if (isLocalProxySource (uri )) {
222
- return handleLocalProxyRequest (uri , request );
223
- }
224
-
225
221
PathHandler handler ;
226
222
synchronized (uriMatcher ) {
227
223
handler = (PathHandler ) uriMatcher .match (uri );
@@ -264,69 +260,34 @@ private WebResourceResponse handleLocalProxyRequest(Uri uri, WebResourceRequest
264
260
URL httpsUrl = new URL (fixedUri );
265
261
URLConnection connection = httpsUrl .openConnection ();
266
262
HttpURLConnection httpConnection = (HttpURLConnection )connection ;
267
- InputStream responseStream = connection .getInputStream ();
268
-
269
- Map <String , String > headers = new HashMap <String , String >();
270
- for (Map .Entry <String , List <String >> entry : connection .getHeaderFields ().entrySet ()) {
271
- String key = entry .getKey ();
272
- headers .put (key , entry .getValue ().get (0 ));
273
- }
274
263
275
- int code = httpConnection . getResponseCode ();
264
+ Map < String , String > headers = new HashMap < String , String > ();
276
265
if (request != null && request .getRequestHeaders ().get ("Range" ) != null ) {
277
266
String rangeString = request .getRequestHeaders ().get ("Range" );
278
- int contentLength = 0 ;
279
-
280
- if (responseStream .available () <= 0 ){
281
- contentLength = Integer .parseInt (headers .get ("Content-Length" ));
282
- } else {
283
- contentLength = responseStream .available ();
284
- }
267
+ httpConnection .addRequestProperty ("Range" , rangeString );
268
+
269
+ String contentHeader = connection .getHeaderFields ().get ("Content-Length" ).get (0 );
285
270
271
+ int contentLength = Integer .parseInt (contentHeader );
286
272
String [] parts = rangeString .split ("=" );
287
273
String [] streamParts = parts [1 ].split ("-" );
288
274
String fromRange = streamParts [0 ];
289
275
int range = contentLength - 1 ;
290
276
291
277
headers .put ("Accept-Ranges" , "bytes" );
292
- // headers.put("Content-Length", headers.get("Content-Length") );
278
+ headers .put ("Content-Length" , contentHeader );
293
279
headers .put ("Content-Range" , "bytes " + fromRange + "-" + range + "/" + contentLength );
294
-
295
- /*
296
- int contentLength = Integer.parseInt(contentStr[1]);
297
- String[] parts = rangeString.split("=");
298
- String[] streamParts = parts[1].split("-");
299
- String fromRange = streamParts[0];
300
- int range = contentLength - 1;
301
-
302
- headers.put("Accept-Ranges", "bytes");
303
- headers.put("Content-Length", contentStr[1]);
304
- headers.put("Content-Range", "bytes " + fromRange + "-" + range + "/" + contentLength);*/
305
-
306
- code = 206 ; // Partial content being served
307
- //String[] contentLength = request.getRequestHeaders().get("Content-Length").split(",");
308
-
309
- /*int currentRange = Integer.parseInt(rangeString.split("=")[1].replace("-", ""));
310
- int totalRange = Integer.parseInt(contentLength[1].trim());
311
-
312
- httpConnection.setRequestProperty("Range", rangeString);
313
- //httpConnection.connect();
314
-
315
- headers.put("Content-Length", contentLength[1].trim());
316
- headers.put("Content-Range", "bytes " + currentRange + "-" + (totalRange - 1) + "/" + totalRange);*/
317
280
}
318
281
319
282
// Bypass CORS
320
283
headers .put ("Access-Control-Allow-Origin" , "*" );
321
284
headers .put ("Access-Control-Allow-Methods" , "GET, POST, DELETE, PUT, OPTIONS" );
322
285
headers .put ("Access-Control-Allow-Headers" , "agent, user-data, Access-Control-Allow-Headers, Origin, Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers" );
323
- headers .put ("Content-Type" , request .getRequestHeaders ().get ("Content-Type" ));
324
286
325
287
return new WebResourceResponse (connection .getContentType (), connection .getContentEncoding (),
326
- code , httpConnection .getResponseMessage (), headers , responseStream );
288
+ httpConnection . getResponseCode () , httpConnection .getResponseMessage (), headers , httpConnection . getInputStream () );
327
289
328
290
} catch (Exception e ) {
329
- //an error occurred
330
291
return null ;
331
292
}
332
293
}
0 commit comments