From c8e07b97a98ecc7ad96954de84b7aace3e226ed1 Mon Sep 17 00:00:00 2001 From: Imanol Fernandez Date: Wed, 4 Mar 2020 12:25:14 +0100 Subject: [PATCH] Load IP based URIs instead of doing a google search --- .../suggestions/SuggestionsProvider.java | 9 ++++---- .../vrbrowser/ui/views/NavigationURLBar.java | 4 ++++ .../org/mozilla/vrbrowser/utils/UrlUtils.java | 21 ++++++++++++++++++- 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/app/src/common/shared/org/mozilla/vrbrowser/search/suggestions/SuggestionsProvider.java b/app/src/common/shared/org/mozilla/vrbrowser/search/suggestions/SuggestionsProvider.java index 7f87fb8e0..7adf9ea58 100644 --- a/app/src/common/shared/org/mozilla/vrbrowser/search/suggestions/SuggestionsProvider.java +++ b/app/src/common/shared/org/mozilla/vrbrowser/search/suggestions/SuggestionsProvider.java @@ -61,7 +61,8 @@ public SuggestionsProvider(Context context) { private String getSearchURLOrDomain(String text) { if (UrlUtils.isDomain(text)) { return text; - + } else if (UrlUtils.isIPUri(text)) { + return text; } else { return mSearchEngineWrapper.getSearchURL(text); } @@ -77,7 +78,7 @@ public void setComparator(Comparator comparator) { mComparator = comparator; } - public CompletableFuture> getBookmarkSuggestions(@NonNull List items) { + private CompletableFuture> getBookmarkSuggestions(@NonNull List items) { CompletableFuture future = new CompletableFuture(); SessionStore.get().getBookmarkStore().searchBookmarks(mFilterText, 100).thenAcceptAsync((bookmarks) -> { bookmarks.stream() @@ -105,7 +106,7 @@ public CompletableFuture> getBookmarkSuggestions(@NonNull L return future; } - public CompletableFuture> getHistorySuggestions(@NonNull final List items) { + private CompletableFuture> getHistorySuggestions(@NonNull final List items) { CompletableFuture future = new CompletableFuture(); SessionStore.get().getHistoryStore().getSuggestions(mFilterText, 100).thenAcceptAsync((history) -> { history.forEach(h -> items.add(SuggestionItem.create( @@ -130,7 +131,7 @@ public CompletableFuture> getHistorySuggestions(@NonNull fi return future; } - public CompletableFuture> getSearchEngineSuggestions(@NonNull final List items) { + private CompletableFuture> getSearchEngineSuggestions(@NonNull final List items) { CompletableFuture future = new CompletableFuture(); // Completion from browser-domains diff --git a/app/src/common/shared/org/mozilla/vrbrowser/ui/views/NavigationURLBar.java b/app/src/common/shared/org/mozilla/vrbrowser/ui/views/NavigationURLBar.java index bd087024a..d47ce9a0d 100644 --- a/app/src/common/shared/org/mozilla/vrbrowser/ui/views/NavigationURLBar.java +++ b/app/src/common/shared/org/mozilla/vrbrowser/ui/views/NavigationURLBar.java @@ -340,6 +340,10 @@ public void handleURLEdit(String text) { if (!hasProtocol && !urlText.contains(" ") && UrlUtils.isDomain(urlText)) { urlText = "https://" + urlText; hasProtocol = true; + } else if (!hasProtocol && !urlText.contains(" ") && UrlUtils.isIPUri(urlText)) { + String protocol = UrlUtils.isLocalIP(urlText) ? "http://" : "https://"; + urlText = protocol + urlText; + hasProtocol = true; } if (hasProtocol) { URL url = new URL(urlText); diff --git a/app/src/common/shared/org/mozilla/vrbrowser/utils/UrlUtils.java b/app/src/common/shared/org/mozilla/vrbrowser/utils/UrlUtils.java index 00109d4e1..393ce8401 100644 --- a/app/src/common/shared/org/mozilla/vrbrowser/utils/UrlUtils.java +++ b/app/src/common/shared/org/mozilla/vrbrowser/utils/UrlUtils.java @@ -7,6 +7,7 @@ import android.content.Context; import android.util.Base64; +import android.util.Patterns; import android.webkit.URLUtil; import androidx.annotation.NonNull; @@ -68,11 +69,29 @@ public static String stripProtocol(@Nullable String host) { return result; } - private static Pattern domainPattern = Pattern.compile("^(http:\\/\\/www\\.|https:\\/\\/www\\.|http:\\/\\/|https:\\/\\/)?[a-zA-Z0-9]+([\\-\\.]{1}[a-zA-Z0-9]+)*\\.[a-zA-Z]{2,5}(:[0-9]{1,5})?(\\/.*)?$"); + private static Pattern domainPattern = Pattern.compile("^(http:\\/\\/www\\.|https:\\/\\/www\\.|http:\\/\\/|https:\\/\\/)?[a-zA-Z0-9]+([\\-\\.]{1}[a-zA-Z0-9]+)*\\.[a-zA-Z]{2,5}(:[0-9]{1,5})?(\\/[^ ]*)?$"); public static boolean isDomain(String text) { return domainPattern.matcher(text).find(); } + private static Pattern ipPattern = Pattern.compile("^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(:[0-9]+)?(/[^ ]*)?"); + private static Pattern localhostPattern = Pattern.compile("^(localhost)(:[0-9]+)?(/[^ ]*)?", Pattern.CASE_INSENSITIVE); + public static boolean isIPUri(@NonNull String aUri) { + String uri = stripProtocol(aUri).trim(); + return localhostPattern.matcher(uri).find() || ipPattern.matcher(uri).find(); + } + + public static boolean isLocalIP(@NonNull String aUri) { + if (!isIPUri(aUri)) { + return false; + } + String uri = stripProtocol(aUri).trim(); + return uri.startsWith("10.") || + uri.startsWith("172.") || + uri.startsWith("192.168.") || // + localhostPattern.matcher(uri).find(); + } + public static boolean isPrivateAboutPage(@NonNull Context context, @NonNull String uri) { InternalPages.PageResources pageResources = InternalPages.PageResources.create(R.raw.private_mode, R.raw.private_style); byte[] privatePageBytes = InternalPages.createAboutPage(context, pageResources);