Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
75 commits
Select commit Hold shift + click to select a range
98c7733
Add URI::WHATWG_PARSER as an opt-in parser
jinroq Nov 3, 2026
9f1ca6c
Run WPT urltestdata.json against URI::WHATWG_PARSER
jinroq Nov 3, 2026
87be3d7
Preprocess input in URI::WHATWG_Parser#split
jinroq Nov 3, 2026
c031133
Parse the scheme in URI::WHATWG_Parser#split
jinroq Nov 3, 2026
551c558
Parse the authority in URI::WHATWG_Parser#split
jinroq Nov 3, 2026
75e22de
Parse domains and opaque hosts in URI::WHATWG_Parser#split
jinroq Nov 3, 2026
cee9979
Parse IPv4 hosts in URI::WHATWG_Parser#split
jinroq Nov 3, 2026
f1a51e2
Parse IPv6 hosts in URI::WHATWG_Parser#split
jinroq Nov 3, 2026
ae7125b
Parse the port in URI::WHATWG_Parser#split
jinroq Nov 3, 2026
f84e158
Split the query and fragment in URI::WHATWG_Parser#split
jinroq Nov 3, 2026
b63d69e
Parse and normalize the path in URI::WHATWG_Parser#split
jinroq Nov 3, 2026
1bae0bd
Percent-encode components in URI::WHATWG_Parser#split
jinroq Nov 3, 2026
7c924e8
Build a URL record in URI::WHATWG_Parser before splitting
jinroq Nov 3, 2026
f75397d
Resolve relative references in URI::WHATWG_Parser#join
jinroq Nov 3, 2026
1bb2857
Keep the query of URI::WHATWG_Parser results unescaped
jinroq Nov 3, 2026
de45375
Use URI::Generic for schemes whose classes disagree with WHATWG
jinroq Nov 3, 2026
4ada2c4
Parse file URLs in URI::WHATWG_Parser
jinroq Nov 3, 2026
35abfaf
Add a WPT workflow that runs weekly and on every change
jinroq Nov 3, 2026
705c1d1
Resolve URI#merge with URI::WHATWG_Parser#join for WHATWG URIs
jinroq Nov 3, 2026
b9db5d3
Give the remaining WPT exclusions their reason
jinroq Nov 3, 2026
756e3f8
Remove the unused host_required parameter from parse_authority
jinroq Nov 3, 2026
eab9592
Document URI::WHATWG_PARSER
jinroq Nov 3, 2026
21b105d
Avoid a void-context warning in a WHATWG_Parser merge test
jinroq Nov 3, 2026
68f27e3
Add Punycode encoding and decoding for WHATWG_Parser
jinroq Nov 18, 2026
c53f5c3
Generate the Unicode data for domain to ASCII
jinroq Nov 18, 2026
af9af3e
Write non-ASCII strings in WHATWG_Parser tests as escapes
jinroq Nov 18, 2026
ad71abc
Implement domain to ASCII for non-ASCII domains
jinroq Nov 18, 2026
03e860d
Run Unicode's IdnaTestV2.txt in rake test:wpt
jinroq Nov 18, 2026
00f1ab1
Convert non-ASCII domains to ASCII in WHATWG_Parser
jinroq Nov 18, 2026
8c93d88
Encode Punycode in O(n log n) time
jinroq Nov 18, 2026
0b91dbd
Report a newer Unicode version than the IDNA data in rake test:wpt
jinroq Nov 18, 2026
108b48a
Generate the Unicode data for NFC normalization
jinroq Nov 18, 2026
467eb62
Add NFC normalization with the IDNA Unicode data
jinroq Nov 18, 2026
9d08f95
Run Unicode's NormalizationTest.txt in rake test:wpt
jinroq Nov 18, 2026
538fb7c
Normalize domains with URI::WHATWG_Parser::NFC
jinroq Nov 18, 2026
4854d55
Run WPT toascii.json in rake test:wpt
jinroq Nov 18, 2026
a700d6b
Describe URI::WHATWG_PARSER in the README
jinroq Nov 18, 2026
7219974
Follow the setters of the URL Standard for WHATWG URIs
jinroq Nov 18, 2026
09a876d
Let WPT::Exclusions identify cases by other fields
jinroq Nov 18, 2026
981c5ea
Remove the port only for an empty value in port=
jinroq Nov 18, 2026
2f6af2d
Run WPT setters_tests.json in rake test:wpt
jinroq Nov 18, 2026
bd72da1
Document the setters of WHATWG URIs
jinroq Nov 18, 2026
11a6a99
Port WPT url-setters-stripping.any.js
jinroq Nov 18, 2026
3ff9ca2
Follow the URL Standard in userinfo= and opaque= of WHATWG URIs
jinroq Nov 18, 2026
94f8cf0
Convert the values of WHATWG setters to UTF-8
jinroq Nov 18, 2026
50a3d6d
Compare WHATWG URIs by their serializations
jinroq Nov 18, 2026
67ad222
Raise BadURIError when merging into a relative WHATWG URI
jinroq Nov 18, 2026
ab6e960
Raise BadURIError for a relative reference in WHATWG routes
jinroq Nov 18, 2026
5a2fdb0
Load the Unicode data with uri where Ractors cannot require it safely
jinroq Nov 18, 2026
a76fb7e
Rebuild the URL record of a WHATWG URI from its components
jinroq Nov 18, 2026
9baf41c
Share the conversion of port digits between parsing and port=
jinroq Nov 18, 2026
ff6472a
Find the host of host= as the parser does
jinroq Nov 18, 2026
2860adb
Share the host of a file URL between parsing and host=
jinroq Nov 18, 2026
fbc7d3d
Keep WHATWG URIs as they are in normalize
jinroq Nov 18, 2026
87e0475
Move the WHATWG branches of merge and routes out of URI::Generic
jinroq Nov 18, 2026
98f0507
Rename GenericSetters to URLMethods
jinroq Nov 18, 2026
41a6ce3
Avoid Array#filter_map in the NormalizationTest harness
jinroq Nov 18, 2026
8f59753
Run the tests of the WPT harness in rake test
jinroq Nov 18, 2026
e7426df
Serialize a postgres WHATWG URL without a host without "//"
jinroq Nov 18, 2026
4d0bdd3
Return false from == of a WHATWG URI and an RFC 3986 URI
jinroq Nov 18, 2026
705d0eb
Parse a URI of another parser given to WHATWG_Parser#join
jinroq Nov 18, 2026
3ee4643
Run the hostname setter checks of WPT toascii.json
jinroq Nov 18, 2026
5dbac4e
Find the route between WHATWG URLs by resolving candidates
jinroq Nov 18, 2026
9fb0027
Check the routes of WPT urltestdata.json cases
jinroq Nov 18, 2026
cc8f453
Document that scheme= keeps the class of a WHATWG URI
jinroq Nov 18, 2026
89709da
Decode Punycode without inserting each code point into an array
jinroq Nov 18, 2026
4deb5f9
Merge branch 'master' into whatwg-parser
jinroq Nov 23, 2026
8d7b658
Build the NFC tables from those of String#unicode_normalize
jinroq Nov 24, 2026
0466d7a
Skip IdnaTestV2 successes with code points unknown to Ruby
jinroq Nov 24, 2026
7af0e3f
Derive the IDNA mapping from the Unicode data of Ruby
jinroq Nov 24, 2026
80c9a0c
Freeze the IDS operator pattern of IDNA
jinroq Nov 24, 2026
e131a58
Document that IDNA uses the Unicode data of Ruby
jinroq Nov 24, 2026
48acafd
Disallow code points newer than the generated IDNA data
jinroq Nov 24, 2026
8594733
Derive the IDNA status of a code point once per domain
jinroq Nov 24, 2026
1df20f4
Document the schemes that become URI::Generic with WHATWG_PARSER
jinroq Nov 24, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions .github/workflows/wpt.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: WPT

