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-04 11:33:27 +1300
commit24e4da1815a3ef22c4e65117f5157e43b24e23ea (patch)
treefcab14fb66d89f9cd975319faeb229eb84ae1662
parent16455b4c9a73d2fa65d3eed8a68b37ed06ebfe40 (diff)
downloadrack-24e4da1815a3ef22c4e65117f5157e43b24e23ea.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 e11a9e5b..93a71f26 100644
--- a/lib/rack/chunked.rb
+++ b/lib/rack/chunked.rb
@@ -69,7 +69,7 @@ module Rack
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 9d8eaefb..85aa9de4 100644
--- a/lib/rack/common_logger.rb
+++ b/lib/rack/common_logger.rb
@@ -30,10 +30,10 @@ module Rack
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 24200053..e325a655 100644
--- a/lib/rack/conditional_get.rb
+++ b/lib/rack/conditional_get.rb
@@ -23,7 +23,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 1acd79a7..a96aa83d 100644
--- a/lib/rack/content_length.rb
+++ b/lib/rack/content_length.rb
@@ -12,7 +12,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 4814d200..b632608d 100644
--- a/lib/rack/content_type.rb
+++ b/lib/rack/content_type.rb
@@ -17,7 +17,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 0a4a79d7..148c17a8 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