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

merge of the actual 3.0 into the 3.1 #5804

Merged
merged 15 commits into from
Nov 20, 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 @@ -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()));
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
* Generic wrapper template for InputStream.
Expand Down Expand Up @@ -54,6 +55,22 @@ public int read(byte[] b, int off, int len) throws IOException {
return getWrappedIOE().read(b, off, len);
}

@Override
public byte[] readAllBytes() throws IOException {
return getWrappedIOE().readAllBytes();
}

@Override
public int readNBytes(byte[] b, int off, int len) throws IOException {
return getWrappedIOE().readNBytes(b, off, len);
}

@Override
public long transferTo(OutputStream out) throws IOException {
return getWrappedIOE().transferTo(out);
}


@Override
public long skip(long n) throws IOException {
return getWrappedIOE().skip(n);
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 @@ -125,7 +125,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 @@ -154,7 +154,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 @@ -182,7 +182,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 @@ -210,7 +210,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 @@ -225,7 +225,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 @@ -270,7 +270,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 @@ -346,7 +346,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 @@ -402,6 +402,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