gnodet-bot commented on code in PR #31251:
URL: https://github.com/apache/camel/pull/31251#discussion_r4770918337


##########
components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/DefaultNettyHttpBinding.java:
##########
@@ -175,11 +175,33 @@ public void populateCamelHeaders(
         headers.put(NettyHttpConstants.HTTP_URI, uri.getPath());
         headers.put(NettyHttpConstants.HTTP_QUERY, uri.getQuery());
         headers.put(NettyHttpConstants.HTTP_RAW_QUERY, uri.getRawQuery());
-        headers.put(NettyHttpConstants.HTTP_SCHEME, uri.getScheme());
-        headers.put(NettyHttpConstants.HTTP_HOST, uri.getHost());
+        // when the request URI is relative (e.g. /foo?x=1), uri.getScheme() 
and
+        // uri.getHost() return null; fall back to the endpoint configuration.
+        // For absolute URIs (e.g. proxy-style: GET http://example.com/foo 
HTTP/1.1),
+        // use the URI's own values so the proxy forwards to the correct 
target.
+        final boolean isRelativeUri = uri.getHost() == null;
+        final boolean ssl = configuration.isSsl() || 
"https".equals(uri.getScheme());
+        headers.put(NettyHttpConstants.HTTP_SCHEME, uri.getScheme() != null ? 
uri.getScheme() : (ssl ? "https" : "http"));
+        if (isRelativeUri) {
+            // Relative request line: prefer the Host HTTP header (sent by all 
HTTP/1.1 clients),
+            // then fall back to configuration.getHost() (consistent with 
platform-http-vertx).
+            String hostHeader = request.headers().get(HttpHeaderNames.HOST);
+            if (hostHeader != null && !hostHeader.isEmpty()) {
+                // Strip optional port from the Host header value (e.g. 
"example.com:8080" -> "example.com")
+                int colonIdx = hostHeader.lastIndexOf(':');
+                hostHeader = colonIdx > 0 ? hostHeader.substring(0, colonIdx) 
: hostHeader;

Review Comment:
   ⚠️ **Missing test for `Host: hostname:port` format.** The port-stripping 
logic (`lastIndexOf(':')`, line 191–192) is exercised only on bare hostnames — 
`testRelativeUriPrefersHostHeader` sets `Host: realhost` without a port 
component. Standard HTTP/1.1 clients (and browsers) commonly include the port 
in the `Host` header when it is non-standard (e.g. `Host: realhost:9090`). 
Without a test, a regression in the stripping will silently emit 
`HTTP_HOST=realhost:9090` raw.
   
   Suggested addition:
   ```java
   @Test
   public void testRelativeUriHostHeaderWithPort() throws Exception {
       CamelContext context = new DefaultCamelContext();
       Exchange exchange = new DefaultExchange(context);
   
       NettyHttpConfiguration configuration = new NettyHttpConfiguration();
       configuration.setHost("0.0.0.0");
       configuration.setPort(8080);
       configuration.setSsl(false);
   
       DefaultHttpRequest request = new 
DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/foo");
       request.headers().set(HttpHeaderNames.HOST, "realhost:9090");
   
       Map<String, Object> headers = new HashMap<>();
       new DefaultNettyHttpBinding().populateCamelHeaders(request, headers, 
exchange, configuration);
   
       assertEquals("realhost", headers.get(NettyHttpConstants.HTTP_HOST));
       assertEquals(8080, headers.get(NettyHttpConstants.HTTP_PORT)); // 
consumer port, not 9090
   }
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to