You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2023/10/12 10:16:11 UTC

[httpcomponents-core] 04/11: HTTPCORE-756: Expect-Continue handshake improvements per RFC 9110 section 10.1.1 * ignore Expect header in requests older than HTTP/1.1 * Servers to respond with 417 in case of unexpected Expect token

This is an automated email from the ASF dual-hosted git repository.

olegk pushed a commit to branch 5.3.x
in repository https://gitbox.apache.org/repos/asf/httpcomponents-core.git

commit 39fd7f26d4fc55020d0b44f075e3fc4bb6017306
Author: Oleg Kalnichevski <ol...@apache.org>
AuthorDate: Thu Oct 5 12:57:48 2023 +0200

    HTTPCORE-756: Expect-Continue handshake improvements per RFC 9110 section 10.1.1
    * ignore Expect header in requests older than HTTP/1.1
    * Servers to respond with 417 in case of unexpected Expect token
---
 .../BasicHttpServerExpectationDecorator.java       |  13 ++-
 .../io/support/HttpServerExpectationFilter.java    |  16 +--
 .../nio/support/AsyncServerExpectationFilter.java  |  32 +++---
 .../hc/core5/http/support/ExpectSupport.java       |  71 +++++++++++++
 .../apache/hc/core5/http/support/Expectation.java  |  42 ++++++++
 .../hc/core5/http/support/TestExpectSupport.java   | 110 +++++++++++++++++++++
 6 files changed, 258 insertions(+), 26 deletions(-)

diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/io/support/BasicHttpServerExpectationDecorator.java b/httpcore5/src/main/java/org/apache/hc/core5/http/io/support/BasicHttpServerExpectationDecorator.java
index d45a79184..0ed496195 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/http/io/support/BasicHttpServerExpectationDecorator.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/http/io/support/BasicHttpServerExpectationDecorator.java
@@ -31,13 +31,12 @@ import java.io.IOException;
 
 import org.apache.hc.core5.http.ClassicHttpRequest;
 import org.apache.hc.core5.http.ClassicHttpResponse;
-import org.apache.hc.core5.http.Header;
-import org.apache.hc.core5.http.HeaderElements;
 import org.apache.hc.core5.http.HttpException;
-import org.apache.hc.core5.http.HttpHeaders;
 import org.apache.hc.core5.http.HttpStatus;
 import org.apache.hc.core5.http.io.HttpServerRequestHandler;
 import org.apache.hc.core5.http.message.BasicClassicHttpResponse;
+import org.apache.hc.core5.http.support.ExpectSupport;
+import org.apache.hc.core5.http.support.Expectation;
 import org.apache.hc.core5.http.protocol.HttpContext;
 import org.apache.hc.core5.util.Args;
 
