summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAkira Matsuda <ronnie@dio.jp>2023-03-13 16:07:21 +0900
committerGitHub <noreply@github.com>2023-03-13 20:07:21 +1300
commit95d2f64a84a6010d5b1a5179a647b42cb53356d1 (patch)
treef0173d1f9e2c11036f4e9dc57b7b328be6b00b86
parent0b45107cb717c6e1c13f01f908f5c7f2b095fe57 (diff)
downloadrack-95d2f64a84a6010d5b1a5179a647b42cb53356d1.tar.gz
Store downcased common headers at class level (#2046)
so we need not to downcase all headers per each request
-rw-r--r--lib/rack/headers.rb87
1 files changed, 85 insertions, 2 deletions
diff --git a/lib/rack/headers.rb b/lib/rack/headers.rb
index cef1ae01..bf12f465 100644
--- a/lib/rack/headers.rb
+++ b/lib/rack/headers.rb
@@ -6,6 +6,89 @@ module Rack
# (by using non-lowercase response header keys), automatically handling
# the downcasing of keys.
class Headers < Hash
+ KNOWN_HEADERS = {}
+ %w(
+ Accept-CH
+ Accept-Patch
+ Accept-Ranges
+ Access-Control-Allow-Credentials
+ Access-Control-Allow-Headers
+ Access-Control-Allow-Methods
+ Access-Control-Allow-Origin
+ Access-Control-Expose-Headers
+ Access-Control-Max-Age
+ Age
+ Allow
+ Alt-Svc
+ Cache-Control
+ Connection
+ Content-Disposition
+ Content-Encoding
+ Content-Language
+ Content-Length
+ Content-Location
+ Content-MD5
+ Content-Range
+ Content-Security-Policy
+ Content-Security-Policy-Report-Only
+ Content-Type
+ Date
+ Delta-Base
+ ETag
+ Expect-CT
+ Expires
+ Feature-Policy
+ IM
+ Last-Modified
+ Link
+ Location
+ NEL
+ P3P
+ Permissions-Policy
+ Pragma
+ Preference-Applied
+ Proxy-Authenticate
+ Public-Key-Pins
+ Referrer-Policy
+ Refresh
+ Report-To
+ Retry-After
+ Server
+ Set-Cookie
+ Status
+ Strict-Transport-Security
+ Timing-Allow-Origin
+ Tk
+ Trailer
+ Transfer-Encoding
+ Upgrade
+ Vary
+ Via
+ WWW-Authenticate
+ Warning
+ X-Cascade
+ X-Content-Duration
+ X-Content-Security-Policy
+ X-Content-Type-Options
+ X-Correlation-ID
+ X-Correlation-Id
+ X-Download-Options
+ X-Frame-Options
+ X-Frame-Options
+ X-Permitted-Cross-Domain-Policies
+ X-Powered-By
+ X-Redirect-By
+ X-Request-ID
+ X-Request-Id
+ X-Runtime
+ X-UA-Compatible
+ X-WebKit-CS
+ X-XSS-Protection
+ ).each do |str|
+ downcased = str.downcase.freeze
+ KNOWN_HEADERS[str] = KNOWN_HEADERS[downcased] = downcased
+ end
+
def self.[](*items)
if items.length % 2 != 0
if items.length == 1 && items.first.is_a?(Hash)
@@ -30,7 +113,7 @@ module Rack
end
def []=(key, value)
- super(key.downcase.freeze, value)
+ super(KNOWN_HEADERS[key] || key.downcase.freeze, value)
end
alias store []=
@@ -150,7 +233,7 @@ module Rack
private
def downcase_key(key)
- key.is_a?(String) ? key.downcase : key
+ key.is_a?(String) ? KNOWN_HEADERS[key] || key.downcase : key
end
end
end