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
# 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 ||= []
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:



184
185
186
187
188
# File 'lib/arachni/http/response.rb', line 184

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



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/arachni/http/response.rb', line 198

def self.from_typhoeus( response )
    redirections = response.redirections.map do |redirect|
        new(
            url:     redirect.headers['Location'] || response.effective_url,
            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,
        total_time:     response.total_time,
        return_code:    response.return_code,
        return_message: response.return_message
    )
end

Instance Method Details

#==(other) ⇒ Object



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

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

#body=(body) ⇒ Object



142
143
144
145
146
147
148
149
# File 'lib/arachni/http/response.rb', line 142

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

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

    @body.freeze
end

#hashObject



194
195
196
# File 'lib/arachni/http/response.rb', line 194

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.



110
111
112
# File 'lib/arachni/http/response.rb', line 110

def modified?
    code != 304
end

#platformsPlatform

Returns Applicable platforms for the page.

Returns:

  • (Platform)

    Applicable platforms for the page.



76
77
78
# File 'lib/arachni/http/response.rb', line 76

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.



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

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

#status_lineString

Returns First line of the response.

Returns:

  • (String)

    First line of the response.



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

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.



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/arachni/http/response.rb', line 117

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.



138
139
140
# File 'lib/arachni/http/response.rb', line 138

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

#to_hHash

Returns:



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/arachni/http/response.rb', line 157

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:



152
153
154
# File 'lib/arachni/http/response.rb', line 152

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.



176
177
178
179
180
# File 'lib/arachni/http/response.rb', line 176

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.



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

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