我正在使用 Snappy ( https://github.com/fdmanana/snappy-erlang-nif ) 作为 zlib 替代 Mochiweb 应用程序上的 HTTP 压缩。
虽然 Snappy 一般适用于我,并且 zlib 在发送响应之前可以很好地压缩响应,但使用 Snappy 作为 zlib 替代品会导致客户端浏览器混淆响应。
- Snappy 是否编码为 utf-8 字符集?
- 一般来说,客户端浏览器是否知道如何解压缩 Snappy 压缩文档?
- 我在做一些明显的事情吗?
这有效:
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.