Replace URL with URI where possible

URL bad for anon and has traps like equals()
This commit is contained in:
zzz
2015-11-07 22:38:05 +00:00
parent 1e5a35c7f8
commit 8e77188560
12 changed files with 151 additions and 114 deletions

View File

@@ -3,8 +3,8 @@ package org.klomp.snark;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
@@ -590,10 +590,10 @@ public class I2PSnarkUtil {
*/
public boolean isKnownOpenTracker(String url) {
try {
URL u = new URL(url);
URI u = new URI(url);
String host = u.getHost();
return host != null && SnarkManager.KNOWN_OPENTRACKERS.contains(host);
} catch (MalformedURLException mue) {
} catch (URISyntaxException use) {
return false;
}
}

View File

@@ -23,8 +23,8 @@ package org.klomp.snark;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
@@ -880,13 +880,13 @@ public class TrackerClient implements Runnable {
* @since 0.7.12
*/
public static boolean isValidAnnounce(String ann) {
URL url;
URI url;
try {
url = new URL(ann);
} catch (MalformedURLException mue) {
url = new URI(ann);
} catch (URISyntaxException use) {
return false;
}
return url.getProtocol().equals("http") &&
return "http".equals(url.getScheme()) && url.getHost() != null &&
(url.getHost().endsWith(".i2p") || url.getHost().equals("i2p"));
}
@@ -896,13 +896,13 @@ public class TrackerClient implements Runnable {
* @since 0.9.5
*/
private static Hash getHostHash(String ann) {
URL url;
URI url;
try {
url = new URL(ann);
} catch (MalformedURLException mue) {
url = new URI(ann);
} catch (URISyntaxException use) {
return null;
}
if (!url.getProtocol().equals("http"))
if (!"http".equals(url.getScheme()))
return null;
String host = url.getHost();
if (host.endsWith(".i2p"))

View File

@@ -5,7 +5,8 @@ import java.io.IOException;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.UnknownHostException;
import java.net.URL;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Locale;
import java.util.Properties;
@@ -112,8 +113,8 @@ public class I2PSocketEepGet extends EepGet {
if (_socket != null) try { _socket.close(); } catch (IOException ioe) {}
try {
URL url = new URL(_actualURL);
if ("http".equals(url.getProtocol())) {
URI url = new URI(_actualURL);
if ("http".equals(url.getScheme())) {
String host = url.getHost();
int port = url.getPort();
if (port <= 0 || port > 65535)
@@ -123,13 +124,13 @@ public class I2PSocketEepGet extends EepGet {
// Rewrite the url to strip out the /i2p/,
// as the naming service accepts B64KEY (but not B64KEY.i2p atm)
if ("i2p".equals(host)) {
String file = url.getFile();
String file = url.getPath();
try {
int slash = 1 + file.substring(1).indexOf("/");
host = file.substring(1, slash);
_actualURL = "http://" + host + file.substring(slash);
} catch (IndexOutOfBoundsException ioobe) {
throw new IOException("Bad /i2p/ format: " + _actualURL);
throw new MalformedURLException("Bad /i2p/ format: " + _actualURL);
}
}
@@ -173,12 +174,14 @@ public class I2PSocketEepGet extends EepGet {
opts.setPort(port);
_socket = _socketManager.connect(dest, opts);
} else {
throw new IOException("Unsupported protocol: " + _actualURL);
throw new MalformedURLException("Unsupported protocol: " + _actualURL);
}
} catch (MalformedURLException mue) {
throw new IOException("Request URL is invalid: " + _actualURL);
} catch (URISyntaxException use) {
IOException ioe = new MalformedURLException("Bad URL");
ioe.initCause(use);
throw ioe;
} catch (I2PException ie) {
throw new IOException(ie.toString());
throw new IOException("I2P error", ie);
}
_proxyIn = _socket.getInputStream();
@@ -202,7 +205,14 @@ public class I2PSocketEepGet extends EepGet {
@Override
protected String getRequest() throws IOException {
StringBuilder buf = new StringBuilder(2048);
URL url = new URL(_actualURL);
URI url;
try {
url = new URI(_actualURL);
} catch (URISyntaxException use) {
IOException ioe = new MalformedURLException("Bad URL");
ioe.initCause(use);
throw ioe;
}
//String host = url.getHost();
String path = url.getPath();
String query = url.getQuery();

View File

@@ -2,8 +2,8 @@ package net.i2p.router.web;
import java.io.InputStream;
import java.io.IOException;
import java.net.URL;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@@ -37,10 +37,10 @@ public class ConfigReseedHandler extends FormHandler {
addFormError(_t("You must enter a URL"));
return;
}
URL url;
URI url;
try {
url = new URL(val);
} catch (MalformedURLException mue) {
url = new URI(val);
} catch (URISyntaxException mue) {
addFormError(_t("Bad URL {0}", val));
return;
}

View File

@@ -18,7 +18,8 @@ import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.URL;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Locale;
import net.i2p.I2PAppContext;
@@ -115,18 +116,16 @@ public class UrlLauncher implements ClientApp {
* @return success
*/
private static boolean waitForServer(String urlString) {
URL url;
URI url;
try {
url = new URL(urlString);
} catch (MalformedURLException e) {
url = new URI(urlString);
} catch (URISyntaxException e) {
return false;
}
String host = url.getHost();
int port = url.getPort();
if (port <= 0) {
port = url.getDefaultPort();
if (port <= 0)
return false;
port = "https".equals(url.getScheme()) ? 443 : 80;
}
SocketAddress sa;
try {
@@ -261,8 +260,8 @@ public class UrlLauncher implements ClientApp {
private static boolean validateUrlFormat(String urlString) {
try {
// just to check validity
new URL(urlString);
} catch (MalformedURLException e) {
new URI(urlString);
} catch (URISyntaxException e) {
return false;
}
return true;