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
1 change: 1 addition & 0 deletions java/org/apache/coyote/http2/LocalStrings.properties
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ stream.inputBuffer.reset=Stream reset
stream.inputBuffer.signal=Data added to inBuffer when read thread is waiting. Signalling that thread to continue
stream.inputBuffer.swallowUnread=Swallowing [{0}] bytes previously read into input stream buffer
stream.notWritable=Connection [{0}], Stream [{1}], This stream is not writable
stream.outputBuffer.contentLength=Connection [{0}], Stream [{1}], The response was ended after [{3}] bytes of body but its content length header value is [{2}]
stream.outputBuffer.flush.debug=Connection [{0}], Stream [{1}], flushing output with buffer at position [{2}], writeInProgress [{3}] and closed [{4}]
stream.recycle.duplicate=Connection [{0}], Stream [{1}] Duplicate request to recycle the associated request and response has been ignored
stream.recycle.first=Connection [{0}], Stream [{1}] The associated request and response have been recycled
Expand Down
14 changes: 14 additions & 0 deletions java/org/apache/coyote/http2/Stream.java
Original file line number Diff line number Diff line change
Expand Up @@ -1222,6 +1222,20 @@ public final void end() throws IOException {
}
if (!closed) {
closed = true;
/*
* Ending the stream with less data than the content-length header field of the response states
* would present a truncated response as a complete one. Reset the stream instead. A response that is
* defined to have no body has already closed the buffer so it never gets here (RFC 9113, section
* 8.1.1).
*/
long contentLength = coyoteResponse.getContentLengthLong();
if (contentLength > -1 && written < contentLength) {
reset = new StreamException(
sm.getString("stream.outputBuffer.contentLength", getConnectionId(), getIdAsString(),
Long.valueOf(contentLength), Long.valueOf(written)),
Http2Error.PROTOCOL_ERROR, getIdAsInt());
throw new CloseNowException(reset);
}
flush(true);
writeTrailers();
}
Expand Down
75 changes: 75 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,59 @@ 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. Ending the stream normally would present a truncated response as a
* complete one so the stream is reset.
*/
@Test
public void testResponseShorterThanContentLength() throws Exception {
doTestResponseShorterThanContentLength(false);
}


@Test
public void testResponseShorterThanContentLengthFlushed() throws Exception {
doTestResponseShorterThanContentLength(true);
}


private void doTestResponseShorterThanContentLength(boolean flush) 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, "short", new ShortContent(flush));
ctxt.addServletMapping("/short", "short");

tomcat.start();

openClientConnection();
doHttpUpgrade();
sendClientPreface();
validateHttp2InitialResponse();

byte[] frameHeader = new byte[9];
ByteBuffer headersPayload = ByteBuffer.allocate(128);
buildGetRequest(frameHeader, headersPayload, null, 3, "/short");
writeFrame(frameHeader, headersPayload);

parser.readFrame();
while (!output.getTrace().contains("3-RST")) {
parser.readFrame();
}

String trace = output.getTrace();
Assert.assertTrue(trace, trace.contains("3-Header-[content-length]-[10]"));
Assert.assertTrue(trace, trace.contains("3-RST-[" + Http2Error.PROTOCOL_ERROR.getCode() + "]"));
Assert.assertFalse(trace, trace.contains("3-EndOfStream"));
}


@Test
public void testAsyncDispatch() throws Exception {
enableHttp2();
Expand Down Expand Up @@ -967,4 +1020,26 @@ private void waitForRequestBody(HttpServletRequest req) throws IOException {
}
}
}

private static final class ShortContent extends HttpServlet {

private static final long serialVersionUID = 1L;

private final boolean flush;

ShortContent(boolean flush) {
this.flush = flush;
}

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setContentType("text/plain");
response.setContentLength(10);
// Less than the content length
response.getOutputStream().write("01234".getBytes(StandardCharsets.ISO_8859_1));
if (flush) {
response.flushBuffer();
}
}
}
}
Loading