Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/2.x' into 'origin/3.0'
Browse files Browse the repository at this point in the history
Signed-off-by: Maxim Nesen <[email protected]>
  • Loading branch information
senivam committed Nov 15, 2024
2 parents 24ef916 + 1b99237 commit 525b915
Show file tree
Hide file tree
Showing 17 changed files with 443 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.util.Iterator;
import java.util.List;
Expand Down Expand Up @@ -48,7 +47,6 @@
import io.netty.handler.codec.http.HttpContent;
import io.netty.handler.codec.http.HttpObject;
import io.netty.handler.codec.http.HttpResponse;
import io.netty.handler.codec.http.HttpUtil;
import io.netty.handler.codec.http.LastHttpContent;
import io.netty.handler.timeout.IdleStateEvent;
import org.glassfish.jersey.uri.internal.JerseyUriBuilder;
Expand Down Expand Up @@ -146,7 +144,21 @@ protected void notifyResponse() {
ClientRequest newReq = new ClientRequest(jerseyRequest);
newReq.setUri(newUri);
restrictRedirectRequest(newReq, cr);
connector.execute(newReq, redirectUriHistory, responseAvailable);

final NettyConnector newConnector = new NettyConnector(newReq.getClient());
newConnector.execute(newReq, redirectUriHistory, new CompletableFuture<ClientResponse>() {
@Override
public boolean complete(ClientResponse value) {
newConnector.close();
return responseAvailable.complete(value);
}

@Override
public boolean completeExceptionally(Throwable ex) {
newConnector.close();
return responseAvailable.completeExceptionally(ex);
}
});
}
} catch (IllegalArgumentException e) {
responseAvailable.completeExceptionally(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -416,11 +416,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)) {
nettyRequest.headers().add(HttpHeaderNames.HOST, jerseyRequest.getUri().getHost());
}
setHostHeader(jerseyRequest, nettyRequest);
}

if (jerseyRequest.hasEntity()) {
Expand Down Expand Up @@ -468,9 +464,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 @@ -620,4 +614,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
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, 2023 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2022, 2024 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand All @@ -24,16 +24,20 @@
import java.util.logging.Logger;

import jakarta.ws.rs.GET;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.ProcessingException;
import jakarta.ws.rs.client.Client;
import jakarta.ws.rs.client.ClientBuilder;
import jakarta.ws.rs.client.Entity;
import jakarta.ws.rs.client.WebTarget;
import jakarta.ws.rs.core.Application;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;

import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.client.ClientProperties;
import org.glassfish.jersey.client.RequestEntityProcessing;
import org.glassfish.jersey.logging.LoggingFeature;
import org.glassfish.jersey.netty.connector.internal.RedirectException;
import org.glassfish.jersey.server.ResourceConfig;
Expand All @@ -60,6 +64,11 @@ public String get() {
return "GET";
}

@POST
public String post() {
return "POST";
}

@GET
@Path("redirect")
public Response redirect() {
Expand All @@ -77,6 +86,12 @@ public Response loop() {
public Response redirect2() {
return Response.seeOther(URI.create(TEST_URL_REF.get() + "/redirect")).build();
}

@POST
@Path("status307")
public Response status307() {
return Response.temporaryRedirect(URI.create(TEST_URL_REF.get())).build();
}
}

@Override
Expand Down Expand Up @@ -169,4 +184,15 @@ public void testRedirectNoLimitReached() {
assertEquals(200, r.getStatus());
assertEquals("GET", r.readEntity(String.class));
}

@Test
public void testRedirect307PostBuffered() {
try (Response response = target("test/status307")
.property(ClientProperties.FOLLOW_REDIRECTS, true)
.property(ClientProperties.REQUEST_ENTITY_PROCESSING, RequestEntityProcessing.BUFFERED)
.request().post(Entity.entity("Something", MediaType.TEXT_PLAIN_TYPE))) {
assertEquals(200, response.getStatus());
assertEquals("POST", response.readEntity(String.class));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* Copyright (c) 2024 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/

package org.glassfish.jersey.netty.connector;

import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.test.JerseyTest;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;

import jakarta.ws.rs.GET;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.client.ClientBuilder;
import jakarta.ws.rs.client.Entity;
import jakarta.ws.rs.core.Application;
import jakarta.ws.rs.core.Context;
import jakarta.ws.rs.core.HttpHeaders;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;

public class HostHeaderTest extends JerseyTest {

private static final String HTTP_HEADER_NAME = "HTTP_PORT_INT";

@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);
String hostPort = headers.getHeaderString(HttpHeaders.HOST);
int indexColon = hostPort.indexOf(':');
if (indexColon != -1) {
hostPort = hostPort.substring(indexColon + 1);
}
if (sPort.equals(hostPort.trim())) {
return GET.class.getName();
} else {
return "Expected port " + sPort + " but found " + hostPort;
}
}
}

@Override
protected Application configure() {
return new ResourceConfig(HostHeaderTestEchoResource.class);
}

@Test
public void testHostHeaderAndPort() {
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)
.get()) {
MatcherAssert.assertThat(response.getStatus(), Matchers.is(200));
MatcherAssert.assertThat(response.readEntity(String.class), Matchers.is(GET.class.getName()));
}
}

