2009-01-27 19:54:01 -05:00
|
|
|
module ActionDispatch
|
2008-01-11 01:39:56 -05:00
|
|
|
module Http
|
2012-10-18 18:20:30 -04:00
|
|
|
class Headers
|
|
|
|
include Enumerable
|
|
|
|
|
2012-10-18 18:32:44 -04:00
|
|
|
def initialize(env = {})
|
|
|
|
@headers = env
|
2009-12-28 19:28:26 -05:00
|
|
|
end
|
2008-08-08 02:43:12 -04:00
|
|
|
|
2008-01-11 01:39:56 -05:00
|
|
|
def [](header_name)
|
2012-10-18 18:20:30 -04:00
|
|
|
@headers[env_name(header_name)]
|
2012-05-02 14:14:40 -04:00
|
|
|
end
|
|
|
|
|
2012-10-18 18:20:30 -04:00
|
|
|
def []=(k,v); @headers[k] = v; end
|
|
|
|
def key?(k); @headers.key? k; end
|
|
|
|
alias :include? :key?
|
|
|
|
|
2012-10-18 18:22:58 -04:00
|
|
|
def fetch(header_name, *args, &block)
|
|
|
|
@headers.fetch env_name(header_name), *args, &block
|
2012-10-18 18:20:30 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def each(&block)
|
|
|
|
@headers.each(&block)
|
2008-01-11 01:39:56 -05:00
|
|
|
end
|
2008-08-08 02:43:12 -04:00
|
|
|
|
2008-01-11 01:39:56 -05:00
|
|
|
private
|
2012-10-18 18:32:44 -04:00
|
|
|
|
|
|
|
# Converts a HTTP header name to an environment variable name if it is
|
|
|
|
# not contained within the headers hash.
|
|
|
|
def env_name(header_name)
|
|
|
|
@headers.include?(header_name) ? header_name : cgi_name(header_name)
|
|
|
|
end
|
|
|
|
|
|
|
|
def cgi_name(k)
|
|
|
|
"HTTP_#{k.upcase.gsub(/-/, '_')}"
|
|
|
|
end
|
2008-01-11 01:39:56 -05:00
|
|
|
end
|
|
|
|
end
|
2008-08-08 02:43:12 -04:00
|
|
|
end
|