You are viewing a plain text version of this content. The canonical link for it is here.
Posted to httpclient-users@hc.apache.org by Johannes Kienzle <jk...@salesforce.com> on 2013/10/08 23:13:55 UTC

HttpAsyncClient 4.0-beta4 - GZip decompression doesn't work anymore

Hello,

it appears that in the latest version of the HttpAsyncClient (4.0-beta4),
adding a response interceptor for GZip decompression (GzipDecompressingEntity)
doesn't work anymore. Getting the HttpEntity from the response returns the
compressed gzip stream, rather than letting the interceptor decompress it.
It worked fine in previous releases. The same code also works fine with the
regular HttpClient (tested 4.3 and 4.3.1) as seen below.

Note that the request interceptor works fine and is utilized correctly (see
tests below).

Am I doing something wrong? Here are two tests to show the behavior, one
with HttpClient which works, one with HttpAsyncClient which doesn't work,
adopted from the official examples:

@Test
    public void testGZipSync() throws Exception {
     CloseableHttpClient httpclient = HttpClients.custom()
                .addInterceptorFirst(new HttpRequestInterceptor() {

                public void process(
                        final HttpRequest request,
                        final HttpContext context) throws HttpException,
IOException {
                    if (!request.containsHeader("Accept-Encoding")) {
                        request.addHeader("Accept-Encoding", "gzip");
                    }

                }}).addInterceptorFirst(new HttpResponseInterceptor() {

                public void process(
                        final HttpResponse response,
                        final HttpContext context) throws HttpException,
IOException {
                    HttpEntity entity = response.getEntity();
                    if (entity != null) {
                        Header ceheader = entity.getContentEncoding();
                        if (ceheader != null) {
                            HeaderElement[] codecs = ceheader.getElements();
                            for (int i = 0; i < codecs.length; i++) {
                                if
(codecs[i].getName().equalsIgnoreCase("gzip")) {
                                    response.setEntity(
                                            new
GzipDecompressingEntity(response.getEntity()));
                                    return;
                                }
                            }
                        }
                    }
                }

            }).build();
        try {
            HttpGet httpget = new HttpGet("http://www.apache.org/");
            CloseableHttpResponse response = httpclient.execute(httpget);
            try {
                HttpEntity entity = response.getEntity();

                if (entity != null) {
                    // this content is correct
                    String content = EntityUtils.toString(entity);
                    System.out.println(content);
                }
            } finally {
                response.close();
            }
        } finally {
            httpclient.close();
        }
    }

    @Test
    public void testGZipAsync() throws Exception {
     CloseableHttpAsyncClient httpclient = HttpAsyncClients.custom()
                .addInterceptorFirst(new HttpRequestInterceptor() {

                public void process(
                        final HttpRequest request,
                        final HttpContext context) throws HttpException,
IOException {
                    if (!request.containsHeader("Accept-Encoding")) {
                        request.addHeader("Accept-Encoding", "gzip");
                    }

                }}).addInterceptorFirst(new HttpResponseInterceptor() {

                public void process(
                        final HttpResponse response,
                        final HttpContext context) throws HttpException,
IOException {
                    HttpEntity entity = response.getEntity();
                    if (entity != null) {
                        Header ceheader = entity.getContentEncoding();
                        if (ceheader != null) {
                            HeaderElement[] codecs = ceheader.getElements();
                            for (int i = 0; i < codecs.length; i++) {
                                if
(codecs[i].getName().equalsIgnoreCase("gzip")) {
                                    response.setEntity(
                                            new
GzipDecompressingEntity(response.getEntity()));
                                    return;
                                }
                            }
                        }
                    }
                }

            }).build();

        httpclient.start();
        try {
            final HttpGet request = new HttpGet("http://www.apache.org/");
            final Future<HttpResponse> future = httpclient.execute(request,
null);
            final HttpResponse response = future.get();
            HttpEntity entity = response.getEntity();

            if (entity != null) {
                // this content is incorrect! It's not being decompressed.
                String content = EntityUtils.toString(entity);
                System.out.println(content);
            }
        } finally {
            httpclient.close();
        }
    }


Thanks,
Johannes

Re: HttpAsyncClient 4.0-beta4 - GZip decompression doesn't work anymore

Posted by Mark <ma...@mashape.com>.
So how would you decompress a gzip response?






---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


Re: HttpAsyncClient 4.0-beta4 - GZip decompression doesn't work anymore

Posted by Oleg Kalnichevski <ol...@apache.org>.
On Tue, 2013-10-08 at 14:13 -0700, Johannes Kienzle wrote:
> Hello,
> 
> it appears that in the latest version of the HttpAsyncClient (4.0-beta4),
> adding a response interceptor for GZip decompression (GzipDecompressingEntity)
> doesn't work anymore. Getting the HttpEntity from the response returns the
> compressed gzip stream, rather than letting the interceptor decompress it.
> It worked fine in previous releases. The same code also works fine with the
> regular HttpClient (tested 4.3 and 4.3.1) as seen below.
> 
> Note that the request interceptor works fine and is utilized correctly (see
> tests below).
> 
> Am I doing something wrong? Here are two tests to show the behavior, one
> with HttpClient which works, one with HttpAsyncClient which doesn't work,
> adopted from the official examples:
> 

Johannes

HttpAsyncClient 4.0 does not support transparent content decompression.
The fact that it somehow worked for early released was merely
coincidental (it certainly could only have worked for responses whose
entire content was buffered in memory). Some things that are trivial
with bocking i/o are far from that for non-blocking i/o. Transparent
content decompression might (or might not) be supported in 4.1 branch.

Cheers

Oleg 



---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org