Skip to content

Commit

Permalink
Make URI#to_s prepend relative path with / if there is a host or port
Browse files Browse the repository at this point in the history
Otherwise, the path could be considered part of the host or port.

This is better than modifying the path to make it absolute when
a host or port is set.  We could also raise for invalid paths
when a host or port is set using check_path, but that results
in weird errors, and won't catch issues (such as ftp allowing a
relative path).

Fixes [Bug #19916]
  • Loading branch information
jeremyevans committed Jan 5, 2024
1 parent ec26408 commit ac32aa0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/uri/generic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1364,6 +1364,9 @@ def to_s
str << ':'
str << @port.to_s
end
if (@host || @port) && !@path.empty? && !@path.start_with?('/')
str << '/'
end
str << @path
if @query
str << '?'
Expand Down
11 changes: 11 additions & 0 deletions test/uri/test_generic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@ def test_to_s
assert_equal "postgres:///foo", URI("postgres:///foo").to_s
assert_equal "http:///foo", URI("http:///foo").to_s
assert_equal "http:/foo", URI("http:/foo").to_s

uri = URI('rel_path')
assert_equal "rel_path", uri.to_s
uri.scheme = 'http'
assert_equal "http:rel_path", uri.to_s
uri.host = 'h'
assert_equal "http://h/rel_path", uri.to_s
uri.port = 8080
assert_equal "http://h:8080/rel_path", uri.to_s
uri.host = nil
assert_equal "http::8080/rel_path", uri.to_s
end

def test_parse
Expand Down

0 comments on commit ac32aa0

Please sign in to comment.