GeezerVison is showing github.com — "Illegal Character in query" in HttpConversionUtil · Issue #14591 · netty/netty
"Illegal Character in query" in HttpConversionUtil #14591DescriptionNetty should be able to handle any valid URI. But currently io.netty.handler.codec.http2.HttpConversionUtil is relying on java.net.URI class, which supports https://datatracker.ietf.org/doc/rfc2396/, but does not support https://datatracker.ietf.org/doc/rfc3986/ . So, some legal characters in the URI path, e.g. {, are breaking the conversion.
Expected behavior
Any HTTP header with rfc3986-compliant path (and probably non-compiant too) should be convertable to Http2Headers
Actual behavior
IllegalArgumentException is rethrown caused by an exception in java.net.URI:
java.lang.IllegalArgumentException: Illegal character in query at index 63: https://bh.contextweb.com/bh/rtset?pid=558355&ev=1&us_privacy=${us_privacy}
at java.base/java.net.URI.create(URI.java:906)
at io.netty.handler.codec.http2.HttpConversionUtil.toHttp2Headers(HttpConversionUtil.java:441)
...
Caused by: java.net.URISyntaxException: Illegal character in query at index 63: https://bh.contextweb.com/bh/rtset?pid=558355&ev=1&us_privacy=${us_privacy}
at java.base/java.net.URI$Parser.fail(URI.java:2976)
at java.base/java.net.URI$Parser.checkChars(URI.java:3147)
at java.base/java.net.URI$Parser.parseHierarchical(URI.java:3235)
at java.base/java.net.URI$Parser.parse(URI.java:3177)
at java.base/java.net.URI.<init>(URI.java:623)
at java.base/java.net.URI.create(URI.java:904)
... 71 more
Steps to reproduce
Use any code flow that converts HttpMessage object to Http2Headers using HttpConversionUtil with a URL with characters not supported by rfc2396.
Minimal yet complete reproducer code (or URL to code)
public class HttpConversionUtilTest {
@Test
void testToHttp2HeadersFromHttpRequest() {
HttpRequest httpRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "https://bh.contextweb.com/bh/rtset?pid=558355&ev=1&us_privacy=${us_privacy}");
Http2Headers http2Headers = HttpConversionUtil.toHttp2Headers(httpRequest, true);
assertEquals("/bh/rtset?pid=558355&ev=1&us_privacy=${us_privacy}", http2Headers.path().toString());
}
}
Netty version
4.1.115
JVM version (e.g. java -version)
OpenJDK 64-Bit Server VM Temurin-17.0.7+7 (build 17.0.7+7, mixed mode, sharing)
OS version (e.g. uname -a)
Darwin us-m-ccloud01 22.6.0 Darwin Kernel Version 22.6.0: Fri Nov 15 17:21:49 PST 2024; root:xnu-8796.141.3.709.7~2/RELEASE_X86_64 x86_64 Activitynormanmaurer commented on Dec 31, 2024 yyy commented on Jan 3, 2025 @normanmaurer That's definitely good to know, thank you! Unfortunately, we're still on JDK17 for now.
I suppose it would be better to have consistent behavior across different Java versions, especially since the minimum required Java version for Netty is 1.6+ per https://netty.io/downloads.html. yyy commented on May 2, 2025 Can I suggest an improvement to fall back in case URI parsing fails?
I'm not too familiar with the PR process, but I feel this is a pretty trivial change.
Ideally, I hope it can go into 4.1.x and up.
===================================================================
diff --git a/node-server/services/web-proxy/src/main/java/io/netty/handler/codec/http2/HttpConversionUtil.java b/node-server/services/web-proxy/src/main/java/io/netty/handler/codec/http2/HttpConversionUtil.java
--- a/node-server/services/web-proxy/src/main/java/io/netty/handler/codec/http2/HttpConversionUtil.java
+++ b/node-server/services/web-proxy/src/main/java/io/netty/handler/codec/http2/HttpConversionUtil.java (date 1746153163686)
@@ -438,9 +438,18 @@
out.path(new AsciiString(request.uri()));
setHttp2Scheme(inHeaders, out);
} else {
- URI requestTargetUri = URI.create(request.uri());
- out.path(toHttp2Path(requestTargetUri));
- // Take from the request-line if HOST header was empty
+ URI requestTargetUri;
+ try {
+ requestTargetUri = URI.create(request.uri());
+ out.path(toHttp2Path(requestTargetUri));
+ // Take from the request-line if HOST header was empty
+ } catch (IllegalArgumentException e) {
+ // Unable to create parse request.uri as URI object
+ // fall back to extracting the path manually
+ String[] splitUrl = splitPath(request.uri());
+ requestTargetUri = URI.create(splitUrl[0]);
+ out.path(new AsciiString(splitUrl[1]));
+ }
host = isNullOrEmpty(host) ? requestTargetUri.getAuthority() : host;
setHttp2Scheme(inHeaders, requestTargetUri, out);
}
@@ -456,6 +465,26 @@
return out;
}
+ private static String[] splitPath(String url) {
+ String baseURL;
+ String pathWithQuery;
+
+ int schemeEnd = url.indexOf("://");
+ int startIdx = (schemeEnd != -1) ? schemeEnd + 3 : 0;
+
+ int pathStart = url.indexOf('/', startIdx);
+ if (pathStart != -1) {
+ baseURL = url.substring(0, pathStart);
+ pathWithQuery = url.substring(pathStart);
+ } else {
+ baseURL = url;
+ pathWithQuery = "/";
+ }
+
+ return new String[] { baseURL, pathWithQuery };
+ }
+
+
public static Http2Headers toHttp2Headers(HttpHeaders inHeaders, boolean validateHeaders) {
if (inHeaders.isEmpty()) {
return EmptyHttp2Headers.INSTANCE;
added a commit that references this issue on May 11, 2026This issue will close once commit c80f29c is merged into the '4.1' branch.c80f29c
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|