Skip to content

Commit

Permalink
Remove all usage of headers for non-header use cases.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Apr 27, 2022
1 parent 1f66e65 commit c3a646a
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 50 deletions.
6 changes: 3 additions & 3 deletions lib/rack/common_logger.rb
Expand Up @@ -54,18 +54,18 @@ def log(env, status, response_headers, began_at)

msg = FORMAT % [
request.ip || "-",
request.get_header("REMOTE_USER") || "-",
request.env['REMOTE_USER'] || "-",
Time.now.strftime("%d/%b/%Y:%H:%M:%S %z"),
request.request_method,
request.script_name,
request.path_info,
request.query_string.empty? ? "" : "?#{request.query_string}",
request.get_header(SERVER_PROTOCOL),
request.env[SERVER_PROTOCOL],
status.to_s[0..3],
length,
Utils.clock_time - began_at ]

logger = @logger || request.get_header(RACK_ERRORS)
logger = @logger || request.env[RACK_ERRORS]
# Standard library logger doesn't support write but it supports << which actually
# calls to write on the log device without formatting
if logger.respond_to?(:write)
Expand Down
4 changes: 2 additions & 2 deletions lib/rack/files.rb
Expand Up @@ -78,7 +78,7 @@ def serving(request, path)
return [200, { 'allow' => ALLOW_HEADER, CONTENT_LENGTH => '0' }, []]
end
last_modified = ::File.mtime(path).httpdate
return [304, {}, []] if request.get_header('HTTP_IF_MODIFIED_SINCE') == last_modified
return [304, {}, []] if request.get_header('if-modified-since') == last_modified

headers = { "last-modified" => last_modified }
mime_type = mime_type path, @default_mime
Expand All @@ -90,7 +90,7 @@ def serving(request, path)
status = 200
size = filesize path

ranges = Rack::Utils.get_byte_ranges(request.get_header('HTTP_RANGE'), size)
ranges = Rack::Utils.get_byte_ranges(request.get_header('range'), size)
if ranges.nil?
# No ranges:
ranges = [0..size - 1]
Expand Down
4 changes: 2 additions & 2 deletions lib/rack/method_override.rb
Expand Up @@ -48,9 +48,9 @@ def allowed_methods
def method_override_param(req)
req.POST[METHOD_OVERRIDE_PARAM_KEY]
rescue Utils::InvalidParameterError, Utils::ParameterTypeError
req.get_header(RACK_ERRORS).puts "Invalid or incomplete POST params"
req.env[RACK_ERRORS].puts "Invalid or incomplete POST params"
rescue EOFError
req.get_header(RACK_ERRORS).puts "Bad request content body"
req.env[RACK_ERRORS].puts "Bad request content body"
end
end
end
98 changes: 55 additions & 43 deletions lib/rack/request.rb
Expand Up @@ -131,17 +131,19 @@ def set_header(name, value)
#
# http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2
def add_header(name, value)
key = env_key(name)

if value.nil?
get_header(name)
elsif current = get_header(name)
@env[key]
elsif current = env[key]
case current
when Array
current << value
else
set_header(name, [current, value])
@env[key] = [current, value]
end
else
set_header(name, value)
@env[key] = value
end
end

Expand Down Expand Up @@ -346,14 +348,14 @@ def trace?; request_method == TRACE end
def unlink?; request_method == UNLINK end

def scheme
if get_header(HTTPS) == 'on'
if env['HTTPS'] == 'on'
'https'
elsif get_header(HTTP_X_FORWARDED_SSL) == 'on'
elsif env['HTTP_X_FORWARDED_SSL'] == 'on'
'https'
elsif forwarded_scheme
forwarded_scheme
else
get_header(RACK_URL_SCHEME)
env[RACK_URL_SCHEME]
end
end

Expand Down Expand Up @@ -382,32 +384,34 @@ def server_authority
end

def server_name
get_header(SERVER_NAME)
env[SERVER_NAME]
end

