Skip to content

Commit

Permalink
Load IP based URIs instead of doing a google search (#2915)
Browse files Browse the repository at this point in the history
  • Loading branch information
MortimerGoro committed Mar 4, 2020
1 parent 143852c commit 7acc7a7
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -77,7 +78,7 @@ public void setComparator(Comparator comparator) {
mComparator = comparator;
}

public CompletableFuture<List<SuggestionItem>> getBookmarkSuggestions(@NonNull List<SuggestionItem> items) {
private CompletableFuture<List<SuggestionItem>> getBookmarkSuggestions(@NonNull List<SuggestionItem> items) {
CompletableFuture future = new CompletableFuture();
SessionStore.get().getBookmarkStore().searchBookmarks(mFilterText, 100).thenAcceptAsync((bookmarks) -> {
bookmarks.stream()
Expand Down Expand Up @@ -105,7 +106,7 @@ public CompletableFuture<List<SuggestionItem>> getBookmarkSuggestions(@NonNull L
return future;
}

public CompletableFuture<List<SuggestionItem>> getHistorySuggestions(@NonNull final List<SuggestionItem> items) {
private CompletableFuture<List<SuggestionItem>> getHistorySuggestions(@NonNull final List<SuggestionItem> items) {
CompletableFuture future = new CompletableFuture();
SessionStore.get().getHistoryStore().getSuggestions(mFilterText, 100).thenAcceptAsync((history) -> {
history.forEach(h -> items.add(SuggestionItem.create(
Expand All @@ -130,7 +131,7 @@ public CompletableFuture<List<SuggestionItem>> getHistorySuggestions(@NonNull fi
return future;
}

public CompletableFuture<List<SuggestionItem>> getSearchEngineSuggestions(@NonNull final List<SuggestionItem> items) {
private CompletableFuture<List<SuggestionItem>> getSearchEngineSuggestions(@NonNull final List<SuggestionItem> items) {
CompletableFuture future = new CompletableFuture();

// Completion from browser-domains
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
21 changes: 20 additions & 1 deletion app/src/common/shared/org/mozilla/vrbrowser/utils/UrlUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import android.content.Context;
import android.util.Base64;
import android.util.Patterns;
import android.webkit.URLUtil;

import androidx.annotation.NonNull;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 7acc7a7

Please sign in to comment.