Class: Webmachine::Request

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/webmachine/request.rb

Overview

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

Constant Summary collapse

GET_METHOD =
"GET"
HEAD_METHOD =
"HEAD"
POST_METHOD =
"POST"
PUT_METHOD =
"PUT"
DELETE_METHOD =
"DELETE"
OPTIONS_METHOD =
"OPTIONS"
TRACE_METHOD =
"TRACE"
CONNECT_METHOD =
"CONNECT"
STANDARD_HTTP_METHODS =
[
 GET_METHOD, HEAD_METHOD, POST_METHOD,
 PUT_METHOD, DELETE_METHOD, TRACE_METHOD,
 CONNECT_METHOD, OPTIONS_METHOD
].map!(&:freeze)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method, uri, headers, body) ⇒ Request

Returns a new instance of Request

Parameters:

  • method (String)

    the HTTP request method

  • uri (URI)

    the requested URI, including host, scheme and port

  • headers (Headers)

    the HTTP request headers

  • body (String, #to_s, #each, nil)

    the entity included in the request, if present


33
34
35
# File 'lib/webmachine/request.rb', line 33

def initialize(method, uri, headers, body)
  @method, @uri, @headers, @body = method, uri, headers, body
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object

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


42
43
44
45
46
47
48
49
# File 'lib/webmachine/request.rb', line 42

def method_missing(m, *args, &block)
  if m.to_s =~ /^(?:[a-z0-9])+(?:_[a-z0-9]+)*$/i
    # Access headers more easily as underscored methods.
    self[m.to_s.tr('_', '-')]
  else
    super
  end
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body


9
10
11
# File 'lib/webmachine/request.rb', line 9

def body
  @body
end

#disp_pathObject

Returns the value of attribute disp_path


10
11
12
# File 'lib/webmachine/request.rb', line 10

def disp_path
  @disp_path
end

#headersObject (readonly)

Returns the value of attribute headers


9
10
11
# File 'lib/webmachine/request.rb', line 9

def headers
  @headers
end

#methodObject (readonly)

Returns the value of attribute method


9
10
11
# File 'lib/webmachine/request.rb', line 9

def method
  @method
end

#path_infoObject

Returns the value of attribute path_info


10
11
12
# File 'lib/webmachine/request.rb', line 10

def path_info
  @path_info
end

#path_tokensObject

Returns the value of attribute path_tokens


10
11
12
# File 'lib/webmachine/request.rb', line 10

def path_tokens
  @path_tokens
end

#uriObject (readonly)

Returns the value of attribute uri


9
10
11
# File 'lib/webmachine/request.rb', line 9

def uri
  @uri
end

Instance Method Details

#base_uriURI

The root URI for the request, ignoring path and query. This is useful for calculating relative paths to resources.

Returns:

  • (URI)

59
60
61
62
63
64
# File 'lib/webmachine/request.rb', line 59

def base_uri
  @base_uri ||= uri.dup.tap do |u|
    u.path = "/"
    u.query = nil
  end
end

#connect?Boolean

Is this a CONNECT request?

Returns:

  • (Boolean)

    true if this request was made with the CONNECT method


155
156
157
# File 'lib/webmachine/request.rb', line 155

def connect?
  method == CONNECT_METHOD
end

#cookiesHash

The cookies sent in the request.

Returns:

  • (Hash)

    {} if no Cookies header set


88
89
90
91
92
93
# File 'lib/webmachine/request.rb', line 88

def cookies
  unless @cookies
    @cookies = Webmachine::Cookie.parse(headers['Cookie'])
  end
  @cookies
end

#delete?Boolean

Is this a DELETE request?

Returns:

  • (Boolean)

    true if this request was made with the DELETE method


139
140
141
# File 'lib/webmachine/request.rb', line 139

def delete?
  method == DELETE_METHOD
end

#get?Boolean

Is this a GET request?

Returns:

  • (Boolean)

    true if this request was made with the GET method


107
108
109
# File 'lib/webmachine/request.rb', line 107

def get?
  method == GET_METHOD
end

#has_body?Boolean

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

Returns:

  • (Boolean)

52
53
54
# File 'lib/webmachine/request.rb', line 52

def has_body?
  !(body.nil? || body.empty?)
end

#head?Boolean

Is this a HEAD request?

Returns:

  • (Boolean)

    true if this request was made with the HEAD method


115
116
117
# File 'lib/webmachine/request.rb', line 115

def head?
  method == HEAD_METHOD
end

#https?Boolean

Is this an HTTPS request?

Returns:

  • (Boolean)

    true if this request was made via HTTPS


99
100
101
# File 'lib/webmachine/request.rb', line 99

def https?
  uri.scheme == "https"
end

#options?Boolean

Is this an OPTIONS request?

Returns:

  • (Boolean)

    true if this request was made with the OPTIONS method


163
164
165
# File 'lib/webmachine/request.rb', line 163

def options?
  method == OPTIONS_METHOD
end

#post?Boolean

Is this a POST request?

Returns:

  • (Boolean)

    true if this request was made with the GET method


123
124
125
# File 'lib/webmachine/request.rb', line 123

def post?
  method == POST_METHOD
end

#put?Boolean

Is this a PUT request?

Returns:

  • (Boolean)

    true if this request was made with the PUT method


131
132
133
# File 'lib/webmachine/request.rb', line 131

def put?
  method == PUT_METHOD
end

#queryHash

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.

Returns:

  • (Hash)

    query parameters


70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/webmachine/request.rb', line 70

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?Boolean

Is this a TRACE request?

Returns:

  • (Boolean)

    true if this request was made with the TRACE method


147
148
149
# File 'lib/webmachine/request.rb', line 147

def trace?
  method == TRACE_METHOD
end