Skip to content

Commit 3c1f933

Browse files
authored
[ruby/rack] Only set headers not created by servers (#9569)
+------------+------------------------------------+------+-----+-----+------+-------+---------+--------------+ | name| branch_name| json| db|query|update|fortune|plaintext|weighted_score| +------------+------------------------------------+------+-----+-----+------+-------+---------+--------------+ | rack-iodine| master|272762|92478|82287| 25252| 76641| 356731| 4319| | rack-iodine|rack/remove-redundant-content-length|329655|95705|83260| 25488| 80504| 498301| 4458| +------------+------------------------------------+------+-----+-----+------+-------+---------+--------------+
1 parent befd25d commit 3c1f933

File tree

2 files changed

+36
-18
lines changed

2 files changed

+36
-18
lines changed

frameworks/Ruby/agoo/app.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
QUERIES_MAX = 500
2222

2323
CONTENT_TYPE = 'Content-Type'
24-
CONTENT_LENGTH = 'Content-Length'
2524
DATE = 'Date'
2625
SERVER = 'Server'
2726
SERVER_STRING = 'Agoo'

frameworks/Ruby/rack/hello_world.rb

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@ class HelloWorld
2525
PLAINTEXT_TYPE = 'text/plain'
2626
DATE = 'Date'
2727
SERVER = 'Server'
28-
SERVER_STRING = if defined?(PhusionPassenger)
29-
'Passenger'
30-
elsif defined?(Puma)
28+
SERVER_STRING = if defined?(Puma)
3129
'Puma'
3230
elsif defined?(Iodine)
3331
'Iodine'
@@ -64,20 +62,6 @@ def initialize
6462
@db = PgDb.new(DEFAULT_DATABASE_URL, max_connections)
6563
end
6664

67-
def respond(content_type, body = '')
68-
headers = {
69-
CONTENT_TYPE => content_type,
70-
DATE => Time.now.utc.httpdate,
71-
SERVER => SERVER_STRING
72-
}
73-
headers[CONTENT_LENGTH] = body.bytesize.to_s if defined?(Unicorn)
74-
[
75-
200,
76-
headers,
77-
[body]
78-
]
79-
end
80-
8165
def fortunes
8266
fortunes = @db.select_fortunes
8367
fortunes << { id: 0, message: 'Additional fortune added at request time.' }
@@ -118,4 +102,39 @@ def call(env)
118102
respond PLAINTEXT_TYPE, 'Hello, World!'
119103
end
120104
end
105+
106+
private
107+
108+
def respond(content_type, body)
109+
[
110+
200,
111+
headers(content_type, body),
112+
[body]
113+
]
114+
end
115+
116+
if defined?(Unicorn)
117+
def headers(content_type, body)
118+
{
119+
CONTENT_TYPE => content_type,
120+
SERVER => SERVER_STRING,
121+
CONTENT_LENGTH => body.bytesize.to_s
122+
}
123+
end
124+
elsif defined?(Falcon) || defined?(Puma)
125+
def headers(content_type, _)
126+
{
127+
CONTENT_TYPE => content_type,
128+
SERVER => SERVER_STRING,
129+
DATE => Time.now.utc.httpdate
130+
}
131+
end
132+
else
133+
def headers(content_type, _)
134+
{
135+
CONTENT_TYPE => content_type,
136+
SERVER => SERVER_STRING
137+
}
138+
end
139+
end
121140
end

0 commit comments

Comments
 (0)