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

#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


151
152
153
# File 'lib/webmachine/request.rb', line 151

def connect?
  method == CONNECT_METHOD
end

#cookiesHash

The cookies sent in the request.

Returns:

  • (Hash)

    {} if no Cookies header set


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

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


135
136
137
# File 'lib/webmachine/request.rb', line 135

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


103
104
105
# File 'lib/webmachine/request.rb', line 103

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


111
112
113
# File 'lib/webmachine/request.rb', line 111

def head?
  method == HEAD_METHOD
end

#https?Boolean

Is this an HTTPS request?

Returns:

  • (Boolean)

    true if this request was made via HTTPS


95
96
97
# File 'lib/webmachine/request.rb', line 95

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


159
160
161
# File 'lib/webmachine/request.rb', line 159

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


119
120
121
# File 'lib/webmachine/request.rb', line 119

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


127
128
129
# File 'lib/webmachine/request.rb', line 127

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


143
144
145
# File 'lib/webmachine/request.rb', line 143

def trace?
  method == TRACE_METHOD
end