Apparently brackets are not allowed in URI paths.
I'm not sure if this is a Tomcat problem but I'm getting request with paths that contains ]
.
In otherwords
request.getRequestURL() == "http://localhost:8080/a]b"
request.getRequestURI() == "/a]b"
BTW getRequestURL() and URI are generally escaped ie for http://localhost:8080/a b
request.getRequestURL() == "http://localhost:8080/a%20b"
So if you try to do:
new URI("http://localhost:8080/a]b")
new URI(request.getRequestURL())
It will fail with a URI parsing exception.
If I escape the path that will make the %20
double escaped.
How do I turn Servlet Request URLs into URIs?
According to the URL specification, the square brackets are not valid URL characters.
Curly brackets are unsafe in URLs. cURL (unlike Google Chrome) tries to do you a favor and automatically encodes the URL. In other words, it transforms { to %7B and } to &7D . To prevent that behavior, you can pass the query string parameters using -d instead.
Making links with square bracket tags The simplest example is to surround the url (which should start with https://) with square brackets. For example, [http://google.com] will make a link like this: http://google.com.
Java's URI appears to be very strict and requires escaping for the Excluded US-ASCII Charset.
To fix this I encode those and only those characters minus the '%'
and '#'
as the URL may already contain those character. I used Http Clients URI utils which for some reason is not in HttpComponents.
private static BitSet badUriChars = new BitSet(256);
static {
badUriChars.set(0, 255, true);
badUriChars.andNot(org.apache.commons.httpclient.URI.unwise);
badUriChars.andNot(org.apache.commons.httpclient.URI.space);
badUriChars.andNot(org.apache.commons.httpclient.URI.control);
badUriChars.set('<', false);
badUriChars.set('>', false);
badUriChars.set('"', false);
}
public static URI toURIorFail(String url) throws URISyntaxException {
URI uri = URIUtil.encode(url, badUriChars, "UTF-8");
return new URI(uri);
}
Edit: Here are some related SO posts (more to come):
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With