hc-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Sean McDowell (JIRA)" <j...@apache.org>
Subject [jira] [Comment Edited] (HTTPCLIENT-1550) "deflate" encoding issue : java.util.zip.ZipException: invalid stored block lengths
Date Mon, 06 Oct 2014 18:32:33 GMT

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

Sean McDowell edited comment on HTTPCLIENT-1550 at 10/6/14 6:32 PM:
--------------------------------------------------------------------

I experienced the same issue as originally logged. Code in DeflateInputStream.java tries to
detect whether the stream is zlib or deflate. It reads 6 bytes and uses that data to try to
read the first byte from the stream. There is no guarantee that a byte can be read from only
6 bytes of compressed data. It may work if the FDICT flag is set because that causes the test
to succeed.

Here is a fix that works for my issue and also for the URI that Guillaume specifies above.
It considers the stream to be zlib if no exception is thrown after feeding it 6 bytes and
calling inflate, even if a byte was not able to be read.

        try {
            inf.setInput(peeked);
            final int n = inf.inflate(dummy);
            if (n == -1) {
                throw new IOException("Unable to read the response");
            }
            /* No exception occurred, it appears to be a valid stream */
            pushback.unread(peeked, 0, headerLength);
            sourceStream = new DeflateStream(pushback, new Inflater());
        } catch (final DataFormatException e) {
            /* Presume that it's an RFC1951 deflate stream rather than RFC1950 zlib stream.
*/
            pushback.unread(peeked, 0, headerLength);
            sourceStream = new DeflateStream(pushback, new Inflater(true));
        } finally {
            inf.end();
        }


was (Author: sean.mcdowell):
I experienced the same issue as originally logged. Code in DeflateInputStream.java tries to
detect whether the stream is zlib or deflate. It reads 6 bytes and uses that data to try to
read the first byte from the stream. There is no guarantee that a byte can be read from only
6 bytes of compressed data. It may work if the FDICT flag is set because that causes the test
to succeed.

Here is a fix that works for my issue and also for the URI that Guillaume specifies above.
It considers the stream to be zlib if no exception is thrown after feeding it 6 bytes and
calling inflate, even if a byte was not able to be read.

        boolean nowrap = false;
        try {
            inf.setInput(peeked);
            final int n = inf.inflate(dummy);
            if (n == -1) {
                throw new IOException("Unable to read the response");
            }
            /* No exceptions occurred, it appears to be a valid stream */
        } catch (final DataFormatException e) {
            /* Presume that it's an RFC1951 deflate stream rather than RFC1950 zlib stream.
*/
            nowrap = true;
        } finally {
            pushback.unread(peeked, 0, headerLength);
            sourceStream = new DeflateStream(pushback, new Inflater(nowrap));
            inf.end();
        }


> "deflate" encoding issue : java.util.zip.ZipException: invalid stored block lengths
> -----------------------------------------------------------------------------------
>
>                 Key: HTTPCLIENT-1550
>                 URL: https://issues.apache.org/jira/browse/HTTPCLIENT-1550
>             Project: HttpComponents HttpClient
>          Issue Type: Bug
>    Affects Versions: 4.3.5
>         Environment: Windows
>            Reporter: Guillaume Serre
>            Priority: Minor
>
> When trying to GET any nzb files from http://nzbindex.nl with a httpclient request, a
ZipException is raised.
> {code:title=JunitTestFailing.java}
> 	@Test
> 	public void testDeflateIssueFailing() throws ClientProtocolException, IOException {
> 		HttpGet httpget = new HttpGet("http://nzbindex.nl/download/72441827/BackTrack5R3.Gnome.iSO.32bit-LiNUX-0171-bt5r2gis32.par2.nzb");
> 		HttpClient client = HttpClientBuilder.create().build();
> 		HttpResponse response = client.execute(httpget);
> 		HttpEntity entity = response.getEntity();
> 		byte[] data = EntityUtils.toByteArray( entity );
> 		Assert.assertEquals( data.length, 423548 );
> 	}
> {code}
> The complete stack trace I get is this:
> {code}
> java.util.zip.ZipException: invalid stored block lengths
> 	at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:164)
> 	at java.io.FilterInputStream.read(FilterInputStream.java:107)
> 	at org.apache.http.client.entity.DeflateInputStream.read(DeflateInputStream.java:144)
> 	at org.apache.http.client.entity.LazyDecompressingInputStream.read(LazyDecompressingInputStream.java:68)
> 	at org.apache.http.util.EntityUtils.toByteArray(EntityUtils.java:136)
> 	at hclient.HTTPClientTest.testDeflateIssueFailing(HTTPClientTest.java:39)
> {code}
> A workaround I found is to set the Accept-Encoding header to only accept gzip, as follows:
> {code:title=JunitTestWorking.java}
> 	
> 	@Test
> 	public void testDeflateIssueWorking() throws ClientProtocolException, IOException {
> 		HttpGet httpget = new HttpGet("http://nzbindex.nl/download/72441827/BackTrack5R3.Gnome.iSO.32bit-LiNUX-0171-bt5r2gis32.par2.nzb");
> 		httpget.setHeader("Accept-Encoding", "gzip");
> 		HttpClient client = HttpClientBuilder.create().build();
> 		HttpResponse response = client.execute(httpget);
> 		HttpEntity entity = response.getEntity();
> 		byte[] data = EntityUtils.toByteArray( entity );
> 		Assert.assertEquals( data.length, 423548 );
> 	}
> {code}
> Thanks



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

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


Mime
View raw message