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

HTTP_HEADERS_MATCH =
/^(?:[a-z0-9])+(?:_[a-z0-9]+)*$/i.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



22
23
24
25
# File 'lib/webmachine/request.rb', line 22

def initialize(method, uri, headers, body)
  @method, @headers, @body = method, headers, body
  @uri = build_uri(uri, headers)
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.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/webmachine/request.rb', line 32

def method_missing(m, *args, &block)
  if m =~ HTTP_HEADERS_MATCH
    # 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__
      def #{m}
        headers["#{header_name}"]
      end
      RUBY
    end
    header_value
  else
    super
  end
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



13
14
15
# File 'lib/webmachine/request.rb', line 13

def body
  @body
end

#disp_pathObject

Returns the value of attribute disp_path.



14
15
16
# File 'lib/webmachine/request.rb', line 14

def disp_path
  @disp_path
end

#headersObject (readonly)

Returns the value of attribute headers.



13
14
15
# File 'lib/webmachine/request.rb', line 13

def headers
  @headers
end

#methodObject (readonly)

Returns the value of attribute method.



13
14
15
# File 'lib/webmachine/request.rb', line 13

def method
  @method
end

#path_infoObject

Returns the value of attribute path_info.



14
15
16
# File 'lib/webmachine/request.rb', line 14

def path_info
  @path_info
end

#path_tokensObject

Returns the value of attribute path_tokens.



14
15
16
# File 'lib/webmachine/request.rb', line 14

def path_tokens
  @path_tokens
end

#uriObject (readonly)

Returns the value of attribute uri.



13
14
15
# File 'lib/webmachine/request.rb', line 13

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)


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

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

#connect?Boolean

Is this a CONNECT request?

Returns:

  • (Boolean)

    true if this request was made with the CONNECT method



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

def connect?
  method == CONNECT_METHOD
end

#cookiesHash

The cookies sent in the request.

Returns:

  • (Hash)

    {} if no Cookies header set



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

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



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

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



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

def get?
  method == GET_METHOD
end

#has_body?Boolean

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

Returns:

  • (Boolean)


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

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



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

def head?
  method == HEAD_METHOD
end

#https?Boolean

Is this an HTTPS request?

Returns:

  • (Boolean)

    true if this request was made via HTTPS



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

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



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

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



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

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



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

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



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

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



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

def trace?
  method == TRACE_METHOD
end