2009-01-27 19:54:01 -05:00
|
|
|
module ActionDispatch
|
2008-01-11 01:39:56 -05:00
|
|
|
module Http
|
2014-05-08 14:24:38 -04:00
|
|
|
# Provides access to the request's HTTP headers from the environment.
|
2014-05-07 12:50:12 -04:00
|
|
|
#
|
|
|
|
# env = { "CONTENT_TYPE" => "text/plain" }
|
|
|
|
# headers = ActionDispatch::Http::Headers.new(env)
|
|
|
|
# headers["Content-Type"] # => "text/plain"
|
2012-10-18 18:20:30 -04:00
|
|
|
class Headers
|
2014-06-05 22:05:02 -04:00
|
|
|
CGI_VARIABLES = Set.new(%W[
|
|
|
|
AUTH_TYPE
|
|
|
|
CONTENT_LENGTH
|
|
|
|
CONTENT_TYPE
|
|
|
|
GATEWAY_INTERFACE
|
|
|
|
HTTPS
|
|
|
|
PATH_INFO
|
|
|
|
PATH_TRANSLATED
|
|
|
|
QUERY_STRING
|
|
|
|
REMOTE_ADDR
|
|
|
|
REMOTE_HOST
|
|
|
|
REMOTE_IDENT
|
|
|
|
REMOTE_USER
|
|
|
|
REQUEST_METHOD
|
|
|
|
SCRIPT_NAME
|
|
|
|
SERVER_NAME
|
|
|
|
SERVER_PORT
|
|
|
|
SERVER_PROTOCOL
|
|
|
|
SERVER_SOFTWARE
|
|
|
|
]).freeze
|
|
|
|
|
2013-03-13 11:25:28 -04:00
|
|
|
HTTP_HEADER = /\A[A-Za-z0-9-]+\z/
|
2013-03-13 06:14:49 -04:00
|
|
|
|
2012-10-18 18:20:30 -04:00
|
|
|
include Enumerable
|
2013-03-13 07:25:32 -04:00
|
|
|
attr_reader :env
|
2012-10-18 18:20:30 -04:00
|
|
|
|
2014-05-09 12:29:29 -04:00
|
|
|
def initialize(env = {}) # :nodoc:
|
2013-03-13 13:05:01 -04:00
|
|
|
@env = env
|
2009-12-28 19:28:26 -05:00
|
|
|
end
|
2008-08-08 02:43:12 -04:00
|
|
|
|
2014-05-09 12:29:29 -04:00
|
|
|
# Returns the value for the given key mapped to @env.
|
2013-03-13 06:30:45 -04:00
|
|
|
def [](key)
|
|
|
|
@env[env_name(key)]
|
2012-05-02 14:14:40 -04:00
|
|
|
end
|
|
|
|
|
2014-05-10 09:44:43 -04:00
|
|
|
# Sets the given value for the key mapped to @env.
|
2013-03-13 06:30:45 -04:00
|
|
|
def []=(key, value)
|
|
|
|
@env[env_name(key)] = value
|
|
|
|
end
|
|
|
|
|
2014-05-07 13:01:34 -04:00
|
|
|
def key?(key)
|
|
|
|
@env.key? env_name(key)
|
|
|
|
end
|
2012-10-18 18:20:30 -04:00
|
|
|
alias :include? :key?
|
|
|
|
|
2014-05-09 12:29:29 -04:00
|
|
|
# Returns the value for the given key mapped to @env.
|
2014-05-10 09:44:43 -04:00
|
|
|
#
|
|
|
|
# If the key is not found and an optional code block is not provided,
|
|
|
|
# raises a <tt>KeyError</tt> exception.
|
|
|
|
#
|
|
|
|
# If the code block is provided, then it will be run and
|
|
|
|
# its result returned.
|
2013-03-13 06:30:45 -04:00
|
|
|
def fetch(key, *args, &block)
|
|
|
|
@env.fetch env_name(key), *args, &block
|
2012-10-18 18:20:30 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def each(&block)
|
2013-03-13 06:30:45 -04:00
|
|
|
@env.each(&block)
|
2008-01-11 01:39:56 -05:00
|
|
|
end
|
2008-08-08 02:43:12 -04:00
|
|
|
|
2014-05-09 12:29:29 -04:00
|
|
|
# Returns a new Http::Headers instance containing the contents of
|
|
|
|
# <tt>headers_or_env</tt> and the original instance.
|
2013-03-13 07:25:32 -04:00
|
|
|
def merge(headers_or_env)
|
|
|
|
headers = Http::Headers.new(env.dup)
|
|
|
|
headers.merge!(headers_or_env)
|
|
|
|
headers
|
|
|
|
end
|
|
|
|
|
2014-05-09 12:29:29 -04:00
|
|
|
# Adds the contents of <tt>headers_or_env</tt> to original instance
|
2014-05-10 09:44:43 -04:00
|
|
|
# entries; duplicate keys are overwritten with the values from
|
2014-05-09 12:29:29 -04:00
|
|
|
# <tt>headers_or_env</tt>.
|
2013-03-13 07:25:32 -04:00
|
|
|
def merge!(headers_or_env)
|
|
|
|
headers_or_env.each do |key, value|
|
|
|
|
self[env_name(key)] = value
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-01-11 01:39:56 -05:00
|
|
|
private
|
2014-05-09 12:29:29 -04:00
|
|
|
# Converts a HTTP header name to an environment variable name if it is
|
|
|
|
# not contained within the headers hash.
|
2013-03-13 06:30:45 -04:00
|
|
|
def env_name(key)
|
2013-03-13 11:25:28 -04:00
|
|
|
key = key.to_s
|
|
|
|
if key =~ HTTP_HEADER
|
|
|
|
key = key.upcase.tr('-', '_')
|
|
|
|
key = "HTTP_" + key unless CGI_VARIABLES.include?(key)
|
2013-03-13 06:30:45 -04:00
|
|
|
end
|
|
|
|
key
|
2012-10-18 18:32:44 -04:00
|
|
|
end
|
2008-01-11 01:39:56 -05:00
|
|
|
end
|
|
|
|
end
|
2008-08-08 02:43:12 -04:00
|
|
|
end
|