fix not to throw URISyntaxException

This commit is contained in:
Naoyuki Kanezawa
2015-06-28 02:00:54 +09:00
parent acd7426a48
commit f8c723289e
3 changed files with 25 additions and 22 deletions

View File

@@ -5,7 +5,6 @@ import com.github.nkzawa.socketio.parser.Parser;
import javax.net.ssl.HostnameVerifier; import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext; import javax.net.ssl.SSLContext;
import java.net.MalformedURLException;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.net.URL; import java.net.URL;
@@ -42,7 +41,7 @@ public class IO {
return socket(new URI(uri), opts); return socket(new URI(uri), opts);
} }
public static Socket socket(URI uri) throws URISyntaxException { public static Socket socket(URI uri) {
return socket(uri, null); return socket(uri, null);
} }
@@ -54,18 +53,18 @@ public class IO {
* @return {@link Socket} instance. * @return {@link Socket} instance.
* @throws URISyntaxException * @throws URISyntaxException
*/ */
public static Socket socket(URI uri, Options opts) throws URISyntaxException { public static Socket socket(URI uri, Options opts) {
if (opts == null) { if (opts == null) {
opts = new Options(); opts = new Options();
} }
URL parsed; URL parsed = Url.parse(uri);
URI source;
try { try {
parsed = Url.parse(uri); source = parsed.toURI();
} catch (MalformedURLException e) { } catch (URISyntaxException e) {
throw new URISyntaxException(uri.toString(), e.getMessage()); throw new RuntimeException(e);
} }
URI source = parsed.toURI();
Manager io; Manager io;
if (opts.forceNew || !opts.multiplex) { if (opts.forceNew || !opts.multiplex) {

View File

@@ -13,11 +13,11 @@ public class Url {
private Url() {} private Url() {}
public static URL parse(String uri) throws URISyntaxException, MalformedURLException { public static URL parse(String uri) throws URISyntaxException {
return parse(new URI(uri)); return parse(new URI(uri));
} }
public static URL parse(URI uri) throws MalformedURLException { public static URL parse(URI uri) {
String protocol = uri.getScheme(); String protocol = uri.getScheme();
if (protocol == null || !protocol.matches("^https?|wss?$")) { if (protocol == null || !protocol.matches("^https?|wss?$")) {
protocol = "https"; protocol = "https";
@@ -40,13 +40,17 @@ 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();
return new URL(protocol + "://" try {
+ (userInfo != null ? userInfo + "@" : "") return new URL(protocol + "://"
+ uri.getHost() + (userInfo != null ? userInfo + "@" : "")
+ (port != -1 ? ":" + port : "") + uri.getHost()
+ path + (port != -1 ? ":" + port : "")
+ (query != null ? "?" + query : "") + path
+ (fragment != null ? "#" + fragment : "")); + (query != null ? "?" + query : "")
+ (fragment != null ? "#" + fragment : ""));
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
} }
public static String extractId(String url) throws MalformedURLException { public static String extractId(String url) throws MalformedURLException {

View File

@@ -16,13 +16,13 @@ import static org.junit.Assert.assertThat;
public class UrlTest { public class UrlTest {
@Test @Test
public void parse() throws MalformedURLException, URISyntaxException { public void parse() throws URISyntaxException {
assertThat(Url.parse("http://username:password@host:8080/directory/file?query#ref").toString(), assertThat(Url.parse("http://username:password@host:8080/directory/file?query#ref").toString(),
is("http://username:password@host:8080/directory/file?query#ref")); is("http://username:password@host:8080/directory/file?query#ref"));
} }
@Test @Test
public void parseRelativePath() throws MalformedURLException, URISyntaxException { public void parseRelativePath() throws URISyntaxException {
URL url = Url.parse("https://woot.com/test"); URL url = Url.parse("https://woot.com/test");
assertThat(url.getProtocol(), is("https")); assertThat(url.getProtocol(), is("https"));
assertThat(url.getHost(), is("woot.com")); assertThat(url.getHost(), is("woot.com"));
@@ -30,7 +30,7 @@ public class UrlTest {
} }
@Test @Test
public void parseNoProtocol() throws MalformedURLException, URISyntaxException { public void parseNoProtocol() throws URISyntaxException {
URL url = Url.parse("//localhost:3000"); URL url = Url.parse("//localhost:3000");
assertThat(url.getProtocol(), is("https")); assertThat(url.getProtocol(), is("https"));
assertThat(url.getHost(), is("localhost")); assertThat(url.getHost(), is("localhost"));
@@ -38,14 +38,14 @@ public class UrlTest {
} }
@Test @Test
public void parseNamespace() throws MalformedURLException, URISyntaxException { public void parseNamespace() throws URISyntaxException {
assertThat(Url.parse("http://woot.com/woot").getPath(), is("/woot")); 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("/"));
assertThat(Url.parse("http://google.com/").getPath(), is("/")); assertThat(Url.parse("http://google.com/").getPath(), is("/"));
} }
@Test @Test
public void parseDefaultPort() throws MalformedURLException, URISyntaxException { public void parseDefaultPort() throws URISyntaxException {
assertThat(Url.parse("http://google.com/").toString(), is("http://google.com:80/")); assertThat(Url.parse("http://google.com/").toString(), is("http://google.com:80/"));
assertThat(Url.parse("https://google.com/").toString(), is("https://google.com:443/")); assertThat(Url.parse("https://google.com/").toString(), is("https://google.com:443/"));
} }