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

@ -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();