Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set correct HOST header for Netty & POST #5801

Merged
merged 1 commit into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()));
}
}

}