diff --git a/java/org/apache/coyote/http2/Stream.java b/java/org/apache/coyote/http2/Stream.java index 6893955b05bc..26c7d8ea3f4b 100644 --- a/java/org/apache/coyote/http2/Stream.java +++ b/java/org/apache/coyote/http2/Stream.java @@ -52,6 +52,7 @@ import org.apache.tomcat.util.http.parser.Host; import org.apache.tomcat.util.http.parser.HttpParser; import org.apache.tomcat.util.http.parser.Priority; +import org.apache.tomcat.util.http.parser.TokenList; import org.apache.tomcat.util.net.ApplicationBufferHandler; import org.apache.tomcat.util.net.SocketEvent; import org.apache.tomcat.util.net.WriteBuffer; @@ -552,8 +553,21 @@ public final void emitHeader(String name, String value) throws HpackException { break; } default: { - if ("expect".equals(name) && "100-continue".equals(value)) { - coyoteRequest.setExpectation(true); + if ("expect".equals(name)) { + // RFC 9110, section 10.1.1: the field value is a case-insensitive list of expectations + if ("100-continue".equalsIgnoreCase(value)) { + coyoteRequest.setExpectation(true); + } else { + Set expectations = new HashSet<>(); + try { + TokenList.parseTokenList(new StringReader(value), expectations); + } catch (IOException ioe) { + // Not possible with StringReader + } + if (expectations.contains("100-continue")) { + coyoteRequest.setExpectation(true); + } + } } if (pseudoHeader) { headerException = new StreamException( diff --git a/test/org/apache/coyote/http2/Http2TestBase.java b/test/org/apache/coyote/http2/Http2TestBase.java index 42a8d4e18ec3..48d2b596f109 100644 --- a/test/org/apache/coyote/http2/Http2TestBase.java +++ b/test/org/apache/coyote/http2/Http2TestBase.java @@ -383,6 +383,13 @@ protected void buildPostRequest(byte[] headersFrameHeader, ByteBuffer headersPay protected void buildPostRequest(byte[] headersFrameHeader, ByteBuffer headersPayload, boolean useExpectation, String contentType, long contentLength, String path, byte[] dataFrameHeader, ByteBuffer dataPayload, byte[] padding, boolean withTrailers, int streamId) { + buildPostRequest(headersFrameHeader, headersPayload, useExpectation ? "100-continue" : null, contentType, + contentLength, path, dataFrameHeader, dataPayload, padding, withTrailers, streamId); + } + + protected void buildPostRequest(byte[] headersFrameHeader, ByteBuffer headersPayload, String expectation, + String contentType, long contentLength, String path, byte[] dataFrameHeader, ByteBuffer dataPayload, + byte[] padding, boolean withTrailers, int streamId) { MimeHeaders headers = new MimeHeaders(); headers.addValue(":method").setString(Method.POST); @@ -393,8 +400,8 @@ protected void buildPostRequest(byte[] headersFrameHeader, ByteBuffer headersPay } headers.addValue(":path").setString(path); headers.addValue(":authority").setString("localhost:" + getPort()); - if (useExpectation) { - headers.addValue("expect").setString("100-continue"); + if (expectation != null) { + headers.addValue("expect").setString(expectation); } if (contentType != null) { headers.addValue("content-type").setString(contentType); diff --git a/test/org/apache/coyote/http2/TestHttp2Section_8_1.java b/test/org/apache/coyote/http2/TestHttp2Section_8_1.java index 55cda91103ef..d3e2de61cfae 100644 --- a/test/org/apache/coyote/http2/TestHttp2Section_8_1.java +++ b/test/org/apache/coyote/http2/TestHttp2Section_8_1.java @@ -152,6 +152,43 @@ public void setContinueHandlingResponsePolicy(ContinueResponseTiming policy) thr @Test public void testSendAck() throws Exception { + doTestSendAck("100-continue", true); + } + + + @Test + public void testSendAckMixedCase() throws Exception { + // RFC 9110, section 10.1.1: the Expect field value is case-insensitive + doTestSendAck("100-Continue", true); + } + + + @Test + public void testSendAckList() throws Exception { + // RFC 9110, section 10.1.1: the Expect field value is a list of expectations + doTestSendAck("foo, 100-continue", true); + } + + + @Test + public void testSendAckListWithEmptyElements() throws Exception { + doTestSendAck(",100-continue, ,foo", true); + } + + + @Test + public void testNoAckForOtherExpectation() throws Exception { + doTestSendAck("foo", false); + } + + + @Test + public void testNoAckForContinueWithParameter() throws Exception { + doTestSendAck("100-continue=1", false); + } + + + private void doTestSendAck(String expectation, boolean ack) throws Exception { // makes a request that expects a 100 Continue response and verifies // that the 100 Continue response is received. This does not check // that the correct ContinueHandlingResponsePolicy was followed, just @@ -167,16 +204,19 @@ public void testSendAck() throws Exception { byte[] dataFrameHeader = new byte[9]; ByteBuffer dataPayload = ByteBuffer.allocate(256); - buildPostRequest(headersFrameHeader, headersPayload, true, null, -1, "/simple", dataFrameHeader, dataPayload, - null, false, 3); + buildPostRequest(headersFrameHeader, headersPayload, expectation, null, -1, "/simple", dataFrameHeader, + dataPayload, null, false, 3); // Write the headers writeFrame(headersFrameHeader, headersPayload); - parser.readFrame(); + if (ack) { + parser.readFrame(); - Assert.assertEquals("3-HeadersStart\n" + "3-Header-[:status]-[100]\n" + "3-HeadersEnd\n", output.getTrace()); - output.clearTrace(); + Assert.assertEquals("3-HeadersStart\n" + "3-Header-[:status]-[100]\n" + "3-HeadersEnd\n", + output.getTrace()); + output.clearTrace(); + } // Write the body writeFrame(dataFrameHeader, dataPayload); diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml index 40cee9a06286..6911897a91df 100644 --- a/webapps/docs/changelog.xml +++ b/webapps/docs/changelog.xml @@ -370,6 +370,20 @@ that is in asynchronous mode are released if the connection thread raises a stream error for that stream. (markt) + + Treat the value of the Expect header as case insensitive + for HTTP/2 requests, as required by RFC 9110, section 10.1.1, so that a + 100 (Continue) response is sent for + Expect: 100-Continue. Pull request 1263 provided + by Arturo Bernal. + + + Treat the value of the Expect header as a list of + expectations for HTTP/2 requests, as required by RFC 9110, section + 10.1.1, so that a 100 (Continue) response is sent when the + list contains 100-continue. Pull request 1265 + provided by Arturo Bernal. +