def server_port
if port = get_header(SERVER_PORT)
if port = env[SERVER_PORT]
Integer(port)
end
end

def cookies
hash = fetch_header(RACK_REQUEST_COOKIE_HASH) do |key|
set_header(key, {})
env = self.env

hash = env.fetch(RACK_REQUEST_COOKIE_HASH) do |key|
env[key] = {}
end

string = get_header(HTTP_COOKIE)
string = env[HTTP_COOKIE]

unless string == get_header(RACK_REQUEST_COOKIE_STRING)
unless string == env[RACK_REQUEST_COOKIE_STRING]
hash.replace Utils.parse_cookies_header(string)
set_header(RACK_REQUEST_COOKIE_STRING, string)
env[RACK_REQUEST_COOKIE_STRING] = string
end

hash
end

def content_type
content_type = get_header('CONTENT_TYPE')
content_type = env['CONTENT_TYPE']
content_type.nil? || content_type.empty? ? nil : content_type
end

Expand All @@ -417,7 +421,7 @@ def xhr?

# The `HTTP_HOST` header.
def host_authority
get_header(HTTP_HOST)
env[HTTP_HOST]
end

def host_with_port(authority = self.authority)
Expand Down Expand Up @@ -475,7 +479,7 @@ def forwarded_for
end)
end
when :x_forwarded
if value = get_header(HTTP_X_FORWARDED_FOR)
if value = env[HTTP_X_FORWARDED_FOR]
return(split_header(value).map do |authority|
split_authority(wrap_ipv6(authority))[1]
end)
Expand All @@ -496,7 +500,7 @@ def forwarded_port
end.compact)
end
when :x_forwarded
if value = get_header(HTTP_X_FORWARDED_PORT)
if value = env[HTTP_X_FORWARDED_PORT]
return split_header(value).map(&:to_i)
end
end
Expand All @@ -513,7 +517,7 @@ def forwarded_authority
return forwarded.last
end
when :x_forwarded
if value = get_header(HTTP_X_FORWARDED_HOST)
if value = env[HTTP_X_FORWARDED_HOST]
return wrap_ipv6(split_header(value).last)
end
end
Expand All @@ -527,7 +531,7 @@ def ssl?
end

def ip
remote_addresses = split_header(get_header('REMOTE_ADDR'))
remote_addresses = split_header(env['REMOTE_ADDR'])
external_addresses = reject_trusted_ip_addresses(remote_addresses)

unless external_addresses.empty?
Expand Down Expand Up @@ -586,7 +590,7 @@ def content_charset
# content-type header is provided and the request_method is POST.
def form_data?
type = media_type
meth = get_header(RACK_METHODOVERRIDE_ORIGINAL_METHOD) || get_header(REQUEST_METHOD)
meth = env[RACK_METHODOVERRIDE_ORIGINAL_METHOD] || env[REQUEST_METHOD]

(meth == POST && type.nil?) || FORM_DATA_MEDIA_TYPES.include?(type)
end
Expand All @@ -599,12 +603,14 @@ def parseable_data?

# Returns the data received in the query string.
def GET
if get_header(RACK_REQUEST_QUERY_STRING) == query_string
get_header(RACK_REQUEST_QUERY_HASH)
env = self.env

if env[RACK_REQUEST_QUERY_STRING] == query_string
env[RACK_REQUEST_QUERY_HASH]
else
query_hash = parse_query(query_string, '&')
set_header(RACK_REQUEST_QUERY_STRING, query_string)
set_header(RACK_REQUEST_QUERY_HASH, query_hash)
env[RACK_REQUEST_QUERY_STRING] = query_string
env[RACK_REQUEST_QUERY_HASH] = query_hash
end
end

Expand All @@ -613,27 +619,31 @@ def GET
# This method support both application/x-www-form-urlencoded and
# multipart/form-data.
def POST
if get_header(RACK_INPUT).nil?
env = self.env

