From: Soren Stoutner Date: Fri, 24 Mar 2017 00:09:56 +0000 (-0700) Subject: Correctly encode URLs in `loadUrlFromTextBox()`. Fixes https://redmine.stoutner... X-Git-Tag: v2.1~5 X-Git-Url: https://gitweb.stoutner.com/?p=PrivacyBrowserAndroid.git;a=commitdiff_plain;h=42bb1bc6642f58b63c920d31e32958b7b2154956 Correctly encode URLs in `loadUrlFromTextBox()`. Fixes https://redmine.stoutner.com/issues/92. --- diff --git a/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.java b/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.java index 1ec9e746..5525439d 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.java +++ b/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.java @@ -107,6 +107,7 @@ import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.net.MalformedURLException; import java.net.URL; +import java.net.URLDecoder; import java.net.URLEncoder; import java.util.HashMap; import java.util.HashSet; @@ -1786,9 +1787,6 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation // Get the text from urlTextBox and convert it to a string. trim() removes white spaces from the beginning and end of the string. String unformattedUrlString = urlTextBox.getText().toString().trim(); - URL unformattedUrl = null; - Uri.Builder formattedUri = new Uri.Builder(); - // Check to see if unformattedUrlString is a valid URL. Otherwise, convert it into a Duck Duck Go search. if (Patterns.WEB_URL.matcher(unformattedUrlString).matches()) { // Add http:// at the beginning if it is missing. Otherwise the app will segfault. @@ -1796,6 +1794,9 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation unformattedUrlString = "http://" + unformattedUrlString; } + // Initialize `unformattedUrl`. + URL unformattedUrl = null; + // Convert unformattedUrlString to a URL, then to a URI, and then back to a string, which sanitizes the input and adds in any missing components. try { unformattedUrl = new URL(unformattedUrlString); @@ -1803,17 +1804,21 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation e.printStackTrace(); } - // The ternary operator (? :) makes sure that a null pointer exception is not thrown, which would happen if .get was called on a null value. + // The ternary operator (? :) makes sure that a null pointer exception is not thrown, which would happen if `.get` was called on a `null` value. final String scheme = unformattedUrl != null ? unformattedUrl.getProtocol() : null; final String authority = unformattedUrl != null ? unformattedUrl.getAuthority() : null; final String path = unformattedUrl != null ? unformattedUrl.getPath() : null; final String query = unformattedUrl != null ? unformattedUrl.getQuery() : null; final String fragment = unformattedUrl != null ? unformattedUrl.getRef() : null; + // Build the URI. + Uri.Builder formattedUri = new Uri.Builder(); formattedUri.scheme(scheme).authority(authority).path(path).query(query).fragment(fragment); - formattedUrlString = formattedUri.build().toString(); + + // Decode `formattedUri` as a `String` in `UTF-8`. + formattedUrlString = URLDecoder.decode(formattedUri.build().toString(), "UTF-8"); } else { - // Sanitize the search input and convert it to a DuckDuckGo search. + // Sanitize the search input and convert it to a search. final String encodedUrlString = URLEncoder.encode(unformattedUrlString, "UTF-8"); // Use the correct search URL.