diff options
Diffstat (limited to 'actionpack/lib/action_dispatch/http/headers.rb')
-rw-r--r-- | actionpack/lib/action_dispatch/http/headers.rb | 24 |
1 files changed, 15 insertions, 9 deletions
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 # <tt>headers_or_env</tt> 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) |