Skip to content

WebClient resolves URI with underscore in hostname to localhost #10248

@hozumi

Description

@hozumi

Helidon Version: 4.2.3

Environment Details

  • Helidon Version: 4.2.3
  • Helidon SE
  • JDK version: eclipse-temurin:24.0.1_9-jdk
  • OS: Ubuntu 24.04.2 LTS (as container in k8s), MacOS Ventura 13.7.4

Description

When using WebClient, if a URI is provided with a hostname containing an underscore (_), the request is incorrectly routed to localhost.

For example, the following code demonstrates the issue:

WebClient client = WebClient.create();
HttpClientRequest req = client.get();
req.uri("http://a_1.example.com/foo");
System.out.println("uri(): " + req.uri().toString());

Actual Output:

uri(): http://localhost:80/foo

Expected Output:
The URI should be correctly parsed as http://a_1.example.com/foo.

Cause

This behavior appears to be caused by java.net.URI, which does not allow underscores in hostnames according to older RFCs, failing to align with the more permissive RFC 3986. When java.net.URI.create() is called with such a string, the host component of the resulting URI object is null. This is a known OpenJDK issue: JDK-8019345.

While this is fundamentally a JDK limitation, it makes handling real-world URLs inconvenient within Helidon's WebClient.

Workaround

I am currently using the following workaround. It checks if uri.getHost() is null and, if so, manually constructs a ClientUri to set the host correctly.

WebClient client = WebClient.create();
HttpClientRequest req = client.get();

java.net.URI uri = java.net.URI.create("http://a_1.example.com/foo");

if (uri.getHost() == null) {
    // Workaround for hostnames with underscores
    try {
        java.net.URL url = uri.toURL();
        ClientUri cUri = ClientUri.create(uri);
        cUri.host(url.getHost());
        req.uri(cUri);
    } catch (java.net.MalformedURLException e) {
        // Handle exception
    }
} else {
    req.uri(uri);
}

System.out.println("uri(): " + req.uri().toString()); // Correctly prints http://a_1.example.com/foo

This workaround is cumbersome, especially since io.helidon.webclient.api.ClientUri cannot be created directly from a complete URL string.

Proposal

Could you consider handling this case more gracefully within the WebClient API? It would be a significant improvement if WebClient could internally manage URIs containing underscores in the hostname without requiring a manual workaround.

Thank you for your consideration.

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions