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

[ruby/rack] Only set headers not created by servers #9569

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion frameworks/Ruby/agoo/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
QUERIES_MAX = 500

CONTENT_TYPE = 'Content-Type'
CONTENT_LENGTH = 'Content-Length'
DATE = 'Date'
SERVER = 'Server'
SERVER_STRING = 'Agoo'
Expand Down
53 changes: 36 additions & 17 deletions frameworks/Ruby/rack/hello_world.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ class HelloWorld
PLAINTEXT_TYPE = 'text/plain'
DATE = 'Date'
SERVER = 'Server'
SERVER_STRING = if defined?(PhusionPassenger)
'Passenger'
elsif defined?(Puma)
SERVER_STRING = if defined?(Puma)
'Puma'
elsif defined?(Iodine)
'Iodine'
Expand Down Expand Up @@ -64,20 +62,6 @@ def initialize
@db = PgDb.new(DEFAULT_DATABASE_URL, max_connections)
end

def respond(content_type, body = '')
headers = {
CONTENT_TYPE => content_type,
DATE => Time.now.utc.httpdate,
SERVER => SERVER_STRING
}
headers[CONTENT_LENGTH] = body.bytesize.to_s if defined?(Unicorn)
[
200,
headers,
[body]
]
end

def fortunes
fortunes = @db.select_fortunes
fortunes << { id: 0, message: 'Additional fortune added at request time.' }
Expand Down Expand Up @@ -118,4 +102,39 @@ def call(env)
respond PLAINTEXT_TYPE, 'Hello, World!'
end
end

private

def respond(content_type, body)
[
200,
headers(content_type, body),
[body]
]
end

if defined?(Unicorn)
def headers(content_type, body)
{
CONTENT_TYPE => content_type,
SERVER => SERVER_STRING,
CONTENT_LENGTH => body.bytesize.to_s
}
end
elsif defined?(Falcon) || defined?(Puma)
def headers(content_type, _)
{
CONTENT_TYPE => content_type,
SERVER => SERVER_STRING,
DATE => Time.now.utc.httpdate
}
end
else
def headers(content_type, _)
{
CONTENT_TYPE => content_type,
SERVER => SERVER_STRING
}
end
end
end
Loading