# Runs web-platform-tests' url/resources/urltestdata.json, toascii.json and
# setters_tests.json, fetched from WPT master, against URI::WHATWG_PARSER
# (rake test:wpt). The same task runs Unicode's IdnaTestV2.txt for the
# Unicode version of the generated IDNA data and NormalizationTest.txt for
# that of Ruby, and fails when Unicode has released a newer version than
# the generated data.
#
# The URL Standard is a living standard and WPT follows its changes, so the
# scheduled run is how a change in the standard is noticed: it fails when a
# case newer than test/wpt/*_exclusions.json fails, or when an excluded case
# passes or disappears. On push and pull requests a failure is reported
# without failing the checks, since it can come from WPT rather than from
# the change being tested. (GitHub runs scheduled workflows on the
# default branch only, and notifies of a failed scheduled run.)

on:
push:
pull_request:
schedule:
- cron: '17 3 * * 1'
workflow_dispatch:

permissions:
contents: read

jobs:
wpt:
runs-on: ubuntu-latest
continue-on-error: ${{ github.event_name == 'push' || github.event_name == 'pull_request' }}
steps:
- uses: actions/checkout@v7.0.1
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: ruby
- run: bundle install --jobs 4 --retry 3
- name: Run WPT urltestdata
run: bundle exec rake test:wpt
52 changes: 52 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,62 @@ uri.fragment #=> "time=1305298413"
uri.to_s #=> "http://foo.com/posts?id=30&limit=5#time=1305298413"
```

### WHATWG URL Standard

The default parser follows RFC 3986. `URI::WHATWG_PARSER` parses URLs as web browsers do, following the [WHATWG URL Standard](https://url.spec.whatwg.org/), including domain to ASCII (IDNA, [UTS #46](https://www.unicode.org/reports/tr46/)) for non-ASCII domains. IDNA uses the Unicode data of the running Ruby, so a domain with a character added in a later version of Unicode than `RbConfig::CONFIG["UNICODE_VERSION"]`, or than that of the data bundled for IDNA (18.0.0), is invalid:

```ruby
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"

