Skip to content

Commit

Permalink
fix(Android): Handle Range Requests for proper media file handling (#298
Browse files Browse the repository at this point in the history
)

Properly handle requests with Range header and return appropriate headers so audio and video tags
work correcly and you can go forward or backward ond them

fix #248, fix #205, fix #141
  • Loading branch information
jcesarmobile authored Feb 4, 2019
1 parent 6668669 commit 6f18248
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,13 @@ public ServerClient(SystemWebViewEngine parentEngine, ConfigXmlParser parser) {
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
return localServer.shouldInterceptRequest(request.getUrl());
return localServer.shouldInterceptRequest(request.getUrl(), request);
}

@TargetApi(Build.VERSION_CODES.KITKAT)
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
return localServer.shouldInterceptRequest(Uri.parse(url));
return localServer.shouldInterceptRequest(Uri.parse(url), null);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import android.net.Uri;
import android.os.Build;
import android.util.Log;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;

import org.apache.cordova.ConfigXmlParser;
Expand Down Expand Up @@ -213,7 +214,7 @@ private static WebResourceResponse createWebResourceResponse(String mimeType, St
* @param uri the request Uri to process.
* @return a response if the request URL had a matching handler, null if no handler was found.
*/
public WebResourceResponse shouldInterceptRequest(Uri uri) {
public WebResourceResponse shouldInterceptRequest(Uri uri, WebResourceRequest request) {
PathHandler handler;
synchronized (uriMatcher) {
handler = (PathHandler) uriMatcher.match(uri);
Expand All @@ -224,7 +225,7 @@ public WebResourceResponse shouldInterceptRequest(Uri uri) {

if (isLocalFile(uri) || uri.getAuthority().equals(this.authority)) {
Log.d("SERVER", "Handling local request: " + uri.toString());
return handleLocalRequest(uri, handler);
return handleLocalRequest(uri, handler, request);
} else {
return handleProxyRequest(uri, handler);
}
Expand All @@ -237,9 +238,33 @@ private boolean isLocalFile(Uri uri) {
}
return false;
}
private WebResourceResponse handleLocalRequest(Uri uri, PathHandler handler) {
String path = uri.getPath();


private WebResourceResponse handleLocalRequest(Uri uri, PathHandler handler, WebResourceRequest request) {
String path = uri.getPath();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && request != null && request.getRequestHeaders().get("Range") != null) {
InputStream responseStream = new LollipopLazyInputStream(handler, uri);
String mimeType = getMimeType(path, responseStream);
Map<String, String> tempResponseHeaders = handler.getResponseHeaders();
int statusCode = 206;
try {
int totalRange = responseStream.available();
String rangeString = request.getRequestHeaders().get("Range");
String[] parts = rangeString.split("=");
String[] streamParts = parts[1].split("-");
String fromRange = streamParts[0];
int range = totalRange-1;
if (streamParts.length > 1) {
range = Integer.parseInt(streamParts[1]);
}
tempResponseHeaders.put("Accept-Ranges", "bytes");
tempResponseHeaders.put("Content-Range", "bytes " + fromRange + "-" + range + "/" + totalRange);
} catch (IOException e) {
statusCode = 404;
}
return createWebResourceResponse(mimeType, handler.getEncoding(),
statusCode, handler.getReasonPhrase(), tempResponseHeaders, responseStream);
}
if (isLocalFile(uri)) {
InputStream responseStream = new LollipopLazyInputStream(handler, uri);
String mimeType = getMimeType(path, responseStream);
Expand Down

0 comments on commit 6f18248

Please sign in to comment.