From 34fa6658dd1b779b21e586f01ee64c6f59ca1537 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Thu, 20 Aug 2015 15:57:15 -0700 Subject: pass a request object to the headers object --- actionpack/lib/action_dispatch/http/headers.rb | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) (limited to 'actionpack/lib/action_dispatch/http/headers.rb') diff --git a/actionpack/lib/action_dispatch/http/headers.rb b/actionpack/lib/action_dispatch/http/headers.rb index bc5410dc38..40053e521a 100644 --- a/actionpack/lib/action_dispatch/http/headers.rb +++ b/actionpack/lib/action_dispatch/http/headers.rb @@ -30,24 +30,27 @@ module ActionDispatch HTTP_HEADER = /\A[A-Za-z0-9-]+\z/ include Enumerable - attr_reader :env - def initialize(env = {}) # :nodoc: - @env = env + def self.from_hash(hash) + new ActionDispatch::Request.new hash + end + + def initialize(request) # :nodoc: + @req = request end # Returns the value for the given key mapped to @env. def [](key) - @env[env_name(key)] + env[env_name(key)] end # Sets the given value for the key mapped to @env. def []=(key, value) - @env[env_name(key)] = value + env[env_name(key)] = value end def key?(key) - @env.key? env_name(key) + env.key? env_name(key) end alias :include? :key? @@ -59,17 +62,17 @@ module ActionDispatch # If the code block is provided, then it will be run and # its result returned. def fetch(key, *args, &block) - @env.fetch env_name(key), *args, &block + env.fetch env_name(key), *args, &block end def each(&block) - @env.each(&block) + env.each(&block) end # Returns a new Http::Headers instance containing the contents of # headers_or_env and the original instance. def merge(headers_or_env) - headers = Http::Headers.new(env.dup) + headers = Http::Headers.new(ActionDispatch::Request.new(env.dup)) headers.merge!(headers_or_env) headers end @@ -83,7 +86,10 @@ module ActionDispatch end end + def env; @req.env; end + private + # Converts a HTTP header name to an environment variable name if it is # not contained within the headers hash. def env_name(key) -- cgit v1.2.3