Skip to content

Commit 306ada4

Browse files
committed
[ruby/rack-sequel] Only set headers not created by servers
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 523534b commit 306ada4

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,30 @@
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+
JSON_TYPE = 'application/json'
13+
HTML_TYPE = 'text/html; charset=utf-8'
14+
PLAINTEXT_TYPE = 'text/plain'
15+
DATE = 'Date'
16+
SERVER = 'Server'
17+
SERVER_STRING = if defined?(PhusionPassenger)
18+
'Passenger'
19+
elsif defined?(Puma)
20+
'Puma'
21+
elsif defined?(Iodine)
22+
'Iodine'
23+
elsif defined?(Unicorn)
24+
'Unicorn'
25+
else
26+
'Ruby Rack'
27+
end
1028

1129
def bounded_queries(env)
1230
params = Rack::Utils.parse_query(env['QUERY_STRING'])
@@ -87,35 +105,61 @@ def updates(env)
87105
end
88106

89107
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
108+
case env['PATH_INFO']
109+
when '/json'
110+
# Test type 1: JSON serialization
111+
respond JSON_TYPE, { message: 'Hello, World!' }.to_json
112+
when '/db'
113+
# Test type 2: Single database query
114+
respond JSON_TYPE, db.to_json
115+
when '/queries'
116+
# Test type 3: Multiple database queries
117+
respond JSON_TYPE, queries(env).to_json
118+
when '/fortunes'
119+
# Test type 4: Fortunes
120+
respond HTML_TYPE, fortunes
121+
when '/updates'
122+
# Test type 5: Database updates
123+
respond JSON_TYPE, updates(env).to_json
124+
when '/plaintext'
125+
# Test type 6: Plaintext
126+
respond PLAINTEXT_TYPE, 'Hello, World!'
127+
end
128+
end
129+
130+
private
111131

132+
def respond(content_type, body = '')
112133
[
113134
200,
114-
DEFAULT_HEADERS.merge(
115-
CONTENT_TYPE => content_type,
116-
DATE_HEADER => Time.now.httpdate
117-
),
118-
body
135+
headers(content_type),
136+
[body]
119137
]
120138
end
139+
140+
if defined?(Unicorn)
141+
def headers(content_type)
142+
{
143+
CONTENT_TYPE => content_type,
144+
SERVER => SERVER_STRING,
145+
DATE => Time.now.utc.httpdate,
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)