From f7a551e59013bb3ee50c7ed9f490368a639bf361 Mon Sep 17 00:00:00 2001 From: Roberto Huertas Date: Wed, 12 Dec 2018 13:21:18 +0100 Subject: [PATCH] feat(WebViewLocalServer.java): return 404 error code when a local file 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 --- .../cordova/webview/WebViewLocalServer.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/android/com/ionicframework/cordova/webview/WebViewLocalServer.java b/src/android/com/ionicframework/cordova/webview/WebViewLocalServer.java index b2b81295..69b71cc4 100644 --- a/src/android/com/ionicframework/cordova/webview/WebViewLocalServer.java +++ b/src/android/com/ionicframework/cordova/webview/WebViewLocalServer.java @@ -202,7 +202,15 @@ private static Uri parseAndVerifyUrl(String url) { private static WebResourceResponse createWebResourceResponse(String mimeType, String encoding, int statusCode, String reasonPhrase, Map 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); }