Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.net.URI cannot parse domain starting with a number

Tags:

java

uri

I have a URI, http://my-host.1domain:1234/path, that throws a URISyntaxException using one URI constructor:

new URI("http", /*userInfo*/null, /*host*/"my-host.1domain", 1234, "/path", /*query*/null, /*fragment*/null);

Exception in thread "main" java.net.URISyntaxException: Illegal character in hostname at index 15: http://my-host.1domain:1234/path
    at java.base/java.net.URI$Parser.fail(URI.java:2974)
    at java.base/java.net.URI$Parser.parseHostname(URI.java:3517)
    at java.base/java.net.URI$Parser.parseServer(URI.java:3358)
    at java.base/java.net.URI$Parser.parseAuthority(URI.java:3277)
    at java.base/java.net.URI$Parser.parseHierarchical(URI.java:3219)
    at java.base/java.net.URI$Parser.parse(URI.java:3175)
    at java.base/java.net.URI.<init>(URI.java:708)

but parses correctly using a different URI constructor:

// parses correctly
new URI("http", /*authority*/"my-host.1domain:1234", "/path", /*query*/null, /*fragment*/null);

Tested in OpenJDK 17.0.1. I've checked that domain names can start with a digit. So am I misusing the URI constructor or is this a bug?

(Background: the failing constructor is called from UriComponentsBuilder in Spring Web, who closed this as not-a-bug in their code)

like image 532
hertzsprung Avatar asked Oct 15 '22 19:10

hertzsprung


1 Answers

In your example 1domain is a TLD (top level domain). If to look at this spec it states:

A TLD label MUST be at least two characters long and MAY be as long as 63 characters - not counting any leading or trailing periods (.). It MUST consist of only ASCII characters from the groups "letters" (A-Z), "digits" (0-9) and "hyphen" (-), and it MUST start with an ASCII "letter", and it MUST NOT end with a "hyphen". Upper and lower case MAY be mixed at random, since DNS lookups are case-insensitive.

like image 104
Alexey R. Avatar answered Oct 18 '22 22:10

Alexey R.