diff --git a/src/main/java/io/socket/client/IO.java b/src/main/java/io/socket/client/IO.java index a5455f48..69637ec5 100644 --- a/src/main/java/io/socket/client/IO.java +++ b/src/main/java/io/socket/client/IO.java @@ -7,7 +7,6 @@ import java.net.URI; import java.net.URISyntaxException; -import java.net.URL; import java.util.concurrent.ConcurrentHashMap; import java.util.logging.Level; import java.util.logging.Logger; @@ -58,17 +57,12 @@ public static Socket socket(URI uri, Options opts) { opts = new Options(); } - URL parsed = Url.parse(uri); - URI source; - try { - source = parsed.toURI(); - } catch (URISyntaxException e) { - throw new RuntimeException(e); - } - String id = Url.extractId(parsed); - String path = parsed.getPath(); + Url.ParsedURI parsed = Url.parse(uri); + URI source = parsed.uri; + String id = parsed.id; + boolean sameNamespace = managers.containsKey(id) - && managers.get(id).nsps.containsKey(path); + && managers.get(id).nsps.containsKey(source.getPath()); boolean newConnection = opts.forceNew || !opts.multiplex || sameNamespace; Manager io; @@ -87,12 +81,12 @@ public static Socket socket(URI uri, Options opts) { io = managers.get(id); } - String query = parsed.getQuery(); + String query = source.getQuery(); if (query != null && (opts.query == null || opts.query.isEmpty())) { opts.query = query; } - return io.socket(parsed.getPath(), opts); + return io.socket(source.getPath(), opts); } diff --git a/src/main/java/io/socket/client/Url.java b/src/main/java/io/socket/client/Url.java index c9185d29..451eee8b 100644 --- a/src/main/java/io/socket/client/Url.java +++ b/src/main/java/io/socket/client/Url.java @@ -1,16 +1,11 @@ package io.socket.client; -import java.net.MalformedURLException; import java.net.URI; -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 { - private static Pattern PATTERN_HTTP = Pattern.compile("^http|ws$"); - private static Pattern PATTERN_HTTPS = Pattern.compile("^(http|ws)s$"); /** * Expected format: "[id:password@]host[:port]" */ @@ -18,11 +13,17 @@ public class Url { private Url() {} - public static URL parse(String uri) throws URISyntaxException { - return parse(new URI(uri)); + static class ParsedURI { + public final URI uri; + public final String id; + + public ParsedURI(URI uri, String id) { + this.uri = uri; + this.id = id; + } } - public static URL parse(URI uri) { + public static ParsedURI parse(URI uri) { String protocol = uri.getScheme(); if (protocol == null || !protocol.matches("^https?|wss?$")) { protocol = "https"; @@ -30,9 +31,9 @@ public static URL parse(URI uri) { int port = uri.getPort(); if (port == -1) { - if (PATTERN_HTTP.matcher(protocol).matches()) { + if ("http".equals(protocol) || "ws".equals(protocol)) { port = 80; - } else if (PATTERN_HTTPS.matcher(protocol).matches()) { + } else if ("https".equals(protocol) || "wss".equals(protocol)) { port = 443; } } @@ -50,35 +51,18 @@ public static URL parse(URI uri) { // might happen on some of Samsung Devices such as S4. _host = extractHostFromAuthorityPart(uri.getRawAuthority()); } - try { - return new URL(protocol + "://" - + (userInfo != null ? userInfo + "@" : "") - + _host - + (port != -1 ? ":" + port : "") - + path - + (query != null ? "?" + query : "") - + (fragment != null ? "#" + fragment : "")); - } catch (MalformedURLException e) { - throw new RuntimeException(e); - } - } - - public static String extractId(String url) throws MalformedURLException { - return extractId(new URL(url)); + URI completeUri = URI.create(protocol + "://" + + (userInfo != null ? userInfo + "@" : "") + + _host + + (port != -1 ? ":" + port : "") + + path + + (query != null ? "?" + query : "") + + (fragment != null ? "#" + fragment : "")); + String id = protocol + "://" + _host + ":" + port; + + return new ParsedURI(completeUri, id); } - public static String extractId(URL url) { - String protocol = url.getProtocol(); - int port = url.getPort(); - if (port == -1) { - if (PATTERN_HTTP.matcher(protocol).matches()) { - port = 80; - } else if (PATTERN_HTTPS.matcher(protocol).matches()) { - port = 443; - } - } - return protocol + "://" + url.getHost() + ":" + port; - } private static String extractHostFromAuthorityPart(String authority) { diff --git a/src/test/java/io/socket/client/UrlTest.java b/src/test/java/io/socket/client/UrlTest.java index fbcf42de..47a1a0c1 100644 --- a/src/test/java/io/socket/client/UrlTest.java +++ b/src/test/java/io/socket/client/UrlTest.java @@ -4,9 +4,7 @@ import org.junit.runner.RunWith; import org.junit.runners.JUnit4; -import java.net.MalformedURLException; -import java.net.URISyntaxException; -import java.net.URL; +import java.net.URI; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.not; @@ -15,58 +13,85 @@ @RunWith(JUnit4.class) public class UrlTest { + private URI parse(String uri) { + return Url.parse(URI.create(uri)).uri; + } + + private String extractId(String uri) { + return Url.parse(URI.create(uri)).id; + } + @Test - public void parse() throws URISyntaxException { - assertThat(Url.parse("http://username:password@host:8080/directory/file?query#ref").toString(), + public void parse() { + assertThat(parse("http://username:password@host:8080/directory/file?query#ref").toString(), is("http://username:password@host:8080/directory/file?query#ref")); } @Test - public void parseRelativePath() throws URISyntaxException { - URL url = Url.parse("https://woot.com/test"); - assertThat(url.getProtocol(), is("https")); - assertThat(url.getHost(), is("woot.com")); - assertThat(url.getPath(), is("/test")); + public void parseRelativePath() { + URI uri = parse("https://woot.com/test"); + assertThat(uri.getScheme(), is("https")); + assertThat(uri.getHost(), is("woot.com")); + assertThat(uri.getPath(), is("/test")); } @Test - public void parseNoProtocol() throws URISyntaxException { - URL url = Url.parse("//localhost:3000"); - assertThat(url.getProtocol(), is("https")); - assertThat(url.getHost(), is("localhost")); - assertThat(url.getPort(), is(3000)); + public void parseNoProtocol() { + URI uri = parse("//localhost:3000"); + assertThat(uri.getScheme(), is("https")); + assertThat(uri.getHost(), is("localhost")); + assertThat(uri.getPort(), is(3000)); } @Test - public void parseNamespace() throws URISyntaxException { - assertThat(Url.parse("http://woot.com/woot").getPath(), is("/woot")); - assertThat(Url.parse("http://google.com").getPath(), is("/")); - assertThat(Url.parse("http://google.com/").getPath(), is("/")); + public void parseNamespace() { + assertThat(parse("http://woot.com/woot").getPath(), is("/woot")); + assertThat(parse("http://google.com").getPath(), is("/")); + assertThat(parse("http://google.com/").getPath(), is("/")); } @Test - public void parseDefaultPort() throws URISyntaxException { - assertThat(Url.parse("http://google.com/").toString(), is("http://google.com:80/")); - assertThat(Url.parse("https://google.com/").toString(), is("https://google.com:443/")); + public void parseDefaultPort() { + assertThat(parse("http://google.com/").toString(), is("http://google.com:80/")); + assertThat(parse("https://google.com/").toString(), is("https://google.com:443/")); } @Test - public void extractId() throws MalformedURLException { - String id1 = Url.extractId("http://google.com:80/"); - String id2 = Url.extractId("http://google.com/"); - String id3 = Url.extractId("https://google.com/"); + public void testWsProtocol() { + URI uri = parse("ws://woot.com/test"); + assertThat(uri.getScheme(), is("ws")); + assertThat(uri.getHost(), is("woot.com")); + assertThat(uri.getPort(), is(80)); + assertThat(uri.getPath(), is("/test")); + } + + @Test + public void testWssProtocol() { + URI uri = parse("wss://woot.com/test"); + assertThat(uri.getScheme(), is("wss")); + assertThat(uri.getHost(), is("woot.com")); + assertThat(uri.getPort(), is(443)); + assertThat(uri.getPath(), is("/test")); + } + + @Test + public void extractId() { + String id1 = extractId("http://google.com:80/"); + String id2 = extractId("http://google.com/"); + String id3 = extractId("https://google.com/"); assertThat(id1, is(id2)); assertThat(id1, is(not(id3))); assertThat(id2, is(not(id3))); } @Test - public void ipv6() throws URISyntaxException, MalformedURLException { + public void ipv6() { String url = "http://[::1]"; - URL parsed = Url.parse(url); - assertThat(parsed.getProtocol(), is("http")); + URI parsed = parse(url); + assertThat(parsed.getScheme(), is("http")); assertThat(parsed.getHost(), is("[::1]")); assertThat(parsed.getPort(), is(80)); - assertThat(Url.extractId(url), is("http://[::1]:80")); + assertThat(extractId(url), is("http://[::1]:80")); } + }