Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from May 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/puma/binder.rb
Expand Up @@ -41,6 +41,7 @@ def initialize(events, conf = Configuration.new)
"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
Expand Down
7 changes: 7 additions & 0 deletions lib/puma/dsl.rb
Expand Up @@ -381,6 +381,13 @@ def rackup(path)
@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
Expand Down
2 changes: 1 addition & 1 deletion lib/puma/request.rb
Expand Up @@ -51,7 +51,7 @@ def handle_request(client, lines, requests)
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|
Expand Down
21 changes: 20 additions & 1 deletion test/test_puma_server.rb
Expand Up @@ -304,7 +304,6 @@ def test_doesnt_print_backtrace_in_production
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

Expand Down Expand Up @@ -1312,4 +1311,24 @@ def test_drain_on_shutdown(drain=true)
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