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 current master into 3.x #4691

Merged
merged 8 commits into from
Jan 21, 2021
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
6 changes: 5 additions & 1 deletion bundles/apidocs/pom.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

Copyright (c) 2012, 2020 Oracle and/or its affiliates. All rights reserved.
Copyright (c) 2012, 2021 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 Down Expand Up @@ -248,6 +248,10 @@
</goals>
<configuration>
<includeDependencySources>true</includeDependencySources>
<sourceFileExcludes>
<fileExclude>META-INF/versions/12/org/glassfish/jersey/wadl/doclet/*.java</fileExclude>
<fileExclude>org/glassfish/jersey/helidon/connector/*.java</fileExclude>
</sourceFileExcludes>
<dependencySourceIncludes>
<dependencySourceInclude>org.glassfish.jersey.*:*</dependencySourceInclude>
</dependencySourceIncludes>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2021 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 Down Expand Up @@ -464,7 +464,8 @@ private boolean isInseparableHeader() {
/* Authenticate headers contain comma separated list of properties, which would be normally treated as separate header
values */
return Constants.WWW_AUTHENTICATE.equalsIgnoreCase(headerParsingState.headerName)
|| Constants.PROXY_AUTHENTICATE.equalsIgnoreCase(headerParsingState.headerName);
|| Constants.PROXY_AUTHENTICATE.equalsIgnoreCase(headerParsingState.headerName)
|| HttpHeaders.SET_COOKIE.equalsIgnoreCase(headerParsingState.headerName);

}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2021 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 @@ -17,6 +17,7 @@
package org.glassfish.jersey.jdk.connector.internal;

import java.net.CookiePolicy;
import java.util.List;

import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
Expand All @@ -33,7 +34,6 @@
import org.glassfish.jersey.jdk.connector.JdkConnectorProvider;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.test.JerseyTest;

import org.junit.Test;

import static org.junit.Assert.assertEquals;
Expand All @@ -53,6 +53,17 @@ public Response get(@Context HttpHeaders h) {
String e = (c == null) ? "NO-COOKIE" : c.getValue();
return Response.ok(e).cookie(new NewCookie("name", "value")).build();
}

@Path("/issue4678")
@GET
public Response issue4678(@Context HttpHeaders h) {
// Read the cookie
Cookie c = h.getCookies().get("foo");
// Write the value in a new cookie foo2. So we test cookies in both ways.
return Response.ok().header(HttpHeaders.SET_COOKIE,
"foo2=" + c.getValue() + "; expires=Wed, 10-Feb-2021 16:16:26 GMT; HttpOnly; Path=/; SameSite=Lax")
.build();
}
}

@Override
Expand Down Expand Up @@ -82,4 +93,18 @@ public void testDisabledCookies() {
assertEquals("NO-COOKIE", target.request().get(String.class));
assertEquals("NO-COOKIE", target.request().get(String.class));
}

