程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了当请求Content-Type为gzip时,Jetty响应400错误请求大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决当请求Content-Type为gzip时,Jetty响应400错误请求?

开发过程中遇到当请求Content-Type为gzip时,Jetty响应400错误请求的问题如何解决?下面主要结合日常开发的经验,给出你关于当请求Content-Type为gzip时,Jetty响应400错误请求的解决方法建议,希望对你解决当请求Content-Type为gzip时,Jetty响应400错误请求有所启发或帮助;

首先,GzipFilter从web.xml中删除它不再相关(从Jetty 9.3.0开始)

GzipHandler是所有旧的过滤器基于gzip的过滤器新更换的(GzipFilterAsyncGzipFilter,和IncludableGzipFilter)。

它被设计为在连接流更基本的水平上起作用,这是基于过滤器的方法在异步I / O领域中无法做到的。

话虽如此… … GzipHandler在Jetty 9.3.0中仅具有针对Response正文内容设计的实现。(就像之前的GzipFilters一样)

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

我们在https://bugs.eclipse.org/471577上有一个有关此问题的开放错误(DropWizard为自己的BIDiGzipFilter扩展了GzipFilter)

我们认识到请求内容主体上的Gzip可能有一些有用的功能,但是我们还没有实现。 (提示,提示,在我们身上贴上补丁)

解决方法

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甚至支持压缩请求吗?(无法找到明确的证据)

我的配置出了什么问题?

大佬总结

以上是大佬教程为你收集整理的当请求Content-Type为gzip时,Jetty响应400错误请求全部内容,希望文章能够帮你解决当请求Content-Type为gzip时,Jetty响应400错误请求所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: