Skip to content

Commit

Permalink
Merge pull request #723 from andrepiske/host-always-first-header
Browse files Browse the repository at this point in the history
Make Host header be always the first in requests
  • Loading branch information
geemus committed Oct 16, 2020
2 parents 8e351c9 + bc9fda1 commit 6b20581
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
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')
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

0 comments on commit 6b20581

Please sign in to comment.