RFD: URLs with empty host component but non-empty ports
Daniel Fuchs
daniel.fuchs at oracle.com
Thu Feb 26 10:51:30 UTC 2026
Hi Eirik,
URL constructors have been deprecated.
See https://bugs.openjdk.org/browse/JDK-8295949
best regards,
-- daniel
On 26/02/2026 10:36, Eirik Bjørsnøs wrote:
> Hi, Consider the following: URL url = new URL("http", "", 80, "/image.
> gif", null); String authority = url. getAuthority(); int port = url.
> getPort(); The authority here ends up as ": 80" and the port as 80,
> ZjQcmQRYFpfptBannerStart
> This Message Is From an External Sender
> This message came from outside your organization.
> Report Suspicious
> <https://us-phishalarm-ewt.proofpoint.com/EWT/v1/ACWV5N9M2RV99hQ!Op20OCpm1CSlHRBJ-tPf12nl3k_6GN1ic7WEXJSOfVbYiJQByqLC763zEAytIuoVlI1CA9GVsFUjFx5w6ImyE3D40okwerr2ThbNBv3UBn423r83aHREC0zc54kZ$>
> ZjQcmQRYFpfptBannerEnd
> Hi,
>
> Consider the following:
>
> URL url = new URL("http", "", 80, "/image.gif", null);
> String authority = url.getAuthority();
> int port = url.getPort();
>
> The authority here ends up as ":80" and the port as 80, the full string
> representation "http://:80/image.gif".
>
> Note that if we pass null instead of the empty string for host, we end
> up with a URL with a null authority and a -1 port. The URL string will
> be "http:/image.gif".
>
> Two things stand out from this observation:
>
> 1: This URL constructor treats null and empty string differently,
> which seems strange
> 2: When passing the empty host component and a port != -1, the resulting
> URL has a server-based authority component without any host part, which
> seems strange
>
> Is there a way out of this situation?
>
> Eirik.
>
>
More information about the net-dev
mailing list