Skip to content
Open
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
13 changes: 13 additions & 0 deletions java/org/apache/coyote/http2/Stream.java
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
55 changes: 55 additions & 0 deletions test/org/apache/coyote/http2/TestStreamProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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));
}
}
}
Loading