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,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)