curl post请求不适用于选项--http2,但是当我使用--http2-prior-knowledge时,它工作正常

时间:2019-03-28 06:38:20

标签: spring-boot curl tls1.2 http2 tomcat9

我已经用tomcat 9.0.16,spring-boot 2.1.3.RELEASE,JDK1.8创建了spring-boot应用程序。 当我使用--http2发出curl post请求时,它说“ curl:(56)Recv失败:对等重置连接”。

但是当我使用--http-prior-knowledge时,它工作正常。

我的application.property文件

server.port=8080
server.http2.enabled=true

和congif文件

@Bean
    public WebServerFactoryCustomizer tomcatCustomizer() {
        return (container) -> {
            if (container instanceof TomcatServletWebServerFactory) {
                ((TomcatServletWebServerFactory) container)
                        .addConnectorCustomizers((connector) -> {
                            connector.addUpgradeProtocol(new Http2Protocol());
                        });
            }
        };
    }
  1. 用于curl -vvv --http2 -H'内容类型:application / json'-H'缓存控制:no-cache'-XPOST http://localhost:8080/save -d'{“ xyz”:“ xyz” }' curl->
  2. 的日志
*   Trying ::1...
* TCP_NODELAY set
* Expire in 150000 ms for 3 (transfer 0x7fc78a808a00)
* Expire in 200 ms for 4 (transfer 0x7fc78a808a00)
* Connected to localhost (::1) port 8080 (#0)
> POST /save HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.64.0
> Accept: */*
> Connection: Upgrade, HTTP2-Settings
> Upgrade: h2c
> HTTP2-Settings: AAMAAABkAARAAAAAAAIAAAAA
> Content-Type: application/json
> Postman-Token: 52e0708b-ce97-4baa-a567-2dabc675f3dd
> cache-control: no-cache
> Content-Length: 702
>
* upload completely sent off: 702 out of 702 bytes
< HTTP/1.1 101
< Connection: Upgrade
< Upgrade: h2c
< Date: Wed, 27 Mar 2019 12:29:18 GMT
* Received 101
* Using HTTP2, server supports multi-use
* Connection state changed (HTTP/2 confirmed)
* Copying HTTP/2 data in stream buffer to connection buffer after upgrade: len=0
* Connection state changed (MAX_CONCURRENT_STREAMS == 200)!
* Recv failure: Connection reset by peer
* Failed receiving HTTP2 data
* Send failure: Broken pipe
* Failed sending HTTP2 data
* Connection #0 to host localhost left intact
curl: (56) Recv failure: Connection reset by peer
  1. curl -vvv --http2-prior-knowledge -H'内容类型:application / json'-H'邮递员令牌:52e0708b-ce97-4baa-a567-2dabc675f3dd'-H'缓存控制:无-缓存” -XPOST http://localhost:8080/save -d'{“ xyz”:“ xyz”}'
* Expire in 0 ms for 6 (transfer 0x7fc5c0808a00)
*   Trying ::1...
* TCP_NODELAY set
* Expire in 150000 ms for 3 (transfer 0x7fc5c0808a00)
* Expire in 200 ms for 4 (transfer 0x7fc5c0808a00)
* Connected to localhost (::1) port 8080 (#0)
* Using HTTP2, server supports multi-use
* Connection state changed (HTTP/2 confirmed)
* Copying HTTP/2 data in stream buffer to connection buffer after upgrade: len=0
* Using Stream ID: 1 (easy handle 0x7fc5c0808a00)
> POST /save HTTP/2
> Host: localhost:8080
> User-Agent: curl/7.64.0
> Accept: */*
> Content-Type: application/json
> Postman-Token: 52e0708b-ce97-4baa-a567-2dabc675f3dd
> cache-control: no-cache
> Content-Length: 702
>
* We are completely uploaded and fine
* Connection state changed (MAX_CONCURRENT_STREAMS == 200)!
< HTTP/2 200
< content-type: application/json;charset=UTF-8
< date: Wed, 27 Mar 2019 12:32:26 GMT
<
* Connection #0 to host localhost left intact
true%

1 个答案:

答案 0 :(得分:1)

您不能使用POST方法执行HTTP / 1.1升级,因此Tomcat可能由于这个原因而阻塞了您的第一个请求(curl --http2 ...)。

我是Jetty的HTTP / 2实现者,在这种情况下,Jetty也不升级到HTTP / 2,尽管它以HTTP / 1.1 200响应请求,而不是阻塞。

将第一个请求转换为不带任何内容的GET,按预期,该升级在Jetty中成功,并带有HTTP / 1.1 101响应。

第二个请求不是HTTP / 1.1升级,而是先验的HTTP / 2请求;没有升级,因此对可以使用的HTTP方法没有限制,因此该请求在Jetty和Tomcat中均成功。