fix: handle case where URI.getHost() returns null (#484)
It seems that URI.getHost() might return null on some Samsung devices. Related: https://stackoverflow.com/questions/39645789/android-websocket-connection-failed-galaxy-s4
This commit is contained in:
committed by
Damien Arrachequesne
parent
858907f9be
commit
567372ecfa
@@ -5,11 +5,16 @@ import java.net.URI;
|
|||||||
import java.net.URISyntaxException;
|
import java.net.URISyntaxException;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
|
||||||
public class Url {
|
public class Url {
|
||||||
|
|
||||||
private static Pattern PATTERN_HTTP = Pattern.compile("^http|ws$");
|
private static Pattern PATTERN_HTTP = Pattern.compile("^http|ws$");
|
||||||
private static Pattern PATTERN_HTTPS = Pattern.compile("^(http|ws)s$");
|
private static Pattern PATTERN_HTTPS = Pattern.compile("^(http|ws)s$");
|
||||||
|
/**
|
||||||
|
* Expected format: "[id:password@]host[:port]"
|
||||||
|
*/
|
||||||
|
private static Pattern PATTERN_AUTHORITY = Pattern.compile("^(.*@)?([^:]+)(:\\d+)?$");
|
||||||
|
|
||||||
private Url() {}
|
private Url() {}
|
||||||
|
|
||||||
@@ -40,10 +45,15 @@ public class Url {
|
|||||||
String userInfo = uri.getRawUserInfo();
|
String userInfo = uri.getRawUserInfo();
|
||||||
String query = uri.getRawQuery();
|
String query = uri.getRawQuery();
|
||||||
String fragment = uri.getRawFragment();
|
String fragment = uri.getRawFragment();
|
||||||
|
String _host = uri.getHost();
|
||||||
|
if (_host == null) {
|
||||||
|
// might happen on some of Samsung Devices such as S4.
|
||||||
|
_host = extractHostFromAuthorityPart(uri.getRawAuthority());
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
return new URL(protocol + "://"
|
return new URL(protocol + "://"
|
||||||
+ (userInfo != null ? userInfo + "@" : "")
|
+ (userInfo != null ? userInfo + "@" : "")
|
||||||
+ uri.getHost()
|
+ _host
|
||||||
+ (port != -1 ? ":" + port : "")
|
+ (port != -1 ? ":" + port : "")
|
||||||
+ path
|
+ path
|
||||||
+ (query != null ? "?" + query : "")
|
+ (query != null ? "?" + query : "")
|
||||||
@@ -70,4 +80,21 @@ public class Url {
|
|||||||
return protocol + "://" + url.getHost() + ":" + port;
|
return protocol + "://" + url.getHost() + ":" + port;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static String extractHostFromAuthorityPart(String authority)
|
||||||
|
{
|
||||||
|
if (authority == null) {
|
||||||
|
throw new RuntimeException("unable to parse the host from the authority");
|
||||||
|
}
|
||||||
|
|
||||||
|
Matcher matcher = PATTERN_AUTHORITY.matcher(authority);
|
||||||
|
|
||||||
|
// If the authority part does not match the expected format.
|
||||||
|
if (!matcher.matches()) {
|
||||||
|
throw new RuntimeException("unable to parse the host from the authority");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return the host part.
|
||||||
|
return matcher.group(2);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user