我使用Snappy(https://github.com/fdmanana/snappy-erlang-nif)作为Mochiweb应用程序上HTTP压缩的zlib替代品。
虽然Snappy一般对我有效,并且zlib在发送之前压缩响应工作正常,但使用Snappy作为zlib替换会导致客户端浏览器混淆响应。
这有效:
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.
这不是
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.
答案 0 :(得分:0)
没有一个浏览器支持snappy压缩;)你不能选择任何你喜欢的压缩算法,你必须选择浏览器支持的算法。大多数浏览器只支持deflate和gzip。