curl / Mailing Lists / curl-library / Single Mail
Buy commercial curl support from WolfSSL. We help you work out your issues, debug your libcurl applications, use the API, port to new platforms, add new features and more. With a team lead by the curl founder himself.

A canonical URL host name dilemma

From: Daniel Stenberg via curl-library <>
Date: Sat, 9 Oct 2021 11:42:44 +0200 (CEST)

Hello friends.

Let me take you through a bug, my current work and the little dilemma I'm
facing in regards to how to "canonicalize" host names in URLs! I'll end the
mail with a question about a possible solution I've thought of.

# Not parsing percent-encoded host names in URLs

     $ curl
     curl: (6) Could not resolve host:

instead of:

     $ curl
     [content from]


## Obvious first take

  Make sure that the URL parser **decodes** percent-encoded host names. %41
  becomes `A` etc.

  The parser rejects "control codes" while decoding. %00, %0a and %0d makes the
  host name illegal.

## Canonical host name

  The URL API can also *extract* the full URL so it needs to be able to reverse
  the process and here begins the challenges.

  My first simplistic (or maybe *naive*) approach works like this:

  Setting `` is extracted again as `` but
  setting `` is extracted as `` (since anything
  non-ASCII is not "URL compliant").

## IDN input

  Enter IDN. Internation Domain Names. They are specified outside of the
  regular URL spec (RFC 3986) and they are specified using non-ASCII byte

  Example name: `räksmörgå` (clients puny-encode this name to
  `` for DNS etc).

  Since this host/URL uses non-ASCII letters, the naive approch mentioned above
  would then, when the URL API is used to extract this again, use a sequence of
  percent-encoded UTF-8 ``.

  It would **not** extract back to `räksmörgå`, which probably is what a
  user will expect.

  Next-level complication: mix in percent-encoding to the IDN name:


  The two percent-encoded bytes is UTF-8 sequence for `ä`, which makes this
  host name work the same way.

## IDN output

  How do we know how to encode the host name when the user wants to extract it?

  Alternatives I can think of:

### A) Don't

  Store the originally provided name and use that for retrieval as well. This
  is bad as then the same URL with differently encoded host names will appear
  as two different ones. Users probably will not expect nor appreciate this.

### B) Always

  Always percent-encode (this is what the PR currently does). It makes the host
  name canonical and it still works IDN wise, but the retrieved URL is ugly and
  user hostile.

### C) Puny-encode

  Return the **puny-encoded** version of the name if it was an IDN name,
  otherwise percent-encode. Makes the host name canonical, it still works IDN
  wise, but the retrieved URL is ugly and user hostile. Just possibly a little
  less hostile than version B. An upside could be that a puny-code version of
  the host name works even with clients that don't speak IDN. This method then
  works differently if libcurl was built with or without IDN support.

### D) Heuristics

  If the host name was a valid IDN name, then return that name without
  encoding, otherwise perecent-encode. This makes `r%c3%a4ksmörgå` as input
  generate `räksmörgå` as output. This method then works differently if
  libcurl was built with or without IDN support.

Can we make version (D) work and would that be preferred?

  | Commercial curl support up to 24x7 is available!
  | Private help, bug fixes, support, ports, new features

Received on 2021-10-09