[ 
https://issues.apache.org/jira/browse/HTTPCLIENT-2211?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17520834#comment-17520834
 ] 

Patrick Barry commented on HTTPCLIENT-2211:
-------------------------------------------

Hi Oleg

Here is a test you can use and I will include output as well. The endpoint this 
hits will return all of the headers sent to it. Also notice content-length does 
not show up in headers if you have org.apache.hc.client5.http.headers turned up 
to Debug, so what ever is adding it, is further down the pipeline than when 
these headers are printed out to logs.

 
{code:java}
@Test
public void contentLengthHeaderIssue() throws ExecutionException, 
InterruptedException {
    CloseableHttpAsyncClient client = HttpAsyncClients.custom()
            .setVersionPolicy(FORCE_HTTP_1)
            .build();
    client.start();

    AsyncRequestBuilder requestBuilder = AsyncRequestBuilder.create("GET")
            .setUri("http://headers.jsontest.com/";)
            .setEntity(new BasicAsyncEntityProducer("", 
ContentType.APPLICATION_JSON,  true));
    var execFuture = client.execute(
            requestBuilder.build(),
            new BasicResponseConsumer<>(new StringAsyncEntityConsumer()),
            HttpClientContext.create(),
            null);
   String response=  execFuture.get().getBody();
   System.out.println(response);
} {code}
Output
{noformat}
18:53:02.963 [httpclient-dispatch-1] DEBUG org.apache.hc.client5.http.headers - 
c-0000000000 >> GET / HTTP/1.1
18:53:02.964 [httpclient-dispatch-1] DEBUG org.apache.hc.client5.http.headers - 
c-0000000000 >> User-Agent: Apache-HttpAsyncClient/5.1.3 (Java/11.0.11)
18:53:02.964 [httpclient-dispatch-1] DEBUG org.apache.hc.client5.http.headers - 
c-0000000000 >> Transfer-Encoding: chunked
18:53:02.964 [httpclient-dispatch-1] DEBUG org.apache.hc.client5.http.headers - 
c-0000000000 >> Content-Type: application/json; charset=UTF-8
18:53:02.964 [httpclient-dispatch-1] DEBUG org.apache.hc.client5.http.headers - 
c-0000000000 >> Host: headers.jsontest.com
18:53:02.964 [httpclient-dispatch-1] DEBUG org.apache.hc.client5.http.headers - 
c-0000000000 >> Connection: keep-alive
18:53:03.055 [httpclient-dispatch-1] DEBUG org.apache.hc.client5.http.headers - 
c-0000000000 << HTTP/1.1 200 OK
18:53:03.055 [httpclient-dispatch-1] DEBUG org.apache.hc.client5.http.headers - 
c-0000000000 << Access-Control-Allow-Origin: *
18:53:03.055 [httpclient-dispatch-1] DEBUG org.apache.hc.client5.http.headers - 
c-0000000000 << Content-Type: application/json
18:53:03.055 [httpclient-dispatch-1] DEBUG org.apache.hc.client5.http.headers - 
c-0000000000 << X-Cloud-Trace-Context: cd1db3c54a25dae98c9489fd7353e14b
18:53:03.055 [httpclient-dispatch-1] DEBUG org.apache.hc.client5.http.headers - 
c-0000000000 << Date: Mon, 11 Apr 2022 22:53:03 GMT
18:53:03.055 [httpclient-dispatch-1] DEBUG org.apache.hc.client5.http.headers - 
c-0000000000 << Server: Google Frontend
18:53:03.055 [httpclient-dispatch-1] DEBUG org.apache.hc.client5.http.headers - 
c-0000000000 << Content-Length: 344
{
   "X-Cloud-Trace-Context": 
"cd1db3c54a25dae98c9489fd7353e14b/13896109684351631146",
   "traceparent": "00-cd1db3c54a25dae98c9489fd7353e14b-c0d8e62217a4ff2a-00",
   "User-Agent": "Apache-HttpAsyncClient/5.1.3 (Java/11.0.11)",
   "Host": "headers.jsontest.com",
   "Content-Length": "0",
   "Content-Type": "application/json; charset=UTF-8"
}
{noformat}

> content-length header should not be sent when chunked encoding specified
> ------------------------------------------------------------------------
>
>                 Key: HTTPCLIENT-2211
>                 URL: https://issues.apache.org/jira/browse/HTTPCLIENT-2211
>             Project: HttpComponents HttpClient
>          Issue Type: Bug
>    Affects Versions: 5.1.3
>            Reporter: Patrick Barry
>            Priority: Major
>
> According to HTTP 1.1 spec, content-length header should never be sent with 
> Transfer-Encoding: chunked on request.  However, when you send a request 
> using a non-body Method like GET or DELETE using this async client, it is 
> adding content-length:0 to the request.  This header should not be sent 
> according to RFC.
> Because it is sent, it causes the server that is responding to either reject 
> the request with a 400 or go down other paths. For example, Tomcat 9 chose to 
> accept the request but will close the connection, even if 
> Connection:Keep-Alive is requested on request.  This has major performance 
> drawbacks, and is only one way a server chose to handle this.
>  
> As per RFC 7230:
> <quote>
> A sender MUST NOT send a Content-Length header field in any message that 
> contains a Transfer-Encoding header field.
> </quote>



--
This message was sent by Atlassian Jira
(v8.20.1#820001)

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@hc.apache.org
For additional commands, e-mail: dev-h...@hc.apache.org

Reply via email to