以编程方式设置Jetty GzipHandler [英] Setup Jetty GzipHandler programmatically

查看:178
本文介绍了以编程方式设置Jetty GzipHandler的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在玩Jetty GzipHandler,它似乎工作得很奇怪:它只压缩已经压缩的文件.

I'm playing with Jetty GzipHandler and it seems to work rather strangely: It only compresses already compressed files.

我的整个设置是

GzipHandler gzipHandler = new GzipHandler();
gzipHandler.setHandler(myHandler);
server.setHandler(gzipHandler);

浏览器(Chromium)总是 发送包含以下内容的标头

The browser (Chromium) always sends a header containing

Accept-Encoding:gzip,deflate,sdch

所以根据文档

GZIP处理程序如果出现以下情况,此处理程序将gzip响应的内容:

GZIP Handler This handler will gzip the content of a response if:

  • 过滤器已映射到匹配路径
  • 响应状态代码为> = 200和< 300
  • 内容长度未知或大于minGzipSize initParameter或minGzipSize为0(默认值)
  • 内容类型在mimeTypes initParameter中设置的mimeTypes的逗号分隔列表中,或者,如果未定义mimeTypes,则内容类型不是"application/gzip"
  • 资源未指定内容编码
  • The filter is mapped to a matching path
  • The response status code is >=200 and <300
  • The content length is unknown or more than the minGzipSize initParameter or the minGzipSize is 0 (default)
  • The content-type is in the comma separated list of mimeTypes set in the mimeTypes initParameter or if no mimeTypes are defined the content-type is not "application/gzip"
  • No content-encoding is specified by the resource

它应该对两个都起作用.我只是不确定path部分,但是在没有指定任何内容的情况下,我希望它对两者都起作用.

it should work for both. I'm only unsure about the path part, but without having specified any, I'd expect it to work for both or neither.

我使用了window.location.reload(true)来强制重新加载.标头相当长,因此,我将它们链接起来: css png .

I used window.location.reload(true) to force reload. The headers are rather long so, I'm linking them: css and png.

我尝试设置一些属性,但是没有成功.我应该找到 jetty-servlets-9.1.3.v20140225-sources.jar,我将对其进行调试.问题是:为什么GzipHandler决定仅压缩压缩文件?它完全是确定性的:jpgpng被压缩(无论大小如何),并且没有其他文件可以压缩.

I've tried to set some properties, but without any success. Should I ever find jetty-servlets-9.1.3.v20140225-sources.jar, I'll debug it. The question is: Why does GzipHandler decide to compress only compressed files? It's perfectly deterministic: jpg and png get compressed (no matter how small) and no other files do.

通过setMimeTypes我可以排除图像.我调试了它,但仍然不知道为什么其他静态资源永远不会被压缩.我再次检查了myHandler是否均等地对待它们(它们都可以直接从预先计算的Map<String, byte[]>中获得).

Via setMimeTypes I could exclude the images. I debugged it and I'm still having no clue, why other static resources never get compressed. I double checked that myHandler treats them all uniformly (they all get served directly from a precomputed Map<String, byte[]>).

推荐答案

我们可以使用GzipFilter并获得此结果. GzipFilter 的码头文档提供了许多支持的参数列表细节.要以编程方式启用它,请参考此问题.

We could use GzipFilter and achieve this result. Jetty documentation of GzipFilter provides list of parameters supported with lot of details. To enable it programmatically, refer this question.

GzipFilter基本上是服务器端过滤器,在处理压缩需求方面非常有效.

GzipFilter is basically a server side filter and is very efficient in handling compression needs.

样本过滤器配置

<filter>
 <filter-name>GZipFilter</filter-name>
 <display-name>Jetty's GZip Filter</display-name>
 <description>Filter that zips all the content on-the-fly</description>
 <filter-class>org.mortbay.servlet.GzipFilter</filter-class>
 <init-param>
  <param-name>mimeTypes</param-name>
  <param-value>text/html</param-value>
 </init-param>
</filter>

<filter-mapping>
 <filter-name>GZipFilter</filter-name>
 <url-pattern>/*</url-pattern>
</filter-mapping>

这篇关于以编程方式设置Jetty GzipHandler的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