class Readme::HttpRequest

Constants

HTTP_NON_HEADERS

Public Class Methods

new(env) click to toggle source
# File lib/readme/http_request.rb, line 16
def initialize(env)
  @request = Rack::Request.new(env)
end

Public Instance Methods

body() click to toggle source
# File lib/readme/http_request.rb, line 65
def body
  @request.body.rewind
  content = @request.body.read
  @request.body.rewind

  content
end
content_length() click to toggle source
# File lib/readme/http_request.rb, line 48
def content_length
  @request.content_length.to_i
end
content_type() click to toggle source
# File lib/readme/http_request.rb, line 40
def content_type
  @request.content_type
end
cookies() click to toggle source
# File lib/readme/http_request.rb, line 28
def cookies
  @request.cookies
end
form_data?() click to toggle source
# File lib/readme/http_request.rb, line 44
def form_data?
  @request.form_data?
end
headers() click to toggle source
# File lib/readme/http_request.rb, line 56
def headers
  @request
    .each_header
    .select { |key, _| http_header?(key) }
    .to_h
    .transform_keys { |header| normalize_header_name(header) }
    .merge unprefixed_headers
end
http_version() click to toggle source
# File lib/readme/http_request.rb, line 32
def http_version
  @request.get_header(Rack::HTTP_VERSION)
end
options?() click to toggle source
# File lib/readme/http_request.rb, line 52
def options?
  @request.request_method == "OPTIONS"
end
parsed_form_data() click to toggle source
# File lib/readme/http_request.rb, line 73
def parsed_form_data
  @request.POST
end
query_params() click to toggle source
# File lib/readme/http_request.rb, line 24
def query_params
  @request.GET
end
request_method() click to toggle source
# File lib/readme/http_request.rb, line 36
def request_method
  @request.request_method
end
url() click to toggle source
# File lib/readme/http_request.rb, line 20
def url
  @request.url
end

Private Instance Methods

http_header?(name) click to toggle source

“headers” in Rack::Request just means any key in the env. The HTTP headers are all the headers prefixed with `HTTP_` as per the spec: github.com/rack/rack/blob/master/SPEC.rdoc#the-environment- Other “headers” like version and host are prefixed with `HTTP_` by Rack but don't seem to be considered legit HTTP headers.

# File lib/readme/http_request.rb, line 84
def http_header?(name)
  name.start_with?("HTTP") && !HTTP_NON_HEADERS.include?(name)
end
normalize_header_name(header) click to toggle source

Headers like `Content-Type: application/json` come into rack like `“HTTP_CONTENT_TYPE” => “application/json”`.

# File lib/readme/http_request.rb, line 90
def normalize_header_name(header)
  header.delete_prefix("HTTP_").split("_").map(&:capitalize).join("-")
end
unprefixed_headers() click to toggle source

These special headers are explicitly not prefixed with HTTP_ in the Rack env so we need to add them in manually

# File lib/readme/http_request.rb, line 96
def unprefixed_headers
  {"Content-Type" => @request.content_type,
   "Content-Length" => @request.content_length}.compact
end