Skip to content

Commit

Permalink
[ruby/uri] Fix RFC3986 regexps
Browse files Browse the repository at this point in the history
  • Loading branch information
nobu authored and matzbot committed Jun 13, 2023
1 parent 3924dba commit 6d734a8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
9 changes: 5 additions & 4 deletions lib/uri/rfc3986_parser.rb
Expand Up @@ -34,6 +34,7 @@ class RFC3986_Parser # :nodoc:

SCHEME = %r[[A-Za-z][+\-.0-9A-Za-z]*+].source
SEG = %r[(?:%\h\h|[!$&-.0-9:;=@A-Z_a-z~/])].source
SEG_NC = %r[(?:%\h\h|[!$&-.0-9;=@A-Z_a-z~])].source
FRAGMENT = %r[(?:%\h\h|[!$&-.0-9:;=@A-Z_a-z~/?])*+].source

RFC3986_URI = %r[\A
Expand All @@ -43,8 +44,8 @@ class RFC3986_Parser # :nodoc:
(?<hier-part>//
(?<authority>#{AUTHORITY})
(?<path-abempty>(?:/\g<seg>*+)?)
| (?<path-absolute>/\g<seg>*+)
| (?<path-rootless>(?!=/)\g<seg>++)
| (?<path-absolute>/((?!/)\g<seg>++)?)
| (?<path-rootless>(?!/)\g<seg>++)
| (?<path-empty>)
)
(?:\?(?<query>[^\#]*+))?
Expand All @@ -58,7 +59,7 @@ class RFC3986_Parser # :nodoc:
(?<authority>#{AUTHORITY})
(?<path-abempty>(?:/\g<seg>*+)?)
| (?<path-absolute>/\g<seg>*+)
| (?<path-noscheme>(?!=[:/])\g<seg>++)
| (?<path-noscheme>#{SEG_NC}++(?:/\g<seg>*+)?)
| (?<path-empty>)
)
(?:\?(?<query>[^#]*+))?
Expand Down Expand Up @@ -156,7 +157,7 @@ def default_regexp # :nodoc:
USERINFO: %r[\A#{USERINFO}\z]o,
HOST: %r[\A#{HOST}\z]o,
ABS_PATH: %r[\A/#{SEG}*+\z]o,
REL_PATH: %r[\A(?!=/)#{SEG}++\z]o,
REL_PATH: %r[\A(?!/)#{SEG}++\z]o,
QUERY: %r[\A(?:%\h\h|[!$&-.0-9:;=@A-Z_a-z~/?])*+\z],
FRAGMENT: %r[\A#{FRAGMENT}\z]o,
OPAQUE: %r[\A(?:[^/].*)?\z],
Expand Down
8 changes: 8 additions & 0 deletions test/uri/test_parser.rb
Expand Up @@ -78,5 +78,13 @@ def test_split
assert_equal(["http", nil, "[0::0]", nil, nil, "", nil, nil, nil], URI.split("http://[0::0]"))
assert_equal([nil, nil, "example.com", nil, nil, "", nil, nil, nil], URI.split("//example.com"))
assert_equal([nil, nil, "[0::0]", nil, nil, "", nil, nil, nil], URI.split("//[0::0]"))

assert_equal(["a", nil, nil, nil, nil, "", nil, nil, nil], URI.split("a:"))
assert_raise(URI::InvalidURIError) do
URI.parse("::")
end
assert_raise(URI::InvalidURIError) do
URI.parse("foo@example:foo")
end
end
end

0 comments on commit 6d734a8

Please sign in to comment.