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

Tom Fitzhenry commented on HTTPCORE-422:
----------------------------------------

{{#onEntityEnclosed}} *is* triggered for HEAD requests.

{code}
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpHead;
import org.apache.http.entity.ContentType;
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
import org.apache.http.impl.nio.client.HttpAsyncClients;
import org.apache.http.nio.client.methods.HttpAsyncMethods;
import org.apache.http.nio.protocol.BasicAsyncResponseConsumer;
import org.apache.http.nio.protocol.HttpAsyncRequestProducer;

import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

public class Main {

    public static void main(String[] args) throws IOException, 
ExecutionException, InterruptedException {
        try (CloseableHttpAsyncClient foo = HttpAsyncClients.createDefault()) {
            foo.start();

            HttpHead request = new HttpHead("http://httpbin.org";);

            Future<HttpResponse> response;
            try (HttpAsyncRequestProducer httpHead = 
HttpAsyncMethods.create(request)) {
                response = foo.execute(httpHead, new 
BasicAsyncResponseConsumer() {
                    @Override
                    protected void onEntityEnclosed(HttpEntity entity, 
ContentType contentType) throws IOException {
                        System.out.println("onEntityEnclosed!");
                        System.out.println(entity);
                        super.onEntityEnclosed(entity, contentType);
                    }
                }, null);
            }

            System.out.println(response.get());
        }
    }

}
{code}

When the above is executed, it prints:
{code}
onEntityEnclosed!
[Content-Type: text/html; charset=utf-8,Content-Length: 12150,Chunked: false]
HTTP/1.1 200 OK [Server: nginx, Date: Wed, 15 Jun 2016 17:16:34 GMT, 
Content-Type: text/html; charset=utf-8, Content-Length: 12150, Connection: 
keep-alive, Access-Control-Allow-Origin: *, Access-Control-Allow-Credentials: 
true]
{code}

It sounds like that is a bug, but once resolved will remove the need for a 
NoopResponseConsumer.

> HttpAsyncRequestExecutor#responseReceived calls 
> HttpAsyncResponseConsumer#responseReceived(HttpResponse), even for HEAD 
> requests
> --------------------------------------------------------------------------------------------------------------------------------
>
>                 Key: HTTPCORE-422
>                 URL: https://issues.apache.org/jira/browse/HTTPCORE-422
>             Project: HttpComponents HttpCore
>          Issue Type: Improvement
>          Components: HttpCore NIO
>    Affects Versions: 4.4.4
>            Reporter: Tom Fitzhenry
>            Assignee: Oleg Kalnichevski
>            Priority: Minor
>
> HttpAsyncRequestExecutor#responseReceived(NHttpClientConnection) calls 
> HttpAsyncResponseConsumer#responseReceived(HttpResponse) via 
> HttpAsyncClientExchangeHandler#responseReceived(HttpResponse).
> See 
> https://github.com/apache/httpcore/blob/4.4.x/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncRequestExecutor.java#L302
>  .
> It does this even if the request is a HEAD request. If your 
> HttpAsyncResponseConsumer is a BasicAsyncResponseConsumer, then this will 
> allocate a buffer of size content-length kB (or 4kB, if content-length does 
> not exist).
> For a simple proxying Java app, profiling revealed this the allocation due to 
> this was a bottleneck.
> It'd be nice if 
> Are there use cases for calling 
> HttpAsyncResponseConsumer#responseReceived(HttpResponse) on HEAD requests? If 
> not, perhaps it could not be called.
> FWIW, it looks like httpclient doe not call the corresponding method for HEAD 
> requests: 
> https://github.com/apache/httpcore/blob/4.4.x/httpcore/src/main/java/org/apache/http/protocol/HttpRequestExecutor.java#L273-L275



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to