1

I'm trying to gzip the output of my controller action to save some bandwidth:

new ByteArrayOutputStream().withStream{ baos ->
  new GZIPOutputStream( baos ).withWriter{ it << m.text.bytes }
  //def gzip = baos.toByteArray().encodeBase64()
  def gzip = new String( baos.toByteArray() )
  response.setHeader 'Content-Type', 'application/x-javascript'
  response.setHeader 'Content-Encoding', 'x-gzip'
  response.outputStream.withStream{ it << gzip }
}

}

when I open the url in a browser it gives me

Unknown Error: net::ERR_CONTENT_DECODING_FAILED

in IE or

Content Encoding Error

in FF

What am I missing?

injecteer
  • 16,220
  • 3
  • 39
  • 72
  • Can you check if you are not saving you file with an utf-8 BOM? (http://stackoverflow.com/questions/2223882/whats-different-between-utf-8-and-utf-8-without-bom) You need a good editor to notice the difference. – e-motiv Dec 30 '14 at 17:50
  • Also if php error_reporting is on, this can give problems. – e-motiv Dec 31 '14 at 15:50
  • no, there's no BOM problem and it has nothing to do with php – injecteer Dec 31 '14 at 15:55

1 Answers1

1
    def index() {
            response.setHeader 'Content-Type', 'application/x-javascript'
            response.setHeader 'Content-Encoding', 'x-gzip'
            new GZIPOutputStream(response.outputStream).withWriter{ it << "Content comes here" }
    }

also consider using the capabilities of a webserver in front of your webapp (e.g. apache's gzip module can handle things like this way better). you would also have to check for the capabilities of the client first (Accept-Encoding header in the client request)

cfrick
  • 29,743
  • 4
  • 46
  • 59