java.net.URI 无法解析以数字开头的域

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

我有一个 URI http://my-host.1domain:1234/path,它使用一个 URI 构造函数抛出 URISyntaxException

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)

但使用不同的 URI 构造函数正确解析:

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

已在 OpenJDK 17.0.1 中测试。我检查过 domain names can start with a digit。那么我是在滥用 URI 构造函数还是这是一个错误?

(背景:失败的构造函数是从 Spring Web 中的 UriComponentsBuilder 调用的,谁将其关闭为 not-a-bug in their code

在您的示例中,1domain 是 TLD(顶级域)。如果要查看 this spec,它表示:

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.