当请求 Content-Type 为 gzip 时,Jetty 响应 400 Bad Request

Jetty responds with 400 Bad Request when request Content-Type is gzip

Spring Jetty 9.3.0.v20150612 上的 MVC Java 1.8.0_45-b14 后端 运行 可以很好地处理未压缩的请求,但无法接受压缩的请求。

我已遵循 Gzip 处理程序配置说明 here,确保这些也支持 POST 请求。虽然它并没有说这个配置完全用于请求......它可能只用于响应。

etc/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 是否支持压缩请求? (找不到明确的证据)

我的配置有什么问题?

首先,从您的 web.xml it's not relevant anymore (starting with Jetty 9.3.0)

中删除 GzipFilter

GzipHandler 是所有基于旧过滤器的 gzip 过滤器 (GzipFilter, AsyncGzipFilter, and IncludableGzipFilter) 的新替代品。

它被设计为在连接流的基础级别上运行,这是基于过滤器的方法在异步世界中无法做到的事情 I/O。

话虽这么说……GzipHandler Jetty 9.3.0 中只有为响应正文内容设计的实现。 (就像之前的 GzipFilters)

然而,我们了解到,一些项目扩展了我们的 GzipFilter 以添加请求内容主体 gzip 处理。我们从 GzipFilter 到 GzipHandler 的更改中断了。

我们在 https://bugs.eclipse.org/471577(DropWizard 为他们自己的 BiDiGzipFilter 扩展了 GzipFilter)

我们认识到请求内容正文中可能有一些适用于 Gzip 的有用功能,但我们还没有相应的实现。 (提示,提示,给我们打个补丁)