@Test
public void testIssue4678() {
Response response = target("/CookieResource/issue4678")
.request().header(HttpHeaders.COOKIE,
"foo=bar; expires=Wed, 10-Feb-2021 16:16:26 GMT; HttpOnly; Path=/; SameSite=Lax")
.get();
// Issue 4678 happens here. HttpParser splits the headers value by comma.
List<Object> setCookies = response.getHeaders().get(HttpHeaders.SET_COOKIE);
assertEquals("Expected 1 cookie, but it received: " + setCookies, 1, setCookies.size());
NewCookie newCookie = response.getCookies().get("foo2");
assertEquals("bar", newCookie.getValue());
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2018 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2021 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 Down Expand Up @@ -75,6 +75,16 @@ private JettyClientProperties() {
public static final String ENABLE_SSL_HOSTNAME_VERIFICATION =
"jersey.config.jetty.client.enableSslHostnameVerification";

/**
* Overrides the default Jetty synchronous listener response max buffer size.
* In practise, this allows you to read larger responses.
* Size in bytes.
* <p/>
* If the property is absent, the value is such as specified by Jetty (currently 2MiB).
*/
public static final String SYNC_LISTENER_RESPONSE_MAX_SIZE =
"jersey.config.jetty.client.syncListenerResponseMaxSize";

/**
* Get the value of the specified property.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2021 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 @@ -23,7 +23,6 @@
import java.io.InputStream;
import java.io.OutputStream;
import java.net.CookieStore;
import java.net.InetSocketAddress;
import java.net.URI;
import java.nio.ByteBuffer;
import java.util.ArrayList;
Expand All @@ -36,7 +35,6 @@
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand All @@ -48,15 +46,14 @@

import javax.net.ssl.SSLContext;

import org.eclipse.jetty.client.ConnectionPool;
import org.eclipse.jetty.client.HttpClientTransport;
import org.eclipse.jetty.client.HttpDestination;
import org.eclipse.jetty.client.HttpRequest;
import org.eclipse.jetty.client.Origin;
import org.eclipse.jetty.client.http.HttpClientTransportOverHTTP;
import org.eclipse.jetty.io.ClientConnector;
import org.eclipse.jetty.io.Connection;
import org.eclipse.jetty.io.EndPoint;
import org.eclipse.jetty.client.util.BasicAuthentication;
import org.eclipse.jetty.client.util.BytesContentProvider;
import org.eclipse.jetty.client.util.FutureResponseListener;
import org.eclipse.jetty.client.util.OutputStreamContentProvider;
import org.glassfish.jersey.client.ClientProperties;
import org.glassfish.jersey.client.ClientRequest;
import org.glassfish.jersey.client.ClientResponse;
Expand All @@ -77,9 +74,6 @@
import org.eclipse.jetty.client.api.Request;
import org.eclipse.jetty.client.api.Response;
import org.eclipse.jetty.client.api.Result;
import org.eclipse.jetty.client.util.BasicAuthentication;
import org.eclipse.jetty.client.util.BytesContentProvider;
import org.eclipse.jetty.client.util.OutputStreamContentProvider;
import org.eclipse.jetty.http.HttpField;
import org.eclipse.jetty.http.HttpFields;
import org.eclipse.jetty.http.HttpHeader;
Expand Down Expand Up @@ -142,6 +136,7 @@ class JettyConnector implements Connector {
private final HttpClient client;
private final CookieStore cookieStore;
private final Configuration configuration;
private final Optional<Integer> syncListenerResponseMaxSize;

/**
* Create the new Jetty client connector.
Expand Down Expand Up @@ -214,6 +209,16 @@ class JettyConnector implements Connector {
client.setCookieStore(new HttpCookieStore.Empty());
}

final Object slResponseMaxSize = configuration.getProperties()
.get(JettyClientProperties.SYNC_LISTENER_RESPONSE_MAX_SIZE);
if (slResponseMaxSize != null && slResponseMaxSize instanceof Integer
&& (Integer) slResponseMaxSize > 0) {
this.syncListenerResponseMaxSize = Optional.of((Integer) slResponseMaxSize);
}
else {
this.syncListenerResponseMaxSize = Optional.empty();
}

try {
client.start();
} catch (final Exception e) {
Expand Down Expand Up @@ -264,7 +269,16 @@ public ClientResponse apply(final ClientRequest jerseyRequest) throws Processing
}

try {
final ContentResponse jettyResponse = jettyRequest.send();
final ContentResponse jettyResponse;
if (!syncListenerResponseMaxSize.isPresent()) {
jettyResponse = jettyRequest.send();
}
else {
final FutureResponseListener listener
= new FutureResponseListener(jettyRequest, syncListenerResponseMaxSize.get());
jettyRequest.send(listener);
jettyResponse = listener.get();
}
HeaderUtils.checkHeaderChanges(clientHeadersSnapshot, jerseyRequest.getHeaders(),
JettyConnector.this.getClass().getName(), jerseyRequest.getConfiguration());

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
/*
* Copyright (c) 2013, 2021 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.jetty.connector;

import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.client.ClientProperties;
import org.glassfish.jersey.logging.LoggingFeature;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.test.JerseyTest;
import org.junit.Test;

import jakarta.ws.rs.GET;
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.WebTarget;
import jakarta.ws.rs.core.Application;
import jakarta.ws.rs.core.Response;
import java.net.URI;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException;
import java.util.logging.Logger;

import static org.hamcrest.CoreMatchers.instanceOf;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

/**
* Default synchronous jetty client implementation has a hard response size limit of 2MiB.
* When response is too big, a processing exception is thrown.
* The original code path was left to preserve this behaviour but could be removed
* and reworked in the future with a custom listener like async path.
*
* This tests the previous behavior with large payloads (>2MiB), the new size override (4MiB)
* and very big payloads (>4MiB).
*
* @author cen1 (cen.is.imba at gmail.com)
*/
public class SyncResponseSizeTest extends JerseyTest {

private static final Logger LOGGER = Logger.getLogger(SyncResponseSizeTest.class.getName());

private static final int maxBufferSize = 4 * 1024 * 1024; //4 MiB

@Path("/test")
public static class TimeoutResource {

private static final byte[] data = new byte[maxBufferSize];

static {
Byte b = "a".getBytes()[0];
for (int i = 0; i < maxBufferSize; i++) data[i] = b.byteValue();
}

@GET
@Path("/small")
public String getSmall() {
return "GET";
}

@GET
@Path("/big")
public String getBig() {
return new String(data);
}

@GET
@Path("/verybig")
public String getVeryBig() {
return new String(data) + "a";
}
}

@Override
protected Application configure() {
ResourceConfig config = new ResourceConfig(TimeoutResource.class);
config.register(new LoggingFeature(LOGGER, LoggingFeature.Verbosity.PAYLOAD_ANY));
return config;
}

@Override
protected void configureClient(ClientConfig config) {
config.connectorProvider(new JettyConnectorProvider());
}

@Test
public void testDefaultSmall() {
Response r = target("test/small").request().get();
assertEquals(200, r.getStatus());
assertEquals("GET", r.readEntity(String.class));
}

@Test
public void testDefaultTooBig() {
final URI u = target().getUri();
ClientConfig config = new ClientConfig().property(ClientProperties.READ_TIMEOUT, 1_000);
config.connectorProvider(new JettyConnectorProvider());

Client c = ClientBuilder.newClient(config);
WebTarget t = c.target(u);
try {
t.path("test/big").request().get();
fail("Exception expected.");
} catch (ProcessingException e) {
// Buffering capacity ... exceeded.
assertTrue(ExecutionException.class.isInstance(e.getCause()));
assertTrue(IllegalArgumentException.class.isInstance(e.getCause().getCause()));
} finally {
c.close();
}
}

@Test
public void testCustomBig() {
final URI u = target().getUri();
ClientConfig config = new ClientConfig().property(ClientProperties.READ_TIMEOUT, 1_000);
config.connectorProvider(new JettyConnectorProvider());
config.property(JettyClientProperties.SYNC_LISTENER_RESPONSE_MAX_SIZE, maxBufferSize);

Client c = ClientBuilder.newClient(config);
WebTarget t = c.target(u);
try {
Response r = t.path("test/big").request().get();
String p = r.readEntity(String.class);
assertEquals(p.length(), maxBufferSize);
} catch (ProcessingException e) {
assertThat("Unexpected processing exception cause",
e.getCause(), instanceOf(TimeoutException.class));
} finally {
c.close();
}
}

@Test
public void testCustomTooBig() {
final URI u = target().getUri();
ClientConfig config = new ClientConfig().property(ClientProperties.READ_TIMEOUT, 1_000);
config.connectorProvider(new JettyConnectorProvider());
config.property(JettyClientProperties.SYNC_LISTENER_RESPONSE_MAX_SIZE, maxBufferSize);

Client c = ClientBuilder.newClient(config);
WebTarget t = c.target(u);
try {
t.path("test/verybig").request().get();
fail("Exception expected.");
} catch (ProcessingException e) {
// Buffering capacity ... exceeded.
assertTrue(ExecutionException.class.isInstance(e.getCause()));
assertTrue(IllegalArgumentException.class.isInstance(e.getCause().getCause()));
} finally {
c.close();
}
}
}
Loading