if env[RACK_INPUT].nil?
raise "Missing rack.input"
elsif get_header(RACK_REQUEST_FORM_INPUT) == get_header(RACK_INPUT)
get_header(RACK_REQUEST_FORM_HASH)
elsif env[RACK_REQUEST_FORM_INPUT] == env[RACK_INPUT]
env[RACK_REQUEST_FORM_INPUT]
elsif form_data? || parseable_data?
unless set_header(RACK_REQUEST_FORM_HASH, parse_multipart)
form_vars = get_header(RACK_INPUT).read
unless (env[RACK_REQUEST_FORM_HASH] = parse_multipart)
form_vars = env[RACK_INPUT].read

# Fix for Safari Ajax postings that always append \0
# form_vars.sub!(/\0\z/, '') # performance replacement:
form_vars.slice!(-1) if form_vars.end_with?("\0")

set_header RACK_REQUEST_FORM_VARS, form_vars
set_header RACK_REQUEST_FORM_HASH, parse_query(form_vars, '&')
env[RACK_REQUEST_FORM_VARS] = form_vars
env[RACK_REQUEST_FORM_HASH] = parse_query(form_vars, '&')
end
set_header RACK_REQUEST_FORM_INPUT, get_header(RACK_INPUT)
get_header RACK_REQUEST_FORM_HASH

env[RACK_REQUEST_FORM_INPUT] = env[RACK_INPUT]
else
set_header RACK_REQUEST_FORM_INPUT, get_header(RACK_INPUT)
set_header(RACK_REQUEST_FORM_HASH, {})
env[RACK_REQUEST_FORM_INPUT] = env[RACK_INPUT]
env[RACK_REQUEST_FORM_HASH] = {}
end

return env[RACK_REQUEST_FORM_HASH]
end

# The union of GET and POST data.
Expand Down Expand Up @@ -691,11 +701,11 @@ def fullpath
end

def accept_encoding
parse_http_accept_header(get_header("HTTP_ACCEPT_ENCODING"))
parse_http_accept_header(env["HTTP_ACCEPT_ENCODING"])
end

def accept_language
parse_http_accept_header(get_header("HTTP_ACCEPT_LANGUAGE"))
parse_http_accept_header(env["HTTP_ACCEPT_LANGUAGE"])
end

def trusted_proxy?(ip)
Expand Down Expand Up @@ -754,7 +764,7 @@ def parse_http_accept_header(header)

# Get an array of values set in the RFC 7239 `Forwarded` request header.
def get_http_forwarded(token)
Utils.forwarded_values(get_header(HTTP_FORWARDED))&.[](token)
Utils.forwarded_values(env[HTTP_FORWARDED])&.[](token)
end

def query_parser
Expand Down Expand Up @@ -822,11 +832,13 @@ def reject_trusted_ip_addresses(ip_addresses)
ip_addresses.reject { |ip| trusted_proxy?(ip) }
end

FORWARDED_SCHEME_HEADERS = {
FORWARDED_SCHEME_KEYS = {
proto: HTTP_X_FORWARDED_PROTO,
scheme: HTTP_X_FORWARDED_SCHEME
}.freeze
private_constant :FORWARDED_SCHEME_HEADERS

private_constant :FORWARDED_SCHEME_KEYS

def forwarded_scheme
forwarded_priority.each do |type|
case type
Expand All @@ -837,8 +849,8 @@ def forwarded_scheme
end
when :x_forwarded
x_forwarded_proto_priority.each do |x_type|
if header = FORWARDED_SCHEME_HEADERS[x_type]
split_header(get_header(header)).reverse_each do |scheme|
if header = FORWARDED_SCHEME_KEYS[x_type]
split_header(env[header]).reverse_each do |scheme|
if allowed_scheme(scheme)
return scheme
end
Expand Down

0 comments on commit c3a646a

Please sign in to comment.