Project

General

Profile

Actions

Feature #22634

open

Add a WHATWG URL Standard parser to uri as URI::WHATWG_PARSER

Feature #22634: Add a WHATWG URL Standard parser to uri as URI::WHATWG_PARSER

Added by jinroq (Jinroq SAITOH) 4 days ago. Updated 4 days ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:<unknown>]

Description

Abstract

I propose adding a parser that follows the WHATWG URL Standard to the uri library, as URI::WHATWG_PARSER. It is an option: URI::DEFAULT_PARSER stays RFC 3986, and the behavior of existing code does not change.

URI::WHATWG_PARSER.parse("HTTP://EXAMPLE.com:80/a/../b c")
#=> #<URI::HTTP http://example.com/b%20c>
URI::WHATWG_PARSER.parse("http://0x7f.1/")
#=> #<URI::HTTP http://127.0.0.1/>
URI::WHATWG_PARSER.parse("http://日本語.jp/").host
#=> "xn--wgv71a119e.jp"
URI::WHATWG_PARSER.join("http://example.com/a/b", "../c")
#=> #<URI::HTTP http://example.com/c>

# It can also replace the default parser for the whole process.
URI.parser = URI::WHATWG_PARSER

Background

Web browsers, and the specifications built on them (HTML, Fetch), parse URLs by the WHATWG URL Standard, which differs from RFC 3986 in many places. A Ruby program that handles URLs coming from the web (crawlers, link checkers, HTTP clients, web frameworks) often needs to parse them as a browser does:

Input URI::WHATWG_PARSER URI::RFC3986_PARSER
"HTTP://EXAMPLE.com:80/a/../b c" http://example.com/b%20c URI::InvalidURIError
"http://0x7f.1/" http://127.0.0.1/ http://0x7f.1/
"http://日本語.jp/" http://xn--wgv71a119e.jp/ URI::InvalidURIError
"https:\\\\example.com\\a\\b" https://example.com/a/b URI::InvalidURIError
" http://example.com/\t" http://example.com/ URI::InvalidURIError
"http://EXAMPLE.com/" http://example.com/ URI::InvalidURIError

In #21617 (IDN support), naruse wrote that "Introducing WHATWG Parser is an option" and "I agree the direction to adopt WHATWG Parser by uri library", while keeping that ticket to IDN. This ticket is for the WHATWG parser itself. Since the URL Standard includes IDNA (domain to ASCII, UTS #46), the parser also covers the IDN conversion that #21617 asks for, for URLs parsed with it.

Proposal

  • URI::WHATWG_Parser, a parser class with the interface that URI.parser= expects (split, parse, join, regexp, extract, make_regexp, escape, unescape), and URI::WHATWG_PARSER, its instance.

  • parse returns instances of the existing classes: URI::HTTP, URI::HTTPS, URI::WS, URI::WSS and URI::File for their schemes, and URI::Generic for the others.

  • The components of a URI built by URI::WHATWG_PARSER are those of the URL record, already serialized, and its setters follow the setters of the URL Standard. A value that the URL Standard ignores raises URI::InvalidComponentError:

    uri = URI::WHATWG_PARSER.parse("http://example.com/")
    uri.host = "日本語.jp"
    uri.port = "8080abc"
    uri.to_s  #=> "http://xn--wgv71a119e.jp:8080/"
    uri.port = "abc"
    # raises URI::InvalidComponentError
    
  • ==, merge, route_to and route_from of such a URI follow the URL Standard (equality by serialization, resolution while parsing).

Updated by jinroq (Jinroq SAITOH) 4 days ago Actions #1

I have opened https://github.com/ruby/uri/pull/274 as an implementation. I would like to ask about four of its design decisions.

1. Where the Unicode data for IDNA comes from

