Class: Ronin::Listener::HTTP::Request
- Inherits:
-
Object
- Object
- Ronin::Listener::HTTP::Request
- Defined in:
- lib/ronin/listener/http/request.rb
Overview
Represents a received HTTP request.
Instance Attribute Summary collapse
-
#body ⇒ String?
readonly
The optional request body.
-
#headers ⇒ Hash{String => String}
readonly
The HTTP request headers.
-
#method ⇒ String
readonly
The HTTP request method.
-
#path ⇒ String
readonly
The request path.
-
#query ⇒ String?
readonly
The request query string.
-
#remote_addr ⇒ Addrinfo
readonly
The remote address that sent the request.
-
#version ⇒ String
readonly
The HTTP version.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Compares the request to another request.
-
#initialize(remote_addr:, method:, path:, query: nil, version:, headers:, body: nil) ⇒ Request
constructor
Initializes the request.
-
#remote_ip ⇒ String
The remote IP address that sent the request.
-
#remote_port ⇒ String
The remote port that sent the request.
-
#to_csv ⇒ String
Converts the request into a CSV line.
-
#to_h ⇒ Hash{Symbol => Object}
(also: #as_json)
Converts the request to a Hash.
-
#to_json(*args) ⇒ String
Converts the HTTP request into JSON.
-
#to_s ⇒ String
Converts the request to a String.
Constructor Details
#initialize(remote_addr:, method:, path:, query: nil, version:, headers:, body: nil) ⇒ Request
Initializes the request.
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/ronin/listener/http/request.rb', line 91 def initialize(remote_addr: , method: , path: , query: nil, version: , headers:, body: nil) @remote_addr = remote_addr @method = method @path = path @query = query @version = version @headers = headers @body = body end |
Instance Attribute Details
#body ⇒ String? (readonly)
The optional request body.
65 66 67 |
# File 'lib/ronin/listener/http/request.rb', line 65 def body @body end |
#headers ⇒ Hash{String => String} (readonly)
The HTTP request headers.
60 61 62 |
# File 'lib/ronin/listener/http/request.rb', line 60 def headers @headers end |
#method ⇒ String (readonly)
The HTTP request method.
40 41 42 |
# File 'lib/ronin/listener/http/request.rb', line 40 def method @method end |
#path ⇒ String (readonly)
The request path.
45 46 47 |
# File 'lib/ronin/listener/http/request.rb', line 45 def path @path end |
#query ⇒ String? (readonly)
The request query string.
50 51 52 |
# File 'lib/ronin/listener/http/request.rb', line 50 def query @query end |
#remote_addr ⇒ Addrinfo (readonly)
The remote address that sent the request.
35 36 37 |
# File 'lib/ronin/listener/http/request.rb', line 35 def remote_addr @remote_addr end |
#version ⇒ String (readonly)
The HTTP version.
55 56 57 |
# File 'lib/ronin/listener/http/request.rb', line 55 def version @version end |
Instance Method Details
#==(other) ⇒ Boolean
Compares the request to another request.
134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/ronin/listener/http/request.rb', line 134 def ==(other) self.class == other.class && remote_ip == other.remote_ip && remote_port == other.remote_port && @method == other.method && @path == other.path && @query == other.query && @version == other.version && @headers == other.headers && @body == other.body end |
#remote_ip ⇒ String
The remote IP address that sent the request.
112 113 114 |
# File 'lib/ronin/listener/http/request.rb', line 112 def remote_ip @remote_addr.ip_address end |
#remote_port ⇒ String
The remote port that sent the request.
121 122 123 |
# File 'lib/ronin/listener/http/request.rb', line 121 def remote_port @remote_addr.ip_port end |
#to_csv ⇒ String
Converts the request into a CSV line.
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/ronin/listener/http/request.rb', line 191 def to_csv CSV.generate_line( [ remote_ip, remote_port, @method, @path, @query, @version, CSV.generate { |csv| @headers.each_pair do |name_value| csv << name_value end }, @body ] ) end |
#to_h ⇒ Hash{Symbol => Object} Also known as: as_json
Converts the request to a Hash.
172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/ronin/listener/http/request.rb', line 172 def to_h { remote_ip: remote_ip, remote_port: remote_port, method: @method, path: @path, query: @query, version: @version, headers: @headers, body: @body } end |
#to_json(*args) ⇒ String
Converts the HTTP request into JSON.
221 222 223 |
# File 'lib/ronin/listener/http/request.rb', line 221 def to_json(*args) as_json.to_json(*args) end |
#to_s ⇒ String
Converts the request to a String.
152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/ronin/listener/http/request.rb', line 152 def to_s request_uri = if @query then "#{@path}?#{@query}" else @path end string = "#{@method} #{request_uri} HTTP/#{@version}\r\n" @headers.each do |name,value| string << "#{name}: #{value}\r\n" end string << "\r\n#{@body}" return string end |