Class: Net::HTTP
- Defined in:
- lib/extensions/net-http/net/http.rb,
lib/extensions/net-http/net/http.rb,
lib/extensions/net-http/net/http.rb
Overview
– for backward compatibility
Defined Under Namespace
Modules: ProxyDelta Classes: Copy, Delete, Get, Head, Lock, Mkcol, Move, Options, Post, Propfind, Proppatch, Put, Trace, Unlock
Constant Summary collapse
- Revision =
:stopdoc:
%q$Revision: 30571 $.split[1]
- HTTPVersion =
'1.1'- SSL_ATTRIBUTES =
%w( ssl_version key cert ca_file ca_path cert_store ciphers verify_mode verify_callback verify_depth ssl_timeout )
- ProxyMod =
ProxyDelta
Class Attribute Summary collapse
-
.proxy_address ⇒ Object
readonly
Returns the value of attribute proxy_address.
-
.proxy_pass ⇒ Object
readonly
Returns the value of attribute proxy_pass.
-
.proxy_port ⇒ Object
readonly
Returns the value of attribute proxy_port.
-
.proxy_user ⇒ Object
readonly
Returns the value of attribute proxy_user.
Instance Attribute Summary collapse
-
#address ⇒ Object
readonly
The host name to connect to.
-
#close_on_empty_response ⇒ Object
Returns the value of attribute close_on_empty_response.
-
#open_timeout ⇒ Object
Seconds to wait until connection is opened.
-
#port ⇒ Object
readonly
The port number to connect to.
-
#read_timeout ⇒ Object
Seconds to wait until reading one block (by one read(2) call).
Class Method Summary collapse
-
.default_port ⇒ Object
The default port to use for HTTP requests; defaults to 80.
-
.get(uri_or_host, path = nil, port = nil) ⇒ Object
Send a GET request to the target and return the response as a string.
-
.get_print(uri_or_host, path = nil, port = nil) ⇒ Object
Get body from target and output it to $stdout.
-
.get_response(uri_or_host, path = nil, port = nil, &block) ⇒ Object
Send a GET request to the target and return the response as a Net::HTTPResponse object.
-
.http_default_port ⇒ Object
The default port to use for HTTP requests; defaults to 80.
-
.https_default_port ⇒ Object
The default port to use for HTTPS requests; defaults to 443.
-
.new(address, port = nil, p_addr = nil, p_port = nil, p_user = nil, p_pass = nil) ⇒ Object
Creates a new Net::HTTP object.
- .newobj ⇒ Object
-
.post_form(url, params) ⇒ Object
Posts HTML form data to the
URL. -
.Proxy(p_addr, p_port = nil, p_user = nil, p_pass = nil) ⇒ Object
Creates an HTTP proxy class.
-
.proxy_class? ⇒ Boolean
returns true if self is a class which was created by HTTP::Proxy.
-
.socket_type ⇒ Object
:nodoc: obsolete.
-
.start(address, *arg, &block) ⇒ Object
call-seq: HTTP.start(address, port, p_addr, p_port, p_user, p_pass, &block) HTTP.start(address, port=nil, p_addr=nil, p_port=nil, p_user=nil, p_pass=nil, opt, &block).
-
.version_1_1 ⇒ Object
Turns on net/http 1.1 (ruby 1.6) features.
-
.version_1_1? ⇒ Boolean
(also: is_version_1_1?)
true if net/http is in version 1.1 compatible mode.
-
.version_1_2 ⇒ Object
Turns on net/http 1.2 (ruby 1.8) features.
-
.version_1_2? ⇒ Boolean
(also: is_version_1_2?)
true if net/http is in version 1.2 mode.
Instance Method Summary collapse
-
#copy(path, initheader = nil) ⇒ Object
Sends a COPY request to the
pathand gets a response, as an HTTPResponse object. -
#delete(path, initheader = {'Depth' => 'Infinity'}) ⇒ Object
Sends a DELETE request to the
pathand gets a response, as an HTTPResponse object. -
#finish ⇒ Object
Finishes HTTP session and closes TCP connection.
-
#get(path, initheader = {}, dest = nil, &block) ⇒ Object
Gets data from
pathon the connected-to host. -
#head(path, initheader = nil) ⇒ Object
Gets only the header from
pathon the connected-to host. -
#initialize(address, port = nil) ⇒ HTTP
constructor
Creates a new Net::HTTP object for the specified
address. - #inspect ⇒ Object
-
#lock(path, body, initheader = nil) ⇒ Object
Sends a LOCK request to the
pathand gets a response, as an HTTPResponse object. -
#mkcol(path, body = nil, initheader = nil) ⇒ Object
Sends a MKCOL request to the
pathand gets a response, as an HTTPResponse object. -
#move(path, initheader = nil) ⇒ Object
Sends a MOVE request to the
pathand gets a response, as an HTTPResponse object. -
#options(path, initheader = nil) ⇒ Object
Sends a OPTIONS request to the
pathand gets a response, as an HTTPResponse object. -
#peer_cert ⇒ Object
return the X.509 certificates the server presented.
-
#post(path, data, initheader = nil, dest = nil, &block) ⇒ Object
Posts
data(must be a String) topath. -
#propfind(path, body = nil, initheader = {'Depth' => '0'}) ⇒ Object
Sends a PROPFIND request to the
pathand gets a response, as an HTTPResponse object. -
#proppatch(path, body, initheader = nil) ⇒ Object
Sends a PROPPATCH request to the
pathand gets a response, as an HTTPResponse object. -
#proxy? ⇒ Boolean
True if self is a HTTP proxy class.
-
#proxy_address ⇒ Object
(also: #proxyaddr)
Address of proxy host.
-
#proxy_pass ⇒ Object
User password for accessing proxy.
-
#proxy_port ⇒ Object
(also: #proxyport)
Port number of proxy host.
-
#proxy_user ⇒ Object
User name for accessing proxy.
-
#put(path, data, initheader = nil) ⇒ Object
:nodoc:.
-
#request(req, body = nil, &block) ⇒ Object
Sends an HTTPRequest object REQUEST to the HTTP server.
-
#request_get(path, initheader = nil, &block) ⇒ Object
(also: #get2)
Sends a GET request to the
pathand gets a response, as an HTTPResponse object. -
#request_head(path, initheader = nil, &block) ⇒ Object
(also: #head2)
Sends a HEAD request to the
pathand gets a response, as an HTTPResponse object. -
#request_post(path, data, initheader = nil, &block) ⇒ Object
(also: #post2)
Sends a POST request to the
pathand gets a response, as an HTTPResponse object. -
#request_put(path, data, initheader = nil, &block) ⇒ Object
(also: #put2)
:nodoc:.
-
#send_request(name, path, data = nil, header = nil) ⇒ Object
Sends an HTTP request to the HTTP server.
-
#set_debug_output(output) ⇒ Object
WARNING This method causes serious security hole.
-
#start ⇒ Object
Opens TCP connection and HTTP session.
-
#started? ⇒ Boolean
(also: #active?)
returns true if the HTTP session is started.
-
#trace(path, initheader = nil) ⇒ Object
Sends a TRACE request to the
pathand gets a response, as an HTTPResponse object. -
#unlock(path, body, initheader = nil) ⇒ Object
Sends a UNLOCK request to the
pathand gets a response, as an HTTPResponse object. -
#use_ssl=(flag) ⇒ Object
Turn on/off SSL.
-
#use_ssl? ⇒ Boolean
returns true if use SSL/TLS with HTTP.
Constructor Details
#initialize(address, port = nil) ⇒ HTTP
Creates a new Net::HTTP object for the specified address. This method does not open the TCP connection.
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 |
# File 'lib/extensions/net-http/net/http.rb', line 522 def initialize(address, port = nil) @address = address @port = (port || HTTP.default_port) @curr_http_version = HTTPVersion @no_keepalive_server = false @close_on_empty_response = false @socket = nil @started = false @open_timeout = nil @read_timeout = 60 @debug_output = nil @use_ssl = false @ssl_context = nil @ssl_session = nil @enable_post_connection_check = true @compression = nil @sspi_enabled = false if defined?(SSL_ATTRIBUTES) SSL_ATTRIBUTES.each do |name| instance_variable_set "@#{name}", nil end end end |
Class Attribute Details
.proxy_address ⇒ Object (readonly)
Returns the value of attribute proxy_address.
784 785 786 |
# File 'lib/extensions/net-http/net/http.rb', line 784 def proxy_address @proxy_address end |
.proxy_pass ⇒ Object (readonly)
Returns the value of attribute proxy_pass.
787 788 789 |
# File 'lib/extensions/net-http/net/http.rb', line 787 def proxy_pass @proxy_pass end |
.proxy_port ⇒ Object (readonly)
Returns the value of attribute proxy_port.
785 786 787 |
# File 'lib/extensions/net-http/net/http.rb', line 785 def proxy_port @proxy_port end |
.proxy_user ⇒ Object (readonly)
Returns the value of attribute proxy_user.
786 787 788 |
# File 'lib/extensions/net-http/net/http.rb', line 786 def proxy_user @proxy_user end |
Instance Attribute Details
#address ⇒ Object (readonly)
The host name to connect to.
565 566 567 |
# File 'lib/extensions/net-http/net/http.rb', line 565 def address @address end |
#close_on_empty_response ⇒ Object
Returns the value of attribute close_on_empty_response.
593 594 595 |
# File 'lib/extensions/net-http/net/http.rb', line 593 def close_on_empty_response @close_on_empty_response end |
#open_timeout ⇒ Object
Seconds to wait until connection is opened. If the HTTP object cannot open a connection in this many seconds, it raises a TimeoutError exception.
573 574 575 |
# File 'lib/extensions/net-http/net/http.rb', line 573 def open_timeout @open_timeout end |
#port ⇒ Object (readonly)
The port number to connect to.
568 569 570 |
# File 'lib/extensions/net-http/net/http.rb', line 568 def port @port end |
#read_timeout ⇒ Object
Seconds to wait until reading one block (by one read(2) call). If the HTTP object cannot open a connection in this many seconds, it raises a TimeoutError exception.
578 579 580 |
# File 'lib/extensions/net-http/net/http.rb', line 578 def read_timeout @read_timeout end |
Class Method Details
.default_port ⇒ Object
The default port to use for HTTP requests; defaults to 80.
440 441 442 |
# File 'lib/extensions/net-http/net/http.rb', line 440 def HTTP.default_port http_default_port() end |
.get(uri_or_host, path = nil, port = nil) ⇒ Object
381 382 383 |
# File 'lib/extensions/net-http/net/http.rb', line 381 def HTTP.get(uri_or_host, path = nil, port = nil) get_response(uri_or_host, path, port).body end |
.get_print(uri_or_host, path = nil, port = nil) ⇒ Object
362 363 364 365 366 367 368 369 |
# File 'lib/extensions/net-http/net/http.rb', line 362 def HTTP.get_print(uri_or_host, path = nil, port = nil) get_response(uri_or_host, path, port) {|res| res.read_body do |chunk| $stdout.print chunk end } nil end |
.get_response(uri_or_host, path = nil, port = nil, &block) ⇒ Object
Send a GET request to the target and return the response as a Net::HTTPResponse object. The target can either be specified as (uri), or as (host, path, port = 80); so:
res = Net::HTTP.get_response(URI.parse('http://www.example.com/index.html'))
print res.body
or:
res = Net::HTTP.get_response('www.example.com', '/index.html')
print res.body
397 398 399 400 401 402 403 404 405 406 407 408 409 |
# File 'lib/extensions/net-http/net/http.rb', line 397 def HTTP.get_response(uri_or_host, path = nil, port = nil, &block) if path host = uri_or_host new(host, port || HTTP.default_port).start {|http| return http.request_get(path, &block) } else uri = uri_or_host new(uri.host, uri.port).start {|http| return http.request_get(uri.request_uri, &block) } end end |
.http_default_port ⇒ Object
The default port to use for HTTP requests; defaults to 80.
445 446 447 |
# File 'lib/extensions/net-http/net/http.rb', line 445 def HTTP.http_default_port 80 end |
.https_default_port ⇒ Object
The default port to use for HTTPS requests; defaults to 443.
450 451 452 |
# File 'lib/extensions/net-http/net/http.rb', line 450 def HTTP.https_default_port 443 end |
.new(address, port = nil, p_addr = nil, p_port = nil, p_user = nil, p_pass = nil) ⇒ Object
Creates a new Net::HTTP object. If proxy_addr is given, creates an Net::HTTP object with proxy support. This method does not open the TCP connection.
512 513 514 515 516 517 518 |
# File 'lib/extensions/net-http/net/http.rb', line 512 def HTTP.new(address, port = nil, p_addr = nil, p_port = nil, p_user = nil, p_pass = nil) h = Proxy(p_addr, p_port, p_user, p_pass).newobj(address, port) h.instance_eval { @newimpl = ::Net::HTTP.version_1_2? } h end |
.newobj ⇒ Object
506 |
# File 'lib/extensions/net-http/net/http.rb', line 506 alias newobj new |
.post_form(url, params) ⇒ Object
Posts HTML form data to the URL. Form data must be represented as a Hash of String to String, e.g:
{ "cmd" => "search", "q" => "ruby", "max" => "50" }
This method also does Basic Authentication iff URL.user exists.
Example:
require 'net/http'
require 'uri'
HTTP.post_form URI.parse('http://www.example.com/search.cgi'),
{ "q" => "ruby", "max" => "50" }
426 427 428 429 430 431 432 433 |
# File 'lib/extensions/net-http/net/http.rb', line 426 def HTTP.post_form(url, params) req = Post.new(url.path) req.form_data = params req.basic_auth url.user, url.password if url.user new(url.host, url.port).start {|http| http.request(req) } end |
.Proxy(p_addr, p_port = nil, p_user = nil, p_pass = nil) ⇒ Object
Creates an HTTP proxy class. Arguments are address/port of proxy host and username/password if authorization on proxy server is required. You can replace the HTTP class with created proxy class.
If ADDRESS is nil, this method returns self (Net::HTTP).
# Example
proxy_class = Net::HTTP::Proxy('proxy.example.com', 8080)
:
proxy_class.start('www.ruby-lang.org') {|http|
# connecting proxy.foo.org:8080
:
}
762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 |
# File 'lib/extensions/net-http/net/http.rb', line 762 def HTTP.Proxy(p_addr, p_port = nil, p_user = nil, p_pass = nil) return self unless p_addr delta = ProxyDelta proxyclass = Class.new(self) proxyclass.module_eval { include delta # with proxy @is_proxy_class = true @proxy_address = p_addr @proxy_port = p_port || default_port() @proxy_user = p_user @proxy_pass = p_pass } proxyclass end |
.proxy_class? ⇒ Boolean
returns true if self is a class which was created by HTTP::Proxy.
780 781 782 |
# File 'lib/extensions/net-http/net/http.rb', line 780 def proxy_class? @is_proxy_class end |
.socket_type ⇒ Object
:nodoc: obsolete
454 455 456 |
# File 'lib/extensions/net-http/net/http.rb', line 454 def HTTP.socket_type #:nodoc: obsolete BufferedIO end |
.start(address, *arg, &block) ⇒ Object
call-seq:
HTTP.start(address, port, p_addr, p_port, p_user, p_pass, &block)
HTTP.start(address, port=nil, p_addr=nil, p_port=nil, p_user=nil, p_pass=nil, opt, &block)
creates a new Net::HTTP object and opens its TCP connection and HTTP session.
Argments are following:
- address
-
hostname or IP address of the server
- port
-
port of the server
- p_addr
-
address of proxy
- p_port
-
port of proxy
- p_user
-
user of proxy
- p_pass
-
pass of proxy
- opt
-
optional hash
opt sets following values by its accessor. The keys are ca_file, ca_path, cert, cert_store, ciphers, close_on_empty_response, key, open_timeout, read_timeout, ssl_timeout, ssl_version, use_ssl, verify_callback, verify_depth and verify_mode. If you set :use_ssl as true, you can use https and default value of verify_mode is set as OpenSSL::SSL::VERIFY_PEER.
If the optional block is given, the newly created Net::HTTP object is passed to it and closed when the block finishes. In this case, the return value of this method is the return value of the block. If no block is given, the return value of this method is the newly created Net::HTTP object itself, and the caller is responsible for closing it upon completion.
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 |
# File 'lib/extensions/net-http/net/http.rb', line 487 def HTTP.start(address, *arg, &block) # :yield: +http+ arg.pop if opt = Hash.try_convert(arg[-1]) port, p_addr, p_port, p_user, p_pass = *arg port = https_default_port if !port && opt && opt[:use_ssl] http = new(address, port, p_addr, p_port, p_user, p_pass) if opt opt = {verify_mode: OpenSSL::SSL::VERIFY_PEER}.update(opt) if opt[:use_ssl] http.methods.grep(/\A(\w+)=\z/) do |meth| key = $1.to_sym opt.key?(key) or next http.__send__(meth, opt[key]) end end http.start(&block) end |
.version_1_1 ⇒ Object
Turns on net/http 1.1 (ruby 1.6) features. Defaults to OFF in ruby 1.8.
326 327 328 |
# File 'lib/extensions/net-http/net/http.rb', line 326 def HTTP.version_1_1 @newimpl = false end |
.version_1_1? ⇒ Boolean Also known as: is_version_1_1?
true if net/http is in version 1.1 compatible mode. Defaults to true.
338 339 340 |
# File 'lib/extensions/net-http/net/http.rb', line 338 def HTTP.version_1_1? not @newimpl end |
.version_1_2 ⇒ Object
320 321 322 |
# File 'lib/extensions/net-http/net/http.rb', line 320 def HTTP.version_1_2 @newimpl = true end |
.version_1_2? ⇒ Boolean Also known as: is_version_1_2?
true if net/http is in version 1.2 mode. Defaults to true.
332 333 334 |
# File 'lib/extensions/net-http/net/http.rb', line 332 def HTTP.version_1_2? @newimpl end |
Instance Method Details
#copy(path, initheader = nil) ⇒ Object
Sends a COPY request to the path and gets a response, as an HTTPResponse object.
1063 1064 1065 |
# File 'lib/extensions/net-http/net/http.rb', line 1063 def copy(path, initheader = nil) request(Copy.new(path, initheader)) end |
#delete(path, initheader = {'Depth' => 'Infinity'}) ⇒ Object
Sends a DELETE request to the path and gets a response, as an HTTPResponse object.
1051 1052 1053 |
# File 'lib/extensions/net-http/net/http.rb', line 1051 def delete(path, initheader = {'Depth' => 'Infinity'}) request(Delete.new(path, initheader)) end |
#finish ⇒ Object
Finishes HTTP session and closes TCP connection. Raises IOError if not started.
722 723 724 725 |
# File 'lib/extensions/net-http/net/http.rb', line 722 def finish raise IOError, 'HTTP session not yet started' unless started? do_finish end |
#get(path, initheader = {}, dest = nil, &block) ⇒ Object
Gets data from path on the connected-to host. initheader must be a Hash like { ‘Accept’ => ‘/’, … }, and it defaults to an empty hash. If initheader doesn’t have the key ‘accept-encoding’, then a value of “gzip;q=1.0,deflate;q=0.6,identity;q=0.3” is used, so that gzip compression is used in preference to deflate compression, which is used in preference to no compression. Ruby doesn’t have libraries to support the compress (Lempel-Ziv) compression, so that is not supported. The intent of this is to reduce bandwidth by default. If this routine sets up compression, then it does the decompression also, removing the header as well to prevent confusion. Otherwise it leaves the body as it found it.
In version 1.1 (ruby 1.6), this method returns a pair of objects, a Net::HTTPResponse object and the entity body string. In version 1.2 (ruby 1.8), this method returns a Net::HTTPResponse object.
If called with a block, yields each fragment of the entity body in turn as a string as it is read from the socket. Note that in this case, the returned response object will not contain a (meaningful) body.
dest argument is obsolete. It still works but you must not use it.
In version 1.1, this method might raise an exception for 3xx (redirect). In this case you can get a HTTPResponse object by “anException.response”.
In version 1.2, this method never raises exception.
# version 1.1 (bundled with Ruby 1.6)
response, body = http.get('/index.html')
# version 1.2 (bundled with Ruby 1.8 or later)
response = http.get('/index.html')
# using block
File.open('result.txt', 'w') {|f|
http.get('/~foo/') do |str|
f.write str
end
}
902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 |
# File 'lib/extensions/net-http/net/http.rb', line 902 def get(path, initheader = {}, dest = nil, &block) # :yield: +body_segment+ res = nil if HAVE_ZLIB unless initheader.keys.any?{|k| k.downcase == "accept-encoding"} initheader = initheader.merge({ "accept-encoding" => "gzip;q=1.0,deflate;q=0.6,identity;q=0.3" }) @compression = true end end request(Get.new(path, initheader)) {|r| if r.key?("content-encoding") and @compression @compression = nil # Clear it till next set. the_body = r.read_body dest, &block case r["content-encoding"] when "gzip" r.body= Zlib::GzipReader.new(StringIO.new(the_body), encoding: "ASCII-8BIT").read r.delete("content-encoding") when "deflate" r.body= Zlib::Inflate.inflate(the_body); r.delete("content-encoding") when "identity" ; # nothing needed else ; # Don't do anything dramatic, unless we need to later end else r.read_body dest, &block end res = r } unless @newimpl res.value return res, res.body end res end |
#head(path, initheader = nil) ⇒ Object
Gets only the header from path on the connected-to host. header is a Hash like { ‘Accept’ => ‘/’, … }.
This method returns a Net::HTTPResponse object.
In version 1.1, this method might raise an exception for 3xx (redirect). On the case you can get a HTTPResponse object by “anException.response”. In version 1.2, this method never raises an exception.
response = nil
Net::HTTP.start('some.www.server', 80) {|http|
response = http.head('/index.html')
}
p response['content-type']
957 958 959 960 961 |
# File 'lib/extensions/net-http/net/http.rb', line 957 def head(path, initheader = nil) res = request(Head.new(path, initheader)) res.value unless @newimpl res end |
#inspect ⇒ Object
546 547 548 |
# File 'lib/extensions/net-http/net/http.rb', line 546 def inspect "#<#{self.class} #{@address}:#{@port} open=#{started?}>" end |
#lock(path, body, initheader = nil) ⇒ Object
Sends a LOCK request to the path and gets a response, as an HTTPResponse object.
1027 1028 1029 |
# File 'lib/extensions/net-http/net/http.rb', line 1027 def lock(path, body, initheader = nil) request(Lock.new(path, initheader), body) end |
#mkcol(path, body = nil, initheader = nil) ⇒ Object
Sends a MKCOL request to the path and gets a response, as an HTTPResponse object.
1069 1070 1071 |
# File 'lib/extensions/net-http/net/http.rb', line 1069 def mkcol(path, body = nil, initheader = nil) request(Mkcol.new(path, initheader), body) end |
#move(path, initheader = nil) ⇒ Object
Sends a MOVE request to the path and gets a response, as an HTTPResponse object.
1057 1058 1059 |
# File 'lib/extensions/net-http/net/http.rb', line 1057 def move(path, initheader = nil) request(Move.new(path, initheader)) end |
#options(path, initheader = nil) ⇒ Object
Sends a OPTIONS request to the path and gets a response, as an HTTPResponse object.
1039 1040 1041 |
# File 'lib/extensions/net-http/net/http.rb', line 1039 def (path, initheader = nil) request(Options.new(path, initheader)) end |
#peer_cert ⇒ Object
return the X.509 certificates the server presented.
619 620 621 622 623 624 |
# File 'lib/extensions/net-http/net/http.rb', line 619 def peer_cert if not use_ssl? or not @socket return nil end @socket.io.peer_cert end |
#post(path, data, initheader = nil, dest = nil, &block) ⇒ Object
Posts data (must be a String) to path. header must be a Hash like { ‘Accept’ => ‘/’, … }.
In version 1.1 (ruby 1.6), this method returns a pair of objects, a Net::HTTPResponse object and an entity body string. In version 1.2 (ruby 1.8), this method returns a Net::HTTPResponse object.
If called with a block, yields each fragment of the entity body in turn as a string as it are read from the socket. Note that in this case, the returned response object will not contain a (meaningful) body.
dest argument is obsolete. It still works but you must not use it.
In version 1.1, this method might raise an exception for 3xx (redirect). In this case you can get an HTTPResponse object by “anException.response”. In version 1.2, this method never raises exception.
# version 1.1
response, body = http.post('/cgi-bin/search.rb', 'query=foo')
# version 1.2
response = http.post('/cgi-bin/search.rb', 'query=foo')
# using block
File.open('result.txt', 'w') {|f|
http.post('/cgi-bin/search.rb', 'query=foo') do |str|
f.write str
end
}
You should set Content-Type: header field for POST. If no Content-Type: field given, this method uses “application/x-www-form-urlencoded” by default.
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 |
# File 'lib/extensions/net-http/net/http.rb', line 1000 def post(path, data, initheader = nil, dest = nil, &block) # :yield: +body_segment+ res = nil request(Post.new(path, initheader), data) {|r| r.read_body dest, &block res = r } unless @newimpl res.value return res, res.body end res end |
#propfind(path, body = nil, initheader = {'Depth' => '0'}) ⇒ Object
Sends a PROPFIND request to the path and gets a response, as an HTTPResponse object.
1045 1046 1047 |
# File 'lib/extensions/net-http/net/http.rb', line 1045 def propfind(path, body = nil, initheader = {'Depth' => '0'}) request(Propfind.new(path, initheader), body) end |
#proppatch(path, body, initheader = nil) ⇒ Object
Sends a PROPPATCH request to the path and gets a response, as an HTTPResponse object.
1021 1022 1023 |
# File 'lib/extensions/net-http/net/http.rb', line 1021 def proppatch(path, body, initheader = nil) request(Proppatch.new(path, initheader), body) end |
#proxy? ⇒ Boolean
True if self is a HTTP proxy class.
791 792 793 |
# File 'lib/extensions/net-http/net/http.rb', line 791 def proxy? self.class.proxy_class? end |
#proxy_address ⇒ Object Also known as: proxyaddr
Address of proxy host. If self does not use a proxy, nil.
796 797 798 |
# File 'lib/extensions/net-http/net/http.rb', line 796 def proxy_address self.class.proxy_address end |
#proxy_pass ⇒ Object
User password for accessing proxy. If self does not use a proxy, nil.
811 812 813 |
# File 'lib/extensions/net-http/net/http.rb', line 811 def proxy_pass self.class.proxy_pass end |
#proxy_port ⇒ Object Also known as: proxyport
Port number of proxy host. If self does not use a proxy, nil.
801 802 803 |
# File 'lib/extensions/net-http/net/http.rb', line 801 def proxy_port self.class.proxy_port end |
#proxy_user ⇒ Object
User name for accessing proxy. If self does not use a proxy, nil.
806 807 808 |
# File 'lib/extensions/net-http/net/http.rb', line 806 def proxy_user self.class.proxy_user end |
#put(path, data, initheader = nil) ⇒ Object
:nodoc:
1013 1014 1015 1016 1017 |
# File 'lib/extensions/net-http/net/http.rb', line 1013 def put(path, data, initheader = nil) #:nodoc: res = request(Put.new(path, initheader), data) res.value unless @newimpl res end |
#request(req, body = nil, &block) ⇒ Object
Sends an HTTPRequest object REQUEST to the HTTP server. This method also sends DATA string if REQUEST is a post/put request. Giving DATA for get/head request causes ArgumentError.
When called with a block, yields an HTTPResponse object. The body of this response will not have been read yet; the caller can process it using HTTPResponse#read_body, if desired.
Returns a HTTPResponse object.
This method never raises Net::* exceptions.
1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 |
# File 'lib/extensions/net-http/net/http.rb', line 1190 def request(req, body = nil, &block) # :yield: +response+ unless started? start { req['connection'] ||= 'close' return request(req, body, &block) } end if proxy_user() req.proxy_basic_auth proxy_user(), proxy_pass() unless use_ssl? end req.set_body_internal body res = transport_request(req, &block) if sspi_auth?(res) sspi_auth(req) res = transport_request(req, &block) end res end |
#request_get(path, initheader = nil, &block) ⇒ Object Also known as: get2
Sends a GET request to the path and gets a response, as an HTTPResponse object.
When called with a block, yields an HTTPResponse object. The body of this response will not have been read yet; the caller can process it using HTTPResponse#read_body, if desired.
Returns the response.
This method never raises Net::* exceptions.
response = http.request_get('/index.html')
# The entity body is already read here.
p response['content-type']
puts response.body
# using block
http.request_get('/index.html') {|response|
p response['content-type']
response.read_body do |str| # read body now
print str
end
}
1104 1105 1106 |
# File 'lib/extensions/net-http/net/http.rb', line 1104 def request_get(path, initheader = nil, &block) # :yield: +response+ request(Get.new(path, initheader), &block) end |
#request_head(path, initheader = nil, &block) ⇒ Object Also known as: head2
Sends a HEAD request to the path and gets a response, as an HTTPResponse object.
Returns the response.
This method never raises Net::* exceptions.
response = http.request_head('/index.html')
p response['content-type']
1118 1119 1120 |
# File 'lib/extensions/net-http/net/http.rb', line 1118 def request_head(path, initheader = nil, &block) request(Head.new(path, initheader), &block) end |
#request_post(path, data, initheader = nil, &block) ⇒ Object Also known as: post2
Sends a POST request to the path and gets a response, as an HTTPResponse object.
When called with a block, yields an HTTPResponse object. The body of this response will not have been read yet; the caller can process it using HTTPResponse#read_body, if desired.
Returns the response.
This method never raises Net::* exceptions.
# example
response = http.request_post('/cgi-bin/nice.rb', 'datadatadata...')
p response.status
puts response.body # body is already read
# using block
http.request_post('/cgi-bin/nice.rb', 'datadatadata...') {|response|
p response.status
p response['content-type']
response.read_body do |str| # read body now
print str
end
}
1148 1149 1150 |
# File 'lib/extensions/net-http/net/http.rb', line 1148 def request_post(path, data, initheader = nil, &block) # :yield: +response+ request Post.new(path, initheader), data, &block end |
#request_put(path, data, initheader = nil, &block) ⇒ Object Also known as: put2
:nodoc:
1152 1153 1154 |
# File 'lib/extensions/net-http/net/http.rb', line 1152 def request_put(path, data, initheader = nil, &block) #:nodoc: request Put.new(path, initheader), data, &block end |
#send_request(name, path, data = nil, header = nil) ⇒ Object
Sends an HTTP request to the HTTP server. This method also sends DATA string if DATA is given.
Returns a HTTPResponse object.
This method never raises Net::* exceptions.
response = http.send_request('GET', '/index.html')
puts response.body
1172 1173 1174 1175 |
# File 'lib/extensions/net-http/net/http.rb', line 1172 def send_request(name, path, data = nil, header = nil) r = HTTPGenericRequest.new(name,(data ? true : false),true,path,header) request r, data end |
#set_debug_output(output) ⇒ Object
WARNING This method causes serious security hole. Never use this method in production code.
Set an output stream for debugging.
http = Net::HTTP.new
http.set_debug_output $stderr
http.start { .... }
559 560 561 562 |
# File 'lib/extensions/net-http/net/http.rb', line 559 def set_debug_output(output) warn 'Net::HTTP#set_debug_output called after HTTP started' if started? @debug_output = output end |
#start ⇒ Object
Opens TCP connection and HTTP session.
When this method is called with block, gives a HTTP object to the block and closes the TCP connection / HTTP session after the block executed.
When called with a block, returns the return value of the block; otherwise, returns self.
635 636 637 638 639 640 641 642 643 644 645 646 647 |
# File 'lib/extensions/net-http/net/http.rb', line 635 def start # :yield: http raise IOError, 'HTTP session already opened' if @started if block_given? begin do_start return yield(self) ensure do_finish end end do_start self end |
#started? ⇒ Boolean Also known as: active?
returns true if the HTTP session is started.
587 588 589 |
# File 'lib/extensions/net-http/net/http.rb', line 587 def started? @started end |
#trace(path, initheader = nil) ⇒ Object
Sends a TRACE request to the path and gets a response, as an HTTPResponse object.
1075 1076 1077 |
# File 'lib/extensions/net-http/net/http.rb', line 1075 def trace(path, initheader = nil) request(Trace.new(path, initheader)) end |
#unlock(path, body, initheader = nil) ⇒ Object
Sends a UNLOCK request to the path and gets a response, as an HTTPResponse object.
1033 1034 1035 |
# File 'lib/extensions/net-http/net/http.rb', line 1033 def unlock(path, body, initheader = nil) request(Unlock.new(path, initheader), body) end |
#use_ssl=(flag) ⇒ Object
Turn on/off SSL. This flag must be set before starting session. If you change use_ssl value after session started, a Net::HTTP object raises IOError.
604 605 606 607 608 609 610 |
# File 'lib/extensions/net-http/net/http.rb', line 604 def use_ssl=(flag) flag = (flag ? true : false) if started? and @use_ssl != flag raise IOError, "use_ssl value changed, but session already started" end @use_ssl = flag end |
#use_ssl? ⇒ Boolean
returns true if use SSL/TLS with HTTP.
596 597 598 |
# File 'lib/extensions/net-http/net/http.rb', line 596 def use_ssl? @use_ssl end |