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

Direct Known Subclasses

Adapters::Rack::RackRequest

Constant Summary collapse

HTTP_HEADERS_MATCH =
/^(?:[a-z0-9])+(?:_[a-z0-9]+)*$/i.freeze
ROUTING_PATH_MATCH =
/^\/(.*)/.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method, uri, headers, body, routing_tokens = nil, base_uri = nil) ⇒ 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



24
25
26
27
28
29
30
31
32
# 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

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`.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# 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

Instance Attribute Details

#base_uriObject (readonly)

Returns the value of attribute base_uri.



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

def base_uri
  @base_uri
end

#bodyObject (readonly)

Returns the value of attribute body.



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

def body
  @body
end

#disp_pathObject

Returns the value of attribute disp_path.



16
17
18
# File 'lib/webmachine/request.rb', line 16

def disp_path
  @disp_path
end

#headersObject (readonly)

Returns the value of attribute headers.



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

def headers
  @headers
end

#methodObject (readonly)

Returns the value of attribute method.



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

def method
  @method
end

#path_infoObject

Returns the value of attribute path_info.



16
17
18
# File 'lib/webmachine/request.rb', line 16

def path_info
  @path_info
end

#path_tokensObject

Returns the value of attribute path_tokens.



16
17
18
# File 'lib/webmachine/request.rb', line 16

def path_tokens
  @path_tokens
end

#routing_tokensObject (readonly)

Returns the value of attribute routing_tokens.



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

def routing_tokens
  @routing_tokens
end

#uriObject (readonly)

Returns the value of attribute uri.



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

def uri
  @uri
end

Instance Method Details

#connect?Boolean

Is this a CONNECT request?

Returns:

  • (Boolean)

    true if this request was made with the CONNECT method



149
150
151
# File 'lib/webmachine/request.rb', line 149

def connect?
  method == CONNECT_METHOD
end

#cookiesHash

The cookies sent in the request.

Returns:

  • (Hash)

    {} if no Cookies header set



84
85
86
87
# File 'lib/webmachine/request.rb', line 84

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

#delete?Boolean

Is this a DELETE request?

Returns:

  • (Boolean)

    true if this request was made with the DELETE method



133
134
135
# File 'lib/webmachine/request.rb', line 133

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



101
102
103
# File 'lib/webmachine/request.rb', line 101

def get?
  method == GET_METHOD
end

#has_body?Boolean

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

Returns:

  • (Boolean)


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

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



109
110
111
# File 'lib/webmachine/request.rb', line 109

def head?
  method == HEAD_METHOD
end

#https?Boolean

Is this an HTTPS request?

Returns:

  • (Boolean)

    true if this request was made via HTTPS



93
94
95
# File 'lib/webmachine/request.rb', line 93

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



157
158
159
# File 'lib/webmachine/request.rb', line 157

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



117
118
119
# File 'lib/webmachine/request.rb', line 117

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



125
126
127
# File 'lib/webmachine/request.rb', line 125

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



66
67
68
69
70
71
72
73
74
75
76
77
78
# 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?Boolean

Is this a TRACE request?

Returns:

  • (Boolean)

    true if this request was made with the TRACE method



141
142
143
# File 'lib/webmachine/request.rb', line 141

def trace?
  method == TRACE_METHOD
end