11/*
2- * Copyright (c) 2011-2020 Contributors to the Eclipse Foundation
2+ * Copyright (c) 2011-2025 Contributors to the Eclipse Foundation
33 *
44 * This program and the accompanying materials are made available under the
55 * terms of the Eclipse Public License 2.0 which is available at
1010 */
1111package io .vertx .httpproxy .impl ;
1212
13- import io .vertx .core .*;
13+ import io .vertx .core .Future ;
14+ import io .vertx .core .MultiMap ;
1415import io .vertx .core .buffer .Buffer ;
15- import io .vertx .core .http .*;
16+ import io .vertx .core .http .HttpClientRequest ;
17+ import io .vertx .core .http .HttpHeaders ;
18+ import io .vertx .core .http .HttpMethod ;
19+ import io .vertx .core .http .HttpServerRequest ;
20+ import io .vertx .core .http .HttpVersion ;
1621import io .vertx .core .internal .ContextInternal ;
1722import io .vertx .core .internal .http .HttpServerRequestInternal ;
1823import io .vertx .core .net .HostAndPort ;
2429import java .util .Map ;
2530import java .util .Objects ;
2631
32+ import static io .vertx .core .http .HttpHeaders .CONNECTION ;
33+ import static io .vertx .core .http .HttpHeaders .CONTENT_LENGTH ;
34+ import static io .vertx .core .http .HttpHeaders .KEEP_ALIVE ;
35+ import static io .vertx .core .http .HttpHeaders .PROXY_AUTHENTICATE ;
36+ import static io .vertx .core .http .HttpHeaders .PROXY_AUTHORIZATION ;
37+ import static io .vertx .core .http .HttpHeaders .TRANSFER_ENCODING ;
38+ import static io .vertx .core .http .HttpHeaders .UPGRADE ;
39+
2740public class ProxiedRequest implements ProxyRequest {
2841
2942 private static final CharSequence X_FORWARDED_HOST = HttpHeaders .createOptimized ("x-forwarded-host" );
3043
3144 private static final MultiMap HOP_BY_HOP_HEADERS = MultiMap .caseInsensitiveMultiMap ()
32- .add (HttpHeaders . CONNECTION , "whatever" )
33- .add (HttpHeaders . KEEP_ALIVE , "whatever" )
34- .add (HttpHeaders . PROXY_AUTHENTICATE , "whatever" )
35- .add (HttpHeaders . PROXY_AUTHORIZATION , "whatever" )
45+ .add (CONNECTION , "whatever" )
46+ .add (KEEP_ALIVE , "whatever" )
47+ .add (PROXY_AUTHENTICATE , "whatever" )
48+ .add (PROXY_AUTHORIZATION , "whatever" )
3649 .add ("te" , "whatever" )
3750 .add ("trailer" , "whatever" )
38- .add (HttpHeaders . TRANSFER_ENCODING , "whatever" )
39- .add (HttpHeaders . UPGRADE , "whatever" );
51+ .add (TRANSFER_ENCODING , "whatever" )
52+ .add (UPGRADE , "whatever" );
4053
4154 final ContextInternal context ;
4255 private HttpMethod method ;
@@ -53,7 +66,7 @@ public ProxiedRequest(HttpServerRequest proxiedRequest) {
5366
5467 // Determine content length
5568 long contentLength = -1L ;
56- String contentLengthHeader = proxiedRequest .getHeader (HttpHeaders . CONTENT_LENGTH );
69+ String contentLengthHeader = proxiedRequest .getHeader (CONTENT_LENGTH );
5770 if (contentLengthHeader != null ) {
5871 try {
5972 contentLength = Long .parseLong (contentLengthHeader );
@@ -170,24 +183,31 @@ Future<ProxyResponse> sendRequest() {
170183 }
171184 }
172185
173- long len = body .length ();
174- if (len >= 0 ) {
175- request .putHeader (HttpHeaders .CONTENT_LENGTH , Long .toString (len ));
186+ if (body == null ) {
187+ if (proxiedRequest .headers ().contains (CONTENT_LENGTH )) {
188+ request .putHeader (CONTENT_LENGTH , "0" );
189+ }
190+ request .end ();
176191 } else {
177- Boolean isChunked = HttpUtils .isChunked (proxiedRequest .headers ());
178- request .setChunked (len == -1 && Boolean .TRUE == isChunked );
179- }
180-
181- Pipe <Buffer > pipe = body .stream ().pipe ();
182- pipe .endOnComplete (true );
183- pipe .endOnFailure (false );
184- pipe .to (request ).onComplete (ar -> {
185- if (ar .failed ()) {
186- request .reset ();
192+ long len = body .length ();
193+ if (len >= 0 ) {
194+ request .putHeader (CONTENT_LENGTH , Long .toString (len ));
195+ } else {
196+ Boolean isChunked = HttpUtils .isChunked (proxiedRequest .headers ());
197+ request .setChunked (len == -1 && Boolean .TRUE == isChunked );
187198 }
188- });
189199
190- return request .response ().<ProxyResponse >map (r -> {
200+ Pipe <Buffer > pipe = body .stream ().pipe ();
201+ pipe .endOnComplete (true );
202+ pipe .endOnFailure (false );
203+ pipe .to (request ).onComplete (ar -> {
204+ if (ar .failed ()) {
205+ request .reset ();
206+ }
207+ });
208+ }
209+
210+ return request .response ().map (r -> {
191211 r .pause (); // Pause it
192212 return new ProxiedResponse (this , proxiedRequest .response (), r );
193213 });
0 commit comments