# A relative reference is resolved against a base URL.
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:

```ruby
URI.parser = URI::WHATWG_PARSER
URI("http://EXAMPLE.com/")
#=> #<URI::HTTP http://example.com/>
URI.parser = URI::RFC3986_PARSER
```

Only http, https, ws, wss and file URLs become instances of their scheme's class. Other schemes, ftp, mailto and ldap among them, become `URI::Generic`, since `URI::FTP`, `URI::MailTo` and `URI::LDAP` interpret their components by their own RFCs. With `URI.parser = URI::WHATWG_PARSER`, code that relies on those classes no longer works for such URLs: for example, `URI.open("ftp://...")` of open-uri, which opens a `URI::FTP` only, tries to open a local file instead and raises `Errno::ENOENT`.

The setters of a URI built by `URI::WHATWG_PARSER` follow the setters of the URL Standard. A value that the URL Standard ignores raises `URI::InvalidComponentError`:

```ruby
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
```

As for any URI, `scheme=` does not change the class of the object. Parse the URI again to get the class of its new scheme, before handing it to code that checks the class, such as open-uri, which uses TLS for a `URI::HTTPS` only:

```ruby
uri = URI::WHATWG_PARSER.parse("http://example.com/")
uri.scheme = "https"
uri #=> #<URI::HTTP https://example.com/>
URI::WHATWG_PARSER.parse(uri.to_s) #=> #<URI::HTTPS https://example.com/>
```

## Development

After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.

`rake test:wpt` runs the conformance tests of `URI::WHATWG_PARSER`: web-platform-tests' `urltestdata.json`, `toascii.json` and `setters_tests.json`, and Unicode's `IdnaTestV2.txt` and `NormalizationTest.txt` (the latter for the Unicode version of the running Ruby; the cases of the former that succeed with a character unknown to it are not run). It fetches them over the network, unless `WPT_URLTESTDATA`, `WPT_TOASCII`, `WPT_SETTERS`, `IDNA_TEST_V2` and `NORMALIZATION_TEST` name local files. The cases that are known to fail are listed in `test/wpt/urltestdata_exclusions.json` and `test/wpt/setters_tests_exclusions.json`; regenerate them with `rake test:wpt:update_exclusions`.

The Unicode data that IDNA needs and Ruby has not, the Bidi_Class and Joining_Type values in `lib/uri/whatwg_parser/idna_data.rb`, is generated by `rake idna:generate`. Set `UNICODE_VERSION` (for example `UNICODE_VERSION=18.0.0`) to generate it for another version of Unicode.

To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).

