Someome tried gzip encoding of http responses?

583 views
Skip to first unread message

Philipp Brüll

unread,
Sep 2, 2012, 12:09:04 PM9/2/12
to ve...@googlegroups.com
Hi all,

I've implemented an asset server and tried to switch an gzip compression. Here is the code

response = request.response
response.headers["Content-Type"] = "text/javascript; charset=utf-8"
response.headers["Content-Encoding"] = "gzip"
response.end compress(code.to_s)

The compress method looks like this

def compress(data)
output = StringIO.new
gz = Zlib::GzipWriter.new output
gz.write data
gz.close
output.string
end

The asset is served, but Chrome drops a parser error after loading the asset. Any good idea?

Chrome Version 21.0.1180.89
JRuby 1.6.7.2
Vert.x 1.2.3

Best regards

Pid

unread,
Sep 3, 2012, 4:27:28 AM9/3/12
to ve...@googlegroups.com
On 02/09/2012 17:09, Philipp Brüll wrote:
> Hi all,
>
> I've implemented an asset server and tried to switch an gzip
> compression. Here is the code
>
> response = request.response
> response.headers["Content-Type"] = "text/javascript; charset=utf-8"
> response.headers["Content-Encoding"] = "gzip"
> response.end compress(code.to_s)

Have a look at the web-server module.

https://github.com/vert-x/mod-web-server/blob/master/src/main/java/org/vertx/mods/WebServer.java


p

> The compress method looks like this
>
> def compress(data)
>
> output = StringIO.new
>
> gz = Zlib::GzipWriter.new output
>
> gz.write data
>
> gz.close
>
> output.string
>
> end
>
>
> The asset is served, but Chrome drops a parser error after loading the
> asset. Any good idea?
>
> Chrome Version 21.0.1180.89
> JRuby 1.6.7.2
> Vert.x 1.2.3
>
> Best regards
>
> --
> You received this message because you are subscribed to the Google
> Groups "vert.x" group.
> To view this discussion on the web, visit
> https://groups.google.com/d/msg/vertx/-/oYJZ2FNeR08J.
> To post to this group, send an email to ve...@googlegroups.com.
> To unsubscribe from this group, send email to
> vertx+un...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/vertx?hl=en-GB.


--

[key:62590808]

signature.asc

Philipp Brüll

unread,
Sep 3, 2012, 6:20:22 AM9/3/12
to ve...@googlegroups.com
Thanks for the fast reply!
I've seen that, but I would like to gzip things on-the-fly, because some of the assets are created on-the-fly too.

I didn't digged into detail, but seems that the toString encoding of the gzipped output causes to problem (what a surprise :-) ). I've implemented a workaround by using directly to java classes and wrap the result into a Vertx::Buffer which than respond. Here is the code:

data = compress code.to_s
response = request.response
response.headers["Content-Type"] = "text/javascript; charset=utf-8"
response.headers["Content-Encoding"] = "gzip"
response.headers["Content-Length"] = data.length
response.write_buffer data
response.end

The conpress method:

def compress(data)
writer.write data
writer.flush
writer.close
stream.finish
Vertx::Buffer.new org.vertx.java.core.buffer.Buffer.new(buffer.to_byte_array)
end
 
I could think of a better solution, where the response is not processed at once and pumped into the response stream, but this is working for now. 
Reply all
Reply to author
Forward
0 new messages