Skip to content

Commit

Permalink
Don't set content length header for chunked encoding with HTTP/1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
vietj committed Jun 29, 2017
1 parent 7bdbe08 commit 0ba8b3b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ private void end0(ByteBuf data) {
if (!headWritten) {
// if the head was not written yet we can write out everything in one go
// which is cheaper.
prepareHeaders();
prepareHeaders(bytesWritten);
FullHttpResponse resp;
if (trailing != null) {
resp = new AssembledFullHttpResponse(response, data, trailing.trailingHeaders(), trailing.getDecoderResult());
Expand Down Expand Up @@ -469,7 +469,7 @@ private void doSendFile(String filename, long offset, long length, Handler<Async
putHeader(HttpHeaders.CONTENT_TYPE, contentType);
}
}
prepareHeaders();
prepareHeaders(bytesWritten);

RandomAccessFile raf = null;
try {
Expand Down Expand Up @@ -560,7 +560,7 @@ private void checkWritten() {
}
}

private void prepareHeaders() {
private void prepareHeaders(long contentLength) {
if (version == HttpVersion.HTTP_1_0 && keepAlive) {
headers.set(HttpHeaders.CONNECTION, HttpHeaders.KEEP_ALIVE);
} else if (version == HttpVersion.HTTP_1_1 && !keepAlive) {
Expand All @@ -569,8 +569,8 @@ private void prepareHeaders() {
if (!head) {
if (chunked) {
headers.set(HttpHeaders.TRANSFER_ENCODING, HttpHeaders.CHUNKED);
} else if (!headers.contentLengthSet()) {
String value = bytesWritten == 0 ? "0" : String.valueOf(bytesWritten);
} else if (!headers.contentLengthSet() && contentLength >= 0) {
String value = contentLength == 0 ? "0" : String.valueOf(contentLength);
headers.set(HttpHeaders.CONTENT_LENGTH, value);
}
}
Expand All @@ -587,14 +587,12 @@ private HttpServerResponseImpl write(ByteBuf chunk) {
if (version != HttpVersion.HTTP_1_0) {
throw new IllegalStateException("You must set the Content-Length header to be the total size of the message "
+ "body BEFORE sending any data if you are not using HTTP chunked encoding.");
} else {
headers.set(HttpHeaders.CONTENT_LENGTH, "0");
}
}

bytesWritten += chunk.readableBytes();
if (!headWritten) {
prepareHeaders();
prepareHeaders(-1);
conn.writeToChannel(new AssembledHttpResponse(response, chunk));
} else {
conn.writeToChannel(new DefaultHttpContent(chunk));
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/io/vertx/test/core/Http1xTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3689,7 +3689,7 @@ public void testUnknownContentLengthIsSetToZeroWithHTTP_1_0() throws Exception {
client.close();
client = vertx.createHttpClient(new HttpClientOptions().setProtocolVersion(HttpVersion.HTTP_1_0));
client.getNow(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, resp -> {
assertEquals("0", resp.getHeader("Content-Length"));
assertNull(resp.getHeader("Content-Length"));
testComplete();
});
await();
Expand Down

0 comments on commit 0ba8b3b

Please sign in to comment.