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

curl post request is not working with option --http2, but it works fine when I use --http2-prior-knowledge

我已经使用 tomcat 9.0.16、spring-boot 2.1.3.RELEASE、JDK1.8 创建了 spring-boot 应用程序。 当我用 --http2 发出 curl post 请求时,它说 "curl: (56) Recv failure: Connection reset by peer".

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

我的application.property文件

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

和配置文件

@Bean
    public WebServerFactoryCustomizer tomcatCustomizer() {
        return (container) -> {
            if (container instanceof TomcatServletWebServerFactory) {
                ((TomcatServletWebServerFactory) container)
                        .addConnectorCustomizers((connector) -> {
                            connector.addUpgradeProtocol(new Http2Protocol());
                        });
            }
        };
    }
  1. for curl -vvv --http2 -H 'Content-Type: application/json' -H 'cache-control: no-cache' -XPOST http://localhost:8080/save -d '{"xyz":"xyz"}' curl 的日志->
*   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 'Content-Type: application/json' -H 'Postman-Token: 52e0708b-ce97-4baa-a567-2dabc675f3dd' -H 'cache-control: no-cache' -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%

您不能使用 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.

中均成功