@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()));
}
}

}
6 changes: 3 additions & 3 deletions core-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@
</goals>
</execution>
<execution>
<!-- Compile these files with jdk 8 and put them aside to be included in multirelase jar -->
<!-- Compile these files with jdk 8 and put them aside to be included in multi-release jar -->
<!-- Multi-release jar is built by jdk 11+, but these classes are buildable by jdk 8 only -->
<id>compile-2-java8</id>
<phase>process-resources</phase>
Expand Down Expand Up @@ -495,10 +495,10 @@
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/generated-sources/rsrc-gen/META-INF/versions/11/org/glassfish/jersey/internal/jsr166</outputDirectory>
<outputDirectory>${project.build.directory}/generated-sources/rsrc-gen/META-INF/versions/11/org/glassfish/jersey</outputDirectory>
<resources>
<resource>
<directory>${java11.sourceDirectory}/org/glassfish/jersey/internal/jsr166</directory>
<directory>${java11.sourceDirectory}/org/glassfish/jersey</directory>
</resource>
</resources>
</configuration>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2023 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2024 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018 Payara Foundation and/or its affiliates.
*
* This program and the accompanying materials are made available under the
Expand Down Expand Up @@ -121,7 +121,7 @@ public String toString(final T value) throws IllegalArgumentException {
@Singleton
public static class StringConstructor extends ParamConverterCompliance implements ParamConverterProvider {

private StringConstructor(boolean canReturnNull) {
protected StringConstructor(boolean canReturnNull) {
super(canReturnNull);
}

Expand Down Expand Up @@ -150,7 +150,7 @@ protected T _fromString(final String value) throws Exception {
@Singleton
public static class TypeValueOf extends ParamConverterCompliance implements ParamConverterProvider {

private TypeValueOf(boolean canReturnNull) {
protected TypeValueOf(boolean canReturnNull) {
super(canReturnNull);
}

Expand Down Expand Up @@ -178,7 +178,7 @@ public T _fromString(final String value) throws Exception {
@Singleton
public static class TypeFromString extends ParamConverterCompliance implements ParamConverterProvider {

private TypeFromString(boolean canReturnNull) {
protected TypeFromString(boolean canReturnNull) {
super(canReturnNull);
}

Expand Down Expand Up @@ -206,7 +206,7 @@ public T _fromString(final String value) throws Exception {
@Singleton
public static class TypeFromStringEnum extends TypeFromString {

private TypeFromStringEnum(boolean canReturnNull) {
protected TypeFromStringEnum(boolean canReturnNull) {
super(canReturnNull);
}

Expand All @@ -221,7 +221,7 @@ public <T> ParamConverter<T> getConverter(final Class<T> rawType,
@Singleton
public static class CharacterProvider extends ParamConverterCompliance implements ParamConverterProvider {

private CharacterProvider(boolean canReturnNull) {
protected CharacterProvider(boolean canReturnNull) {
super(canReturnNull);
}

Expand Down Expand Up @@ -266,7 +266,7 @@ public String toString(T value) {
@Singleton
public static class DateProvider extends ParamConverterCompliance implements ParamConverterProvider {

private DateProvider(boolean canReturnNull) {
protected DateProvider(boolean canReturnNull) {
super(canReturnNull);
}

Expand Down Expand Up @@ -309,7 +309,7 @@ public static class OptionalCustomProvider extends ParamConverterCompliance impl
// Delegates to this provider when the type of Optional is extracted.
private final InjectionManager manager;

public OptionalCustomProvider(InjectionManager manager, boolean canReturnNull) {
protected OptionalCustomProvider(InjectionManager manager, boolean canReturnNull) {
super(canReturnNull);
this.manager = manager;
}
Expand Down Expand Up @@ -365,6 +365,8 @@ public String toString(T value) throws IllegalArgumentException {
@Singleton
public static class OptionalProvider implements ParamConverterProvider {

protected OptionalProvider() {}

@Override
public <T> ParamConverter<T> getConverter(Class<T> rawType, Type genericType, Annotation[] annotations) {
final Optionals optionals = Optionals.getOptional(rawType);
Expand Down
Loading

0 comments on commit 525b915

Please sign in to comment.