File tree Expand file tree Collapse file tree 2 files changed +64
-4
lines changed
main/java/io/vertx/core/http/impl
test/java/io/vertx/tests/http Expand file tree Collapse file tree 2 files changed +64
-4
lines changed Original file line number Diff line number Diff line change @@ -482,16 +482,28 @@ 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 ()) {
492- headers ().set (CONTENT_LENGTH , HttpUtils .positiveLongToString (buff .readableBytes ()));
497+ if (buff != null && requiresContentLength (true )) {
498+ boolean headWritten ;
499+ synchronized (this ) {
500+ headWritten = this .headWritten ;
501+ }
502+ if (!headWritten ) {
503+ headers ().set (CONTENT_LENGTH , HttpUtils .positiveLongToString (buff .readableBytes ()));
504+ }
493505 }
494- } else if (requiresContentLength ()) {
506+ } else if (requiresContentLength (false )) {
495507 throw new IllegalStateException ("You must set the Content-Length header to be the total size of the message "
496508 + "body BEFORE sending any data if you are not using HTTP chunked encoding." );
497509 }
Original file line number Diff line number Diff 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}
You can’t perform that action at this time.
0 commit comments