16

I have a large amount of data (~100k) that my C# app is sending to my Apache server with mod_gzip installed. I'm attempting to gzip the data first using System.IO.Compression.GZipStream. PHP receives the raw gzipped data, so Apache is not uncompressing it as I would expect. Am I missing something?

System.Net.WebRequest req = WebRequest.Create(this.Url);
req.Method = this.Method; // "post"
req.Timeout = this.Timeout;
req.ContentType = "application/x-www-form-urlencoded";
req.Headers.Add("Content-Encoding: gzip");

System.IO.Stream reqStream = req.GetRequestStream();

GZipStream gz = new GZipStream(reqStream, CompressionMode.Compress);

System.IO.StreamWriter sw = new System.IO.StreamWriter(gz, Encoding.ASCII);
sw.Write( large_amount_of_data );
sw.Close();

gz.Close();
reqStream.Close()


System.Net.WebResponse resp = req.GetResponse();
// (handle response...)

I'm not entirely sure "Content-Encoding: gzip" applies to client-supplied headers.

  • 6
    +1 for posting code that compresses the posted data. I believe you are the only one to have done so in the entire Internet era ;-) – Serge Wautier Feb 17 '10 at 09:05
  • Would love to hear if any of the ideas below helped you - I'm trying to solve the same problem. – Tom Lianza Feb 25 '11 at 03:24

5 Answers5

4

I looked at the source code for mod_gzip and I could not find any code that decompresses data. Apparently mod_gzip only compresses outgoing data which isn't too surprising after all. The functionality you are looking for is probably rarely used, and I'm afraid you have to do your own decompression on the server.

Martin Liversage
  • 104,481
  • 22
  • 209
  • 256
4

Regarding your question whether Content-Encoding is applicable to client-supplied headers - according to HTTP/1.1 standard, it is:

(from section 7)

Request and Response messages MAY transfer an entity if not otherwise restricted by the request method or response status code.

(from section 7.1)

   entity-header  = Allow                    ; Section 14.7
                  | Content-Encoding         ; Section 14.11
                  | Content-Language         ; Section 14.12
                  | Content-Length           ; Section 14.13
                  | Content-Location         ; Section 14.14
                  | Content-MD5              ; Section 14.15
                  | Content-Range            ; Section 14.16
                  | Content-Type             ; Section 14.17
                  | Expires                  ; Section 14.21
                  | Last-Modified            ; Section 14.29
                  | extension-header
Sergii Volchkov
  • 1,169
  • 17
  • 23
  • 6
    This is true, with the caveat that most servers won't do decompression of HTTP requests, because of the possibility of so-called "Zip Bomb" attacks. – EricLaw Jan 08 '10 at 21:16
2

You need to change

req.Headers.Add("Content-Encoding: gzip");

to

req.Headers.Add("Content-Encoding","gzip");
nos
  • 223,662
  • 58
  • 417
  • 506
1

According to http://www.dominoexperts.com/articles/GZip-servlet-to-gzip-your-pages

You should setContentType() to the original format, as you are doing with the application/x-www-form-urlencoded I assume. Then...

 // See if browser can handle gzip
 String encoding=req.getHeader("Accept-Encoding");
 if (encoding != null && encoding.indexOf("gzip") >=0 ) {  // gzip browser 
      res.setHeader("Content-Encoding","gzip");
      OutputStream o=res.getOutputStream();
      GZIPOutputStream gz=new GZIPOutputStream(o);
      gz.write(content.getBytes());
      gz.close();
      o.close();
            } else {  // Some old browser -> give them plain text.                        PrintWriter o = res.getWriter();
                    o.println(content);
                    o.flush();
                    o.close();
            }
Sean A.O. Harney
  • 23,901
  • 4
  • 30
  • 30
0

On the PHP side this will strip out header and footer from file

function gzip_stream_uncompress($data) { return gzinflate(substr($data, 10, -8)); }

addr010
  • 31
  • 4