Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can curl default to using https?

Tags:

bash

curl

I have a script which acts as a wrapper around curl: it accepts all of curl's arguments but also adds some of its own (like -H 'Content-Type: application/json'), and then it does some parsing of the output.

The problem is that curl accepts curl google.com as meaning curl http://google.com. I want to force an HTTPS connection, but I don't want to parse curl's command line to find and edit the hostname. (The user might have typed curlwrapper -H "foo: bar" -XPOST google.com -d '{"hello":"world"}')

Is there any way to tell curl "use an HTTPS connection when you're not given a URL scheme"?

like image 214
Nathaniel Waisbrot Avatar asked Aug 30 '26 00:08

Nathaniel Waisbrot


2 Answers

HTTPS protocol for URL with missing scheme part (and thus also bypass protocol guessing mentioned in (obsolete) answer by @FatalError) can be set with option

--proto-default https

since version 7.45.0 from October 2015. See also https://github.com/curl/curl/pull/351.

It can be put into ~/.curlrc.

Example:

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

(Note that it's not a magic option to assure security. It e.g. won't affect http proxy, if set, according to the manual.)

like image 159
mykhal Avatar answered Aug 31 '26 22:08

mykhal


It does not appear to be possible due to how libcurl determines the protocol to use when no scheme is given. An excerpt from the code:

  /*
   * 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";
  }
like image 32
FatalError Avatar answered Aug 31 '26 23:08

FatalError



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!