Question

I am using Snappy (https://github.com/fdmanana/snappy-erlang-nif) as a zlib replacement for HTTP compression on a Mochiweb application.

While Snappy works in general for me, and zlib works fine for compressing responses before sending them out, using Snappy as a zlib replacement results in client-browsers garbling the response.

  1. Does Snappy encode into utf-8 charset?
  2. Do client-browsers, in general, know how to decompress Snappy compressed documents?
  3. Am I doing something obviously supid?

This works:

success(Req, Code, Body) ->
    case iolist_size(Body) of
    N when N > 1024000 ->
        Data = zlib:gzip(Body),
        Req:respond({Code, [{"Vary","Accept-Encoding"},
                      {"Content-Encoding","gzip"},
                      {"Content-Type", "application/json"}], 
                     Data});
     _ ->
        Req:respond({Code, [{"Content-Type", "application/json"}], Body})
end.

This doesn't

success(Req, Code, Body) ->
    case iolist_size(Body) of
    N when N > 1024000 ->
        {ok, Data} = snappy:compress(Body),
        Req:respond({Code, [{"Vary","Accept-Encoding"},
                      {"Content-Encoding","snappy"},
                      {"Content-Type", "application/json"}], 
                     Data});
    _ ->
        Req:respond({Code, [{"Content-Type", "application/json"}], Body})
end.
Was it helpful?

Solution

There's not one browser which does support snappy compression ;) You can't choose any compression algorithm you like, you have to choose the algorithms browsers support. And most browsers only support deflate and gzip.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top