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

Expect:100-Continue header handling #4576

Merged
merged 3 commits into from
Sep 24, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2019 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2020 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 @@ -405,6 +405,31 @@ public final class ClientProperties {
*/
public static final String REQUEST_ENTITY_PROCESSING = "jersey.config.client.request.entity.processing";

/**
* Registers 100-Continue value for Expect header processing to be set in HttpUrlConnector (default Jersey
senivam marked this conversation as resolved.
Show resolved Hide resolved
* connector)
*
* @since 2.32
*/
public static final String EXPECT_100_CONTINUE = "jersey.config.client.request.expect.100.continue.processing";

/**
* Property for threshold size for content length after which Expect:100-Continue header would be applied
* before the main request
*
* @since 2.32
*/
public static final String
EXPECT_100_CONTINUE_THRESHOLD_SIZE = "jersey.config.client.request.expect.100.continue.threshold.size";

/**
* Default threshold size (64kb) after which which Expect:100-Continue header would be applied before
* the main request
*
* @since 2.32
*/
public static final Long DEFAULT_EXPECT_100_CONTINUE_THRESHOLD_SIZE = 64000L;

private ClientProperties() {
// prevents instantiation
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright (c) 2020 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.client.http;

import org.glassfish.jersey.client.ClientProperties;

import javax.ws.rs.core.Feature;
import javax.ws.rs.core.FeatureContext;

public class Expect100ContinueFeature implements Feature {

private long thresholdSize;

public Expect100ContinueFeature() {
this(ClientProperties.DEFAULT_EXPECT_100_CONTINUE_THRESHOLD_SIZE);
}
public Expect100ContinueFeature(long thresholdSize) {
senivam marked this conversation as resolved.
Show resolved Hide resolved
this.thresholdSize = thresholdSize;
}

@Override
public boolean configure(FeatureContext configurableContext) {
if (configurableContext.getConfiguration().getProperty(
ClientProperties.EXPECT_100_CONTINUE) == null) {
configurableContext.property(ClientProperties.EXPECT_100_CONTINUE, Boolean.TRUE);
} else {
return false; //Expect:100-Continue handling is already done via property config
}
if (configurableContext.getConfiguration().getProperty(
ClientProperties.EXPECT_100_CONTINUE_THRESHOLD_SIZE) == null) {
configurableContext.property(ClientProperties.EXPECT_100_CONTINUE_THRESHOLD_SIZE, thresholdSize);
}
return true;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -333,9 +333,16 @@ private ClientResponse _apply(final ClientRequest request) throws IOException {
if (entity != null) {
RequestEntityProcessing entityProcessing = request.resolveProperty(
ClientProperties.REQUEST_ENTITY_PROCESSING, RequestEntityProcessing.class);
final Long expectContinueSizeThreshold = ClientProperties.getValue(
request.getConfiguration().getProperties(),
ClientProperties.EXPECT_100_CONTINUE_THRESHOLD_SIZE,
ClientProperties.DEFAULT_EXPECT_100_CONTINUE_THRESHOLD_SIZE, Long.class);

final long length = request.getLengthLong();
final boolean allowStreaming = length > expectContinueSizeThreshold
|| entityProcessing == RequestEntityProcessing.CHUNKED;

if (entityProcessing == null || entityProcessing != RequestEntityProcessing.BUFFERED) {
final long length = request.getLengthLong();
if (fixLengthStreaming && length > 0) {
uc.setFixedLengthStreamingMode(length);
} else if (entityProcessing == RequestEntityProcessing.CHUNKED) {
Expand All @@ -351,6 +358,8 @@ private ClientResponse _apply(final ClientRequest request) throws IOException {
}
}

processExpect100Continue(request, uc, allowStreaming);

request.setStreamProvider(contentLength -> {
setOutboundHeaders(request.getStringHeaders(), uc);
return uc.getOutputStream();
Expand Down Expand Up @@ -527,6 +536,20 @@ public Object run() throws NoSuchFieldException,
}
}

private void processExpect100Continue(ClientRequest request, HttpURLConnection uc, boolean allowStreaming) {

final Boolean expectContinueActivated = ClientProperties.getValue(request.getConfiguration().getProperties(),
senivam marked this conversation as resolved.
Show resolved Hide resolved
ClientProperties.EXPECT_100_CONTINUE, Boolean.class);

if (!Boolean.TRUE.equals(expectContinueActivated)
|| !("POST".equals(uc.getRequestMethod()) || "PUT".equals(uc.getRequestMethod()))
|| !allowStreaming
) {
return;
}
uc.setRequestProperty("Expect", "100-Continue");
}

@Override
public String getName() {
return "HttpUrlConnection " + AccessController.doPrivileged(PropertiesHelper.getSystemProperty("java.version"));
Expand Down
5 changes: 4 additions & 1 deletion tests/e2e-client/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) 2017, 2019 Oracle and/or its affiliates. All rights reserved.
Copyright (c) 2017, 2020 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 @@ -42,6 +42,9 @@
<reuseForks>false</reuseForks>
<enableAssertions>false</enableAssertions>
<skipTests>${skip.e2e}</skipTests>
<systemPropertyVariables>
<sun.net.http.allowRestrictedHeaders>true</sun.net.http.allowRestrictedHeaders>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/*
* Copyright (c) 2020 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.tests.e2e.client.httpurlconnector;

import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.client.ClientProperties;
import org.glassfish.jersey.client.HttpUrlConnectorProvider;
import org.glassfish.jersey.client.RequestEntityProcessing;
import org.glassfish.jersey.client.http.Expect100ContinueFeature;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.test.JerseyTest;
import org.junit.Test;

import javax.ws.rs.HeaderParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.Response;

import static org.junit.Assert.assertEquals;

public class Expect100ContinueTest extends JerseyTest {

private static final String RESOURCE_PATH = "expect";

@Path(RESOURCE_PATH)
public static class Expect100ContinueResource {

/**
* Disclamer - in tests we do not process 100-Continue response properly, so we operate 204 (no content)
* and 200 (ok) response codes in order to distinguish between proper and not proper Expect:100-Continue
* request handling.
*
* @param expect - Header value for Expect
*
* @return 200 (no expectations), 204 (Expect:100-Continue header processed)
*/
@POST
public Response publishResource(@HeaderParam("Expect") String expect) {
if ("100-Continue".equalsIgnoreCase(expect)) {
return Response.noContent().build();
}
return Response.ok().build();
}

}

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

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

@Test
public void testExpect100Continue() {
final Response response = target(RESOURCE_PATH).request().post(Entity.text("123456789012345678901234"
+ "56789012345678901234567890123456789012345678901234567890"));
assertEquals("Expected 200", 200, response.getStatus()); //no Expect header sent - responce OK
}

@Test
public void testExpect100ContinueChunked() {
final Response response = target(RESOURCE_PATH).register(new Expect100ContinueFeature())
.property(ClientProperties.REQUEST_ENTITY_PROCESSING,
RequestEntityProcessing.CHUNKED).request().post(Entity.text("123456789012345678901234"
+ "56789012345678901234567890123456789012345678901234567890"));
assertEquals("Expected 204", 204, response.getStatus()); //Expect header sent - No Content response
}

@Test
public void testExpect100ContinueBuffered() {
final Response response = target(RESOURCE_PATH).register(new Expect100ContinueFeature())
.property(ClientProperties.REQUEST_ENTITY_PROCESSING,
RequestEntityProcessing.BUFFERED).request().header(HttpHeaders.CONTENT_LENGTH, 65000L)
.post(Entity.text("12345678901234567890123456789012345678901234567890123456789012345678901234567890"));
assertEquals("Expected 204", 204, response.getStatus()); //Expect header sent - No Content response
}

@Test
public void testExpect100ContinueCustomLength() {
final Response response = target(RESOURCE_PATH).register(new Expect100ContinueFeature(100L))
.request().header(HttpHeaders.CONTENT_LENGTH, 101L)
.post(Entity.text("12345678901234567890123456789012345678901234567890123456789012345678901234567890"));
assertEquals("Expected 204", 204, response.getStatus()); //Expect header sent - No Content response
}

@Test
public void testExpect100ContinueCustomLengthWrong() {
final Response response = target(RESOURCE_PATH).register(new Expect100ContinueFeature(100L))
.request().header(HttpHeaders.CONTENT_LENGTH, 99L)
.post(Entity.text("12345678901234567890123456789012345678901234567890123456789012345678901234567890"));
assertEquals("Expected 200", 200, response.getStatus()); //Expect header sent - No Content response
}

@Test
public void testExpect100ContinueCustomLengthProperty() {
final Response response = target(RESOURCE_PATH)
.property(ClientProperties.EXPECT_100_CONTINUE_THRESHOLD_SIZE, 555L)
.register(new Expect100ContinueFeature())
.request().header(HttpHeaders.CONTENT_LENGTH, 666L)
.post(Entity.text("12345678901234567890123456789012345678901234567890123456789012345678901234567890"));
assertEquals("Expected 204", 204, response.getStatus()); //Expect header sent - No Content response
}

@Test
public void testExpect100ContinueRegisterViaCustomProperty() {
final Response response = target(RESOURCE_PATH)
.property(ClientProperties.EXPECT_100_CONTINUE_THRESHOLD_SIZE, 43L)
.property(ClientProperties.EXPECT_100_CONTINUE, Boolean.TRUE)
.request().header(HttpHeaders.CONTENT_LENGTH, 44L)
.post(Entity.text("12345678901234567890123456789012345678901234567890123456789012345678901234567890"));
assertEquals("Expected 204", 204, response.getStatus()); //Expect header sent - No Content response
}
}