diff options
author | Jeremy Evans <[email protected]> | 2023-10-24 13:26:03 -0700 |
---|---|---|
committer | git <[email protected]> | 2024-01-05 16:07:55 +0000 |
commit | 37657c79b66994147e41f31139ceb9c0c840868f (patch) | |
tree | fc9c8cb272b27b91195dfd16bf3015a736eee536 | |
parent | 557f1a5705cdcfb059b67495ed63767cb2e2c72b (diff) |
[ruby/uri] Make URI#to_s prepend relative path with / if there is a host or port
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]
https://github.com/ruby/uri/commit/ac32aa005b
-rw-r--r-- | lib/uri/generic.rb | 3 | ||||
-rw-r--r-- | test/uri/test_generic.rb | 11 |
2 files changed, 14 insertions, 0 deletions
diff --git a/lib/uri/generic.rb b/lib/uri/generic.rb index f3540a24bb..9ea2335cea 100644 --- a/lib/uri/generic.rb +++ b/lib/uri/generic.rb @@ -1364,6 +1364,9 @@ module URI str << ':' str << @port.to_s end + if (@host || @port) && [email protected]? && [email protected]_with?('/') + str << '/' + end str << @path if @query str << '?' diff --git a/test/uri/test_generic.rb b/test/uri/test_generic.rb index e6619373c6..8209363b82 100644 --- a/test/uri/test_generic.rb +++ b/test/uri/test_generic.rb @@ -26,6 +26,17 @@ class URI::TestGeneric < Test::Unit::TestCase 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 |