Class: Ronin::Support::Web::Agent
- Inherits:
-
Object
- Object
- Ronin::Support::Web::Agent
- Defined in:
- lib/ronin/support/web/agent.rb,
lib/ronin/support/web/agent/mixin.rb
Overview
Web Agent represents a stripped-down web browser, which can request URLs, follow redirects, and parse responses.
Features
- Automatically follows redirects.
- Provides low-level HTTP methods.
- Provides high-level methods for requesting and parsing HTML, XML, or JSON.
- Maintains a persistent connection pool.
Anti-Features
- Does not cache files or write to the disk.
- Does not evaluate JavaScript.
Defined Under Namespace
Modules: Mixin Classes: ContentTypeError, Error, TooManyRedirects
Instance Attribute Summary collapse
-
#max_redirects ⇒ Integer
readonly
Maximum number of redirects to follow.
-
#proxy ⇒ URI::HTTP, ...
readonly
The proxy to send requests through.
-
#user_agent ⇒ String?
readonly
The
User-Agent
header value.
Instance Method Summary collapse
-
#follow_redirects? ⇒ Boolean
Indicates whether redirects will automatically be followed.
-
#get(url, follow_redirects: @follow_redirects, max_redirects: @max_redirects, **kwargs) {|response| ... } ⇒ Net::HTTPResponse
Gets a URL and returns the response.
-
#get_html(url, **kwargs) ⇒ Nokogiri::HTML::Document
Gets the URL and returns the parsed HTML.
-
#get_json(url, **kwargs) ⇒ Hash{String => Object}, Array
Gets the URL and returns the parsed JSON.
-
#get_xml(url, **kwargs) ⇒ Nokogiri::XML::Document
Gets the URL and returns the parsed XML.
-
#http_allowed_methods(url, **kwargs) ⇒ Array<Symbol>
Performs a
OPTIONS
HTTP request for the given URI and parses theAllow
response header. -
#http_copy(url, **kwargs) {|response| ... } ⇒ Net::HTTPResponse
Performs a
COPY
request for the given URI. -
#http_delete(url, **kwargs) {|response| ... } ⇒ Net::HTTPResponse
Performs a
DELETE
request for the given URI. -
#http_get(url, **kwargs) {|response| ... } ⇒ Net::HTTPResponse
Performs a
GET
request for the given URI. -
#http_get_body(url, **kwargs) ⇒ String
Performs a
GET
request for the given URI and returns the response body. -
#http_get_cookies(url, **kwargs) ⇒ Array<SetCookie>?
Sends an HTTP request and returns the parsed
Set-Cookie
header(s). -
#http_get_headers(url, **kwargs) ⇒ Hash{String => String}
Performs a
GET
request for the given URI and returns the response headers. -
#http_head(url, **kwargs) {|response| ... } ⇒ Net::HTTPResponse
Performs a
HEAD
request for the given URI. -
#http_lock(url, **kwargs) {|response| ... } ⇒ Net::HTTPResponse
Performs a
LOCK
request for the given URI. -
#http_mkcol(url, **kwargs) {|response| ... } ⇒ Net::HTTPResponse
Performs a
MKCOL
request for the given URI. -
#http_move(url, **kwargs) {|response| ... } ⇒ Net::HTTPResponse
Performs a
MOVE
request for the given URI. -
#http_ok?(method = :head, url, **kwargs) ⇒ Boolean
Sends a HTTP request and determines if the response status was 200.
-
#http_options(url, **kwargs) {|response| ... } ⇒ Net::HTTPResponse
Performs a
OPTIONS
request for the given URI. -
#http_patch(url, **kwargs) {|response| ... } ⇒ Net::HTTPResponse
Performs a
PATCH
request for the given URI. -
#http_post(url, **kwargs) {|response| ... } ⇒ Net::HTTPResponse
Performs a
POST
request for the given URI. -
#http_post_body(url, **kwargs) ⇒ String
Performs a
POST
request for the given URI and returns the response body. -
#http_post_headers(url, **kwargs) ⇒ Hash{String => String}
Performs a
POST
request on the given URI and returns the response headers. -
#http_powered_by_header(url, **kwargs) ⇒ String?
Sends an HTTP request and returns the
X-Powered-By
header. -
#http_propfind(url, **kwargs) {|response| ... } ⇒ Net::HTTPResponse
(also: #http_prop_find)
Performs a
PROPFIND
request for the given URI. -
#http_proppatch(url, **kwargs) {|response| ... } ⇒ Net::HTTPResponse
(also: #http_prop_patch)
Performs a
PROPPATCH
request for the given URI. -
#http_put(url, **kwargs) {|response| ... } ⇒ Net::HTTPResponse
Performs a
PUT
request for the given URI. -
#http_request(method, url, **kwargs) {|response| ... } ⇒ Net::HTTPResponse
Performs and arbitrary HTTP request.
-
#http_response_body(method = :get, url, **kwargs) ⇒ String
Sends an arbitrary HTTP request and returns the response body.
-
#http_response_headers(method = :head, url, **kwargs) ⇒ Hash{String => String}
Sends an arbitrary HTTP request and returns the response headers.
-
#http_response_status(method = :head, url, **kwargs) ⇒ Integer
Sends an arbitrary HTTP request and returns the response status.
-
#http_server_header(url, **kwargs) ⇒ String?
Sends an HTTP request and returns the
Server
header. -
#http_trace(url, **kwargs) {|response| ... } ⇒ Net::HTTPResponse
Performs a
TRACE
request for the given URI. -
#http_unlock(url, **kwargs) {|response| ... } ⇒ Net::HTTPResponse
Performs a
UNLOCK
request for the given URI. -
#initialize(follow_redirects: true, max_redirects: 20, proxy: Support::Network::HTTP.proxy, ssl: nil, user_agent: Support::Network::HTTP.user_agent) ⇒ Agent
constructor
Initializes the Web agent.
-
#post(url, follow_redirects: @follow_redirects, max_redirects: @max_redirects, **kwargs) {|response| ... } ⇒ Net::HTTPResponse
Performs an HTTP POST to the URL.
-
#post_html(url, **kwargs) ⇒ Nokogiri::HTML::Document
Performs an HTTP POST to the URL and parses the HTML response.
-
#post_json(url, **kwargs) ⇒ Hash{String => Object}, Array
Performs an HTTP POST to the URL and parses the JSON response.
-
#post_xml(url, **kwargs) ⇒ Nokogiri::XML::Document
Performs an HTTP POST to the URL and parses the XML response.
Constructor Details
#initialize(follow_redirects: true, max_redirects: 20, proxy: Support::Network::HTTP.proxy, ssl: nil, user_agent: Support::Network::HTTP.user_agent) ⇒ Agent
Initializes the Web agent.
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/ronin/support/web/agent.rb', line 134 def initialize(follow_redirects: true, max_redirects: 20, # HTTP options proxy: Support::Network::HTTP.proxy, ssl: nil, user_agent: Support::Network::HTTP.user_agent) @follow_redirects = follow_redirects @max_redirects = max_redirects # HTTP options @proxy = proxy @ssl = ssl @user_agent = user_agent @sessions = {} end |
Instance Attribute Details
#max_redirects ⇒ Integer (readonly)
Maximum number of redirects to follow.
81 82 83 |
# File 'lib/ronin/support/web/agent.rb', line 81 def max_redirects @max_redirects end |
#proxy ⇒ URI::HTTP, ... (readonly)
The proxy to send requests through.
71 72 73 |
# File 'lib/ronin/support/web/agent.rb', line 71 def proxy @proxy end |
#user_agent ⇒ String? (readonly)
The User-Agent
header value.
76 77 78 |
# File 'lib/ronin/support/web/agent.rb', line 76 def user_agent @user_agent end |
Instance Method Details
#follow_redirects? ⇒ Boolean
Indicates whether redirects will automatically be followed.
156 157 158 |
# File 'lib/ronin/support/web/agent.rb', line 156 def follow_redirects? @follow_redirects end |
#get(url, follow_redirects: @follow_redirects, max_redirects: @max_redirects, **kwargs) {|response| ... } ⇒ Net::HTTPResponse
This method will follow redirects by default.
Gets a URL and returns the response.
1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 |
# File 'lib/ronin/support/web/agent.rb', line 1071 def get(url, follow_redirects: @follow_redirects, max_redirects: @max_redirects, **kwargs) response = http_get(url,**kwargs) if follow_redirects && response.kind_of?(Net::HTTPRedirection) redirect_count = 0 while response.kind_of?(Net::HTTPRedirection) if redirect_count >= max_redirects raise(TooManyRedirects,"maximum number of redirects reached: #{url.inspect}") end location = response['Location'] response = http_get(location) redirect_count += 1 end end yield response if block_given? return response end |
#get_html(url, **kwargs) ⇒ Nokogiri::HTML::Document
This method will follow redirects by default.
Gets the URL and returns the parsed HTML.
1124 1125 1126 1127 1128 1129 1130 1131 1132 |
# File 'lib/ronin/support/web/agent.rb', line 1124 def get_html(url,**kwargs) response = get(url,**kwargs) unless response.content_type.include?('text/html') raise(ContentTypeError,"response 'Content-Type' was not 'text/html': #{response.content_type.inspect}") end return Nokogiri::HTML(response.body) end |
#get_json(url, **kwargs) ⇒ Hash{String => Object}, Array
This method will follow redirects by default.
Gets the URL and returns the parsed JSON.
1203 1204 1205 1206 1207 1208 1209 1210 1211 |
# File 'lib/ronin/support/web/agent.rb', line 1203 def get_json(url,**kwargs) response = get(url,**kwargs) unless response.content_type.include?('application/json') raise(ContentTypeError,"response 'Content-Type' was not 'application/json': #{response.content_type.inspect}") end return ::JSON.parse(response.body) end |
#get_xml(url, **kwargs) ⇒ Nokogiri::XML::Document
This method will follow redirects by default.
Gets the URL and returns the parsed XML.
1163 1164 1165 1166 1167 1168 1169 1170 1171 |
# File 'lib/ronin/support/web/agent.rb', line 1163 def get_xml(url,**kwargs) response = get(url,**kwargs) unless response.content_type.include?('text/xml') raise(ContentTypeError,"response 'Content-Type' was not 'text/xml': #{response.content_type.inspect}") end return Nokogiri::XML(response.body) end |
#http_allowed_methods(url, **kwargs) ⇒ Array<Symbol>
Performs a OPTIONS
HTTP request for the given URI and parses the
Allow
response header.
756 757 758 759 760 761 762 763 764 |
# File 'lib/ronin/support/web/agent.rb', line 756 def http_allowed_methods(url,**kwargs) uri = normalize_url(url) session_for(uri).allowed_methods( uri.request_uri, user: uri.user, password: uri.password, **kwargs ) end |
#http_copy(url, **kwargs) {|response| ... } ⇒ Net::HTTPResponse
Performs a COPY
request for the given URI.
435 436 437 438 439 440 441 442 443 |
# File 'lib/ronin/support/web/agent.rb', line 435 def http_copy(url,**kwargs,&block) uri = normalize_url(url) session_for(uri).copy( uri.request_uri, user: uri.user, password: uri.password, **kwargs, &block ) end |
#http_delete(url, **kwargs) {|response| ... } ⇒ Net::HTTPResponse
Performs a DELETE
request for the given URI.
466 467 468 469 470 471 472 473 474 |
# File 'lib/ronin/support/web/agent.rb', line 466 def http_delete(url,**kwargs,&block) uri = normalize_url(url) session_for(uri).delete( uri.request_uri, user: uri.user, password: uri.password, **kwargs, &block ) end |
#http_get(url, **kwargs) {|response| ... } ⇒ Net::HTTPResponse
Performs a GET
request for the given URI.
497 498 499 500 501 502 503 504 505 |
# File 'lib/ronin/support/web/agent.rb', line 497 def http_get(url,**kwargs,&block) uri = normalize_url(url) session_for(uri).get( uri.request_uri, user: uri.user, password: uri.password, **kwargs, &block ) end |
#http_get_body(url, **kwargs) ⇒ String
Performs a GET
request for the given URI and returns the response
body.
575 576 577 578 579 580 581 582 583 |
# File 'lib/ronin/support/web/agent.rb', line 575 def http_get_body(url,**kwargs) uri = normalize_url(url) session_for(uri).get_body( uri.request_uri, user: uri.user, password: uri.password, **kwargs ) end |
#http_get_cookies(url, **kwargs) ⇒ Array<SetCookie>?
Sends an HTTP request and returns the parsed Set-Cookie
header(s).
549 550 551 552 553 554 555 556 557 |
# File 'lib/ronin/support/web/agent.rb', line 549 def (url,**kwargs) uri = normalize_url(url) session_for(uri).( uri.request_uri, user: uri.user, password: uri.password, **kwargs ) end |
#http_get_headers(url, **kwargs) ⇒ Hash{String => String}
Performs a GET
request for the given URI and returns the response
headers.
523 524 525 526 527 528 529 530 531 |
# File 'lib/ronin/support/web/agent.rb', line 523 def http_get_headers(url,**kwargs) uri = normalize_url(url) session_for(uri).get_headers( uri.request_uri, user: uri.user, password: uri.password, **kwargs ) end |
#http_head(url, **kwargs) {|response| ... } ⇒ Net::HTTPResponse
Performs a HEAD
request for the given URI.
606 607 608 609 610 611 612 613 614 |
# File 'lib/ronin/support/web/agent.rb', line 606 def http_head(url,**kwargs,&block) uri = normalize_url(url) session_for(uri).head( uri.request_uri, user: uri.user, password: uri.password, **kwargs, &block ) end |
#http_lock(url, **kwargs) {|response| ... } ⇒ Net::HTTPResponse
Performs a LOCK
request for the given URI.
637 638 639 640 641 642 643 644 645 |
# File 'lib/ronin/support/web/agent.rb', line 637 def http_lock(url,**kwargs,&block) uri = normalize_url(url) session_for(uri).lock( uri.request_uri, user: uri.user, password: uri.password, **kwargs, &block ) end |
#http_mkcol(url, **kwargs) {|response| ... } ⇒ Net::HTTPResponse
Performs a MKCOL
request for the given URI.
668 669 670 671 672 673 674 675 676 |
# File 'lib/ronin/support/web/agent.rb', line 668 def http_mkcol(url,**kwargs,&block) uri = normalize_url(url) session_for(uri).mkcol( uri.request_uri, user: uri.user, password: uri.password, **kwargs, &block ) end |
#http_move(url, **kwargs) {|response| ... } ⇒ Net::HTTPResponse
Performs a MOVE
request for the given URI.
699 700 701 702 703 704 705 706 707 |
# File 'lib/ronin/support/web/agent.rb', line 699 def http_move(url,**kwargs,&block) uri = normalize_url(url) session_for(uri).move( uri.request_uri, user: uri.user, password: uri.password, **kwargs, &block ) end |
#http_ok?(method = :head, url, **kwargs) ⇒ Boolean
Sends a HTTP request and determines if the response status was 200.
296 297 298 299 300 301 302 303 304 |
# File 'lib/ronin/support/web/agent.rb', line 296 def http_ok?(method=:head,url,**kwargs) uri = normalize_url(url) session_for(uri).ok?( method, uri.request_uri, user: uri.user, password: uri.password, **kwargs ) end |
#http_options(url, **kwargs) {|response| ... } ⇒ Net::HTTPResponse
Performs a OPTIONS
request for the given URI.
730 731 732 733 734 735 736 737 738 |
# File 'lib/ronin/support/web/agent.rb', line 730 def (url,**kwargs,&block) uri = normalize_url(url) session_for(uri).( uri.request_uri, user: uri.user, password: uri.password, **kwargs, &block ) end |
#http_patch(url, **kwargs) {|response| ... } ⇒ Net::HTTPResponse
Performs a PATCH
request for the given URI.
787 788 789 790 791 792 793 794 795 |
# File 'lib/ronin/support/web/agent.rb', line 787 def http_patch(url,**kwargs,&block) uri = normalize_url(url) session_for(uri).patch( uri.request_uri, user: uri.user, password: uri.password, **kwargs, &block ) end |
#http_post(url, **kwargs) {|response| ... } ⇒ Net::HTTPResponse
Performs a POST
request for the given URI.
818 819 820 821 822 823 824 825 826 |
# File 'lib/ronin/support/web/agent.rb', line 818 def http_post(url,**kwargs,&block) uri = normalize_url(url) session_for(uri).post( uri.request_uri, user: uri.user, password: uri.password, **kwargs, &block ) end |
#http_post_body(url, **kwargs) ⇒ String
Performs a POST
request for the given URI and returns the
response body.
870 871 872 873 874 875 876 877 878 |
# File 'lib/ronin/support/web/agent.rb', line 870 def http_post_body(url,**kwargs) uri = normalize_url(url) session_for(uri).post_body( uri.request_uri, user: uri.user, password: uri.password, **kwargs ) end |
#http_post_headers(url, **kwargs) ⇒ Hash{String => String}
Performs a POST
request on the given URI and returns the response
headers.
844 845 846 847 848 849 850 851 852 |
# File 'lib/ronin/support/web/agent.rb', line 844 def http_post_headers(url,**kwargs) uri = normalize_url(url) session_for(uri).post_headers( uri.request_uri, user: uri.user, password: uri.password, **kwargs ) end |
#http_powered_by_header(url, **kwargs) ⇒ String?
Sends an HTTP request and returns the X-Powered-By
header.
375 376 377 378 379 380 381 382 383 384 |
# File 'lib/ronin/support/web/agent.rb', line 375 def http_powered_by_header(url,**kwargs) uri = normalize_url(url) session_for(uri).powered_by_header( user: uri.user, password: uri.password, path: uri.request_uri, **kwargs ) end |
#http_propfind(url, **kwargs) {|response| ... } ⇒ Net::HTTPResponse Also known as: http_prop_find
Performs a PROPFIND
request for the given URI.
901 902 903 904 905 906 907 908 909 |
# File 'lib/ronin/support/web/agent.rb', line 901 def http_propfind(url,**kwargs,&block) uri = normalize_url(url) session_for(uri).propfind( uri.request_uri, user: uri.user, password: uri.password, **kwargs, &block ) end |
#http_proppatch(url, **kwargs) {|response| ... } ⇒ Net::HTTPResponse Also known as: http_prop_patch
Performs a PROPPATCH
request for the given URI.
934 935 936 937 938 939 940 941 942 |
# File 'lib/ronin/support/web/agent.rb', line 934 def http_proppatch(url,**kwargs,&block) uri = normalize_url(url) session_for(uri).proppatch( uri.request_uri, user: uri.user, password: uri.password, **kwargs, &block ) end |
#http_put(url, **kwargs) {|response| ... } ⇒ Net::HTTPResponse
Performs a PUT
request for the given URI.
967 968 969 970 971 972 973 974 975 |
# File 'lib/ronin/support/web/agent.rb', line 967 def http_put(url,**kwargs,&block) uri = normalize_url(url) session_for(uri).put( uri.request_uri, user: uri.user, password: uri.password, **kwargs, &block ) end |
#http_request(method, url, **kwargs) {|response| ... } ⇒ Net::HTTPResponse
Performs and arbitrary HTTP request.
240 241 242 243 244 245 246 247 248 |
# File 'lib/ronin/support/web/agent.rb', line 240 def http_request(method,url,**kwargs,&block) uri = normalize_url(url) session_for(uri).request( method, uri.request_uri, user: uri.user, password: uri.password, **kwargs, &block ) end |
#http_response_body(method = :get, url, **kwargs) ⇒ String
Sends an arbitrary HTTP request and returns the response body.
404 405 406 407 408 409 410 411 412 |
# File 'lib/ronin/support/web/agent.rb', line 404 def http_response_body(method=:get,url,**kwargs) uri = normalize_url(url) session_for(uri).response_body( method, uri.request_uri, user: uri.user, password: uri.password, **kwargs ) end |
#http_response_headers(method = :head, url, **kwargs) ⇒ Hash{String => String}
Sends an arbitrary HTTP request and returns the response headers.
324 325 326 327 328 329 330 331 332 |
# File 'lib/ronin/support/web/agent.rb', line 324 def http_response_headers(method=:head,url,**kwargs) uri = normalize_url(url) session_for(uri).response_headers( method, uri.request_uri, user: uri.user, password: uri.password, **kwargs ) end |
#http_response_status(method = :head, url, **kwargs) ⇒ Integer
Sends an arbitrary HTTP request and returns the response status.
268 269 270 271 272 273 274 275 276 |
# File 'lib/ronin/support/web/agent.rb', line 268 def http_response_status(method=:head,url,**kwargs) uri = normalize_url(url) session_for(uri).response_status( method, uri.request_uri, user: uri.user, password: uri.password, **kwargs ) end |
#http_server_header(url, **kwargs) ⇒ String?
Sends an HTTP request and returns the Server
header.
349 350 351 352 353 354 355 356 357 358 |
# File 'lib/ronin/support/web/agent.rb', line 349 def http_server_header(url,**kwargs) uri = normalize_url(url) session_for(uri).server_header( user: uri.user, password: uri.password, path: uri.request_uri, **kwargs ) end |
#http_trace(url, **kwargs) {|response| ... } ⇒ Net::HTTPResponse
Performs a TRACE
request for the given URI.
998 999 1000 1001 1002 1003 1004 1005 1006 |
# File 'lib/ronin/support/web/agent.rb', line 998 def http_trace(url,**kwargs,&block) uri = normalize_url(url) session_for(uri).trace( uri.request_uri, user: uri.user, password: uri.password, **kwargs, &block ) end |
#http_unlock(url, **kwargs) {|response| ... } ⇒ Net::HTTPResponse
Performs a UNLOCK
request for the given URI.
1029 1030 1031 1032 1033 1034 1035 1036 1037 |
# File 'lib/ronin/support/web/agent.rb', line 1029 def http_unlock(url,**kwargs,&block) uri = normalize_url(url) session_for(uri).unlock( uri.request_uri, user: uri.user, password: uri.password, **kwargs, &block ) end |
#post(url, follow_redirects: @follow_redirects, max_redirects: @max_redirects, **kwargs) {|response| ... } ⇒ Net::HTTPResponse
If the response is an HTTP redirect, then #get will be called to follow any redirects.
Performs an HTTP POST to the URL.
1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 |
# File 'lib/ronin/support/web/agent.rb', line 1247 def post(url, follow_redirects: @follow_redirects, max_redirects: @max_redirects, **kwargs) response = http_post(url,**kwargs) if follow_redirects && response.kind_of?(Net::HTTPRedirection) location = response['Location'] response = begin get(location, follow_redirects: follow_redirects, max_redirects: max_redirects - 1) rescue TooManyRedirects raise(TooManyRedirects,"maximum number of redirects reached: #{url.inspect}") end end yield response if block_given? return response end |
#post_html(url, **kwargs) ⇒ Nokogiri::HTML::Document
If the response is an HTTP redirect, then #get will be called to follow any redirects.
Performs an HTTP POST to the URL and parses the HTML response.
1299 1300 1301 1302 1303 1304 1305 1306 1307 |
# File 'lib/ronin/support/web/agent.rb', line 1299 def post_html(url,**kwargs) response = post(url,**kwargs) unless response.content_type.include?('text/html') raise(ContentTypeError,"response 'Content-Type' was not 'text/html': #{response.content_type.inspect}") end return Nokogiri::HTML(response.body) end |
#post_json(url, **kwargs) ⇒ Hash{String => Object}, Array
If the response is an HTTP redirect, then #get will be called to follow any redirects.
Performs an HTTP POST to the URL and parses the JSON response.
1387 1388 1389 1390 1391 1392 1393 1394 1395 |
# File 'lib/ronin/support/web/agent.rb', line 1387 def post_json(url,**kwargs) response = post(url,**kwargs) unless response.content_type.include?('application/json') raise(ContentTypeError,"response 'Content-Type' was not 'application/json': #{response.content_type.inspect}") end return ::JSON.parse(response.body) end |
#post_xml(url, **kwargs) ⇒ Nokogiri::XML::Document
If the response is an HTTP redirect, then #get will be called to follow any redirects.
Performs an HTTP POST to the URL and parses the XML response.
1341 1342 1343 1344 1345 1346 1347 1348 1349 |
# File 'lib/ronin/support/web/agent.rb', line 1341 def post_xml(url,**kwargs) response = post(url,**kwargs) unless response.content_type.include?('text/xml') raise(ContentTypeError,"response 'Content-Type' was not 'application/json': #{response.content_type.inspect}") end return Nokogiri::XML(response.body) end |