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
Original file line number Diff line number Diff line change
Expand Up @@ -482,18 +482,31 @@ public Future<Void> write(String chunk, String enc) {
return write(BufferInternal.buffer(chunk, enc).getByteBuf(), false);
}

private boolean requiresContentLength() {
private boolean requiresContentLength(boolean writeHead) {
if (writeHead) {
return !chunked && (headers == null || !headers.contains(CONTENT_LENGTH)) && !isConnect;
}
if (version() == HttpVersion.HTTP_2) {
return false;
}
return !chunked && (headers == null || !headers.contains(CONTENT_LENGTH)) && !isConnect;
}

private Future<Void> write(ByteBuf buff, boolean end) {
if (end) {
if (buff != null && requiresContentLength()) {
headers().set(CONTENT_LENGTH, HttpUtils.positiveLongToString(buff.readableBytes()));
if (buff != null && requiresContentLength(true)) {
boolean headWritten;
synchronized (this) {
headWritten = this.headWritten;
}
if (!headWritten) {
headers().set(CONTENT_LENGTH, HttpUtils.positiveLongToString(buff.readableBytes()));
}
}
} else if (requiresContentLength()) {
throw new IllegalStateException("You must set the Content-Length header to be the total size of the message "
} else if (requiresContentLength(false)) {
IllegalStateException e = 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.");
return Future.failedFuture(e);
}
return doWrite(buff, end, false);
}
Expand Down
48 changes: 48 additions & 0 deletions vertx-core/src/test/java/io/vertx/tests/http/Http2ClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2330,4 +2330,52 @@ public void testClearTestDirectServerCloseBeforeSettingsRead() {
}));
await();
}

@Test
public void testWriteChunked() throws Exception {
String p1 = "12345";
String p2 = "56789";
server.requestHandler(req -> {
Buffer buffer = Buffer.buffer();
AtomicInteger handleChunkCount = new AtomicInteger();
req.handler(buff -> {
buffer.appendBuffer(buff);
handleChunkCount.incrementAndGet();
});
req.endHandler(v -> {
assertEquals(handleChunkCount.get(), 2);
assertEquals(buffer.toString(), p1 + p2);

HttpServerResponse response = req.response();
response.setStatusCode(200);
response
.write(p2)
.flatMap(v0 -> response.end(p1));
});
});
startServer();

client.request(requestOptions).onComplete(onSuccess(req -> {
req.write(p1)
.flatMap(v -> req.end(p2));

AtomicInteger handleChunkCount = new AtomicInteger();
Buffer buffer = Buffer.buffer();
req.response().onComplete(onSuccess(response -> {
response.handler(buf -> {
buffer.appendBuffer(buf);
handleChunkCount.incrementAndGet();
});

response.endHandler(v -> {
assertEquals(handleChunkCount.get(), 2);
assertEquals(buffer.toString(), p2 + p1);
complete();
});
}));
}));

await();
}

}
Loading