Skip to content

Commit 556f17d

Browse files
benuboissferik
authored andcommitted
Check for nil host before match?.
1 parent 057f362 commit 556f17d

2 files changed

Lines changed: 14 additions & 4 deletions

File tree

lib/http/request.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -326,11 +326,9 @@ def port
326326
# @return [String]
327327
# @api private
328328
def default_host_header_value
329-
value = PORTS[@scheme] == port ? host : "#{host}:#{port}"
329+
raise RequestError, "Invalid host: #{host.inspect}" if host.nil? || host.match?(/\s/)
330330

331-
raise RequestError, "Invalid host: #{value.inspect}" if value.match?(/\s/)
332-
333-
value
331+
PORTS[@scheme] == port ? host : "#{host}:#{port}"
334332
end
335333

336334
# Parse and normalize the URI, setting scheme

test/http/request_test.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,18 @@ def test_host_header_when_host_contains_whitespace_includes_invalid_host_in_erro
266266
assert_includes err.message, "exam ple.com".inspect
267267
end
268268

269+
def test_host_header_when_host_is_nil_raises_request_error
270+
normalizer = lambda { |uri|
271+
u = HTTP::URI.parse(uri)
272+
u.host = nil
273+
u
274+
}
275+
276+
assert_raises(HTTP::RequestError) do
277+
HTTP::Request.new(verb: :get, uri: "http://example.com/", uri_normalizer: normalizer)
278+
end
279+
end
280+
269281
# User-Agent header
270282

271283
def test_user_agent_header_defaults_to_http_request_user_agent

0 commit comments

Comments
 (0)