Class: Webmachine::ConvertRequestToRackEnv

Inherits:
Object
  • Object
show all
Defined in:
lib/webmachine/convert_request_to_rack_env.rb

Class Method Summary collapse

Class Method Details

.call(request) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/webmachine/convert_request_to_rack_env.rb', line 3

def self.call(request)
  env = {
    'REQUEST_METHOD' => request.method.upcase,
    'CONTENT_TYPE' => request.headers['Content-Type'],
    'PATH_INFO' => request.uri.path,
    'QUERY_STRING' => request.uri.query || "",
    'SERVER_NAME' => request.uri.host,
    'SERVER_PORT' => request.uri.port.to_s,
    'SCRIPT_NAME' => '',
    'rack.url_scheme' => request.uri.scheme,
    'rack.input' => request.body.to_io ? StringIO.new(request.body.to_s) : nil
  }.merge(convert_headers(request))
end

.convert_headers(request) ⇒ Object



17
18
19
20
21
22
# File 'lib/webmachine/convert_request_to_rack_env.rb', line 17

def self.convert_headers(request)
  request.headers.each_with_object({}) do | (key, value), env |
    v = redact?(key) ? '[Filtered]' : value
    env[convert_http_header_name_to_rack_header_name(key)] = v
  end
end

.convert_http_header_name_to_rack_header_name(http_header_name) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/webmachine/convert_request_to_rack_env.rb', line 29

def self.convert_http_header_name_to_rack_header_name(http_header_name)
  if http_header_name.downcase == 'content-type' || http_header_name.downcase ==  'content-length'
    http_header_name.upcase.gsub('-', '_')
  else
    "HTTP_" + http_header_name.upcase.gsub('-', '_')
  end
end

.redact?(http_header_name) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
27
# File 'lib/webmachine/convert_request_to_rack_env.rb', line 24

def self.redact?(http_header_name)
  lower = http_header_name.downcase
  lower == 'authorization' || lower.include?('token')
end