Skip to content

Commit 4c38c3f

Browse files
committed
fixed http client write buffer failed.
1 parent 2f11b16 commit 4c38c3f

File tree

2 files changed

+62
-8
lines changed

2 files changed

+62
-8
lines changed

vertx-core/src/main/java/io/vertx/core/http/impl/HttpClientRequestImpl.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -360,13 +360,13 @@ public Future<Void> writeCustomFrame(int type, int flags, Buffer payload) {
360360
synchronized (this) {
361361
checkEnded();
362362
}
363-
return stream.writeFrame(type, flags, ((BufferInternal)payload).getByteBuf());
363+
return stream.writeFrame(type, flags, ((BufferInternal) payload).getByteBuf());
364364
}
365365

366366
private void handleDrained(Void v) {
367367
Handler<Void> handler;
368368
synchronized (this) {
369-
handler = drainHandler;
369+
handler = drainHandler;
370370
if (handler == null || endFuture.isComplete()) {
371371
return;
372372
}
@@ -381,7 +381,7 @@ private void handleNextRequest(HttpClientRequest next, Promise<HttpClientRespons
381381
next.pushHandler(pushHandler());
382382
next.setFollowRedirects(true);
383383
next.setMaxRedirects(maxRedirects);
384-
((HttpClientRequestImpl)next).numberOfRedirections = numberOfRedirections + 1;
384+
((HttpClientRequestImpl) next).numberOfRedirections = numberOfRedirections + 1;
385385
endFuture.onComplete(ar -> {
386386
if (ar.succeeded()) {
387387
if (timeoutMs > 0) {
@@ -457,7 +457,7 @@ public Future<Void> end(String chunk, String enc) {
457457

458458
@Override
459459
public Future<Void> end(Buffer chunk) {
460-
return write(((BufferInternal)chunk).getByteBuf(), true);
460+
return write(((BufferInternal) chunk).getByteBuf(), true);
461461
}
462462

463463
@Override
@@ -467,7 +467,7 @@ public Future<Void> end() {
467467

468468
@Override
469469
public Future<Void> write(Buffer chunk) {
470-
ByteBuf buf = ((BufferInternal)chunk).getByteBuf();
470+
ByteBuf buf = ((BufferInternal) chunk).getByteBuf();
471471
return write(buf, false);
472472
}
473473

@@ -482,16 +482,22 @@ public Future<Void> write(String chunk, String enc) {
482482
return write(BufferInternal.buffer(chunk, enc).getByteBuf(), false);
483483
}
484484

485-
private boolean requiresContentLength() {
485+
private boolean requiresContentLength(boolean writeHead) {
486+
if (writeHead) {
487+
return !chunked && (headers == null || !headers.contains(CONTENT_LENGTH)) && !isConnect;
488+
}
489+
if (version() == HttpVersion.HTTP_2) {
490+
return false;
491+
}
486492
return !chunked && (headers == null || !headers.contains(CONTENT_LENGTH)) && !isConnect;
487493
}
488494

489495
private Future<Void> write(ByteBuf buff, boolean end) {
490496
if (end) {
491-
if (buff != null && requiresContentLength()) {
497+
if (buff != null && requiresContentLength(true)) {
492498
headers().set(CONTENT_LENGTH, HttpUtils.positiveLongToString(buff.readableBytes()));
493499
}
494-
} else if (requiresContentLength()) {
500+
} else if (requiresContentLength(false)) {
495501
throw new IllegalStateException("You must set the Content-Length header to be the total size of the message "
496502
+ "body BEFORE sending any data if you are not using HTTP chunked encoding.");
497503
}

vertx-core/src/test/java/io/vertx/tests/http/Http2ClientTest.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2330,4 +2330,52 @@ public void testClearTestDirectServerCloseBeforeSettingsRead() {
23302330
}));
23312331
await();
23322332
}
2333+
2334+
@Test
2335+
public void testWriteChunked() throws Exception {
2336+
String p1 = "12345";
2337+
String p2 = "56789";
2338+
server.requestHandler(req -> {
2339+
Buffer buffer = Buffer.buffer();
2340+
AtomicInteger handleChunkCount = new AtomicInteger();
2341+
req.handler(buff -> {
2342+
buffer.appendBuffer(buff);
2343+
handleChunkCount.incrementAndGet();
2344+
});
2345+
req.endHandler(v -> {
2346+
assertEquals(handleChunkCount.get(), 2);
2347+
assertEquals(buffer.toString(), p1 + p2);
2348+
2349+
HttpServerResponse response = req.response();
2350+
response.setStatusCode(200);
2351+
response
2352+
.write(p2)
2353+
.flatMap(v0 -> response.end(p1));
2354+
});
2355+
});
2356+
startServer();
2357+
2358+
client.request(requestOptions).onComplete(onSuccess(req -> {
2359+
req.write(p1)
2360+
.flatMap(v -> req.end(p2));
2361+
2362+
AtomicInteger handleChunkCount = new AtomicInteger();
2363+
Buffer buffer = Buffer.buffer();
2364+
req.response().onComplete(onSuccess(response -> {
2365+
response.handler(buf -> {
2366+
buffer.appendBuffer(buf);
2367+
handleChunkCount.incrementAndGet();
2368+
});
2369+
2370+
response.endHandler(v -> {
2371+
assertEquals(handleChunkCount.get(), 2);
2372+
assertEquals(buffer.toString(), p2 + p1);
2373+
complete();
2374+
});
2375+
}));
2376+
}));
2377+
2378+
await();
2379+
}
2380+
23332381
}

0 commit comments

Comments
 (0)