Left: | ||
Right: |
LEFT | RIGHT |
---|---|
1 /* | 1 /* |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
3 * Copyright (C) 2006-present eyeo GmbH | 3 * Copyright (C) 2006-present eyeo GmbH |
4 * | 4 * |
5 * Adblock Plus is free software: you can redistribute it and/or modify | 5 * Adblock Plus is free software: you can redistribute it and/or modify |
6 * it under the terms of the GNU General Public License version 3 as | 6 * it under the terms of the GNU General Public License version 3 as |
7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
8 * | 8 * |
9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
13 * | 13 * |
14 * You should have received a copy of the GNU General Public License | 14 * You should have received a copy of the GNU General Public License |
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
16 */ | 16 */ |
17 | 17 |
18 package org.adblockplus.browser; | 18 package org.adblockplus.browser; |
19 | 19 |
20 import java.net.MalformedURLException; | 20 import java.net.MalformedURLException; |
21 import java.net.URL; | 21 import java.net.URL; |
22 | 22 |
23 public class UrlUtils | 23 public class UrlUtils |
24 { | 24 { |
25 | 25 |
anton
2017/09/26 06:24:07
the line seems to be not required
diegocarloslima
2017/09/26 12:24:44
Acknowledged.
| |
26 private static final String SCHEME_HTTP = "http"; | 26 private static final String SCHEME_HTTP = "http"; |
27 private static final String SCHEME_SEPARATOR = "://"; | 27 private static final String SCHEME_SEPARATOR = "://"; |
28 private static final String WWW_HOST_PART = "www."; | 28 private static final String WWW_HOST_PART = "www."; |
29 | 29 |
30 public static String formatUrl(String urlStr) | 30 public static String formatUrl(String urlStr) |
31 { | 31 { |
32 if (!urlStr.contains(SCHEME_SEPARATOR)) | 32 try |
33 { | 33 { |
34 urlStr = SCHEME_HTTP + SCHEME_SEPARATOR + urlStr; | 34 URL url = new URL(urlStr); |
35 return url.toExternalForm(); | |
35 } | 36 } |
36 return urlStr; | 37 catch (MalformedURLException e) |
38 { | |
39 try | |
40 { | |
41 // The URL might be malformed due to a missing protocol. If so, we try t o recreate the URL | |
42 // by adding the HTTP protocol | |
43 URL url = new URL(SCHEME_HTTP + SCHEME_SEPARATOR + urlStr); | |
44 return url.toExternalForm(); | |
45 } | |
46 catch (MalformedURLException e2) | |
47 { | |
48 } | |
49 } | |
50 return null; | |
37 } | 51 } |
38 | 52 |
39 public static String getHostFromUrl(String urlStr) | 53 public static String getHostFromUrl(String urlStr) |
40 { | 54 { |
41 try | 55 try |
42 { | 56 { |
43 final URL url = new URL(formatUrl(urlStr)); | 57 final URL url = new URL(formatUrl(urlStr)); |
44 String host = url.getHost(); | 58 String host = url.getHost(); |
45 if (host != null) | 59 if (host != null) |
46 { | 60 { |
61 // The www. part of the host is not relevant for us, so we remove it | |
47 if (host.startsWith(WWW_HOST_PART)) | 62 if (host.startsWith(WWW_HOST_PART)) |
48 { | 63 { |
49 host = host.substring(WWW_HOST_PART.length()); | 64 host = host.substring(WWW_HOST_PART.length()); |
50 } | 65 } |
51 return host; | 66 return host; |
52 } | 67 } |
53 } | 68 } |
54 catch (MalformedURLException e) | 69 catch (MalformedURLException e) |
55 { | 70 { |
56 } | 71 } |
57 return null; | 72 return null; |
58 } | 73 } |
59 | 74 |
60 public static boolean isSchemeHttpOrHttps(String urlStr) | 75 public static boolean isSchemeHttpOrHttps(String urlStr) |
61 { | 76 { |
62 try | 77 try |
63 { | 78 { |
64 return new URL(urlStr).getProtocol().contains(SCHEME_HTTP); | 79 return new URL(urlStr).getProtocol().contains(SCHEME_HTTP); |
65 } | 80 } |
66 catch (MalformedURLException e) | 81 catch (MalformedURLException e) |
67 { | 82 { |
68 } | 83 } |
69 return false; | 84 return false; |
70 } | 85 } |
71 } | 86 } |
LEFT | RIGHT |