## Contributing
Expand Down
4 changes: 3 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ require "rake/testtask"
Rake::TestTask.new(:test) do |t|
t.libs << "test/lib"
t.ruby_opts << "-rhelper"
t.test_files = FileList["test/**/test_*.rb"]
# The WPT and Unicode data tests need the network and run in rake
# test:wpt; the tests of their harness do not.
t.test_files = FileList["test/**/test_*.rb"].exclude(%r{\Atest/wpt/(?!test_\w+_harness\.rb\z)})
end

require "rdoc/task"
Expand Down
57 changes: 57 additions & 0 deletions lib/uri/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

require_relative "rfc2396_parser"
require_relative "rfc3986_parser"
require_relative "whatwg_parser"

module URI
# The default parser instance for RFC 2396.
Expand All @@ -21,6 +22,62 @@ module URI
RFC3986_PARSER = RFC3986_Parser.new
Ractor.make_shareable(RFC3986_PARSER) if defined?(Ractor)

# The parser instance for the WHATWG URL Standard
# (https://url.spec.whatwg.org/), which browsers follow. It is not the
# default; use it directly, or switch to it with
# <tt>URI.parser = URI::WHATWG_PARSER</tt>.
#
# 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/>
#
# A relative reference is only meaningful against a base URL, so it is
# resolved with #join or URI::Generic#merge rather than parsed alone:
#
# URI::WHATWG_PARSER.join("http://example.com/a/b", "../c")
# # => #<URI::HTTP http://example.com/c>
# URI::WHATWG_PARSER.parse("http://example.com/a") + "b"
# # => #<URI::HTTP http://example.com/b>
# URI::WHATWG_PARSER.parse("/relative")
# # raises URI::InvalidURIError
#
# Only http, https, ws, wss and file URLs become instances of their
# scheme's class; other schemes become URI::Generic. A non-ASCII domain
# is converted to ASCII (IDNA, UTS #46):
#
# URI::WHATWG_PARSER.parse("http://\u{65E5}\u{672C}\u{8A9E}.jp/").host
# # => "xn--wgv71a119e.jp"
#
# IDNA uses the Unicode data of the running Ruby, as String#upcase
# does, so a domain with a character added in a later version of
# Unicode than RbConfig::CONFIG["UNICODE_VERSION"], or than that of the
# data bundled for IDNA, is invalid.
#
# As ftp, mailto and ldap URLs become URI::Generic, code that relies on
# URI::FTP, URI::MailTo or URI::LDAP, such as URI.open of open-uri for
# ftp, does not work for them.
#
# The setters of a URI it builds follow the setters of the URL
# Standard, and raise URI::InvalidComponentError for a value that the
# URL Standard ignores:
#
# uri = URI::WHATWG_PARSER.parse("http://example.com/")
# uri.port = "8080abc"
# uri.to_s
# # => "http://example.com:8080/"
#
# As for any URI, scheme= does not change the class of the object.
# Parse the URI again to get the class of its new scheme:
#
# uri.scheme = "https"
# uri
# # => #<URI::HTTP https://example.com:8080/>
# URI::WHATWG_PARSER.parse(uri.to_s)
# # => #<URI::HTTPS https://example.com:8080/>
WHATWG_PARSER = WHATWG_Parser.new
Ractor.make_shareable(WHATWG_PARSER) if defined?(Ractor)

# The default parser instance.
DEFAULT_PARSER = RFC3986_PARSER
Ractor.make_shareable(DEFAULT_PARSER) if defined?(Ractor)
Expand Down
21 changes: 21 additions & 0 deletions lib/uri/generic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -866,6 +866,24 @@ def query=(v)
@query = v
end

#
# == Args
#
# +v+::
# String
#
# == Description
#
# Protected setter for the query component +v+, without the escaping
# done by #query=.
#
# See also URI::Generic.query=.
#
def set_query(v)
@query = v
end
protected :set_query

#
# Checks the opaque +v+ component for RFC2396 compliance and
# against the +parser+ Regexp for :OPAQUE.
Expand Down Expand Up @@ -1215,6 +1233,7 @@ def route_from_path(src, dst)
# :startdoc:

# :stopdoc:

def route_from0(oth)
oth = parser.__send__(:convert_to_uri, oth)
if self.relative?
Expand Down Expand Up @@ -1602,3 +1621,5 @@ def self.use_proxy?(hostname, addr, port, no_proxy) # :nodoc:
end
end
end

require_relative "whatwg_parser/setters"
Loading