Skip to content

Update Uri.java #484

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion src/main/java/io/socket/client/Url.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import java.net.URISyntaxException;
import java.net.URL;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Url {

Expand Down Expand Up @@ -40,10 +42,16 @@ public static URL parse(URI uri) {
String userInfo = uri.getRawUserInfo();
String query = uri.getRawQuery();
String fragment = uri.getRawFragment();
String _host;
// this is because of unsupported uri.getHost() on some of Samsung Devices such as S4.
_host = uri.getHost();
if (_host == null) {
_host = extractHostFromAuthorityPart(uri.getRawAuthority());
}
try {
return new URL(protocol + "://"
+ (userInfo != null ? userInfo + "@" : "")
+ uri.getHost()
+ _host
+ (port != -1 ? ":" + port : "")
+ path
+ (query != null ? "?" + query : "")
Expand All @@ -69,5 +77,28 @@ public static String extractId(URL url) {
}
return protocol + "://" + url.getHost() + ":" + port;
}

static String extractHostFromAuthorityPart(String authority)
{
// If the authority part is not available.
if (authority == null)
{
// Hmm... This should not happen.
return null;
}

// Parse the authority part. The expected format is "[id:password@]host[:port]".
Matcher matcher = Pattern.compile("^(.*@)?([^:]+)(:\\d+)?$").matcher(authority);

// If the authority part does not match the expected format.
if (matcher == null || matcher.matches() == false)
{
// Hmm... This should not happen.
return null;
}

// Return the host part.
return matcher.group(2);
}

}