Skip to content

Commit

Permalink
Add rack_url_scheme to Puma::DSL, allows setting of rack.url_scheme h…
Browse files Browse the repository at this point in the history
…eader (puma#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>
  • Loading branch information
2 people authored and JuanitoFatas committed Sep 9, 2022
1 parent d24e0e5 commit 809e4b4
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
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

0 comments on commit 809e4b4

Please sign in to comment.