summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSamuel Williams <samuel.williams@oriontransfer.co.nz>2020-02-04 10:58:05 +1300
committerSamuel Williams <samuel.williams@oriontransfer.co.nz>2020-02-05 13:35:25 +1300
commitd2e608e8d54b76ec7e52bb7c6508c5a5259c934e (patch)
treef956a408abd3101f2a46f6e72de9fd501e1198a8
parent65992dfca8bf8a2f108d12f3a4c932f23f9c6646 (diff)
downloadrack-d2e608e8d54b76ec7e52bb7c6508c5a5259c934e.tar.gz
Memoize header hash usage. Fixes #738.
-rw-r--r--lib/rack/chunked.rb2
-rw-r--r--lib/rack/common_logger.rb8
-rw-r--r--lib/rack/conditional_get.rb2
-rw-r--r--lib/rack/content_length.rb2
-rw-r--r--lib/rack/content_type.rb2
-rw-r--r--lib/rack/deflater.rb2
-rw-r--r--lib/rack/lint.rb2
-rw-r--r--lib/rack/response.rb2
-rw-r--r--lib/rack/show_status.rb2
-rw-r--r--lib/rack/utils.rb8
-rw-r--r--test/spec_response.rb12
11 files changed, 26 insertions, 18 deletions
diff --git a/lib/rack/chunked.rb b/lib/rack/chunked.rb
index b90a2503..84c66001 100644
--- a/lib/rack/chunked.rb
+++ b/lib/rack/chunked.rb
@@ -96,7 +96,7 @@ module Rack
# modify the response to use chunked Transfer-Encoding.
def call(env)
status, headers, body = @app.call(env)
- headers = HeaderHash.new(headers)
+ headers = HeaderHash[headers]
if chunkable_version?(env[SERVER_PROTOCOL]) &&
!STATUS_WITH_NO_ENTITY_BODY.key?(status.to_i) &&
diff --git a/lib/rack/common_logger.rb b/lib/rack/common_logger.rb
index 5297db7c..3810b269 100644
--- a/lib/rack/common_logger.rb
+++ b/lib/rack/common_logger.rb
@@ -35,10 +35,10 @@ module Rack
# cause the request not to be logged.
def call(env)
began_at = Utils.clock_time
- status, header, body = @app.call(env)
- header = Utils::HeaderHash.new(header)
- body = BodyProxy.new(body) { log(env, status, header, began_at) }
- [status, header, body]
+ status, headers, body = @app.call(env)
+ headers = Utils::HeaderHash[headers]
+ body = BodyProxy.new(body) { log(env, status, headers, began_at) }
+ [status, headers, body]
end
private
diff --git a/lib/rack/conditional_get.rb b/lib/rack/conditional_get.rb
index 0a2d787b..7b7808ac 100644
--- a/lib/rack/conditional_get.rb
+++ b/lib/rack/conditional_get.rb
@@ -25,7 +25,7 @@ module Rack
case env[REQUEST_METHOD]
when "GET", "HEAD"
status, headers, body = @app.call(env)
- headers = Utils::HeaderHash.new(headers)
+ headers = Utils::HeaderHash[headers]
if status == 200 && fresh?(env, headers)
status = 304
headers.delete(CONTENT_TYPE)
diff --git a/lib/rack/content_length.rb b/lib/rack/content_length.rb
index da548857..9e2b5fc4 100644
--- a/lib/rack/content_length.rb
+++ b/lib/rack/content_length.rb
@@ -15,7 +15,7 @@ module Rack
def call(env)
status, headers, body = @app.call(env)
- headers = HeaderHash.new(headers)
+ headers = HeaderHash[headers]
if !STATUS_WITH_NO_ENTITY_BODY.key?(status.to_i) &&
!headers[CONTENT_LENGTH] &&
diff --git a/lib/rack/content_type.rb b/lib/rack/content_type.rb
index 3640e3a2..503f7070 100644
--- a/lib/rack/content_type.rb
+++ b/lib/rack/content_type.rb
@@ -18,7 +18,7 @@ module Rack
def call(env)
status, headers, body = @app.call(env)
- headers = Utils::HeaderHash.new(headers)
+ headers = Utils::HeaderHash[headers]
unless STATUS_WITH_NO_ENTITY_BODY.key?(status.to_i)
headers[CONTENT_TYPE] ||= @content_type
diff --git a/lib/rack/deflater.rb b/lib/rack/deflater.rb
index acfe9b70..bd312610 100644
--- a/lib/rack/deflater.rb
+++ b/lib/rack/deflater.rb
@@ -40,7 +40,7 @@ module Rack
def call(env)
status, headers, body = @app.call(env)
- headers = Utils::HeaderHash.new(headers)
+ headers = Utils::HeaderHash[headers]
unless should_deflate?(env, status, headers, body)
return [status, headers, body]
diff --git a/lib/rack/lint.rb b/lib/rack/lint.rb
index 615a9813..7429da3a 100644
--- a/lib/rack/lint.rb
+++ b/lib/rack/lint.rb
@@ -593,7 +593,7 @@ module Rack
# this check uses headers like a hash, but the spec only requires
# headers respond to #each
- headers = Rack::Utils::HeaderHash.new(headers)
+ headers = Rack::Utils::HeaderHash[headers]
## In order to do this, an application may set the special header
## <tt>rack.hijack</tt> to an object that responds to <tt>call</tt>
diff --git a/lib/rack/response.rb b/lib/rack/response.rb
index 001c285a..408a38fc 100644
--- a/lib/rack/response.rb
+++ b/lib/rack/response.rb
@@ -41,7 +41,7 @@ module Rack
# Providing a body which responds to #to_str is legacy behaviour.
def initialize(body = nil, status = 200, headers = {})
@status = status.to_i
- @headers = Utils::HeaderHash.new(headers)
+ @headers = Utils::HeaderHash[headers]
@writer = self.method(:append)
diff --git a/lib/rack/show_status.rb b/lib/rack/show_status.rb
index 56034750..4d01d448 100644
--- a/lib/rack/show_status.rb
+++ b/lib/rack/show_status.rb
@@ -18,7 +18,7 @@ module Rack
def call(env)
status, headers, body = @app.call(env)
- headers = Utils::HeaderHash.new(headers)
+ headers = Utils::HeaderHash[headers]
empty = headers[CONTENT_LENGTH].to_i <= 0
# client or server error, or explicit message
diff --git a/lib/rack/utils.rb b/lib/rack/utils.rb
index 1ecf2d1c..58d274af 100644
--- a/lib/rack/utils.rb
+++ b/lib/rack/utils.rb
@@ -405,6 +405,14 @@ module Rack
#
# @api private
class HeaderHash < Hash # :nodoc:
+ def self.[] headers
+ if headers.is_a?(HeaderHash)
+ return headers
+ else
+ return self.new(headers)
+ end
+ end
+
def initialize(hash = {})
super()
@names = {}
diff --git a/test/spec_response.rb b/test/spec_response.rb
index c27112fb..b2ba59a8 100644
--- a/test/spec_response.rb
+++ b/test/spec_response.rb
@@ -71,13 +71,13 @@ describe Rack::Response do
end
it "doesn't mutate given headers" do
- [{}, Rack::Utils::HeaderHash.new].each do |header|
- response = Rack::Response.new([], 200, header)
- response.header["Content-Type"] = "text/plain"
- response.header["Content-Type"].must_equal "text/plain"
+ headers = {}
- header.wont_include("Content-Type")
- end
+ response = Rack::Response.new([], 200, headers)
+ response.headers["Content-Type"] = "text/plain"
+ response.headers["Content-Type"].must_equal "text/plain"
+
+ headers.wont_include("Content-Type")
end
it "can override the initial Content-Type with a different case" do