Skip to content

Commit befd25d

Browse files
authored
[ruby/rack-sequel] Only set headers not created by servers (#9570)
Some servers already set the 'Date' or 'Content-Length' headers. +-----------+------------------------------------+-----+-----+------+-------+--------------+ | name| branch_name| db|query|update|fortune|weighted_score| +-----------+------------------------------------+-----+-----+------+-------+--------------+ |rack-sequel| master|44962|19605| 10677| 34058| 1373| |rack-sequel|rack-sequel/remove-redundant-headers|45573|20403| 12342| 34488| 1507| +-----------+------------------------------------+-----+-----+------+-------+--------------+
1 parent a5703ff commit befd25d

File tree

2 files changed

+75
-55
lines changed

2 files changed

+75
-55
lines changed

frameworks/Ruby/rack-sequel/boot.rb

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,7 @@
11
# frozen_string_literal: true
22
require 'bundler/setup'
3-
require 'time'
43

5-
MAX_PK = 10_000
6-
ID_RANGE = (1..10_000).freeze
7-
ALL_IDS = ID_RANGE.to_a
8-
QUERIES_MIN = 1
9-
QUERIES_MAX = 500
104
SEQUEL_NO_ASSOCIATIONS = true
11-
CONTENT_TYPE = 'Content-Type'
12-
JSON_TYPE = 'application/json'
13-
HTML_TYPE = 'text/html; charset=utf-8'
14-
PLAINTEXT_TYPE = 'text/plain'
15-
DATE_HEADER = 'Date'
16-
SERVER_HEADER = 'Server'
17-
18-
SERVER_STRING =
19-
if defined?(PhusionPassenger)
20-
[
21-
PhusionPassenger::SharedConstants::SERVER_TOKEN_NAME,
22-
PhusionPassenger::VERSION_STRING
23-
].join('/').freeze
24-
elsif defined?(Puma)
25-
Puma::Const::PUMA_SERVER_STRING
26-
elsif defined?(Unicorn)
27-
Unicorn::HttpParser::DEFAULTS['SERVER_SOFTWARE']
28-
end
295

306
Bundler.require(:default) # Load core modules
317

frameworks/Ruby/rack-sequel/hello_world.rb

Lines changed: 75 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,31 @@
11
# frozen_string_literal: true
2+
require 'time'
23

34
# Our Rack application to be executed by rackup
45
class HelloWorld
5-
DEFAULT_HEADERS = {}.tap do |h|
6-
h[SERVER_HEADER] = SERVER_STRING if SERVER_STRING
7-
8-
h.freeze
9-
end
6+
MAX_PK = 10_000
7+
ID_RANGE = (1..10_000).freeze
8+
ALL_IDS = ID_RANGE.to_a
9+
QUERIES_MIN = 1
10+
QUERIES_MAX = 500
11+
CONTENT_TYPE = 'Content-Type'
12+
CONTENT_LENGTH = 'Content-Length'
13+
JSON_TYPE = 'application/json'
14+
HTML_TYPE = 'text/html; charset=utf-8'
15+
PLAINTEXT_TYPE = 'text/plain'
16+
DATE = 'Date'
17+
SERVER = 'Server'
18+
SERVER_STRING = if defined?(PhusionPassenger)
19+
'Passenger'
20+
elsif defined?(Puma)
21+
'Puma'
22+
elsif defined?(Iodine)
23+
'Iodine'
24+
elsif defined?(Unicorn)
25+
'Unicorn'
26+
else
27+
'Ruby Rack'
28+
end
1029

1130
def bounded_queries(env)
1231
params = Rack::Utils.parse_query(env['QUERY_STRING'])
@@ -87,35 +106,60 @@ def updates(env)
87106
end
88107

89108
def call(env)
90-
content_type, *body =
91-
case env['PATH_INFO']
92-
when '/json'
93-
# Test type 1: JSON serialization
94-
[JSON_TYPE, { message: 'Hello, World!' }.to_json]
95-
when '/db'
96-
# Test type 2: Single database query
97-
[JSON_TYPE, db.to_json]
98-
when '/queries'
99-
# Test type 3: Multiple database queries
100-
[JSON_TYPE, queries(env).to_json]
101-
when '/fortunes'
102-
# Test type 4: Fortunes
103-
[HTML_TYPE, fortunes]
104-
when '/updates'
105-
# Test type 5: Database updates
106-
[JSON_TYPE, updates(env).to_json]
107-
when '/plaintext'
108-
# Test type 6: Plaintext
109-
[PLAINTEXT_TYPE, 'Hello, World!']
110-
end
109+
case env['PATH_INFO']
110+
when '/json'
111+
# Test type 1: JSON serialization
112+
respond JSON_TYPE, { message: 'Hello, World!' }.to_json
113+
when '/db'
114+
# Test type 2: Single database query
115+
respond JSON_TYPE, db.to_json
116+
when '/queries'
117+
# Test type 3: Multiple database queries
118+
respond JSON_TYPE, queries(env).to_json
119+
when '/fortunes'
120+
# Test type 4: Fortunes
121+
respond HTML_TYPE, fortunes
122+
when '/updates'
123+
# Test type 5: Database updates
124+
respond JSON_TYPE, updates(env).to_json
125+
when '/plaintext'
126+
# Test type 6: Plaintext
127+
respond PLAINTEXT_TYPE, 'Hello, World!'
128+
end
129+
end
130+
131+
private
111132

133+
def respond(content_type, body)
112134
[
113135
200,
114-
DEFAULT_HEADERS.merge(
115-
CONTENT_TYPE => content_type,
116-
DATE_HEADER => Time.now.httpdate
117-
),
118-
body
136+
headers(content_type, body),
137+
[body]
119138
]
120139
end
140+
141+
if defined?(Unicorn)
142+
def headers(content_type, body)
143+
{
144+
CONTENT_TYPE => content_type,
145+
SERVER => SERVER_STRING,
146+
CONTENT_LENGTH => body.bytesize.to_s
147+
}
148+
end
149+
elsif defined?(Puma)
150+
def headers(content_type, _)
151+
{
152+
CONTENT_TYPE => content_type,
153+
SERVER => SERVER_STRING,
154+
DATE => Time.now.utc.httpdate
155+
}
156+
end
157+
else
158+
def headers(content_type, _)
159+
{
160+
CONTENT_TYPE => content_type,
161+
SERVER => SERVER_STRING
162+
}
163+
end
164+
end
121165
end

0 commit comments

Comments
 (0)