0

I am doing an http proxy and encountered a problem with decompression of responses, that come from a server after a client makes request to server.

E.g. client sends get https://stackoverflow.com/questions/some_question . server sends response in several parts. I use following method for decompression of responses parts.

public static void gzipToString(ByteBuf buf) throws IOException {
        Reader reader = null;
        reader = new InputStreamReader(new GZIPInputStream(new ByteBufInputStream(buf)));

        while (true) {
            int ch = reader.read();
            if (ch==-1) {
                break;
            }
            System.out.print((char)ch);
        }
    }

In the first part of response I get

<!DOCTYPE html>
<html itemscope itemtype="http://schema.org/QAPage">

<head>

<title>java - GZIPInputStream to String - Stack Overflow</title>
    <link rel="shortcut icon" href="https://cdn.sstatic.net/Sites/stackoverflow/img/favicon.ico?v=4f32ecc8f43d">
    <link rel="apple-touch-icon image_src" href="https://cdn.sstatic.net/Sites/stackoverflow/img/apple-touch-icon.png?v=c78bd457575a">
    <link rel="search" type="application/opensearchdescription+xml" title="Stack Overflow" href="/opensearch.xml">
    <meta name="twitter:card" content="summary">
    <meta name="twitter:app:id:googleplay" content="comjava.io.EOFException: Unexpected end of ZLIB input stream

at some point I get comjava.io.EOFException: Unexpected end of ZLIB input stream

the rest of the responses would case following exception at this line

reader = new InputStreamReader(new GZIPInputStream(new ByteBufInputStream(buf)));



java.util.zip.ZipException: Not in GZIP format
    at java.util.zip.GZIPInputStream.readHeader(GZIPInputStream.java:165)
    at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:79)
    at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:91)
    at com.verizon.ga.filter.ProxyHttpFilters.gzipToString3(ProxyHttpFilters.java:143)
    at com.verizon.ga.filter.ProxyHttpFilters.serverToProxyResponse(ProxyHttpFilters.java:73)

the fact that on first response I get EOFException makes me think that to decompress response properly I need to put all parts of response together.

but for some reason all other parts return Not in GZIP format. As I understand only the first part has GZIP header. and others, while being GZIP compressed, don't have the header.

What sould i do about it?

Community
  • 1
  • 1
rigby
  • 1,280
  • 3
  • 13
  • 21

1 Answers1

0

You can't slap a GZIPInputStream in front of a ByteBuf, it makes no sense: GZIPInputStream operates on a complete InputStream. See my answer here: https://stackoverflow.com/a/48047974/839733

Abhijit Sarkar
  • 21,927
  • 20
  • 110
  • 219