diff --git a/java/org/apache/coyote/http2/Stream.java b/java/org/apache/coyote/http2/Stream.java index 0d19bc7bf74a..2a87ead26d6a 100644 --- a/java/org/apache/coyote/http2/Stream.java +++ b/java/org/apache/coyote/http2/Stream.java @@ -1078,6 +1078,19 @@ public final int doWrite(ByteBuffer chunk) throws IOException { if (closed) { throw new IOException(sm.getString("stream.closed", getConnectionId(), getIdAsString())); } + // Do not send more data than the content-length header field of the response states. As for HTTP/1.1, + // the data that is beyond it is discarded. Otherwise the response is malformed (RFC 9113, section + // 8.1.1). + long contentLength = coyoteResponse.getContentLengthLong(); + if (contentLength > -1) { + long remaining = contentLength - written; + if (remaining <= 0) { + chunk.position(chunk.limit()); + return 0; + } else if (chunk.remaining() > remaining) { + chunk.limit(chunk.position() + (int) remaining); + } + } // chunk is always fully written int result = chunk.remaining(); if (writeBuffer.isEmpty()) { diff --git a/test/org/apache/coyote/http2/TestStreamProcessor.java b/test/org/apache/coyote/http2/TestStreamProcessor.java index 47eb991a9ad4..d258518bf580 100644 --- a/test/org/apache/coyote/http2/TestStreamProcessor.java +++ b/test/org/apache/coyote/http2/TestStreamProcessor.java @@ -90,6 +90,48 @@ public void testAsyncComplete() throws Exception { } + /* + * RFC 9113, section 8.1.1 states that a response is malformed if the value of the content-length header field is + * not the sum of the DATA frame payloads. As for HTTP/1.1, data written beyond the content length is discarded. + */ + @Test + public void testResponseLongerThanContentLength() throws Exception { + enableHttp2(); + + Tomcat tomcat = getTomcatInstance(); + + // Map the servlet to /simple so we can re-use the HTTP/2 handling logic from the super class. + Context ctxt = getProgrammaticRootContext(); + Tomcat.addServlet(ctxt, "simple", new SimpleServlet()); + ctxt.addServletMapping("/simple", "simple"); + Tomcat.addServlet(ctxt, "excess", new ExcessContent()); + ctxt.addServletMapping("/excess", "excess"); + + tomcat.start(); + + openClientConnection(); + doHttpUpgrade(); + sendClientPreface(); + validateHttp2InitialResponse(); + + byte[] frameHeader = new byte[9]; + ByteBuffer headersPayload = ByteBuffer.allocate(128); + buildGetRequest(frameHeader, headersPayload, null, 3, "/excess"); + writeFrame(frameHeader, headersPayload); + + // Headers and then the body, which ends the stream + parser.readFrame(); + while (!output.getTrace().contains("3-EndOfStream")) { + parser.readFrame(); + } + + Assert.assertEquals("3-HeadersStart\n" + "3-Header-[:status]-[200]\n" + + "3-Header-[content-type]-[text/plain]\n" + "3-Header-[content-length]-[5]\n" + + "3-Header-[date]-[Wed, 11 Nov 2015 19:18:42 GMT]\n" + "3-HeadersEnd\n" + "3-Body-5\n" + + "3-EndOfStream\n", output.getTrace()); + } + + @Test public void testAsyncDispatch() throws Exception { enableHttp2(); @@ -967,4 +1009,17 @@ private void waitForRequestBody(HttpServletRequest req) throws IOException { } } } + + private static final class ExcessContent extends HttpServlet { + + private static final long serialVersionUID = 1L; + + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { + response.setContentType("text/plain"); + response.setContentLength(5); + // More than the content length + response.getOutputStream().write("0123456789".getBytes(StandardCharsets.ISO_8859_1)); + } + } }