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;

View File

@ -15,7 +15,8 @@ import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.net.URL;
import java.net.URI;
import java.net.URISyntaxException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
@ -321,12 +322,12 @@ public class EepGet {
* @return a filename to save the resource as on local filesystem
*/
public static String suggestName(String url) {
URL nameURL = null; // URL object
URI nameURL = null;
String name; // suggested name
try {
nameURL = new URL(url);
} catch (MalformedURLException e) {
nameURL = new URI(url);
} catch (URISyntaxException e) {
System.err.println("Please enter a properly formed URL.");
System.exit(1);
}
@ -722,24 +723,25 @@ public class EepGet {
if (_redirectLocation != null) {
// we also are here after a 407
//try {
try {
if (_redirectLocation.startsWith("http://")) {
_actualURL = _redirectLocation;
} else {
// the Location: field has been required to be an absolute URI at least since
// RFC 1945 (HTTP/1.0 1996), so it isn't clear what the point of this is.
// This oddly adds a ":" even if no port, but that seems to work.
URL url = new URL(_actualURL);
URI url = new URI(_actualURL);
if (_redirectLocation.startsWith("/"))
_actualURL = "http://" + url.getHost() + ":" + url.getPort() + _redirectLocation;
else
// this blows up completely on a redirect to https://, for example
_actualURL = "http://" + url.getHost() + ":" + url.getPort() + "/" + _redirectLocation;
}
// an MUE is an IOE
//} catch (MalformedURLException mue) {
// throw new IOException("Redirected from an invalid URL");
//}
} catch (URISyntaxException use) {
IOException ioe = new MalformedURLException("Redirected to invalid URL");
ioe.initCause(use);
throw ioe;
}
AuthState as = _authState;
if (_responseCode == 407) {
@ -1226,9 +1228,9 @@ public class EepGet {
if (_shouldProxy) {
_proxy = InternalSocket.getSocket(_proxyHost, _proxyPort);
} else {
//try {
URL url = new URL(_actualURL);
if ("http".equals(url.getProtocol())) {
try {
URI url = new URI(_actualURL);
if ("http".equals(url.getScheme())) {
String host = url.getHost();
String hostlc = host.toLowerCase(Locale.US);
if (hostlc.endsWith(".i2p"))
@ -1248,10 +1250,11 @@ public class EepGet {
} else {
throw new MalformedURLException("URL is not supported:" + _actualURL);
}
// an MUE is an IOE
//} catch (MalformedURLException mue) {
// throw new IOException("Request URL is invalid");
//}
} catch (URISyntaxException use) {
IOException ioe = new MalformedURLException("Request URL is invalid");
ioe.initCause(use);
throw ioe;
}
}
_proxyIn = _proxy.getInputStream();
if (!(_proxy instanceof InternalSocket))
@ -1273,7 +1276,14 @@ public class EepGet {
boolean post = false;
if ( (_postData != null) && (_postData.length() > 0) )
post = true;
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();
if (host == null || host.length() <= 0)
throw new MalformedURLException("Bad URL, no host");

View File

@ -6,7 +6,9 @@ import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.URL;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import gnu.getopt.Getopt;
@ -176,24 +178,25 @@ public class EepHead extends EepGet {
// Should we even follow redirects for HEAD?
if (_redirectLocation != null) {
//try {
try {
if (_redirectLocation.startsWith("http://")) {
_actualURL = _redirectLocation;
} else {
// the Location: field has been required to be an absolute URI at least since
// RFC 1945 (HTTP/1.0 1996), so it isn't clear what the point of this is.
// This oddly adds a ":" even if no port, but that seems to work.
URL url = new URL(_actualURL);
URI url = new URI(_actualURL);
if (_redirectLocation.startsWith("/"))
_actualURL = "http://" + url.getHost() + ":" + url.getPort() + _redirectLocation;
else
// this blows up completely on a redirect to https://, for example
_actualURL = "http://" + url.getHost() + ":" + url.getPort() + "/" + _redirectLocation;
}
// an MUE is an IOE
//} catch (MalformedURLException mue) {
// throw new IOException("Redirected from an invalid URL");
//}
} catch (URISyntaxException use) {
IOException ioe = new MalformedURLException("Redirected to invalid URL");
ioe.initCause(use);
throw ioe;
}
AuthState as = _authState;
if (_responseCode == 407) {
if (!_shouldProxy)
@ -252,7 +255,14 @@ public class EepHead extends EepGet {
@Override
protected String getRequest() throws IOException {
StringBuilder buf = new StringBuilder(512);
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();
int port = url.getPort();
String path = url.getPath();

View File

@ -6,7 +6,8 @@ import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Locale;
import gnu.getopt.Getopt;
@ -167,7 +168,14 @@ public class PartialEepGet 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();
if (host == null || host.length() <= 0)
throw new MalformedURLException("Bad URL, no host");

View File

@ -46,7 +46,8 @@ import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URI;
import java.net.URISyntaxException;
import java.security.KeyStore;
import java.security.GeneralSecurityException;
import java.security.cert.CertificateException;
@ -553,11 +554,11 @@ public class SSLEepGet extends EepGet {
String req = getRequest();
//try {
URL url = new URL(_actualURL);
String host = null;
int port = 0;
if ("https".equals(url.getProtocol())) {
String host;
int port;
try {
URI url = new URI(_actualURL);
if ("https".equals(url.getScheme())) {
host = url.getHost();
if (host.toLowerCase(Locale.US).endsWith(".i2p"))
throw new MalformedURLException("I2P addresses unsupported");
@ -589,10 +590,11 @@ public class SSLEepGet extends EepGet {
} else {
throw new MalformedURLException("Only https supported: " + _actualURL);
}
// an MUE is an IOE
//} catch (MalformedURLException mue) {
// throw new IOException("Request URL is invalid");
//}
} catch (URISyntaxException use) {
IOException ioe = new MalformedURLException("Redirected to invalid URL");
ioe.initCause(use);
throw ioe;
}
_proxyIn = _proxy.getInputStream();
_proxyOut = _proxy.getOutputStream();

View File

@ -3,7 +3,7 @@ package net.i2p.router.networkdb.reseed;
import java.io.File;
import java.io.InputStream;
import java.io.IOException;
import java.net.URL;
import java.net.URI;
import java.util.concurrent.atomic.AtomicBoolean;
import net.i2p.data.DataHelper;
@ -131,7 +131,7 @@ public class ReseedChecker {
* @throws IllegalArgumentException if it doesn't end with zip or su3
* @since 0.9.19
*/
public boolean requestReseed(URL url) throws IllegalArgumentException {
public boolean requestReseed(URI url) throws IllegalArgumentException {
if (_inProgress.compareAndSet(false, true)) {
Reseeder reseeder = new Reseeder(_context, this);
try {

View File

@ -7,10 +7,8 @@ import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Collections;
@ -153,7 +151,7 @@ public class Reseeder {
* @throws IllegalArgumentException if it doesn't end with zip or su3
* @since 0.9.19
*/
void requestReseed(URL url) throws IllegalArgumentException {
void requestReseed(URI url) throws IllegalArgumentException {
ReseedRunner reseedRunner = new ReseedRunner(url);
// set to daemon so it doesn't hang a shutdown
Thread reseed = new I2PAppThread(reseedRunner, "Reseed", true);
@ -239,7 +237,7 @@ public class Reseeder {
/** bytes per sec for each su3 downloaded */
private final List<Long> _bandwidths;
private static final int MAX_DATE_SETS = 2;
private final URL _url;
private final URI _url;
/**
* Start a reseed from the default URL list
@ -256,7 +254,7 @@ public class Reseeder {
* @throws IllegalArgumentException if it doesn't end with zip or su3
* @since 0.9.19
*/
public ReseedRunner(URL url) throws IllegalArgumentException {
public ReseedRunner(URI url) throws IllegalArgumentException {
String lc = url.getPath().toLowerCase(Locale.US);
if (!(lc.endsWith(".zip") || lc.endsWith(".su3")))
throw new IllegalArgumentException("Reseed URL must end with .zip or .su3");
@ -412,7 +410,7 @@ public class Reseeder {
* @return count of routerinfos successfully fetched, or -1 if no valid URLs
*/
private int reseed(boolean echoStatus) {
List<URL> URLList = new ArrayList<URL>();
List<URI> URLList = new ArrayList<URI>();
String URLs = _context.getProperty(PROP_RESEED_URL);
boolean defaulted = URLs == null;
boolean SSLDisable = _context.getBooleanProperty(PROP_SSL_DISABLE);
@ -429,29 +427,29 @@ public class Reseeder {
if (!u.endsWith("/"))
u = u + '/';
try {
URLList.add(new URL(u));
} catch (MalformedURLException mue) {}
URLList.add(new URI(u));
} catch (URISyntaxException mue) {}
}
Collections.shuffle(URLList, _context.random());
if (!SSLDisable && !SSLRequired) {
// put the non-SSL at the end of the SSL
List<URL> URLList2 = new ArrayList<URL>();
List<URI> URLList2 = new ArrayList<URI>();
tok = new StringTokenizer(DEFAULT_SEED_URL, " ,");
while (tok.hasMoreTokens()) {
String u = tok.nextToken().trim();
if (!u.endsWith("/"))
u = u + '/';
try {
URLList2.add(new URL(u));
} catch (MalformedURLException mue) {}
URLList2.add(new URI(u));
} catch (URISyntaxException mue) {}
}
Collections.shuffle(URLList2, _context.random());
URLList.addAll(URLList2);
}
} else {
// custom list given
List<URL> SSLList = new ArrayList<URL>();
List<URL> nonSSLList = new ArrayList<URL>();
List<URI> SSLList = new ArrayList<URI>();
List<URI> nonSSLList = new ArrayList<URI>();
StringTokenizer tok = new StringTokenizer(URLs, " ,");
while (tok.hasMoreTokens()) {
// format tokens
@ -461,12 +459,12 @@ public class Reseeder {
// check if ssl or not then add to respective list
if (u.startsWith("https")) {
try {
SSLList.add(new URL(u));
} catch (MalformedURLException mue) {}
SSLList.add(new URI(u));
} catch (URISyntaxException mue) {}
} else {
try {
nonSSLList.add(new URL(u));
} catch (MalformedURLException mue) {}
nonSSLList.add(new URI(u));
} catch (URISyntaxException mue) {}
}
}
// shuffle lists
@ -482,8 +480,8 @@ public class Reseeder {
}
if (!isSNISupported()) {
try {
URLList.remove(new URL("https://netdb.i2p2.no/"));
} catch (MalformedURLException mue) {}
URLList.remove(new URI("https://netdb.i2p2.no/"));
} catch (URISyntaxException mue) {}
}
if (URLList.isEmpty()) {
System.out.println("No valid reseed URLs");
@ -501,19 +499,19 @@ public class Reseeder {
* @param echoStatus apparently always false
* @return count of routerinfos successfully fetched
*/
private int reseed(List<URL> URLList, boolean echoStatus) {
private int reseed(List<URI> URLList, boolean echoStatus) {
int total = 0;
for (int i = 0; i < URLList.size() && _isRunning; i++) {
if (_context.router().gracefulShutdownInProgress()) {
System.out.println("Reseed aborted, shutdown in progress");
return total;
}
URL url = URLList.get(i);
URI url = URLList.get(i);
int dl = 0;
if (ENABLE_SU3) {
try {
dl = reseedSU3(new URL(url.toString() + SU3_FILENAME), echoStatus);
} catch (MalformedURLException mue) {}
dl = reseedSU3(new URI(url.toString() + SU3_FILENAME), echoStatus);
} catch (URISyntaxException mue) {}
}
if (ENABLE_NON_SU3) {
if (dl <= 0)
@ -557,7 +555,7 @@ public class Reseeder {
* @param echoStatus apparently always false
* @return count of routerinfos successfully fetched
**/
private int reseedOne(URL seedURL, boolean echoStatus) {
private int reseedOne(URI seedURL, boolean echoStatus) {
try {
// Don't use context clock as we may be adjusting the time
final long timeLimit = System.currentTimeMillis() + MAX_TIME_PER_HOST;
@ -659,7 +657,7 @@ public class Reseeder {
* @return count of routerinfos successfully fetched
* @since 0.9.14
**/
public int reseedSU3(URL seedURL, boolean echoStatus) {
public int reseedSU3(URI seedURL, boolean echoStatus) {
return reseedSU3OrZip(seedURL, true, echoStatus);
}
@ -673,7 +671,7 @@ public class Reseeder {
* @return count of routerinfos successfully fetched
* @since 0.9.19
**/
public int reseedZip(URL seedURL, boolean echoStatus) {
public int reseedZip(URI seedURL, boolean echoStatus) {
return reseedSU3OrZip(seedURL, false, echoStatus);
}
@ -687,7 +685,7 @@ public class Reseeder {
* @return count of routerinfos successfully fetched
* @since 0.9.19
**/
private int reseedSU3OrZip(URL seedURL, boolean isSU3, boolean echoStatus) {
private int reseedSU3OrZip(URI seedURL, boolean isSU3, boolean echoStatus) {
int fetched = 0;
int errors = 0;
File contentRaw = null;
@ -869,7 +867,7 @@ public class Reseeder {
if (ourHash != null && DataHelper.eq(hash, ourHash.getData()))
return false;
URL url = new URL(seedURL + (seedURL.endsWith("/") ? "" : "/") + ROUTERINFO_PREFIX + peer + ROUTERINFO_SUFFIX);
URI url = new URI(seedURL + (seedURL.endsWith("/") ? "" : "/") + ROUTERINFO_PREFIX + peer + ROUTERINFO_SUFFIX);
byte data[] = readURL(url);
if (data == null || data.length <= 0)
@ -878,7 +876,7 @@ public class Reseeder {
}
/** @return null on error */
private byte[] readURL(URL url) throws IOException {
private byte[] readURL(URI url) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream(4*1024);
EepGet get;
boolean ssl = url.toString().startsWith("https");
@ -923,7 +921,7 @@ public class Reseeder {
* @return null on error
* @since 0.9.14
*/
private File fetchURL(URL url) throws IOException {
private File fetchURL(URI url) throws IOException {
File out = new File(_context.getTempDir(), "reseed-" + _context.random().nextInt() + ".tmp");
EepGet get;
boolean ssl = url.toString().startsWith("https");

View File

@ -4,9 +4,9 @@
package net.i2p.router.transport;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.UnknownHostException;
import java.net.URL;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
@ -823,17 +823,17 @@ class UPnP extends ControlPoint implements DeviceChangeListener, EventListener {
String him = _router.getURLBase();
if (him != null && him.length() > 0) {
try {
URL url = new URL(him);
URI url = new URI(him);
hisIP = url.getHost();
} catch (MalformedURLException mue) {}
} catch (URISyntaxException use) {}
}
if (hisIP == null) {
him = _router.getLocation();
if (him != null && him.length() > 0) {
try {
URL url = new URL(him);
URI url = new URI(him);
hisIP = url.getHost();
} catch (MalformedURLException mue) {}
} catch (URISyntaxException use) {}
}
}
if (hisIP == null)