The URL Standard converts a non-ASCII domain to ASCII with IDNA (UTS #46), so http://日本語.jp/ becomes http://xn--wgv71a119e.jp/. This is not an optional part of the standard: without it, a parser cannot claim to follow the URL Standard. IDNA needs Unicode data: for each code point, whether it is valid, mapped to other code points (for example, full-width letters to ASCII and uppercase to lowercase), ignored, or disallowed (UTS #46 calls this the IDNA Mapping Table), plus a few character properties.

There are two ways to get that data:

  • (a) Bundle it: generate a Ruby file from the Unicode Character Database. With the IDNA Mapping Table and the normalization tables, this file is about 340KB, a large addition to uri, whose lib/ is about 125KB today.
  • (b) Derive it from the Unicode data that Ruby already has. UTS #46 section 6 describes how the IDNA Mapping Table is derived from NFKC, case folding and a few character properties, and Ruby has all of them (String#unicode_normalize, String#downcase(:fold) and \p{...} in regular expressions). Only the Bidi_Class and Joining_Type values, which Ruby does not provide, need to be bundled: 17KB.

The pull request takes (b). For every code point that the running Ruby knows, from Ruby 2.5 (Unicode 10.0) to 4.1 (Unicode 17.0), the derived data is the same as the IDNA Mapping Table of Unicode 18.0. Its consequences are:

  • The result depends on the Unicode version of the running Ruby, as String#upcase does. A domain with a character added in a later Unicode version is invalid: for example, Ruby 4.1 (Unicode 17.0) rejects a domain with a CJK ideograph added in Unicode 18.0.
  • String#unicode_normalize cannot be called in a non-main Ractor, so NFC and NFKC are implemented in uri with the tables of lib/unicode_normalize/tables.rb (UnicodeNormalize::DECOMPOSITION_TABLE, KOMPATIBLE_TABLE, COMPOSITION_TABLE and CLASS_TABLE). These tables are not a public API, so uri would depend on an internal part of Ruby.

Is (b), with these consequences, acceptable? Or would you prefer (a), or another way?

2. How the URIs it builds follow the URL Standard

A URI built by URI::WHATWG_PARSER is an instance of the existing classes (URI::HTTP, URI::Generic and so on), but its setters, ==, normalize!, merge, route_to, route_from and to_s must follow the URL Standard, not RFC 3986. For example, uri.port = "8080abc" sets the port to 8080, as a browser does.

The pull request does this with a module prepended to URI::Generic, whose methods apply only to URIs that URI::WHATWG_PARSER built and call super for the others, so the behavior of other URIs does not change. lib/uri/generic.rb itself only gets a protected set_query and a require at its end. Is a prepended module acceptable, or would you prefer another way, such as subclasses for each scheme?

3. Invalid values given to setters, and some schemes

  • The URL Standard ignores an invalid value given to a setter: in a browser, url.port = "abc" leaves the URL unchanged without an error. The pull request raises URI::InvalidComponentError instead, as the setters of uri do, and also leaves the URI unchanged, so that a caller notices that the value was not set.
  • ftp, mailto and ldap URLs become URI::Generic, not URI::FTP, URI::MailTo or URI::LDAP. Those classes interpret their components by their own RFCs (for example, URI::FTP treats ;type= at the end of the path specially), which would give results that differ from the URL Standard. The port of an ftp URL is 21, its default port in the URL Standard.

Are these acceptable?

4. Load time of require "uri" on Ruby 3.x

The Unicode data above is loaded when a non-ASCII domain is first converted. But a require in a non-main Ractor is not safe before Ruby 4.0, so on Ruby 3.x the pull request loads the data with require "uri" instead, in case the first conversion happens in a Ractor. This makes require "uri" slower on Ruby 3.x, even for programs that never use URI::WHATWG_PARSER:

require "uri" master pull request
Ruby 3.4 22ms, RSS 14.4MB 54ms, RSS 18.8MB
Ruby 4.0 23ms, RSS 15.4MB 27ms, RSS 16.0MB

On Ruby 4.0 and later, the data is loaded on first use. Is this acceptable on Ruby 3.x?

Actions

Also available in: PDF Atom