From 1b992379bed082b2a8ae42f8ec2994d5aa89d714 Mon Sep 17 00:00:00 2001 From: jansupol Date: Thu, 14 Nov 2024 22:37:43 +0100 Subject: [PATCH] Set correct HOST header for Netty & POST Signed-off-by: jansupol --- .../netty/connector/NettyConnector.java | 31 ++++++++++--------- .../netty/connector/HostHeaderTest.java | 24 ++++++++++++++ 2 files changed, 40 insertions(+), 15 deletions(-) diff --git a/connectors/netty-connector/src/main/java/org/glassfish/jersey/netty/connector/NettyConnector.java b/connectors/netty-connector/src/main/java/org/glassfish/jersey/netty/connector/NettyConnector.java index f242f10bc7..671e72a94f 100644 --- a/connectors/netty-connector/src/main/java/org/glassfish/jersey/netty/connector/NettyConnector.java +++ b/connectors/netty-connector/src/main/java/org/glassfish/jersey/netty/connector/NettyConnector.java @@ -415,18 +415,7 @@ protected void initChannel(SocketChannel ch) throws Exception { // headers if (!jerseyRequest.hasEntity()) { setHeaders(jerseyRequest, nettyRequest.headers(), false); - - // host header - http 1.1 - if (!nettyRequest.headers().contains(HttpHeaderNames.HOST)) { - int requestPort = jerseyRequest.getUri().getPort(); - final String hostHeader; - if (requestPort != 80 && requestPort != 443) { - hostHeader = jerseyRequest.getUri().getHost() + ":" + requestPort; - } else { - hostHeader = jerseyRequest.getUri().getHost(); - } - nettyRequest.headers().add(HttpHeaderNames.HOST, hostHeader); - } + setHostHeader(jerseyRequest, nettyRequest); } if (jerseyRequest.hasEntity()) { @@ -474,9 +463,7 @@ public void operationComplete(io.netty.util.concurrent.Future futu @Override public OutputStream getOutputStream(int contentLength) throws IOException { replaceHeaders(jerseyRequest, nettyRequest.headers()); // WriterInterceptor changes - if (!nettyRequest.headers().contains(HttpHeaderNames.HOST)) { - nettyRequest.headers().add(HttpHeaderNames.HOST, jerseyRequest.getUri().getHost()); - } + setHostHeader(jerseyRequest, nettyRequest); headersSet.countDown(); return entityWriter.getOutputStream(); @@ -626,4 +613,18 @@ private static HttpHeaders replaceHeaders(ClientRequest jerseyRequest, HttpHeade private static boolean additionalProxyHeadersToKeep(String key) { return key.length() > 2 && (key.charAt(0) == 'x' || key.charAt(0) == 'X') && (key.charAt(1) == '-'); } + + private static void setHostHeader(ClientRequest jerseyRequest, HttpRequest nettyRequest) { + // host header - http 1.1 + if (!nettyRequest.headers().contains(HttpHeaderNames.HOST)) { + int requestPort = jerseyRequest.getUri().getPort(); + final String hostHeader; + if (requestPort != 80 && requestPort != 443) { + hostHeader = jerseyRequest.getUri().getHost() + ":" + requestPort; + } else { + hostHeader = jerseyRequest.getUri().getHost(); + } + nettyRequest.headers().add(HttpHeaderNames.HOST, hostHeader); + } + } } diff --git a/connectors/netty-connector/src/test/java/org/glassfish/jersey/netty/connector/HostHeaderTest.java b/connectors/netty-connector/src/test/java/org/glassfish/jersey/netty/connector/HostHeaderTest.java index 8d42c3699a..e20eded3ec 100644 --- a/connectors/netty-connector/src/test/java/org/glassfish/jersey/netty/connector/HostHeaderTest.java +++ b/connectors/netty-connector/src/test/java/org/glassfish/jersey/netty/connector/HostHeaderTest.java @@ -24,11 +24,14 @@ import org.junit.jupiter.api.Test; import javax.ws.rs.GET; +import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.client.ClientBuilder; +import javax.ws.rs.client.Entity; import javax.ws.rs.core.Application; import javax.ws.rs.core.Context; import javax.ws.rs.core.HttpHeaders; +import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; public class HostHeaderTest extends JerseyTest { @@ -37,6 +40,12 @@ public class HostHeaderTest extends JerseyTest { @Path("/") public static class HostHeaderTestEchoResource { + + @POST + public String post(@Context HttpHeaders headers) { + return get(headers); + } + @GET public String get(@Context HttpHeaders headers) { String sPort = headers.getHeaderString(HTTP_HEADER_NAME); @@ -72,4 +81,19 @@ public void testHostHeaderAndPort() { } } + @Test + public void testHostHeaderAndPortAfterRemovedFromFilter() { + int port = getPort(); + ClientConfig config = new ClientConfig(); + config.connectorProvider(new NettyConnectorProvider()); + try (Response response = ClientBuilder.newClient(config) + .target(target().getUri()) + .request() + .header(HTTP_HEADER_NAME, port) + .post(Entity.entity("xxx", MediaType.TEXT_PLAIN_TYPE))) { + MatcherAssert.assertThat(response.getStatus(), Matchers.is(200)); + MatcherAssert.assertThat(response.readEntity(String.class), Matchers.is(GET.class.getName())); + } + } + }