Skip to content

Commit

Permalink
feat(WebViewLocalServer.java): return 404 error code when a local fil…
Browse files Browse the repository at this point in the history
…e is not found (#217)

Currently, the Android version of the plugin always returns http code 200 no matter if the resource
you're trying to get exists or not. The iOS version does. This makes the two implementations
inconsistent. This tries to fix that and use the same expected behavior in both of them.

BREAKING CHANGE: Until now, the Android part of the plugin was returning a 200 http code even though
the requested file didn't exist. This behavior was inconsistent with the historical behavior of the
iOS webView. This change makes them both work in the same manner but introduces a breaking change
for the current Android users that are expecting a 200 http code no matter what and are testing the
not found error just by checking if the body is null.

fix #216
  • Loading branch information
robertohuertasm authored and jcesarmobile committed Dec 12, 2018
1 parent f0ca88f commit f7a551e
Showing 1 changed file with 9 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,15 @@ private static Uri parseAndVerifyUrl(String url) {

private static WebResourceResponse createWebResourceResponse(String mimeType, String encoding, int statusCode, String reasonPhrase, Map<String, String> responseHeaders, InputStream data) {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
return new WebResourceResponse(mimeType, encoding, statusCode, reasonPhrase, responseHeaders, data);
int finalStatusCode = statusCode;
try {
if (data.available() == 0) {
finalStatusCode = 404;
}
} catch (IOException e) {
finalStatusCode = 500;
}
return new WebResourceResponse(mimeType, encoding, finalStatusCode, reasonPhrase, responseHeaders, data);
} else {
return new WebResourceResponse(mimeType, encoding, data);
}
Expand Down

1 comment on commit f7a551e

@robcoekaerts
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello,

This check causes our application to stop working, due to the InputStream.available() method returning 0 in our case, even though the local file exists and there are bytes to be read. Per the Java documentation for InputStream.available(), the result is only an estimate of how many bytes can be read without blocking, not an exact amount:

Returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking by the next invocation of a method for this input stream. The next invocation might be the same thread or another thread. A single read or skip of this many bytes will not block, but may read or skip fewer bytes.

(https://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html#available(), note that the default implementation of available() in InputStream also returns 0)

Is it possible to implement this functionality without using available()?

Please sign in to comment.