Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c5974f0

Browse files
authored
Merge pull request #116 from jeremyevans/http-empty-host-reject-20686
Do not allow empty host names, as they are not allowed by RFC 3986
2 parents 0abac72 + c0cfa04 commit c5974f0

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

lib/uri/http.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,18 @@ def self.build(args)
6161
super(tmp)
6262
end
6363

64+
# Do not allow empty host names, as they are not allowed by RFC 3986.
65+
def check_host(v)
66+
ret = super
67+
68+
if ret && v.empty?
69+
raise InvalidComponentError,
70+
"bad component(expected host component): #{v}"
71+
end
72+
73+
ret
74+
end
75+
6476
#
6577
# == Description
6678
#

test/uri/test_generic.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -846,8 +846,10 @@ def test_ipv6
846846
assert_equal("http://[::1]/bar", u.to_s)
847847
u.hostname = "::1"
848848
assert_equal("http://[::1]/bar", u.to_s)
849-
u.hostname = ""
850-
assert_equal("http:///bar", u.to_s)
849+
850+
u = URI("file://foo/bar")
851+
u.hostname = ''
852+
assert_equal("file:///bar", u.to_s)
851853
end
852854

853855
def test_build

test/uri/test_http.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ def test_build
1919
assert_kind_of(URI::HTTP, u)
2020
end
2121

22+
def test_build_empty_host
23+
assert_raise(URI::InvalidComponentError) { URI::HTTP.build(host: '') }
24+
end
25+
2226
def test_parse
2327
u = URI.parse('http://a')
2428
assert_kind_of(URI::HTTP, u)

0 commit comments

Comments
 (0)