Skip to content

Commit 5e426e6

Browse files
author
Francisco Mejia
authored
Stop setting SERVER_PORT env key if nil (#22)
According to the Rack specification, `env["SERVER_PORT"]` should either be a string containing decimal digits or not set at all.
1 parent 16197ef commit 5e426e6

File tree

4 files changed

+27
-3
lines changed

4 files changed

+27
-3
lines changed

lib/protocol/rack/adapter/rack2.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,12 @@ def make_environment(request)
5959

6060
# I'm not sure what sane defaults should be here:
6161
CGI::SERVER_NAME => server_name,
62-
CGI::SERVER_PORT => server_port,
6362
}
63+
64+
# SERVER_PORT is optional but must not be set if it is not present.
65+
if server_port
66+
env[CGI::SERVER_PORT] = server_port
67+
end
6468

6569
self.unwrap_request(request, env)
6670

lib/protocol/rack/adapter/rack3.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,12 @@ def make_environment(request)
5555

5656
# I'm not sure what sane defaults should be here:
5757
CGI::SERVER_NAME => server_name,
58-
CGI::SERVER_PORT => server_port,
5958
}
59+
60+
# SERVER_PORT is optional but must not be set if it is not present.
61+
if server_port
62+
env[CGI::SERVER_PORT] = server_port
63+
end
6064

6165
self.unwrap_request(request, env)
6266

lib/protocol/rack/adapter/rack31.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,12 @@ def make_environment(request)
4646

4747
# I'm not sure what sane defaults should be here:
4848
CGI::SERVER_NAME => server_name,
49-
CGI::SERVER_PORT => server_port,
5049
}
50+
51+
# SERVER_PORT is optional but must not be set if it is not present.
52+
if server_port
53+
env[CGI::SERVER_PORT] = server_port
54+
end
5155

5256
if body = request.body
5357
if body.empty?

test/protocol/rack/adapter/generic.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,18 @@
3535
expect(env).to have_keys("CONTENT_TYPE" => be == "text/plain")
3636
end
3737
end
38+
39+
with "app server without SERVER_PORT" do
40+
let(:request) do
41+
Protocol::HTTP::Request.new("https", "example.com", "GET", "/", "http/1.1", Protocol::HTTP::Headers[{"accept" => "text/html"}], nil)
42+
end
43+
44+
it "does not include SERVER_PORT in the Rack environment" do
45+
env = adapter.make_environment(request)
46+
47+
expect(env).not.to have_keys(Protocol::Rack::CGI::SERVER_PORT)
48+
end
49+
end
3850

3951
with "a app that returns nil" do
4052
include DisableConsoleContext

0 commit comments

Comments
 (0)