class Webmachine::Request

Request represents a single HTTP request sent from a client. It should be instantiated by {Adapters} when a request is received

Constants

HOST_MATCH
HTTP_HEADERS_MATCH
IPV6_MATCH
ROUTING_PATH_MATCH

Attributes

base_uri[R]
body[R]
disp_path[RW]
headers[R]
method[R]
path_info[RW]
path_tokens[RW]
routing_tokens[R]
uri[R]

Public Class Methods

new(method, uri, headers, body, routing_tokens = nil, base_uri = nil) click to toggle source

@param [String] method the HTTP request method @param [URI] uri the requested URI, including host, scheme and

port

@param [Headers] headers the HTTP request headers @param [String,#to_s,#each,nil] body the entity included in the

request, if present
# File lib/webmachine/request.rb, line 24
def initialize(method, uri, headers, body, routing_tokens = nil, base_uri = nil)
  @method, @headers, @body = method, headers, body
  @uri = build_uri(uri, headers)
  @routing_tokens = routing_tokens || @uri.path.match(ROUTING_PATH_MATCH)[1].split(SLASH)
  @base_uri = base_uri || @uri.dup.tap do |u|
    u.path = SLASH
    u.query = nil
  end
end

Public Instance Methods

connect?() click to toggle source

Is this a CONNECT request?

@return [Boolean]

true if this request was made with the CONNECT method
# File lib/webmachine/request.rb, line 149
def connect?
  method == CONNECT_METHOD
end
cookies() click to toggle source

The cookies sent in the request.

@return [Hash]

{} if no Cookies header set
# File lib/webmachine/request.rb, line 84
def cookies
  @cookies ||= Webmachine::Cookie.parse(headers['Cookie'])
  @cookies
end
delete?() click to toggle source

Is this a DELETE request?

@return [Boolean]

true if this request was made with the DELETE method
# File lib/webmachine/request.rb, line 133
def delete?
  method == DELETE_METHOD
end
get?() click to toggle source

Is this a GET request?

@return [Boolean]

true if this request was made with the GET method
# File lib/webmachine/request.rb, line 101
def get?
  method == GET_METHOD
end
has_body?() click to toggle source

@return[true, false] Whether the request body is present.

# File lib/webmachine/request.rb, line 58
def has_body?
  !(body.nil? || body.empty?)
end
head?() click to toggle source

Is this a HEAD request?

@return [Boolean]

true if this request was made with the HEAD method
# File lib/webmachine/request.rb, line 109
def head?
  method == HEAD_METHOD
end
https?() click to toggle source

Is this an HTTPS request?

@return [Boolean]

true if this request was made via HTTPS
# File lib/webmachine/request.rb, line 93
def https?
  uri.scheme == 'https'
end
method_missing(m, *args, &block) click to toggle source

Enables quicker access to request headers by using a lowercased-underscored version of the header name, e.g. ‘if_unmodified_since`.

Calls superclass method
# File lib/webmachine/request.rb, line 39
    def method_missing(m, *args, &block)
      if HTTP_HEADERS_MATCH.match?(m)
        # Access headers more easily as underscored methods.
        header_name = m.to_s.tr(UNDERSCORE, DASH)
        if (header_value = @headers[header_name])
          # Make future lookups faster.
          self.class.class_eval <<-RUBY, __FILE__, __LINE__ + 1
          def #{m}
            @headers["#{header_name}"]
          end
          RUBY
        end
        header_value
      else
        super
      end
    end
options?() click to toggle source

Is this an OPTIONS request?

@return [Boolean]

true if this request was made with the OPTIONS method
# File lib/webmachine/request.rb, line 157
def options?
  method == OPTIONS_METHOD
end
post?() click to toggle source

Is this a POST request?

@return [Boolean]

true if this request was made with the GET method
# File lib/webmachine/request.rb, line 117
def post?
  method == POST_METHOD
end
put?() click to toggle source

Is this a PUT request?

@return [Boolean]

true if this request was made with the PUT method
# File lib/webmachine/request.rb, line 125
def put?
  method == PUT_METHOD
end
query() click to toggle source

Returns a hash of query parameters (they come after the ? in the URI). Note that this does NOT work in the same way as Rails, i.e. it does not support nested arrays and hashes. @return [Hash] query parameters

# File lib/webmachine/request.rb, line 66
def query
  unless @query
    @query = {}
    (uri.query || '').split('&').each do |kv|
      key, value = kv.split('=')
      if key && value
        key, value = CGI.unescape(key), CGI.unescape(value)
        @query[key] = value
      end
    end
  end
  @query
end
trace?() click to toggle source

Is this a TRACE request?

@return [Boolean]

true if this request was made with the TRACE method
# File lib/webmachine/request.rb, line 141
def trace?
  method == TRACE_METHOD
end

Private Instance Methods

build_uri(uri, headers) click to toggle source
# File lib/webmachine/request.rb, line 182
def build_uri(uri, headers)
  uri = URI(uri)
  uri.port ||= 80
  uri.scheme ||= HTTP
  if uri.host
    return uri
  end

  parse_host(uri, headers.fetch(HOST))
end
parse_host(uri, host_string) click to toggle source
# File lib/webmachine/request.rb, line 166
def parse_host(uri, host_string)
  # Split host and port number from string.
  case host_string
  when IPV6_MATCH
    uri.host = IPAddr.new($~[:address], Socket::AF_INET6).to_s
    uri.port = $~[:port].to_i
  when HOST_MATCH
    uri.host = $~[:host]
    uri.port = $~[:port].to_i
  else # string with no port number
    uri.host = host_string
  end

  uri
end