Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: mobile/android/thirdparty/org/adblockplus/browser/UrlUtils.java

Issue 29543774: Issue 2801 - Create 'Whitelisted websites' screen and add link to 'Ad blocking' screen (Closed)
Patch Set: Adjustments accordingly to Thomas's comments. Also, adjusting strings for multilocale build Created Sept. 19, 2017, 3:18 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: mobile/android/thirdparty/org/adblockplus/browser/UrlUtils.java
===================================================================
new file mode 100644
--- /dev/null
+++ b/mobile/android/thirdparty/org/adblockplus/browser/UrlUtils.java
@@ -0,0 +1,86 @@
+/*
+ * This file is part of Adblock Plus <https://adblockplus.org/>,
+ * Copyright (C) 2006-present eyeo GmbH
+ *
+ * Adblock Plus is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 3 as
+ * published by the Free Software Foundation.
+ *
+ * Adblock Plus is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package org.adblockplus.browser;
+
+import java.net.MalformedURLException;
+import java.net.URL;
+
+public class UrlUtils
+{
+
anton 2017/09/26 06:24:07 the line seems to be not required
diegocarloslima 2017/09/26 12:24:44 Acknowledged.
+ private static final String SCHEME_HTTP = "http";
+ private static final String SCHEME_SEPARATOR = "://";
+ private static final String WWW_HOST_PART = "www.";
+
+ public static String formatUrl(String urlStr)
+ {
+ try
+ {
+ URL url = new URL(urlStr);
+ return url.toExternalForm();
+ }
+ catch (MalformedURLException e)
+ {
+ try
+ {
+ // The URL might be malformed due to a missing protocol. If so, we try to recreate the URL
+ // by adding the HTTP protocol
+ URL url = new URL(SCHEME_HTTP + SCHEME_SEPARATOR + urlStr);
+ return url.toExternalForm();
+ }
+ catch (MalformedURLException e2)
+ {
+ }
+ }
+ return null;
+ }
+
+ public static String getHostFromUrl(String urlStr)
+ {
+ try
+ {
+ final URL url = new URL(formatUrl(urlStr));
+ String host = url.getHost();
+ if (host != null)
+ {
+ // The www. part of the host is not relevant for us, so we remove it
+ if (host.startsWith(WWW_HOST_PART))
+ {
+ host = host.substring(WWW_HOST_PART.length());
+ }
+ return host;
+ }
+ }
+ catch (MalformedURLException e)
+ {
+ }
+ return null;
+ }
+
+ public static boolean isSchemeHttpOrHttps(String urlStr)
+ {
+ try
+ {
+ return new URL(urlStr).getProtocol().contains(SCHEME_HTTP);
+ }
+ catch (MalformedURLException e)
+ {
+ }
+ return false;
+ }
+}

Powered by Google App Engine
This is Rietveld