| 24 | } |
| 25 | |
| 26 | public static ParsedURI parse(URI uri) { |
| 27 | String protocol = uri.getScheme(); |
| 28 | if (protocol == null || !protocol.matches("^https?|wss?$")) { |
| 29 | protocol = "https"; |
| 30 | } |
| 31 | |
| 32 | int port = uri.getPort(); |
| 33 | if (port == -1) { |
| 34 | if ("http".equals(protocol) || "ws".equals(protocol)) { |
| 35 | port = 80; |
| 36 | } else if ("https".equals(protocol) || "wss".equals(protocol)) { |
| 37 | port = 443; |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | String path = uri.getRawPath(); |
| 42 | if (path == null || path.length() == 0) { |
| 43 | path = "/"; |
| 44 | } |
| 45 | |
| 46 | String userInfo = uri.getRawUserInfo(); |
| 47 | String query = uri.getRawQuery(); |
| 48 | String fragment = uri.getRawFragment(); |
| 49 | String _host = uri.getHost(); |
| 50 | if (_host == null) { |
| 51 | // might happen on some of Samsung Devices such as S4. |
| 52 | _host = extractHostFromAuthorityPart(uri.getRawAuthority()); |
| 53 | } |
| 54 | URI completeUri = URI.create(protocol + "://" |
| 55 | + (userInfo != null ? userInfo + "@" : "") |
| 56 | + _host |
| 57 | + (port != -1 ? ":" + port : "") |
| 58 | + path |
| 59 | + (query != null ? "?" + query : "") |
| 60 | + (fragment != null ? "#" + fragment : "")); |
| 61 | String id = protocol + "://" + _host + ":" + port; |
| 62 | |
| 63 | return new ParsedURI(completeUri, id); |
| 64 | } |
| 65 | |
| 66 | |
| 67 | private static String extractHostFromAuthorityPart(String authority) |