curl 可以默认使用 https 吗?

Nat*_*rot 3 bash curl

我有一个脚本作为包装器curl:它接受 curl 的所有参数,但也添加一些自己的参数(如-H 'Content-Type: application/json'),然后对输出进行一些解析。

问题是 curl 接受curl google.com为 mean curl http://google.com。我想强制使用 HTTPS 连接,但我不想解析 curl 的命令行来查找和编辑主机名。(用户可能输入了curlwrapper -H "foo: bar" -XPOST google.com -d '{"hello":"world"}'

有什么方法可以告诉 curl“在没有给出 URL 方案时使用 HTTPS 连接”?

Fat*_*ror 5

由于 libcurl 在没有给出方案时如何确定要使用的协议,这似乎是不可能的。代码摘录:

  /*
   * Since there was no protocol part specified, we guess what protocol it
   * is based on the first letters of the server name.
   */

  /* Note: if you add a new protocol, please update the list in
   * lib/version.c too! */

  if(checkprefix("FTP.", conn->host.name))
    protop = "ftp";
  else if(checkprefix("DICT.", conn->host.name))
    protop = "DICT";
  else if(checkprefix("LDAP.", conn->host.name))
    protop = "LDAP";
  else if(checkprefix("IMAP.", conn->host.name))
    protop = "IMAP";
  else if(checkprefix("SMTP.", conn->host.name))
    protop = "smtp";
  else if(checkprefix("POP3.", conn->host.name))
    protop = "pop3";
  else {
    protop = "http";
  }
Run Code Online (Sandbox Code Playgroud)


myk*_*hal 4

缺少方案部分的 URL 的 HTTPS 协议(因此也绕过 @FatalError 的(过时)答案中提到的协议猜测)可以使用选项设置

--proto-default https

自 2015 年 10 月版本7.45.0起。另请参阅https://github.com/curl/curl/pull/351

可以将其放入 ~/.curlrc 中。

例子:

$ curl -v example.org
*   Trying XXXXIPv6redacted:80...
* Connected to example.org (XXXXIPv6redacted) port 80 (#0)
> GET / HTTP/1.1
...
Run Code Online (Sandbox Code Playgroud)
$ curl --proto-default https -v example.org                                                             
*   Trying XXXXIPv6redacted:443...
* Connected to example.org (XXXXIPv6redacted) port 443 (#0)
* ALPN: offers h2
...
Run Code Online (Sandbox Code Playgroud)

(请注意,这不是确保安全的神奇选项。例如,如果根据手册设置,它不会影响 http 代理。)

  • 噢!嗨!我为 Curl 写了那个补丁;感谢您更新这个问题。(对于任何想修复错误或向 Curl 添加功能的人来说:进行更改的过程非常简单 - Curl 维护者的审查非常友好且快速。) (2认同)