Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Host header be always the first in requests #723

Merged
merged 3 commits into from Oct 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/excon/connection.rb
Expand Up @@ -252,6 +252,12 @@ def request(params={}, &block)
datum[:headers]['Host'] ||= datum[:host] + port_string(datum)
end

# RFC 7230, section 5.4, states that the Host header SHOULD be the first one # to be present.
# Some web servers will reject the request if it comes too late, so let's hoist it to the top.
if host = datum[:headers].delete('Host')
geemus marked this conversation as resolved.
Show resolved Hide resolved
datum[:headers] = { 'Host' => host }.merge(datum[:headers])
end

# if path is empty or doesn't start with '/', insert one
unless datum[:path][0, 1] == '/'
datum[:path] = datum[:path].dup.insert(0, '/')
Expand Down
8 changes: 8 additions & 0 deletions tests/request_headers_tests.rb
Expand Up @@ -16,6 +16,14 @@

end

tests('header order') do
tests('host is the first sent header by default').returns('host: localhost:9292') do
response = Excon.post('http://localhost:9292/')

response.body.lines.first.chomp
end
end

end

end