Class: Ronin::Listener::HTTP::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/ronin/listener/http/request.rb

Overview

Represents a received HTTP request.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(remote_addr:, method:, path:, query: nil, version:, headers:, body: nil) ⇒ Request

Initializes the request.

Parameters:

  • remote_addr (Addrinfo)

    The remote address that sent the request.

  • method (String)

    The HTTP request method.

  • path (String)

    The request path.

  • query (String, nil) (defaults to: nil)

    The request query string.

  • version (String)

    The HTTP version.

  • headers (Hash{String => String})

    The HTTP request headers.

  • body (String, nil) (defaults to: nil)

    The optional body sent with 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

#bodyString? (readonly)

The optional request body.

Returns:

  • (String, nil)


65
66
67
# File 'lib/ronin/listener/http/request.rb', line 65

def body
  @body
end

#headersHash{String => String} (readonly)

The HTTP request headers.

Returns:

  • (Hash{String => String})


60
61
62
# File 'lib/ronin/listener/http/request.rb', line 60

def headers
  @headers
end

#methodString (readonly)

The HTTP request method.

Returns:

  • (String)


40
41
42
# File 'lib/ronin/listener/http/request.rb', line 40

def method
  @method
end

#pathString (readonly)

The request path.

Returns:

  • (String)


45
46
47
# File 'lib/ronin/listener/http/request.rb', line 45

def path
  @path
end

#queryString? (readonly)

The request query string.

Returns:

  • (String, nil)


50
51
52
# File 'lib/ronin/listener/http/request.rb', line 50

def query
  @query
end

#remote_addrAddrinfo (readonly)

The remote address that sent the request.

Returns:

  • (Addrinfo)


35
36
37
# File 'lib/ronin/listener/http/request.rb', line 35

def remote_addr
  @remote_addr
end

#versionString (readonly)

The HTTP version.

Returns:

  • (String)


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.

Parameters:

  • other (Object)

    The other object to compare to.

Returns:

  • (Boolean)

    Indicates if the request is equal 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_ipString

The remote IP address that sent the request.

Returns:

  • (String)


112
113
114
# File 'lib/ronin/listener/http/request.rb', line 112

def remote_ip
  @remote_addr.ip_address
end

#remote_portString

The remote port that sent the request.

Returns:

  • (String)


121
122
123
# File 'lib/ronin/listener/http/request.rb', line 121

def remote_port
  @remote_addr.ip_port
end

#to_csvString

Converts the request into a CSV line.

Returns:

  • (String)

    The generated 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_hHash{Symbol => Object} Also known as: as_json

Converts the request to a Hash.

Returns:

  • (Hash{Symbol => Object})


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.

Parameters:

  • args (Array)

    Additional arguments for Hash#to_json.

Returns:

  • (String)

    The raw JSON string.



221
222
223
# File 'lib/ronin/listener/http/request.rb', line 221

def to_json(*args)
  as_json.to_json(*args)
end

#to_sString

Converts the request to a String.

Returns:

  • (String)

    The raw HTTP request.



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