tanglei
tanglei

Reputation: 347

Conflicts between java.net.URL and java.net.URI when dealing with hostname contains underscore( ), is it a bug?

Someone has submiited a bug to JDK, saying that hostname contains underscore(_) is not valid url. The following was the response. reference link

RFC 952 disallows _ underscores in hostnames. So, this is not a bug. I also haven't been able to find examples of actual usage. So, unless a particularly compelling case can be made, I'm closing as not a bug.

I am wondering, if hostname with underscore is not valid, why the result is differrent between java.net.URI and java.net.URL? Is it a bug or a feature? Here is the example.

java.net.URL url = new java.net.URL("http://test_1.tanglei.name");
System.out.println(url.getHost()); //test_1.tanglei.name
java.net.URI uri = new java.net.URI("http://test_1.tanglei.name");
System.out.println(uri.getHost()); //null 

Upvotes: 0

Views: 706

Answers (1)

Stephen C
Stephen C

Reputation: 719446

Is it a bug or a feature?

I don't think the reviewer entirely understood that you were talking about 3-way inconsistency between two different URI constructors and the 4 arg URL constructor. (Your bug report does not spell out precisely which of the inconsistencies is concerning you.) He has viewed it as a report as a 2-way inconsistency between URL and URI.

The review is somewhat terse, but the reviewer's point is the URL constructor is behaving in accordance with its specification. Since the specification explicitly states that no validation is performed, this is not a bug in the code. This is indisputable.

What he didn't spell out is that fixing this inconsistency (by changing the URL class specification) would break lots of peoples' 20+ year old code Java code. That would be a really bad idea. It can't happen.

So ... this inconsistency is a "feature".

Upvotes: 1

Related Questions