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 ⇒ Object
Returns the value of attribute absolute_url.
-
#base_url ⇒ Object
Returns the value of attribute base_url.
-
#method ⇒ Object
Returns the value of attribute method.
-
#post_data ⇒ Object
Returns the value of attribute post_data.
-
#url ⇒ Object
Returns the value of attribute url.
Attributes included from Message
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 ⇒ Hash
Returns the list of query parameters.
-
#raw_server_data=(data) ⇒ void
Sets the _SERVER array.
-
#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, #headers, #initialize_copy, #remove_header, #update_header, #update_headers
Constructor Details
#initialize(method = nil, url = nil, headers = nil, body = nil) ⇒ Request
Creates the request object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/tilia/http/request.rb', line 18 def initialize(method = nil, url = nil, headers = nil, body = nil) super() @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) @method = method if method @url = url if url update_headers(headers) if headers @body = body if body end |
Instance Attribute Details
#absolute_url ⇒ Object
Returns the value of attribute absolute_url.
65 66 67 |
# File 'lib/tilia/http/request.rb', line 65 def absolute_url @absolute_url end |
#base_url ⇒ Object
Returns the value of attribute base_url.
71 72 73 |
# File 'lib/tilia/http/request.rb', line 71 def base_url @base_url end |
#method ⇒ Object
Returns the value of attribute method.
34 35 36 |
# File 'lib/tilia/http/request.rb', line 34 def method @method end |
#post_data ⇒ Object
Returns the value of attribute post_data.
98 99 100 |
# File 'lib/tilia/http/request.rb', line 98 def post_data @post_data end |
#url ⇒ Object
Returns the value of attribute url.
40 41 42 |
# File 'lib/tilia/http/request.rb', line 40 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.
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/tilia/http/request.rb', line 74 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 ⇒ Hash
Returns the list of query parameters.
This is equivalent to PHP’s $_GET superglobal.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/tilia/http/request.rb', line 46 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_data=(data) ⇒ void
This method returns an undefined value.
Sets the _SERVER array.
106 107 108 |
# File 'lib/tilia/http/request.rb', line 106 def raw_server_data=(data) @raw_server_data = data.dup 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.
101 102 103 |
# File 'lib/tilia/http/request.rb', line 101 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.
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/tilia/http/request.rb', line 115 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 |