|
|
@ -33,44 +33,18 @@ import acr.browser.lightning.constant.StartPage; |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public class UrlUtils { |
|
|
|
public class UrlUtils { |
|
|
|
private static final Pattern ACCEPTED_URI_SCHEMA = Pattern.compile( |
|
|
|
private static final Pattern ACCEPTED_URI_SCHEMA = Pattern.compile( |
|
|
|
"(?i)" + // switch on case insensitive matching
|
|
|
|
"(?i)" + // switch on case insensitive matching
|
|
|
|
'(' + // begin group for schema
|
|
|
|
'(' + // begin group for schema
|
|
|
|
"(?:http|https|file)://" + |
|
|
|
"(?:http|https|file)://" + |
|
|
|
"|(?:inline|data|about|javascript):" + |
|
|
|
"|(?:inline|data|about|javascript):" + |
|
|
|
"|(?:.*:.*@)" + |
|
|
|
"|(?:.*:.*@)" + |
|
|
|
')' + |
|
|
|
')' + |
|
|
|
"(.*)"); |
|
|
|
"(.*)"); |
|
|
|
// Google search
|
|
|
|
// Google search
|
|
|
|
public final static String QUERY_PLACE_HOLDER = "%s"; |
|
|
|
public final static String QUERY_PLACE_HOLDER = "%s"; |
|
|
|
// Regular expression to strip http:// and optionally
|
|
|
|
|
|
|
|
// the trailing slash
|
|
|
|
|
|
|
|
private static final Pattern STRIP_URL_PATTERN = |
|
|
|
|
|
|
|
Pattern.compile("^http://(.*?)/?$"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private UrlUtils() { /* cannot be instantiated */ } |
|
|
|
private UrlUtils() { /* cannot be instantiated */ } |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Strips the provided url of preceding "http://" and any trailing "/". Does not |
|
|
|
|
|
|
|
* strip "https://". If the provided string cannot be stripped, the original string |
|
|
|
|
|
|
|
* is returned. |
|
|
|
|
|
|
|
* <p/> |
|
|
|
|
|
|
|
* TODO: Put this in TextUtils to be used by other packages doing something similar. |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @param url a url to strip, like "http://www.google.com/" |
|
|
|
|
|
|
|
* @return a stripped url like "www.google.com", or the original string if it could |
|
|
|
|
|
|
|
* not be stripped |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
@Nullable |
|
|
|
|
|
|
|
public static String stripUrl(@Nullable String url) { |
|
|
|
|
|
|
|
if (url == null) return null; |
|
|
|
|
|
|
|
Matcher m = STRIP_URL_PATTERN.matcher(url); |
|
|
|
|
|
|
|
if (m.matches()) { |
|
|
|
|
|
|
|
return m.group(1); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
return url; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Attempts to determine whether user input is a URL or search |
|
|
|
* Attempts to determine whether user input is a URL or search |
|
|
|
* terms. Anything with a space is passed to search if canBeSearch is true. |
|
|
|
* terms. Anything with a space is passed to search if canBeSearch is true. |
|
|
@ -106,65 +80,19 @@ public class UrlUtils { |
|
|
|
} |
|
|
|
} |
|
|
|
if (canBeSearch) { |
|
|
|
if (canBeSearch) { |
|
|
|
return URLUtil.composeSearchUrl(inUrl, |
|
|
|
return URLUtil.composeSearchUrl(inUrl, |
|
|
|
searchUrl, QUERY_PLACE_HOLDER); |
|
|
|
searchUrl, QUERY_PLACE_HOLDER); |
|
|
|
} |
|
|
|
} |
|
|
|
return ""; |
|
|
|
return ""; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/* package */ |
|
|
|
|
|
|
|
@NonNull |
|
|
|
|
|
|
|
static String fixUrl(@NonNull String inUrl) { |
|
|
|
|
|
|
|
// FIXME: Converting the url to lower case
|
|
|
|
|
|
|
|
// duplicates functionality in smartUrlFilter().
|
|
|
|
|
|
|
|
// However, changing all current callers of fixUrl to
|
|
|
|
|
|
|
|
// call smartUrlFilter in addition may have unwanted
|
|
|
|
|
|
|
|
// consequences, and is deferred for now.
|
|
|
|
|
|
|
|
int colon = inUrl.indexOf(':'); |
|
|
|
|
|
|
|
boolean allLower = true; |
|
|
|
|
|
|
|
for (int index = 0; index < colon; index++) { |
|
|
|
|
|
|
|
char ch = inUrl.charAt(index); |
|
|
|
|
|
|
|
if (!Character.isLetter(ch)) { |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
allLower &= Character.isLowerCase(ch); |
|
|
|
|
|
|
|
if (index == colon - 1 && !allLower) { |
|
|
|
|
|
|
|
inUrl = inUrl.substring(0, colon).toLowerCase() |
|
|
|
|
|
|
|
+ inUrl.substring(colon); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (inUrl.startsWith("http://") || inUrl.startsWith("https://")) |
|
|
|
|
|
|
|
return inUrl; |
|
|
|
|
|
|
|
if (inUrl.startsWith("http:") || |
|
|
|
|
|
|
|
inUrl.startsWith("https:")) { |
|
|
|
|
|
|
|
if (inUrl.startsWith("http:/") || inUrl.startsWith("https:/")) { |
|
|
|
|
|
|
|
inUrl = inUrl.replaceFirst("/", "//"); |
|
|
|
|
|
|
|
} else inUrl = inUrl.replaceFirst(":", "://"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return inUrl; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Returns the filtered URL. Cannot return null, but can return an empty string
|
|
|
|
|
|
|
|
/* package */ |
|
|
|
|
|
|
|
@Nullable |
|
|
|
|
|
|
|
static String filteredUrl(@Nullable String inUrl) { |
|
|
|
|
|
|
|
if (inUrl == null) { |
|
|
|
|
|
|
|
return ""; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (inUrl.startsWith("content:") |
|
|
|
|
|
|
|
|| inUrl.startsWith("browser:")) { |
|
|
|
|
|
|
|
return ""; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return inUrl; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Returns whether the given url is the bookmarks/history page or a normal website |
|
|
|
* Returns whether the given url is the bookmarks/history page or a normal website |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static boolean isSpecialUrl(@Nullable String url) { |
|
|
|
public static boolean isSpecialUrl(@Nullable String url) { |
|
|
|
return url != null && url.startsWith(Constants.FILE) && |
|
|
|
return url != null && url.startsWith(Constants.FILE) && |
|
|
|
(url.endsWith(BookmarkPage.FILENAME) || |
|
|
|
(url.endsWith(BookmarkPage.FILENAME) || |
|
|
|
url.endsWith(HistoryPage.FILENAME) || |
|
|
|
url.endsWith(HistoryPage.FILENAME) || |
|
|
|
url.endsWith(StartPage.FILENAME)); |
|
|
|
url.endsWith(StartPage.FILENAME)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|