Class: Tilia::Http::Request

Inherits:
Object
  • Object
show all
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

Attributes included from Message

#body, #headers, #http_version

Instance Method Summary collapse

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

Parameters:

  • method (String) (defaults to: nil)
  • url (String) (defaults to: nil)
  • array

    headers

  • resource

    body



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)
  initialize_message
  @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_urlString

Returns the absolute url.

Returns:

  • (String)


97
98
99
# File 'lib/tilia/http/request.rb', line 97

def absolute_url
  @absolute_url
end

#base_urlString

Returns the current base url.

Returns:

  • (String)


104
105
106
# File 'lib/tilia/http/request.rb', line 104

def base_url
  @base_url
end

#methodString

Returns the current HTTP method

Returns:

  • (String)


17
18
19
# File 'lib/tilia/http/request.rb', line 17

def method
  @method
end

#post_dataObject

Returns the POST data.

This is equivalent to PHP’s $_POST superglobal.

Returns:

  • array



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.

Parameters:

  • array

    data



188
189
190
# File 'lib/tilia/http/request.rb', line 188

def raw_server_data=(value)
  @raw_server_data = value
end

#urlString

Returns the request url.

Returns:

  • (String)


22
23
24
# File 'lib/tilia/http/request.rb', line 22

def url
  @url
end

Instance Method Details

#pathString

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.

Returns:

  • (String)


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_parametersObject

Returns the list of query parameters.

This is equivalent to PHP’s $_GET superglobal.

Returns:

  • array



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.

Parameters:

  • value_name (String)

Returns:

  • (String, nil)


198
199
200
# File 'lib/tilia/http/request.rb', line 198

def raw_server_value(value_name)
  @raw_server_data[value_name]
end

#to_sString

Serializes the request object as a string.

This is useful for debugging purposes.

Returns:

  • (String)


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