Skip to content

Commit

Permalink
Set correct HOST header for Netty & POST
Browse files Browse the repository at this point in the history
Signed-off-by: jansupol <jan.supol@oracle.com>
  • Loading branch information
jansupol authored and senivam committed Nov 15, 2024
1 parent b231664 commit 1b99237
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down Expand Up @@ -474,9 +463,7 @@ public void operationComplete(io.netty.util.concurrent.Future<? super Void> 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();
Expand Down Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
Expand Down Expand Up @@ -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()));
}
}

}

0 comments on commit 1b99237

Please sign in to comment.