Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 2 additions & 2 deletions lib/uri/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,8 @@ def self.split(uri)
# It's recommended to first ::escape string +uri+
# if it may contain invalid URI characters.
#
def self.parse(uri)
DEFAULT_PARSER.parse(uri)
def self.parse(uri, exception: true)
DEFAULT_PARSER.parse(uri, exception: exception)
end

# Merges the given URI strings +str+
Expand Down
15 changes: 12 additions & 3 deletions lib/uri/rfc2396_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def initialize(opts = {})
attr_reader :regexp

# Returns a split URI against +regexp[:ABS_URI]+.
def split(uri)
def split(uri, exception: true)
case uri
when ''
# null uri
Expand All @@ -139,10 +139,14 @@ def split(uri)
# server = [ [ userinfo "@" ] hostport ]

if !scheme
return unless exception

raise InvalidURIError,
"bad URI (absolute but no scheme): #{uri}"
end
if !opaque && (!path && (!host && !registry))
return unless exception

raise InvalidURIError,
"bad URI (absolute but no path): #{uri}"
end
Expand Down Expand Up @@ -173,6 +177,8 @@ def split(uri)
# server = [ [ userinfo "@" ] hostport ]

else
return unless exception

raise InvalidURIError, "bad URI (is not URI?): #{uri}"
end

Expand Down Expand Up @@ -206,8 +212,11 @@ def split(uri)
# p.parse("ldap://ldap.example.com/dc=example?user=john")
# #=> #<URI::LDAP ldap://ldap.example.com/dc=example?user=john>
#
def parse(uri)
URI.for(*self.split(uri), self)
def parse(uri, exception: true)
scheme = self.split(uri, exception: exception)
return if scheme.nil?

URI.for(*scheme, self)
end

#
Expand Down
17 changes: 13 additions & 4 deletions lib/uri/rfc3986_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,18 @@ def initialize
@regexp = default_regexp.each_value(&:freeze).freeze
end

def split(uri) #:nodoc:
def split(uri, exception: true) #:nodoc:
begin
uri = uri.to_str
rescue NoMethodError
return unless exception
raise InvalidURIError, "bad URI (is not URI?): #{uri.inspect}"
end
uri.ascii_only? or
unless uri.ascii_only?
return unless exception

raise InvalidURIError, "URI must be ascii only #{uri.dump}"
end
if m = RFC3986_URI.match(uri)
query = m["query"]
scheme = m["scheme"]
Expand Down Expand Up @@ -127,12 +131,17 @@ def split(uri) #:nodoc:
m["fragment"]
]
else
return unless exception

raise InvalidURIError, "bad URI (is not URI?): #{uri.inspect}"
end
end

def parse(uri) # :nodoc:
URI.for(*self.split(uri), self)
def parse(uri, exception: true) # :nodoc:
scheme = self.split(uri, exception: exception)
return if scheme.nil?

URI.for(*scheme, self)
end

def join(*uris) # :nodoc:
Expand Down
11 changes: 11 additions & 0 deletions test/uri/test_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,17 @@ def test_split
end
end

def test_split_without_exception
assert_equal(["http", nil, "example.com", nil, nil, "", nil, nil, nil], URI.split("http://example.com"))
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_nil URI.parse("::", exception: false)
assert_nil URI.parse("foo@example:foo", exception: false)
end

def test_rfc2822_parse_relative_uri
pre = ->(length) {
" " * length + "\0"
Expand Down
Loading