@@ -74,8 +73,8 @@ public class BasicHttpServerExpectationDecorator implements HttpServerRequestHan
             final ClassicHttpRequest request,
             final ResponseTrigger responseTrigger,
             final HttpContext context) throws HttpException, IOException {
-        final Header expect = request.getFirstHeader(HttpHeaders.EXPECT);
-        if (expect != null && HeaderElements.CONTINUE.equalsIgnoreCase(expect.getValue())) {
+        final Expectation expectation = ExpectSupport.parse(request, request.getEntity());
+        if (expectation == Expectation.CONTINUE) {
             final ClassicHttpResponse response = verify(request, context);
             if (response == null) {
                 responseTrigger.sendInformation(new BasicClassicHttpResponse(HttpStatus.SC_CONTINUE));
@@ -83,6 +82,10 @@ public class BasicHttpServerExpectationDecorator implements HttpServerRequestHan
                 responseTrigger.submitResponse(response);
                 return;
             }
+        } else if (expectation == Expectation.UNKNOWN) {
+            final ClassicHttpResponse expectationFailed = new BasicClassicHttpResponse(HttpStatus.SC_EXPECTATION_FAILED);
+            responseTrigger.submitResponse(expectationFailed);
+            return;
         }
         requestHandler.handle(request, responseTrigger, context);
     }
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/io/support/HttpServerExpectationFilter.java b/httpcore5/src/main/java/org/apache/hc/core5/http/io/support/HttpServerExpectationFilter.java
index f499a12d2..d4035237c 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/http/io/support/HttpServerExpectationFilter.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/http/io/support/HttpServerExpectationFilter.java
@@ -32,16 +32,15 @@ import org.apache.hc.core5.annotation.Contract;
 import org.apache.hc.core5.annotation.ThreadingBehavior;
 import org.apache.hc.core5.http.ClassicHttpRequest;
 import org.apache.hc.core5.http.ClassicHttpResponse;
-import org.apache.hc.core5.http.Header;
-import org.apache.hc.core5.http.HeaderElements;
 import org.apache.hc.core5.http.HttpEntity;
 import org.apache.hc.core5.http.HttpException;
-import org.apache.hc.core5.http.HttpHeaders;
 import org.apache.hc.core5.http.HttpResponse;
 import org.apache.hc.core5.http.HttpStatus;
 import org.apache.hc.core5.http.io.HttpFilterChain;
 import org.apache.hc.core5.http.io.HttpFilterHandler;
 import org.apache.hc.core5.http.message.BasicClassicHttpResponse;
+import org.apache.hc.core5.http.support.ExpectSupport;
+import org.apache.hc.core5.http.support.Expectation;
 import org.apache.hc.core5.http.protocol.HttpContext;
 
 /**
@@ -83,9 +82,8 @@ public class HttpServerExpectationFilter implements HttpFilterHandler {
             final HttpFilterChain.ResponseTrigger responseTrigger,
             final HttpContext context,
             final HttpFilterChain chain) throws HttpException, IOException {
-        final Header expect = request.getFirstHeader(HttpHeaders.EXPECT);
-        final boolean expectContinue = expect != null && HeaderElements.CONTINUE.equalsIgnoreCase(expect.getValue());
-        if (expectContinue) {
+        final Expectation expectation = ExpectSupport.parse(request, request.getEntity());
+        if (expectation == Expectation.CONTINUE) {
             final boolean verified = verify(request, context);
             if (verified) {
                 responseTrigger.sendInformation(new BasicClassicHttpResponse(HttpStatus.SC_CONTINUE));
@@ -96,6 +94,12 @@ public class HttpServerExpectationFilter implements HttpFilterHandler {
                 responseTrigger.submitResponse(expectationFailed);
                 return;
             }
+        } else if (expectation == Expectation.UNKNOWN) {
+            final ClassicHttpResponse expectationFailed = new BasicClassicHttpResponse(HttpStatus.SC_EXPECTATION_FAILED);
+            final HttpEntity responseContent = generateResponseContent(expectationFailed);
+            expectationFailed.setEntity(responseContent);
+            responseTrigger.submitResponse(expectationFailed);
+            return;
         }
         chain.proceed(request, responseTrigger, context);
     }
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/nio/support/AsyncServerExpectationFilter.java b/httpcore5/src/main/java/org/apache/hc/core5/http/nio/support/AsyncServerExpectationFilter.java
index bfa05a2b0..e11893f58 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/http/nio/support/AsyncServerExpectationFilter.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/http/nio/support/AsyncServerExpectationFilter.java
@@ -31,10 +31,7 @@ import java.io.IOException;
 import org.apache.hc.core5.annotation.Contract;
 import org.apache.hc.core5.annotation.ThreadingBehavior;
 import org.apache.hc.core5.http.EntityDetails;
-import org.apache.hc.core5.http.Header;
-import org.apache.hc.core5.http.HeaderElements;
 import org.apache.hc.core5.http.HttpException;
-import org.apache.hc.core5.http.HttpHeaders;
 import org.apache.hc.core5.http.HttpRequest;
 import org.apache.hc.core5.http.HttpResponse;
 import org.apache.hc.core5.http.HttpStatus;
@@ -43,6 +40,8 @@ import org.apache.hc.core5.http.nio.AsyncDataConsumer;
 import org.apache.hc.core5.http.nio.AsyncEntityProducer;
 import org.apache.hc.core5.http.nio.AsyncFilterChain;
 import org.apache.hc.core5.http.nio.AsyncFilterHandler;
+import org.apache.hc.core5.http.support.ExpectSupport;
+import org.apache.hc.core5.http.support.Expectation;
 import org.apache.hc.core5.http.protocol.HttpContext;
 
 /**
@@ -66,19 +65,22 @@ public class AsyncServerExpectationFilter implements AsyncFilterHandler {
             final HttpContext context,
             final AsyncFilterChain.ResponseTrigger responseTrigger,
             final AsyncFilterChain chain) throws HttpException, IOException {
-        if (entityDetails != null) {
-            final Header h = request.getFirstHeader(HttpHeaders.EXPECT);
-            if (h != null && HeaderElements.CONTINUE.equalsIgnoreCase(h.getValue())) {
-                final boolean verified = verify(request, context);
-                if (verified) {
-                    responseTrigger.sendInformation(new BasicHttpResponse(HttpStatus.SC_CONTINUE));
-                } else {
-                    final HttpResponse expectationFailed = new BasicHttpResponse(HttpStatus.SC_EXPECTATION_FAILED);
-                    final AsyncEntityProducer responseContentProducer = generateResponseContent(expectationFailed);
-                    responseTrigger.submitResponse(expectationFailed, responseContentProducer);
-                    return null;
-                }
+        final Expectation expectation = ExpectSupport.parse(request, entityDetails);
+        if (expectation == Expectation.CONTINUE) {
+            final boolean verified = verify(request, context);
+            if (verified) {
+                responseTrigger.sendInformation(new BasicHttpResponse(HttpStatus.SC_CONTINUE));
+            } else {
+                final HttpResponse expectationFailed = new BasicHttpResponse(HttpStatus.SC_EXPECTATION_FAILED);
+                final AsyncEntityProducer responseContentProducer = generateResponseContent(expectationFailed);
+                responseTrigger.submitResponse(expectationFailed, responseContentProducer);
+                return null;
             }
+        } else if (expectation == Expectation.UNKNOWN) {
+            final HttpResponse expectationFailed = new BasicHttpResponse(HttpStatus.SC_EXPECTATION_FAILED);
+            final AsyncEntityProducer responseContentProducer = generateResponseContent(expectationFailed);
+            responseTrigger.submitResponse(expectationFailed, responseContentProducer);
+            return null;
         }
         return chain.proceed(request, entityDetails, context, responseTrigger);
     }
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/support/ExpectSupport.java b/httpcore5/src/main/java/org/apache/hc/core5/http/support/ExpectSupport.java
new file mode 100644
index 000000000..ae83814e0
--- /dev/null
+++ b/httpcore5/src/main/java/org/apache/hc/core5/http/support/ExpectSupport.java
@@ -0,0 +1,71 @@
+/*
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+package org.apache.hc.core5.http.support;
+
+import java.util.concurrent.atomic.AtomicReference;
+
+import org.apache.hc.core5.annotation.Internal;
+import org.apache.hc.core5.http.EntityDetails;
+import org.apache.hc.core5.http.HeaderElements;
+import org.apache.hc.core5.http.HttpHeaders;
+import org.apache.hc.core5.http.HttpRequest;
+import org.apache.hc.core5.http.HttpVersion;
+import org.apache.hc.core5.http.ProtocolException;
+import org.apache.hc.core5.http.message.MessageSupport;
+import org.apache.hc.core5.util.TextUtils;
+
+/**
+ * Server side Expect header handling support.
+ *
+ * @since 5.3
+ */
+@Internal
+public class ExpectSupport {
+
+    public static Expectation parse(
+            final HttpRequest request,
+            final EntityDetails entityDetails) throws ProtocolException {
+        // Ignore Expect header in messages older than HTTP/1.1
+        if (request.getVersion() != null && request.getVersion().lessEquals(HttpVersion.HTTP_1_0)) {
+            return null;
+        }
+        final AtomicReference<Expectation> expectationRef = new AtomicReference<>();
+        MessageSupport.parseTokens(request, HttpHeaders.EXPECT, t -> {
+            if (t.equalsIgnoreCase(HeaderElements.CONTINUE)) {
+                expectationRef.compareAndSet(null, Expectation.CONTINUE);
+            } else if (!TextUtils.isBlank(t)) {
+                expectationRef.set(Expectation.UNKNOWN);
+            }
+        });
+        final Expectation expectation = expectationRef.get();
+        if (expectation == Expectation.CONTINUE && entityDetails == null) {
+            throw new ProtocolException("Expect-Continue request without an enclosed entity");
+        }
+        return expectation;
+    }
+}
+
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/support/Expectation.java b/httpcore5/src/main/java/org/apache/hc/core5/http/support/Expectation.java
new file mode 100644
index 000000000..ebcce57f0
--- /dev/null
+++ b/httpcore5/src/main/java/org/apache/hc/core5/http/support/Expectation.java
@@ -0,0 +1,42 @@
+/*
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+package org.apache.hc.core5.http.support;
+
+import org.apache.hc.core5.annotation.Internal;
+
+/**
+ * Message expectations. Presently only {@literal 100-continue} is recognized.
+ *
+ * @since 5.3
+ */
+@Internal
+public enum Expectation {
+
+    CONTINUE, UNKNOWN
+
+}
+
diff --git a/httpcore5/src/test/java/org/apache/hc/core5/http/support/TestExpectSupport.java b/httpcore5/src/test/java/org/apache/hc/core5/http/support/TestExpectSupport.java
new file mode 100644
index 000000000..ba6c66d18
--- /dev/null
+++ b/httpcore5/src/test/java/org/apache/hc/core5/http/support/TestExpectSupport.java
@@ -0,0 +1,110 @@
+/*
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.hc.core5.http.support;
+
+import org.apache.hc.core5.http.ContentType;
+import org.apache.hc.core5.http.HttpVersion;
+import org.apache.hc.core5.http.ProtocolException;
+import org.apache.hc.core5.http.impl.BasicEntityDetails;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+public class TestExpectSupport {
+
+    @Test
+    public void testExpectParsingBasics() throws Exception {
+        Assertions.assertEquals(Expectation.CONTINUE,
+                ExpectSupport.parse(
+                        BasicRequestBuilder.post()
+                                .addHeader("Expect", "100-continue")
+                                .build(),
+                        new BasicEntityDetails(100, ContentType.TEXT_PLAIN)));
+    }
+
+    @Test
+    public void testExpectParsingTolerateEmptyTokens() throws Exception {
+        Assertions.assertEquals(Expectation.CONTINUE,
+                ExpectSupport.parse(
+                        BasicRequestBuilder.post()
+                                .addHeader("Expect", ",,,")
+                                .addHeader("Expect", ",100-continue")
+                                .addHeader("Expect", ",,,")
+                                .build(),
+                        new BasicEntityDetails(100, ContentType.TEXT_PLAIN)));
+    }
+
+    @Test
+    public void testExpectParsingMissingEntity() throws Exception {
+        Assertions.assertThrows(ProtocolException.class,
+                () -> ExpectSupport.parse(
+                        BasicRequestBuilder.post()
+                                .addHeader("Expect", "100-continue")
+                                .build(),
+                        null));
+    }
+
+    @Test
+    public void testExpectParsingUnknownExpectation() throws Exception {
+        Assertions.assertEquals(Expectation.UNKNOWN,
+                ExpectSupport.parse(
+                        BasicRequestBuilder.post()
+                                .addHeader("Expect", "whatever")
+                                .addHeader("Expect", "100-continue")
+                                .build(),
+                        new BasicEntityDetails(100, ContentType.TEXT_PLAIN)));
+    }
+
+    @Test
+    public void testExpectParsingUnknownExpectation2() throws Exception {
+        Assertions.assertEquals(Expectation.UNKNOWN,
+                ExpectSupport.parse(
+                        BasicRequestBuilder.post()
+                                .addHeader("Expect", "100-continue, whatever")
+                                .build(),
+                        new BasicEntityDetails(100, ContentType.TEXT_PLAIN)));
+    }
+
+    @Test
+    public void testExpectParsingNoExpectation() throws Exception {
+        Assertions.assertNull(ExpectSupport.parse(
+                        BasicRequestBuilder.post()
+                                .build(),
+                        new BasicEntityDetails(100, ContentType.TEXT_PLAIN)));
+    }
+
+    @Test
+    public void testExpectParsingIgnoreHTTP10() throws Exception {
+        Assertions.assertNull(ExpectSupport.parse(
+                BasicRequestBuilder.post()
+                        .setVersion(HttpVersion.HTTP_1_0)
+                        .addHeader("Expect", "100-continue")
+                        .build(),
+                new BasicEntityDetails(100, ContentType.TEXT_PLAIN)));
+    }
+
+}