@@ -218,6 +218,10 @@ private static WebResourceResponse createWebResourceResponse(String mimeType, St
218218 * @return a response if the request URL had a matching handler, null if no handler was found.
219219 */
220220 public WebResourceResponse shouldInterceptRequest (Uri uri , WebResourceRequest request ) {
221+ if (isLocalProxySource (uri )) {
222+ return handleLocalProxyRequest (uri , request );
223+ }
224+
221225 PathHandler handler ;
222226 synchronized (uriMatcher ) {
223227 handler = (PathHandler ) uriMatcher .match (uri );
@@ -260,26 +264,67 @@ private WebResourceResponse handleLocalProxyRequest(Uri uri, WebResourceRequest
260264 URL httpsUrl = new URL (fixedUri );
261265 URLConnection connection = httpsUrl .openConnection ();
262266 HttpURLConnection httpConnection = (HttpURLConnection )connection ;
267+ InputStream responseStream = connection .getInputStream ();
263268
264- httpConnection .setRequestMethod (request .getMethod ());
265- for (Map .Entry <String , String > entry : request .getRequestHeaders ().entrySet ()) {
266- httpConnection .setRequestProperty (entry .getKey (), entry .getValue ());
267- }
268-
269- httpConnection .connect ();
270-
271- // Pass them trough (Convert String,List<String> to String,String)
272269 Map <String , String > headers = new HashMap <String , String >();
273270 for (Map .Entry <String , List <String >> entry : connection .getHeaderFields ().entrySet ()) {
274- headers .put (entry .getKey (), entry .getValue ().get (0 ));
271+ String key = entry .getKey ();
272+ headers .put (key , entry .getValue ().get (0 ));
275273 }
276274
275+ int code = httpConnection .getResponseCode ();
276+ if (request != null && request .getRequestHeaders ().get ("Range" ) != null ) {
277+ 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+ }
285+
286+ String [] parts = rangeString .split ("=" );
287+ String [] streamParts = parts [1 ].split ("-" );
288+ String fromRange = streamParts [0 ];
289+ int range = contentLength - 1 ;
290+
291+ headers .put ("Accept-Ranges" , "bytes" );
292+ //headers.put("Content-Length", headers.get("Content-Length"));
293+ 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+ }
318+
277319 // Bypass CORS
278320 headers .put ("Access-Control-Allow-Origin" , "*" );
279321 headers .put ("Access-Control-Allow-Methods" , "GET, POST, DELETE, PUT, OPTIONS" );
280322 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" );
281-
282- return new WebResourceResponse (connection .getContentType (), connection .getContentEncoding (), httpConnection .getResponseCode (), httpConnection .getResponseMessage (), headers , connection .getInputStream ());
323+ headers .put ("Content-Type" , request .getRequestHeaders ().get ("Content-Type" ));
324+
325+ return new WebResourceResponse (connection .getContentType (), connection .getContentEncoding (),
326+ code , httpConnection .getResponseMessage (), headers , responseStream );
327+
283328 } catch (Exception e ) {
284329 //an error occurred
285330 return null ;
0 commit comments