Class: Tilia::Http::Request
- Inherits:
-
Object
- Object
- Tilia::Http::Request
- Includes:
- Message, RequestInterface
- Defined in:
- lib/tilia/http/request.rb
Overview
The Request class represents a single HTTP request.
You can either simply construct the object from scratch, or if you need access to the current HTTP request, use Sapi::getRequest.
Instance Attribute Summary collapse
-
#absolute_url ⇒ String
Returns the absolute url.
-
#base_url ⇒ String
Returns the current base url.
-
#method ⇒ String
Returns the current HTTP method.
-
#post_data ⇒ Object
Returns the POST data.
-
#raw_server_data ⇒ void
writeonly
Sets the _SERVER array.
-
#url ⇒ String
Returns the request url.
Attributes included from Message
#body, #headers, #http_version
Instance Method Summary collapse
-
#initialize(method = nil, url = nil, headers = nil, body = nil) ⇒ Request
constructor
Creates the request object.
-
#path ⇒ String
Returns the relative path.
-
#query_parameters ⇒ Object
Returns the list of query parameters.
-
#raw_server_value(value_name) ⇒ String?
Returns an item from the _SERVER array.
-
#to_s ⇒ String
Serializes the request object as a string.
Methods included from MessageInterface
#add_header, #add_headers, #body, #body=, #body_as_stream, #body_as_string, #header, #header?, #header_as_array, #headers, #http_version, #http_version=, #remove_header, #update_header, #update_headers
Methods included from Message
#add_header, #add_headers, #body_as_stream, #body_as_string, #header, #header?, #header_as_array, #initialize_copy, #initialize_message, #remove_header, #update_header, #update_headers
Constructor Details
#initialize(method = nil, url = nil, headers = nil, body = nil) ⇒ Request
Creates the request object
32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/tilia/http/request.rb', line 32 def initialize(method = nil, url = nil, headers = nil, body = nil) @base_url = '/' # RUBY @post_data = {} @raw_server_data = {} fail ArgumentError, 'The first argument for this constructor should be a string or null, not an array. Did you upgrade from sabre/http 1.0 to 2.0?' if method.is_a?(Array) self.method = method if method self.url = url if url update_headers(headers) if headers self.body = body if body end |
Instance Attribute Details
#absolute_url ⇒ String
Returns the absolute url.
97 98 99 |
# File 'lib/tilia/http/request.rb', line 97 def absolute_url @absolute_url end |
#base_url ⇒ String
Returns the current base url.
104 105 106 |
# File 'lib/tilia/http/request.rb', line 104 def base_url @base_url end |
#method ⇒ String
Returns the current HTTP method
17 18 19 |
# File 'lib/tilia/http/request.rb', line 17 def method @method end |
#post_data ⇒ Object
Returns the POST data.
This is equivalent to PHP’s $_POST superglobal.
161 162 163 |
# File 'lib/tilia/http/request.rb', line 161 def post_data @post_data end |
#raw_server_data=(data) ⇒ void
This method returns an undefined value.
Sets the _SERVER array.
188 189 190 |
# File 'lib/tilia/http/request.rb', line 188 def raw_server_data=(value) @raw_server_data = value end |
#url ⇒ String
Returns the request url.
22 23 24 |
# File 'lib/tilia/http/request.rb', line 22 def url @url end |
Instance Method Details
#path ⇒ String
Returns the relative path.
This is being calculated using the base url. This path will not start with a slash, so it will always return something like ‘example/path.html’.
If the full path is equal to the base url, this method will return an empty string.
This method will also urldecode the path, and if the url was incoded as ISO-8859-1, it will convert it to UTF-8.
If the path is outside of the base url, a LogicException will be thrown.
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/tilia/http/request.rb', line 136 def path # Removing duplicated slashes. uri = (url || '').gsub('//', '/') uri = Tilia::Uri.normalize(uri) base_uri = Tilia::Uri.normalize(base_url) if uri.index(base_uri) == 0 # We're not interested in the query part (everything after the ?). uri = uri.split('?').first return Tilia::Http::UrlUtil.decode_path(uri[base_uri.size..-1]).gsub(%r{^/+|/+$}, '') elsif uri + '/' == base_uri # A special case, if the baseUri was accessed without a trailing # slash, we'll accept it as well. return '' end fail "Requested uri (#{url}) is out of base uri (#{base_url})" end |
#query_parameters ⇒ Object
Returns the list of query parameters.
This is equivalent to PHP’s $_GET superglobal.
73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/tilia/http/request.rb', line 73 def query_parameters url = self.url if !(index = url.index('?')) {} else query_params = CGI.parse(url[index + 1..-1]) query_params.keys.each do |key| query_params[key] = query_params[key][0] if query_params[key].size == 1 query_params[key] = nil if query_params[key].size == 0 end query_params end end |
#raw_server_value(value_name) ⇒ String?
Returns an item from the _SERVER array.
If the value does not exist in the array, null is returned.
198 199 200 |
# File 'lib/tilia/http/request.rb', line 198 def raw_server_value(value_name) @raw_server_data[value_name] end |
#to_s ⇒ String
Serializes the request object as a string.
This is useful for debugging purposes.
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/tilia/http/request.rb', line 215 def to_s out = "#{method} #{url} HTTP/#{http_version}\r\n" headers.each do |key, value| value.each do |v| if key == 'Authorization' v = v.split(' ').first v << ' REDACTED' end out << "#{key}: #{v}\r\n" end end out << "\r\n" out << body_as_string out end |