curl - 无法使用 curl 发送 HTTP/2 请求

标签 curl openssl osx-yosemite http2

我安装了支持 http2.0 协议(protocol)的 curl,我在版本信息中看到了 HTTP2。

$ ./curl --version
curl 7.42.1 (x86_64-apple-darwin14.5.0) libcurl/7.42.1 OpenSSL/1.0.2a zlib/1.2.5 nghttp2/0.7.13
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtsp smb smbs smtp smtps telnet tftp 
Features: IPv6 Largefile NTLM NTLM_WB SSL libz TLS-SRP HTTP2 UnixSockets

但是当我通过 curl(详细模式)发送请求时,我看到了 http1.1 协议(protocol)。

./curl -v --http2 https://http2bin.org/get
*   Trying 104.131.161.90...
* Connected to http2bin.org (104.131.161.90) port 443 (#0)
* ALPN, offering h2-14
* ALPN, offering http/1.1
* Cipher selection: ALL:!EXPORT:!EXPORT40:!EXPORT56:!aNULL:!LOW:!RC4:@STRENGTH
* successfully set certificate verify locations:
*   CAfile: /usr/local/etc/openssl/cert.pem
  CApath: none
* TLSv1.2, TLS Unknown, Unknown (22):
* TLSv1.2, TLS handshake, Client hello (1):
* SSLv2, Unknown (22):
* TLSv1.2, TLS handshake, Server hello (2):
* SSLv2, Unknown (22):
* TLSv1.2, TLS handshake, CERT (11):
* SSLv2, Unknown (22):
* TLSv1.2, TLS handshake, Server key exchange (12):
* SSLv2, Unknown (22):
* TLSv1.2, TLS handshake, Server finished (14):
* SSLv2, Unknown (22):
* TLSv1.2, TLS handshake, Client key exchange (16):
* SSLv2, Unknown (20):
* TLSv1.2, TLS change cipher, Client hello (1):
* SSLv2, Unknown (22):
* TLSv1.2, TLS handshake, Finished (20):
* SSLv2, Unknown (20):
* TLSv1.2, TLS change cipher, Client hello (1):
* SSLv2, Unknown (22):
* TLSv1.2, TLS handshake, Finished (20):
* SSL connection using TLSv1.2 / ECDHE-RSA-AES256-GCM-SHA384
* ALPN, server accepted to use h2-14
* Server certificate:
*    subject: OU=Domain Control Validated; OU=EssentialSSL; CN=www.http2bin.org
*    start date: 2015-02-21 00:00:00 GMT
*    expire date: 2016-02-21 23:59:59 GMT
*    subjectAltName: http2bin.org matched
*    issuer: C=GB; ST=Greater Manchester; L=Salford; O=COMODO CA Limited; CN=COMODO RSA Domain Validation Secure Server CA
*    SSL certificate verify ok.
* Using HTTP2
* Using HTTP2
* SSLv2, Unknown (23):
* SSLv2, Unknown (23):
* SSLv2, Unknown (23):
> GET /get HTTP/1.1
> Host: http2bin.org
> User-Agent: curl/7.42.1
> Accept: */*
> 
* http2_recv: 16384 bytes buffer
* SSLv2, Unknown (23):
* SSLv2, Unknown (23):
* http2_recv: 16384 bytes buffer
* SSLv2, Unknown (23):
* http2_recv: 16384 bytes buffer
* SSLv2, Unknown (23):
< HTTP/2.0 200
< server:h2o/1.6.1
< date:Wed, 03 Feb 2016 15:44:23 GMT
< content-type:application/json
< access-control-allow-origin:*
< access-control-allow-credentials:true
< x-clacks-overhead:GNU Terry Pratchett
< content-length:252
< 
{
  "args": {}, 
  "headers": {
    "Accept": "*/*", 
    "Connection": "keep-alive", 
    "Host": "http2bin.org", 
    "User-Agent": "curl/7.42.1", 
    "Via": "2 http2bin.org"
  }, 
  "origin": "193.238.96.12", 
  "url": "https://http2bin.org/get"
}
* Connection #0 to host http2bin.org left intact

现在 cURL 通过 HTTP/1.1 发送请求

如果我设置了 --http1.0 选项,那么 cURL 会通过 HTTP/1.0 发送请求。

为什么 cURL 不通过 HTTP/2.0 发送请求?

操作系统:OS X Yosemite (10.10.5)。

通过 HomeBrew 安装 cURL 和 OpenSSL。

谢谢。

最佳答案

它实际上已经发送和接收 HTTP/2(草案 14 版本,这是规范的预发布版本)。您应该考虑升级您的 curl 和 nghttp2 库,使其支持 HTTP/2 协议(protocol)的最终版本 (RFC 7540)。

服务器提示:

* ALPN, server accepted to use h2-14

curl 使用 HTTP/1 样式显示请求以使其看起来很熟悉,但在幕后它会将其转换为 HTTP/2 并将其发送出去。

然后服务器用 HTTP/2 响应:

< HTTP/2.0 200
< server:h2o/1.6.1
< date:Wed, 03 Feb 2016 15:44:23 GMT
< content-type:application/json
< access-control-allow-origin:*
[snip]

关于curl - 无法使用 curl 发送 HTTP/2 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35181564/

相关文章:

php - 有没有办法告诉 curl 不使用缓存

PHPSecLib 使用 openssl CA 证书不可验证

macos - 与应用程序扩展的核心数据并发

javascript - 使用 JXA 移动创建的文件

objective-c - 如何获取 NSWindow 标题栏的颜色,特别是当窗口失去焦点时?

javascript - 通过 Google Apps 脚本 POST 到 Todoist API 时出现 HTTP 错误 "AUTH_CSFR_ERROR"

c - fwrite动态大小

curl - 如何禁用命令行 Curl TCP_NODELAY 选项?

linux - 如何生成 Debian 弱 key ?

Java AES 128 加密方式与 openssl 不同