ContentLength of HttpResponse is negative when compression is used

0 votes
asked Sep 24 by mdisg (150 points)

Hello,

why is the value of the ContentLength Property -1 if the content is compressed with gzip?

The content length received from the server informs about the length of the compressed content so it's okay that this information is being removed. But why is the caller not informed about the real decompressed content length of the response?
I would expect that ContentLength returns the length of the raw data of the response.

Is there another way to retrieve that information?

Best regards
Maik

commented Sep 24 by Lukas Pokorny (104,290 points)
We will look into this! However, I'm afraid that the length of the raw data might not be available when HttpResponse is retrieved because data is received and decompressed on-the-fly when reading from the response stream. Therefore, the total length of decompressed data might not be available until its actually read.

1 Answer

0 votes
answered Sep 26 by Lukas Matyska (55,430 points)
selected Sep 30 by mdisg
 
Best answer

It is as Lukas Pokorny wrote in comment, the length of decompressed data is not known until whole response is decompressed. There is no HTTP header which contain such value.

I ensured that Rebex has exact same behavior as System.Net.HttpWebRequest class using http://httpbin.org/gzip and this code:

var request = (HttpWebRequest)WebRequest.Create("http://httpbin.org/gzip");
request.AutomaticDecompression = DecompressionMethods.GZip;

using (var response = request.GetResponse())
{
    Console.WriteLine("ContentLength: {0}", response.ContentLength);

    using (var reader = new StreamReader(response.GetResponseStream()))
    {
        string body = reader.ReadToEnd();
        Console.WriteLine("Body length: {0}", body.Length);
    }

    Console.WriteLine("ContentLength: {0}", response.ContentLength);
}

The output of this program is:

ContentLength: -1
Body length: 166
ContentLength: -1
commented Sep 30 by mdisg (150 points)
Thanks for the investigation.

I disabled the decompression at all because for my case i must know the complete content length before reading the stream.
...