Class: Arachni::HTTP::Response

Inherits:
Message show all
Defined in:
lib/arachni/http/response.rb,
lib/arachni/http/response/scope.rb

Overview

HTTP Response representation.

Author:

Defined Under Namespace

Classes: Scope

Instance Attribute Summary collapse

Attributes inherited from Message

#body, #headers, #url

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Message

#parsed_url, #scope

Constructor Details

#initialize(options = {}) ⇒ Response

Returns a new instance of Response.



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/arachni/http/response.rb', line 64

def initialize( options = {} )
    super( options )

    @body ||= ''
    @code ||= 0

    # Holds the redirection responses that eventually led to this one.
    @redirections ||= []

    @time ||= 0.0
end

Instance Attribute Details

#app_timeFloat

Returns Approximate time the web application took to process the #request.

Returns:

  • (Float)

    Approximate time the web application took to process the #request.



62
63
64
# File 'lib/arachni/http/response.rb', line 62

def app_time
  @app_time
end

#codeInteger

Returns HTTP response status code.

Returns:

  • (Integer)

    HTTP response status code.



20
21
22
# File 'lib/arachni/http/response.rb', line 20

def code
  @code
end

#headers_stringString

Returns Raw headers.

Returns:



48
49
50
# File 'lib/arachni/http/response.rb', line 48

def headers_string
  @headers_string
end

#ip_addressString

Returns IP address of the server.

Returns:

  • (String)

    IP address of the server.



24
25
26
# File 'lib/arachni/http/response.rb', line 24

def ip_address
  @ip_address
end

#messageString

Returns HTTP response status message.

Returns:

  • (String)

    HTTP response status message.



28
29
30
# File 'lib/arachni/http/response.rb', line 28

def message
  @message
end

#redirectionsArray<Response>

Returns Automatically followed redirections that eventually led to this response.

Returns:

  • (Array<Response>)

    Automatically followed redirections that eventually led to this response.



36
37
38
# File 'lib/arachni/http/response.rb', line 36

def redirections
  @redirections
end

#requestRequest

Returns HTTP Arachni::HTTP::Request which triggered this Arachni::HTTP::Response.

Returns:



32
33
34
# File 'lib/arachni/http/response.rb', line 32

def request
  @request
end

#return_codeSymbol

Returns ‘libcurl` return code.

Returns:

  • (Symbol)

    ‘libcurl` return code.



40
41
42
# File 'lib/arachni/http/response.rb', line 40

def return_code
  @return_code
end

#return_messageString

Returns ‘libcurl` return code.

Returns:

  • (String)

    ‘libcurl` return code.



44
45
46
# File 'lib/arachni/http/response.rb', line 44

def return_message
  @return_message
end

#timeFloat

Returns Time, in seconds, it took from the start until the full response was received.

Returns:

  • (Float)

    Time, in seconds, it took from the start until the full response was received.



58
59
60
# File 'lib/arachni/http/response.rb', line 58

def time
  @time
end

#total_timeFloat

Returns Total time in seconds for the transfer, including name resolving, TCP connect etc.

Returns:

  • (Float)

    Total time in seconds for the transfer, including name resolving, TCP connect etc.



53
54
55
# File 'lib/arachni/http/response.rb', line 53

def total_time
  @total_time
end

Class Method Details

.from_rpc_data(data) ⇒ Request

Parameters:

Returns:



190
191
192
193
194
# File 'lib/arachni/http/response.rb', line 190

def self.from_rpc_data( data )
    data['request']     = Request.from_rpc_data( data['request'] )
    data['return_code'] = data['return_code'].to_sym if data['return_code']
    new data
end

.from_typhoeus(response) ⇒ Object



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/arachni/http/response.rb', line 204

def self.from_typhoeus( response )
    redirections = response.redirections.map do |redirect|
        rurl   = URI.to_absolute( redirect.headers['Location'],
                                  response.effective_url )
        rurl ||= response.effective_url

        # Broken redirection, skip it...
        next if !rurl

        new(
            url:     rurl,
            code:    redirect.code,
            headers: redirect.headers
        )
    end

    new(
        url:            response.effective_url,
        code:           response.code,
        ip_address:     response.primary_ip,
        headers:        response.headers,
        headers_string: response.response_headers,
        body:           response.body,
        redirections:   redirections,
        time:           response.time,
        app_time:       (response.timed_out? ? response.time :
                            response.start_transfer_time - response.pretransfer_time).to_f,
        total_time:     response.total_time.to_f,
        return_code:    response.return_code,
        return_message: response.return_message
    )
