Add rack_url_scheme to Puma::DSL, allows setting of rack.url_scheme header (#2586)

* Add rack_url_scheme to Puma::DSL, allows setting of rack.url_scheme header

* Clarify comment on DSL re: rack URL

* Add tests to test_puma_server.rb

Co-authored-by: Nate Berkopec <nate.berkopec@gmail.com>
This commit is contained in:
MSP-Greg 2021-05-26 09:15:08 -05:00 committed by GitHub
parent b7a9eb69ac
commit 32852f715a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 29 additions and 2 deletions

View File

@ -41,6 +41,7 @@ module Puma
"rack.multithread".freeze => conf.options[:max_threads] > 1,
"rack.multiprocess".freeze => conf.options[:workers] >= 1,
"rack.run_once".freeze => false,
RACK_URL_SCHEME => conf.options[:rack_url_scheme],
"SCRIPT_NAME".freeze => ENV['SCRIPT_NAME'] || "",
# I'd like to set a default CONTENT_TYPE here but some things

View File

@ -381,6 +381,13 @@ module Puma
@options[:rackup] ||= path.to_s
end
# Allows setting `env['rack.url_scheme']`.
# Only necessary if X-Forwarded-Proto is not being set by your proxy
# Normal values are 'http' or 'https'.
def rack_url_scheme(scheme=nil)
@options[:rack_url_scheme] = scheme
end
def early_hints(answer=true)
@options[:early_hints] = answer
end

View File

@ -51,7 +51,7 @@ module Puma
head = env[REQUEST_METHOD] == HEAD
env[RACK_INPUT] = body
env[RACK_URL_SCHEME] = default_server_port(env) == PORT_443 ? HTTPS : HTTP
env[RACK_URL_SCHEME] ||= default_server_port(env) == PORT_443 ? HTTPS : HTTP
if @early_hints
env[EARLY_HINTS] = lambda { |headers|

View File

@ -304,7 +304,6 @@ EOF
assert_match(/HTTP\/1.0 500 Internal Server Error/, data)
end
def test_eof_on_connection_close_is_not_logged_as_an_error
server_run
@ -1312,4 +1311,24 @@ EOF
def test_not_drain_on_shutdown
test_drain_on_shutdown false
end
def test_rack_url_scheme_dflt
server_run
data = send_http_and_read "GET / HTTP/1.0\r\n\r\n"
assert_equal "http", data.split("\r\n").last
end
def test_rack_url_scheme_user
@port = UniquePort.call
opts = { rack_url_scheme: 'user', binds: ["tcp://#{@host}:#{@port}"] }
conf = Puma::Configuration.new(opts).tap(&:clamp)
@server = Puma::Server.new @app, @events, conf.options
@server.inherit_binder Puma::Binder.new(@events, conf)
@server.binder.parse conf.options[:binds], @events
@server.run
data = send_http_and_read "GET / HTTP/1.0\r\n\r\n"
assert_equal "user", data.split("\r\n").last
end
end