Left: | ||
Right: |
OLD | NEW |
---|---|
(Empty) | |
1 /* | |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | |
3 * Copyright (C) 2006-present eyeo GmbH | |
4 * | |
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 | |
7 * published by the Free Software Foundation. | |
8 * | |
9 * Adblock Plus is distributed in the hope that it will be useful, | |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 * GNU General Public License for more details. | |
13 * | |
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/>. | |
16 */ | |
17 | |
18 package org.adblockplus.browser; | |
19 | |
20 import java.net.MalformedURLException; | |
21 import java.net.URL; | |
22 | |
23 public class UrlUtils | |
24 { | |
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"; | |
27 private static final String SCHEME_SEPARATOR = "://"; | |
28 private static final String WWW_HOST_PART = "www."; | |
29 | |
30 public static String formatUrl(String urlStr) | |
31 { | |
32 try | |
33 { | |
34 URL url = new URL(urlStr); | |
35 return url.toExternalForm(); | |
36 } | |
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; | |
51 } | |
52 | |
53 public static String getHostFromUrl(String urlStr) | |
54 { | |
55 try | |
56 { | |
57 final URL url = new URL(formatUrl(urlStr)); | |
58 String host = url.getHost(); | |
59 if (host != null) | |
60 { | |
61 // The www. part of the host is not relevant for us, so we remove it | |
62 if (host.startsWith(WWW_HOST_PART)) | |
63 { | |
64 host = host.substring(WWW_HOST_PART.length()); | |
65 } | |
66 return host; | |
67 } | |
68 } | |
69 catch (MalformedURLException e) | |
70 { | |
71 } | |
72 return null; | |
73 } | |
74 | |
75 public static boolean isSchemeHttpOrHttps(String urlStr) | |
76 { | |
77 try | |
78 { | |
79 return new URL(urlStr).getProtocol().contains(SCHEME_HTTP); | |
80 } | |
81 catch (MalformedURLException e) | |
82 { | |
83 } | |
84 return false; | |
85 } | |
86 } | |
OLD | NEW |