Skip to content

Commit 583c7dc

Browse files
committed
Fix WEBrick SERVER_PORT handling.
1 parent adc9596 commit 583c7dc

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

lib/rackup/handler/webrick.rb

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,12 +119,20 @@ def service(req, res)
119119
if req.unparsed_uri == "*"
120120
env[::Rack::PATH_INFO] = "*"
121121
env[::Rack::REQUEST_PATH] = "*"
122-
123-
# Ensure SERVER_NAME and SERVER_PORT are set from server config
122+
123+
# Ensure SERVER_NAME and SERVER_PORT are set from server config.
124124
# (WEBrick allows these to be nil for OPTIONS * requests)
125125
# See https://github.com/ruby/webrick/pull/182 for a proper fix.
126-
env[::Rack::SERVER_NAME] ||= @server[:ServerName] || @server[:BindAddress] || "localhost"
127-
env[::Rack::SERVER_PORT] ||= (@server[:Port] || 80).to_s
126+
server_name = env[::Rack::SERVER_NAME]
127+
if server_name.nil? || server_name == ""
128+
env[::Rack::SERVER_NAME] = @server[:ServerName] || @server[:BindAddress] || "localhost"
129+
end
130+
131+
# Legacy versions of WEBrick can set server_port to "" in some cases:
132+
server_port = env[::Rack::SERVER_PORT]
133+
if server_port.nil? || server_port == ""
134+
env[::Rack::SERVER_PORT] = (@server[:Port] || 80).to_s
135+
end
128136
else
129137
unless env[::Rack::PATH_INFO] == ""
130138
# Strip the script name prefix from the path to get path info

test/spec_webrick.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ def is_running?
230230
end
231231

232232
server = Rackup::Handler::WEBrick::Server.new(
233-
app,
233+
Rack::Lint.new(app),
234234
Host: @host,
235235
Port: 9203,
236236
Logger: WEBrick::Log.new(nil, WEBrick::BasicLog::WARN),

0 commit comments

Comments
 (0)