end

Instance Method Details

#==(other) ⇒ Object



196
197
198
# File 'lib/arachni/http/response.rb', line 196

def ==( other )
    hash == other.hash
end

#body=(body) ⇒ Object



148
149
150
151
152
153
154
155
# File 'lib/arachni/http/response.rb', line 148

def body=( body )
    @body = body.to_s.dup

    text_check = text?
    @body.recode! if text_check.nil? || text_check

    @body.freeze
end

#hashObject



200
201
202
# File 'lib/arachni/http/response.rb', line 200

def hash
    to_h.hash
end

#modified?Boolean

Note:

Depends on the response code.

Returns ‘true` if the remote resource has been modified since the date given in the `If-Modified-Since` request header field, `false` otherwise.

Returns:

  • (Boolean)

    ‘true` if the remote resource has been modified since the date given in the `If-Modified-Since` request header field, `false` otherwise.



116
117
118
# File 'lib/arachni/http/response.rb', line 116

def modified?
    code != 304
end

#platformsPlatform

Returns Applicable platforms for the page.

Returns:

  • (Platform)

    Applicable platforms for the page.



82
83
84
# File 'lib/arachni/http/response.rb', line 82

def platforms
    Platform::Manager[url]
end

#redirect?Boolean Also known as: redirection?

Returns ‘true` if the response is a `3xx` redirect and there is a `Location` header field.

Returns:

  • (Boolean)

    ‘true` if the response is a `3xx` redirect and there is a `Location` header field.



102
103
104
# File 'lib/arachni/http/response.rb', line 102

def redirect?
    code >= 300 && code <= 399 && !!headers.location
end

#status_lineString

Returns First line of the response.

Returns:

  • (String)

    First line of the response.



88
89
90
91
# File 'lib/arachni/http/response.rb', line 88

def status_line
    return if !headers_string
    @status_line ||= headers_string.lines.first.to_s.chomp.freeze
end

#text?Bool

Returns ‘true` if the response body is textual in nature, `false` if binary, `nil` if could not be determined.

Returns:

  • (Bool)

    ‘true` if the response body is textual in nature, `false` if binary, `nil` if could not be determined.



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/arachni/http/response.rb', line 123

def text?
    return if !@body

    if (type = headers.content_type)
        return true if type.start_with?( 'text/' )

        # Non "text/" nor "application/" content types will surely not be
        # text-based so bail out early.
        return false if !type.start_with?( 'application/' )
    end

    # Last resort, more resource intensive binary detection.
    begin
        !@body.binary?
    rescue ArgumentError
        nil
    end
end

#timed_out?Boolean

Returns ‘true` if timed out, `false` otherwise.

Returns:

  • (Boolean)

    ‘true` if timed out, `false` otherwise.



144
145
146
# File 'lib/arachni/http/response.rb', line 144

def timed_out?
    [:operation_timedout, :couldnt_connect].include? return_code
end

#to_hHash

Returns:



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/arachni/http/response.rb', line 163

def to_h
    hash = {}
    instance_variables.each do |var|
        hash[var.to_s.gsub( /@/, '' ).to_sym] = instance_variable_get( var )
    end

    hash[:headers] = {}.merge( hash[:headers] )

    hash.delete( :scope )
    hash.delete( :parsed_url )
    hash.delete( :redirections )
    hash.delete( :request )
    hash.delete( :scope )

    hash
end

#to_pageArachni::Page

Returns:



158
159
160
# File 'lib/arachni/http/response.rb', line 158

def to_page
    Page.from_response self
end

#to_rpc_dataHash

Returns Data representing this instance that are suitable the RPC transmission.

Returns:

  • (Hash)

    Data representing this instance that are suitable the RPC transmission.



182
183
184
185
186
# File 'lib/arachni/http/response.rb', line 182

def to_rpc_data
    data = to_h
    data[:request] = request.to_rpc_data
    data.my_stringify_keys(false)
end

#to_sString

Returns HTTP response string.

Returns:

  • (String)

    HTTP response string.



95
96
97
# File 'lib/arachni/http/response.rb', line 95

def to_s
    "#{headers_string}#{body}"
end