@@ -427,16 +427,20 @@ class << HTTP
427427 #
428428 # Gets the body text from the target and outputs it to $stdout. The
429429 # target can either be specified as
430- # (+uri+), or as (+host+, +path+, +port+ = 80); so:
430+ # (+uri+, +headers+ ), or as (+host+, +path+, +port+ = 80); so:
431431 #
432432 # Net::HTTP.get_print URI('http://www.example.com/index.html')
433433 #
434434 # or:
435435 #
436436 # Net::HTTP.get_print 'www.example.com', '/index.html'
437437 #
438- def HTTP . get_print ( uri_or_host , path = nil , port = nil )
439- get_response ( uri_or_host , path , port ) { |res |
438+ # you can also specify request headers:
439+ #
440+ # Net::HTTP.get_print URI('http://www.example.com/index.html'), { 'Accept' => 'text/html' }
441+ #
442+ def HTTP . get_print ( uri_or_host , path_or_headers = nil , port = nil )
443+ get_response ( uri_or_host , path_or_headers , port ) { |res |
440444 res . read_body do |chunk |
441445 $stdout. print chunk
442446 end
@@ -446,21 +450,25 @@ def HTTP.get_print(uri_or_host, path = nil, port = nil)
446450
447451 # Sends a GET request to the target and returns the HTTP response
448452 # as a string. The target can either be specified as
449- # (+uri+), or as (+host+, +path+, +port+ = 80); so:
453+ # (+uri+, +headers+ ), or as (+host+, +path+, +port+ = 80); so:
450454 #
451455 # print Net::HTTP.get(URI('http://www.example.com/index.html'))
452456 #
453457 # or:
454458 #
455459 # print Net::HTTP.get('www.example.com', '/index.html')
456460 #
457- def HTTP . get ( uri_or_host , path = nil , port = nil )
458- get_response ( uri_or_host , path , port ) . body
461+ # you can also specify request headers:
462+ #
463+ # Net::HTTP.get_response(URI('http://www.example.com/index.html'), { 'Accept' => 'text/html' })
464+ #
465+ def HTTP . get ( uri_or_host , path_or_headers = nil , port = nil )
466+ get_response ( uri_or_host , path_or_headers , port ) . body
459467 end
460468
461469 # Sends a GET request to the target and returns the HTTP response
462470 # as a Net::HTTPResponse object. The target can either be specified as
463- # (+uri+), or as (+host+, +path+, +port+ = 80); so:
471+ # (+uri+, +headers+ ), or as (+host+, +path+, +port+ = 80); so:
464472 #
465473 # res = Net::HTTP.get_response(URI('http://www.example.com/index.html'))
466474 # print res.body
@@ -470,17 +478,23 @@ def HTTP.get(uri_or_host, path = nil, port = nil)
470478 # res = Net::HTTP.get_response('www.example.com', '/index.html')
471479 # print res.body
472480 #
473- def HTTP . get_response ( uri_or_host , path = nil , port = nil , &block )
474- if path
481+ # you can also specify request headers:
482+ #
483+ # Net::HTTP.get_response(URI('http://www.example.com/index.html'), { 'Accept' => 'text/html' })
484+ #
485+ def HTTP . get_response ( uri_or_host , path_or_headers = nil , port = nil , &block )
486+ if path_or_headers && !path_or_headers . is_a? ( Hash )
475487 host = uri_or_host
488+ path = path_or_headers
476489 new ( host , port || HTTP . default_port ) . start { |http |
477490 return http . request_get ( path , &block )
478491 }
479492 else
480493 uri = uri_or_host
494+ headers = path_or_headers
481495 start ( uri . hostname , uri . port ,
482496 :use_ssl => uri . scheme == 'https' ) { |http |
483- return http . request_get ( uri , &block )
497+ return http . request_get ( uri , headers , &block )
484498 }
485499 end
486500 end
0 commit comments