Class: Arachni::Module::HTTP
- Inherits:
-
Object
- Object
- Arachni::Module::HTTP
- Includes:
- Arachni::UI::Output
- Defined in:
- lib/module/http.rb
Overview
Arachni::Module::HTTP class
Provides a simple HTTP interface for modules.
Exceptions
Any exceptions or session corruption is handled by the class.
Some are
ignored, on others the HTTP session is refreshed.
Point is, you
don’t need to worry about it.
@author: Anastasios “Zapotek” Laskos
<tasos.laskos@gmail.com> <zapotek@segfault.gr>
@version: 0.1-pre
Instance Attribute Summary (collapse)
-
- (Hash) cookie_jar
readonly
The user supplied cookie jar.
-
- (Hash) init_headers
readonly
The headers with which the HTTP client is initialized
This is always kept updated. -
- (Net::HTTP) session
readonly
The HTTP session.
-
- (URI) url
readonly
The url of the session.
Class Method Summary (collapse)
-
+ (Hash) parse_cookiejar(cookie_jar)
Class method.
Instance Method Summary (collapse)
-
- (Object) add_trainer(&block)
Blocks passed to this method will be passed each HTTP response
and in cases of redirection the new location as well. -
- (HTTP::Response) cookie(url, cookie_vars, url_vars = nil)
Gets a url with cookies and url variables.
-
- (HTTP::Response) get(url, url_vars = {}, redirect = false)
Gets a URL passing the provided variables.
-
- (HTTP::Response) header(url, headers, url_vars = nil)
Gets a url with optional url variables and modified headers.
-
- (Net::HTTP) initialize(url, opts = {})
constructor
Initializes the HTTP session given a start URL respecting system wide settings for HTTP basic auth and proxy.
- - (Object) parse_cookie_str(str)
-
- (URI) parse_url(url)
Encodes and parses a URL String.
-
- (HTTP::Response) post(url, form_vars)
Posts a form to a URL with the provided variables.
-
- (void) set_cookies(cookie_hash)
Sets cookies for the HTTP session.
Methods included from Arachni::UI::Output
#debug!, #debug?, #only_positives!, #only_positives?, #print_debug, #print_debug_backtrace, #print_debug_pp, #print_error, #print_info, #print_line, #print_ok, #print_status, #print_verbose, #verbose!, #verbose?
Constructor Details
- (Net::HTTP) initialize(url, opts = {})
Initializes the HTTP session given a start URL respecting system wide settings for HTTP basic auth and proxy
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/module/http.rb', line 72 def initialize( url, opts = {} ) @url = parse_url( url ) @opts = Hash.new @opts = @opts.merge( opts ) # create a new HTTP session refresh( ) @trainers = [] @init_headers = Hash.new @init_headers['user-agent'] = Options.instance.user_agent @init_headers['cookie'] = '' end |
Instance Attribute Details
- (Hash) cookie_jar (readonly)
The user supplied cookie jar
55 56 57 |
# File 'lib/module/http.rb', line 55 def @cookie_jar end |
- (Hash) init_headers (readonly)
The headers with which the HTTP client is initialized
This is always
kept updated.
48 49 50 |
# File 'lib/module/http.rb', line 48 def init_headers @init_headers end |
- (Net::HTTP) session (readonly)
The HTTP session
62 63 64 |
# File 'lib/module/http.rb', line 62 def session @session end |
- (URI) url (readonly)
The url of the session
40 41 42 |
# File 'lib/module/http.rb', line 40 def url @url end |
Class Method Details
+ (Hash) parse_cookiejar(cookie_jar)
Class method
Parses netscape HTTP cookie file
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 |
# File 'lib/module/http.rb', line 292 def HTTP.( ) = Hash.new jar = File.open( , 'r' ) jar.each_line { |line| # skip empty lines if (line = line.strip).size == 0 then next end # skip comment lines if line[0] == '#' then next end = line.split( "\t" ) [[-2]] = [-1] } end |
Instance Method Details
- (Object) add_trainer(&block)
Blocks passed to this method will be passed each HTTP response
and in
cases of redirection the new location as well.
329 330 331 |
# File 'lib/module/http.rb', line 329 def add_trainer( &block ) @trainers << block end |
- (HTTP::Response) cookie(url, cookie_vars, url_vars = nil)
Gets a url with cookies and url variables
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/module/http.rb', line 177 def ( url, , url_vars = nil) = @init_headers['cookie'].clone = Hash.new jar = ( ) .each_pair { |name, value| # don't audit cookies in the cookie jar # next if Options.instance.exclude_cookies.include?( name ) [name] = value } .reject { || Options.instance..include?( ['name'] ) } ( jar.merge( ) ) # wrap the code in exception handling exception_jail { url = parse_url( url ) if( url.query && url.query.size > 0 ) query = '?' + url.query append = true else query = '' append = false end full_url = url.path + URI.encode( query ) + a_to_s( url_vars, append ) res = @session.get( full_url, @init_headers ) @init_headers['cookie'] = .clone train( res ) return res } end |
- (HTTP::Response) get(url, url_vars = {}, redirect = false)
Gets a URL passing the provided variables
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/module/http.rb', line 97 def get( url, url_vars = {}, redirect = false ) url = parse_url( url ) url_vars = {} if( !url_vars ) url_vars = url_vars.merge( { '__arachni__' => '' } ) # # the exception jail function wraps the block passed to it # in exception handling and runs it # # how cool is Ruby? Seriously.... # exception_jail { if( url.query && url.query.size > 0 ) query = '?' + url.query append = true else query = '' append = false end if( redirect ) full_url = url.path + query else full_url = url.path + URI.encode( query ) + a_to_s( url_vars, append ) end res = @session.get( full_url, @init_headers ) # handle redirections if( ( redir = redirect?( res ) ).is_a?( String ) ) res = get( redir, nil, true ) train( res, redir ) else train( res ) end return res } end |
- (HTTP::Response) header(url, headers, url_vars = nil)
Gets a url with optional url variables and modified headers
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'lib/module/http.rb', line 230 def header( url, headers, url_vars = nil) # wrap the code in exception handling exception_jail { url = parse_url( url ) if( url.query && url.query.size > 0 ) query = '?' + url.query append = true else query = '' append = false end full_url = url.path + URI.encode( query ) + a_to_s( url_vars, append ) orig_headers = @init_headers.clone @init_headers = @init_headers.merge( headers ) res = @session.get( full_url, @init_headers ) @init_headers = orig_headers.clone train( res ) return res } end |
- (Object) parse_cookie_str(str)
274 275 276 277 278 279 280 281 |
# File 'lib/module/http.rb', line 274 def ( str ) = Hash.new str.split( ';' ).each { |kvp| [kvp.split( "=" )[0]] = kvp.split( "=" )[1] } return end |
- (URI) parse_url(url)
Encodes and parses a URL String
321 322 323 |
# File 'lib/module/http.rb', line 321 def parse_url( url ) URI.parse( URI.encode( url ) ) end |
- (HTTP::Response) post(url, form_vars)
Posts a form to a URL with the provided variables
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/module/http.rb', line 148 def post( url, form_vars ) req = Net::HTTP::Post.new( url, @init_headers ) req.set_form_data( form_vars ) exception_jail { res = @session.request( req ) # handle redirections if( ( redir = redirect?( res ) ).is_a?( String ) ) res = get( redir, nil, true ) train( res, redir ) else train( res ) end return res } end |
- (void) set_cookies(cookie_hash)
This method returns an undefined value.
Sets cookies for the HTTP session
266 267 268 269 270 271 272 |
# File 'lib/module/http.rb', line 266 def ( ) @init_headers['cookie'] = '' @cookie_jar = .each_pair { |name, value| @init_headers['cookie'] += "#{name}=#{value};" } end |