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.
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/ronin/support/web/agent.rb', line 128 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.
150 151 152 |
# File 'lib/ronin/support/web/agent.rb', line 150 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.
1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 |
# File 'lib/ronin/support/web/agent.rb', line 1059 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.
1106 1107 1108 1109 1110 1111 1112 1113 1114 |
# File 'lib/ronin/support/web/agent.rb', line 1106 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.
1173 1174 1175 1176 1177 1178 1179 1180 1181 |
# File 'lib/ronin/support/web/agent.rb', line 1173 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.
1139 1140 1141 1142 1143 1144 1145 1146 1147 |
# File 'lib/ronin/support/web/agent.rb', line 1139 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.
750 751 752 753 754 755 756 757 758 |
# File 'lib/ronin/support/web/agent.rb', line 750 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.
429 430 431 432 433 434 435 436 437 |
# File 'lib/ronin/support/web/agent.rb', line 429 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.
460 461 462 463 464 465 466 467 468 |
# File 'lib/ronin/support/web/agent.rb', line 460 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.
491 492 493 494 495 496 497 498 499 |
# File 'lib/ronin/support/web/agent.rb', line 491 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.
569 570 571 572 573 574 575 576 577 |
# File 'lib/ronin/support/web/agent.rb', line 569 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).
543 544 545 546 547 548 549 550 551 |
# File 'lib/ronin/support/web/agent.rb', line 543 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.
517 518 519 520 521 522 523 524 525 |
# File 'lib/ronin/support/web/agent.rb', line 517 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.
600 601 602 603 604 605 606 607 608 |
# File 'lib/ronin/support/web/agent.rb', line 600 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.
631 632 633 634 635 636 637 638 639 |
# File 'lib/ronin/support/web/agent.rb', line 631 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.
662 663 664 665 666 667 668 669 670 |
# File 'lib/ronin/support/web/agent.rb', line 662 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.
693 694 695 696 697 698 699 700 701 |
# File 'lib/ronin/support/web/agent.rb', line 693 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.
290 291 292 293 294 295 296 297 298 |
# File 'lib/ronin/support/web/agent.rb', line 290 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.
724 725 726 727 728 729 730 731 732 |
# File 'lib/ronin/support/web/agent.rb', line 724 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.
781 782 783 784 785 786 787 788 789 |
# File 'lib/ronin/support/web/agent.rb', line 781 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.
812 813 814 815 816 817 818 819 820 |
# File 'lib/ronin/support/web/agent.rb', line 812 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.
864 865 866 867 868 869 870 871 872 |
# File 'lib/ronin/support/web/agent.rb', line 864 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.
838 839 840 841 842 843 844 845 846 |
# File 'lib/ronin/support/web/agent.rb', line 838 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.
369 370 371 372 373 374 375 376 377 378 |
# File 'lib/ronin/support/web/agent.rb', line 369 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.
895 896 897 898 899 900 901 902 903 |
# File 'lib/ronin/support/web/agent.rb', line 895 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.
928 929 930 931 932 933 934 935 936 |
# File 'lib/ronin/support/web/agent.rb', line 928 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.
961 962 963 964 965 966 967 968 969 |
# File 'lib/ronin/support/web/agent.rb', line 961 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.
234 235 236 237 238 239 240 241 242 |
# File 'lib/ronin/support/web/agent.rb', line 234 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.
398 399 400 401 402 403 404 405 406 |
# File 'lib/ronin/support/web/agent.rb', line 398 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.
318 319 320 321 322 323 324 325 326 |
# File 'lib/ronin/support/web/agent.rb', line 318 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.
262 263 264 265 266 267 268 269 270 |
# File 'lib/ronin/support/web/agent.rb', line 262 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.
343 344 345 346 347 348 349 350 351 352 |
# File 'lib/ronin/support/web/agent.rb', line 343 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.
992 993 994 995 996 997 998 999 1000 |
# File 'lib/ronin/support/web/agent.rb', line 992 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.
1023 1024 1025 1026 1027 1028 1029 1030 1031 |
# File 'lib/ronin/support/web/agent.rb', line 1023 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.
1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 |
# File 'lib/ronin/support/web/agent.rb', line 1211 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.
1257 1258 1259 1260 1261 1262 1263 1264 1265 |
# File 'lib/ronin/support/web/agent.rb', line 1257 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.
1333 1334 1335 1336 1337 1338 1339 1340 1341 |
# File 'lib/ronin/support/web/agent.rb', line 1333 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.
1293 1294 1295 1296 1297 1298 1299 1300 1301 |
# File 'lib/ronin/support/web/agent.rb', line 1293 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 |