HTTP请求压缩



一般用例

想象一下,一个客户端正在上传大量的JSON。内容类型应保持为application/json,因为它描述了实际数据。Accept Encoding和Transfer Encoding似乎是为了告诉服务器应该如何格式化响应。响应似乎为此明确使用了内容编码标头,但它不是有效的请求标头。

我有什么东西不见了吗?有人找到一个优雅的解决方案吗?

特定用例

我的用例是,我有一个移动应用程序,它正在生成大量JSON(在某些情况下还有一些二进制数据,但程度较小),压缩请求可以节省大量带宽。我使用Tomcat作为Servlet容器。我使用Spring的MVC注释主要是为了将一些JEE内容抽象成一个更干净、基于注释的接口。我还使用Jackson进行自动(反)序列化。

我也使用nginx,但我不确定这是否是我想要解压的地方。nginx节点只是平衡请求,然后通过数据中心进行分配。在它真正到达要处理它的节点之前,保持它的压缩状态也是很好的

提前感谢

约翰·

编辑:

我和@DaSourcerer之间的讨论对那些在撰写本文时对现状感到好奇的人来说真的很有帮助。

我最终实现了自己的解决方案。注意,这指定了分支";ohmage-3.0";,但它很快就会被合并到master分支中。你可能想在那里查看我是否进行了任何更新/修复。

https://github.com/ohmage/server/blob/ohmage-3.0/src/org/ohmage/servlet/filter/DecompressionFilter.java

[Content Encoding]似乎不是有效的请求标头。

事实并非如此。根据RFC 2616,第14.11节,Content-Encoding实体标头,这意味着它可以应用于http响应和请求的实体。通过多部分MIME消息的功能,甚至可以压缩请求(或响应)的部分

然而,Web服务器对压缩请求体的支持相当少。Apache通过mod_deflate模块在一定程度上支持它。我还不完全清楚nginx是否可以处理压缩请求。

因为原始代码不再可用。万一有人来这里需要的话。我使用"Content-Encoding:gzip"来确定过滤器是否需要解压缩。

这是代码。

 @Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException
{
    HttpServletRequest httpServletRequest = (HttpServletRequest) request;
    String contentEncoding = httpServletRequest.getHeader("Content-Encoding");
    if (contentEncoding != null && contentEncoding.indexOf("gzip") > -1)
    {
        try
        {
            final InputStream decompressStream = StreamHelper.decompressStream(httpServletRequest.getInputStream());
            httpServletRequest = new HttpServletRequestWrapper(httpServletRequest)
            {
                @Override
                public ServletInputStream getInputStream() throws IOException
                {
                    return new DecompressServletInputStream(decompressStream);
                }
                @Override
                public BufferedReader getReader() throws IOException
                {
                    return new BufferedReader(new InputStreamReader(decompressStream));
                }
            };
        }
        catch (IOException e)
        {
            mLogger.error("error while handling the request", e);
        }
    }
    chain.doFilter(httpServletRequest, response);
}

简单Servlet InputStream包装类

public static class DecompressServletInputStream extends ServletInputStream
{
    private InputStream inputStream;
    public DecompressServletInputStream(InputStream input)
    {
        inputStream = input;
    }
    @Override
    public int read() throws IOException
    {
        return inputStream.read();
    }
}

解压缩流代码

public class StreamHelper
{
    /**
     * Gzip magic number, fixed values in the beginning to identify the gzip
     * format <br>
     * http://www.gzip.org/zlib/rfc-gzip.html#file-format
     */
    private static final byte GZIP_ID1 = 0x1f;
    /**
     * Gzip magic number, fixed values in the beginning to identify the gzip
     * format <br>
     * http://www.gzip.org/zlib/rfc-gzip.html#file-format
     */
    private static final byte GZIP_ID2 = (byte) 0x8b;
    /**
     * Return decompression input stream if needed.
     * 
     * @param input
     *            original stream
     * @return decompression stream
     * @throws IOException
     *             exception while reading the input
     */
    public static InputStream decompressStream(InputStream input) throws IOException
    {
        PushbackInputStream pushbackInput = new PushbackInputStream(input, 2);
        byte[] signature = new byte[2];
        pushbackInput.read(signature);
        pushbackInput.unread(signature);
        if (signature[0] == GZIP_ID1 && signature[1] == GZIP_ID2)
        {
            return new GZIPInputStream(pushbackInput);
        }
        return pushbackInput;
    }
}

发送时添加到您的头中:

JSON : "Accept-Encoding" : "gzip, deflate"

客户代码:

HttpUriRequest request = new HttpGet(url);
request.addHeader("Accept-Encoding", "gzip");

@JulianReschke指出,可能存在以下情况:

"Content-Encoding" : "gzip, gzip"

因此扩展的服务器代码将是:

InputStream in = response.getEntity().getContent();
Header encodingHeader = response.getFirstHeader("Content-Encoding");
String gzip = "gzip";
if (encodingHeader != null) {
    String encoding = encodingHeader.getValue().toLowerCase();
    int firstGzip = encoding.indexOf(gzip);
    if (firstGzip > -1) {
      in = new GZIPInputStream(in);
      int secondGzip = encoding.indexOf(gzip, firstGzip + gzip.length());
      if (secondGzip > -1) {
        in = new GZIPInputStream(in);
      }
    }
}

我假设nginx被用作负载均衡器或代理,所以你需要设置tomcat来进行解压缩。

将以下属性添加到Tomcat、上server.xml中的连接器

<Connector 
compression="on"
compressionMinSize="2048"
compressableMimeType="text/html,application/json"
... />

在tomcat中接受gzipped请求是另一回事。您必须在servlet前面放置一个过滤器,以启用请求解压缩。你可以在这里找到更多关于它的信息。

最新更新