当请求Content-Type为gzip时,Jetty响应400错误请求
问题内容:
Java 1.8.0_45-b14
运行在其上的Spring MVC 后端Jetty 9.3.0.v20150612
可以很好地处理未压缩的请求,但是无法接受压缩的请求。
我在这里遵循了Gzip
Handler配置说明,并确保POST
请求也支持这些说明。尽管它没有说此配置完全用于请求……它可能仅用于响应。
等/jetty-gzip.xml-
<Configure id="Server" class="org.eclipse.jetty.server.Server">
<Get id="next" name="handler" />
<Set name="handler">
<New id="GzipHandler" class="org.eclipse.jetty.server.handler.gzip.GzipHandler">
<Set name="handler"><Ref refid="next" /></Set>
<Set name="minGzipSize"><Property name="jetty.gzip.minGzipSize" deprecated="gzip.minGzipSize" default="2048"/></Set>
<Set name="checkGzExists"><Property name="jetty.gzip.checkGzExists" deprecated="gzip.checkGzExists" default="false"/></Set>
<Set name="compressionLevel"><Property name="jetty.gzip.compressionLevel" deprecated="gzip.compressionLevel" default="-1"/></Set>
<Set name="excludedAgentPatterns">
<Array type="String">
<Item><Property name="jetty.gzip.excludedUserAgent" deprecated="gzip.excludedUserAgent" default=".*MSIE.6\.0.*"/></Item>
</Array>
</Set>
<Set name="includedMethods">
<Array type="String">
<Item>GET</Item>
<Item>POST</Item>
</Array>
</Set>
<Set name="includedPaths">
<Array type="String">
<Item>/*</Item>
</Array>
</Set>
</New>
</Set>
</Configure>
在web.xml中-
<filter>
<filter-name>GzipFilter</filter-name>
<filter-class>org.eclipse.jetty.servlets.GzipFilter</filter-class>
<init-param>
<param-name>mimeTypes</param-name>
<param-value>text/html,text/plain,text/xml,application/xhtml+xml,text/css,application/javascript,image/svg+xml,application/json</param-value>
</init-param>
<init-param>
<param-name>minGzipSize</param-name>
<param-value>500</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>GzipFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Android客户端会发生这种情况,HTTP客户端应用(Paw)也会重现这种情况,这是一个请求示例-
POST /report?v=1 HTTP/1.1
Content-Encoding: gzip
Content-Type: application/json
Host: 10.0.0.1:8080
Connection: close
User-Agent: Paw/2.2.2 (Macintosh; OS X/10.10.4) GCDHTTPRequest
Content-Length: 5845
xí\MÇuÝûWÔE(É`_¦(<EtD&)%:¦OTè.EôÔU53¬¼ð"ÇYfÆ'®ì/áÿʽ¯ª
r(ʲä#èúz÷Ý÷^5èýR;Úå;ÕÎÿöºÊuW«ß«v«ß¿ø³:VÕ)Õ .. BINARY ...
回应-
HTTP/1.1 400 Bad Request
Content-Type: text/html;charset=iso-8859-1
Cache-Control: must-revalidate,no-cache,no-store
Content-Length: 242
Connection: close
Jetty甚至支持压缩请求吗?(无法找到明确的证据)
我的配置出了什么问题?
问题答案:
首先,GzipFilter
从web.xml中删除它不再相关(从Jetty
9.3.0开始)
GzipHandler
是所有旧的过滤器基于gzip的过滤器新更换的(GzipFilter
,AsyncGzipFilter
,和IncludableGzipFilter
)。
它被设计为在连接流更基本的水平上起作用,这是基于过滤器的方法在异步I / O领域中无法做到的。
话虽如此… … GzipHandler
在Jetty
9.3.0中仅具有针对Response正文内容设计的实现。(就像之前的GzipFilters一样)
但是,我们了解到的是,有些项目扩展了我们的GzipFilter,以添加请求内容主体gzip处理。从GzipFilter到GzipHandler的更改中断了。
我们在https://bugs.eclipse.org/471577上有一个有关此问题的开放错误(DropWizard为自己的BiDiGzipFilter扩展了GzipFilter)
我们认识到请求内容主体上的Gzip可能有一些有用的功能,但是我们还没有实现。 (提示,提示,在我们身上贴上补丁)