Skip to content

Commit 90f884c

Browse files
committed
Ensure that HTTP_HOST is derived from the authority.
1 parent 391e5b6 commit 90f884c

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

lib/protocol/rack/adapter/generic.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,11 @@ def unwrap_request(request, env)
117117
env[RACK_HIJACK] = proc{request.hijack!.io}
118118
end
119119

120-
# HTTP/2 prefers `:authority` over `host`, so we do this for backwards compatibility.
121-
env[CGI::HTTP_HOST] ||= request.authority
120+
# HTTP/2 prefers `:authority` over `host`:
121+
if authority = request.authority
122+
# Note that we also already have SERVER_NAME and SERVER_PORT which are based on the authority.
123+
env[CGI::HTTP_HOST] = authority
124+
end
122125

123126
if peer = request.peer
124127
env[CGI::REMOTE_ADDR] = peer.ip_address

test/protocol/rack/request.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,16 @@
5959
)
6060
end
6161
end
62+
63+
with "incoming request with both host header and authority" do
64+
let(:headers) {Protocol::HTTP::Headers[{"host" => "header.example.com"}]}
65+
let(:request) {Protocol::HTTP::Request.new("https", "authority.example.com", "GET", "/", "HTTP/1.1", headers, body)}
66+
67+
it "correctly sets HTTP_HOST to the authority instead of host header" do
68+
# According to HTTP/2 semantics, :authority should take precedence over the host header when both are present:
69+
expect(env).to have_keys(
70+
"HTTP_HOST" => be == "authority.example.com"
71+
)
72+
end
73+
end
6274
end

0 commit comments

Comments
 (0)