Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Brackets in a Request URL are legal but not in a URI (Java)?

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?

like image 722
Adam Gent Avatar asked Jun 14 '12 18:06

Adam Gent


People also ask

Are brackets allowed in URLs?

According to the URL specification, the square brackets are not valid URL characters.

Are curly braces valid in URLs?

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.

How do you put square brackets in URL?

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.


1 Answers

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

  • Which characters make a URL invalid?
like image 131
Adam Gent Avatar answered Oct 14 '22 23:10

Adam Gent