2017-07-24 16:20:53 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
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
|
|
|
#
|
2016-01-29 15:01:33 -05:00
|
|
|
# env = { "CONTENT_TYPE" => "text/plain", "HTTP_USER_AGENT" => "curl/7.43.0" }
|
2016-08-12 17:03:34 -04:00
|
|
|
# headers = ActionDispatch::Http::Headers.from_hash(env)
|
2014-05-07 12:50:12 -04:00
|
|
|
# headers["Content-Type"] # => "text/plain"
|
2016-03-29 07:21:21 -04:00
|
|
|
# headers["User-Agent"] # => "curl/7.43.0"
|
2016-01-29 15:01:33 -05:00
|
|
|
#
|
|
|
|
# Also note that when headers are mapped to CGI-like variables by the Rack
|
|
|
|
# server, both dashes and underscores are converted to underscores. This
|
|
|
|
# ambiguity cannot be resolved at this stage anymore. Both underscores and
|
|
|
|
# dashes have to be interpreted as if they were originally sent as dashes.
|
|
|
|
#
|
|
|
|
# # GET / HTTP/1.1
|
|
|
|
# # ...
|
|
|
|
# # User-Agent: curl/7.43.0
|
|
|
|
# # X_Custom_Header: token
|
|
|
|
#
|
|
|
|
# headers["X_Custom_Header"] # => nil
|
|
|
|
# headers["X-Custom-Header"] # => "token"
|
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
|
|
|
|
|
2015-08-20 18:57:15 -04:00
|
|
|
def self.from_hash(hash)
|
|
|
|
new ActionDispatch::Request.new hash
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(request) # :nodoc:
|
|
|
|
@req = request
|
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)
|
2015-08-21 19:48:46 -04:00
|
|
|
@req.get_header 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)
|
2015-08-21 19:48:46 -04:00
|
|
|
@req.set_header env_name(key), value
|
2013-03-13 06:30:45 -04:00
|
|
|
end
|
|
|
|
|
2015-10-03 22:14:37 -04:00
|
|
|
# Add a value to a multivalued header like Vary or Accept-Encoding.
|
|
|
|
def add(key, value)
|
|
|
|
@req.add_header env_name(key), value
|
|
|
|
end
|
|
|
|
|
2014-05-07 13:01:34 -04:00
|
|
|
def key?(key)
|
2015-08-21 19:48:46 -04:00
|
|
|
@req.has_header? env_name(key)
|
2014-05-07 13:01:34 -04:00
|
|
|
end
|
2012-10-18 18:20:30 -04:00
|
|
|
alias :include? :key?
|
|
|
|
|
2015-08-21 19:57:07 -04:00
|
|
|
DEFAULT = Object.new # :nodoc:
|
|
|
|
|
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.
|
2015-08-21 19:57:07 -04:00
|
|
|
def fetch(key, default = DEFAULT)
|
2015-09-04 20:36:15 -04:00
|
|
|
@req.fetch_header(env_name(key)) do
|
2015-08-21 19:57:07 -04:00
|
|
|
return default unless default == DEFAULT
|
|
|
|
return yield if block_given?
|
2016-08-26 13:12:33 -04:00
|
|
|
raise KeyError, key
|
2015-08-21 19:57:07 -04:00
|
|
|
end
|
2012-10-18 18:20:30 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def each(&block)
|
2015-08-21 19:40:53 -04:00
|
|
|
@req.each_header(&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)
|
2015-08-21 19:40:53 -04:00
|
|
|
headers = @req.dup.headers
|
2013-03-13 07:25:32 -04:00
|
|
|
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|
|
2015-08-21 19:43:01 -04:00
|
|
|
@req.set_header env_name(key), value
|
2013-03-13 07:25:32 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-08-21 19:49:44 -04:00
|
|
|
def env; @req.env.dup; end
|
2015-08-20 18:57:15 -04:00
|
|
|
|
2008-01-11 01:39:56 -05:00
|
|
|
private
|
2015-08-20 18:57:15 -04:00
|
|
|
|
2016-09-14 04:57:52 -04:00
|
|
|
# Converts an HTTP header name to an environment variable name if it is
|
|
|
|
# not contained within the headers hash.
|
2016-08-06 13:55:02 -04:00
|
|
|
def env_name(key)
|
|
|
|
key = key.to_s
|
|
|
|
if key =~ HTTP_HEADER
|
|
|
|
key = key.upcase.tr("-", "_")
|
|
|
|
key = "HTTP_" + key unless CGI_VARIABLES.include?(key)
|
|
|
|
end
|
|
|
|
key
|
2013-03-13 06:30:45 -04:00
|
|
|
end
|
2008-01-11 01:39:56 -05:00
|
|
|
end
|
|
|
|
end
|
2008-08-08 02:43:12 -04:00
|
|
|
end
|