forked from soveran/cuba
-
Notifications
You must be signed in to change notification settings - Fork 144
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add plain_hash_response_headers plugin, using a plain hash for respon…
…se headers on Rack 3 for much better performance This plugin changes behavior on Rack 3 to use a plain hash for response headers, as Roda has historically done for Rack 2, instead of the default behavior of using Rack::Headers. Using this plugin gains back most the performance lost in the switch to Rack 3. With Empty Roda App (Roda.route{''}): Before switch to lower case headers on Rack 3: * With Rack 2: 190k i/s * With Rack 3: 120k i/s After switch to lower case headers on Rack 3: * With Rack 2: 185k i/s * With Rack 3: 92k i/s * With Rack 3 and plain_hash_response_headers plugin: 182k i/s Support running all of Roda's specs with the plain_hash_response_headers plugin using the PLAIN_HASH_RESPONSE_HEADERS environment variable.
- Loading branch information
1 parent
837781d
commit bb137bc
Showing
6 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# frozen-string-literal: true | ||
|
||
# | ||
class Roda | ||
module RodaPlugins | ||
# The response_headers_plain_hash plugin will change Roda to | ||
# use a plain hash for response headers. This is Roda's | ||
# default behavior on Rack 2, but on Rack 3+, Roda defaults | ||
# to using Rack::Headers for response headers for backwards | ||
# compatibility (Rack::Headers automatically lower cases header | ||
# keys). | ||
# | ||
# On Rack 3+, you should use this plugin for better performance | ||
# if you are sure all headers in your application and middleware | ||
# are already lower case (lower case response header keys are | ||
# required by the Rack 3 spec). | ||
module PlainHashResponseHeaders | ||
if defined?(Rack::Headers) && Rack::Headers.is_a?(Class) | ||
module ResponseMethods | ||
private | ||
|
||
# Use plain hash for headers | ||
def _initialize_headers | ||
{} | ||
end | ||
end | ||
end | ||
end | ||
|
||
register_plugin(:plain_hash_response_headers, PlainHashResponseHeaders) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
require_relative "../spec_helper" | ||
|
||
describe "plain_hash_response_headers plugin" do | ||
it "uses plain hashes for response headers" do | ||
app(:plain_hash_response_headers) do |r| | ||
r.get 'up' do | ||
response.headers['UP'] = 'U' | ||
end | ||
|
||
response.headers['down'] = 'd' | ||
end | ||
|
||
if Rack.release >= '3' && ENV['LINT'] | ||
proc{req('/up')}.must_raise Rack::Lint::LintError | ||
else | ||
header('up', '/up').must_be_nil | ||
header('UP', '/up').must_equal 'U' | ||
end | ||
header('down').must_equal 'd' | ||
header('DOWN').must_be_nil | ||
req[1].must_be_instance_of Hash | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters