Class: Net::HTTP
- Defined in:
- lib/net/http.rb,
lib/net/http.rb,
lib/net/http.rb,
lib/net/https.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$.split[1]
- HTTPVersion =
'1.1'
- 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.
- .ssl_context_accessor(name) ⇒ Object
-
.start(address, port = nil, p_addr = nil, p_port = nil, p_user = nil, p_pass = nil, &block) ⇒ Object
creates a new Net::HTTP object and opens its TCP connection and HTTP session.
-
.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
path
and gets a response, as an HTTPResponse object. -
#delete(path, initheader = {'Depth' => 'Infinity'}) ⇒ Object
Sends a DELETE request to the
path
and gets a response, as an HTTPResponse object. -
#finish ⇒ Object
Finishes HTTP session and closes TCP connection.
-
#get(path, initheader = nil, dest = nil, &block) ⇒ Object
Gets data from
path
on the connected-to host. -
#head(path, initheader = nil) ⇒ Object
Gets only the header from
path
on 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
path
and gets a response, as an HTTPResponse object. -
#mkcol(path, body = nil, initheader = nil) ⇒ Object
Sends a MKCOL request to the
path
and gets a response, as an HTTPResponse object. -
#move(path, initheader = nil) ⇒ Object
Sends a MOVE request to the
path
and gets a response, as an HTTPResponse object. -
#options(path, initheader = nil) ⇒ Object
Sends a OPTIONS request to the
path
and gets a response, as an HTTPResponse object. - #peer_cert ⇒ Object
-
#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
path
and gets a response, as an HTTPResponse object. -
#proppatch(path, body, initheader = nil) ⇒ Object
Sends a PROPPATCH request to the
path
and 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
path
and gets a response, as an HTTPResponse object. -
#request_head(path, initheader = nil, &block) ⇒ Object
(also: #head2)
Sends a HEAD request to the
path
and gets a response, as an HTTPResponse object. -
#request_post(path, data, initheader = nil, &block) ⇒ Object
(also: #post2)
Sends a POST request to the
path
and 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.
- #ssl_timeout ⇒ Object
- #ssl_timeout=(sec) ⇒ Object (also: #timeout=)
-
#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
path
and gets a response, as an HTTPResponse object. -
#unlock(path, body, initheader = nil) ⇒ Object
Sends a UNLOCK request to the
path
and gets a response, as an HTTPResponse object. -
#use_ssl=(flag) ⇒ Object
Turn on/off SSL.
-
#use_ssl? ⇒ Boolean
(also: #use_ssl)
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.
460 461 462 463 464 465 466 467 468 469 470 471 472 473 |
# File 'lib/net/http.rb', line 460 def initialize(address, port = nil) @address = address @port = (port || HTTP.default_port) @curr_http_version = HTTPVersion @seems_1_0_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 end |
Class Attribute Details
.proxy_address ⇒ Object (readonly)
Returns the value of attribute proxy_address.
660 661 662 |
# File 'lib/net/http.rb', line 660 def proxy_address @proxy_address end |
.proxy_pass ⇒ Object (readonly)
Returns the value of attribute proxy_pass.
663 664 665 |
# File 'lib/net/http.rb', line 663 def proxy_pass @proxy_pass end |
.proxy_port ⇒ Object (readonly)
Returns the value of attribute proxy_port.
661 662 663 |
# File 'lib/net/http.rb', line 661 def proxy_port @proxy_port end |
.proxy_user ⇒ Object (readonly)
Returns the value of attribute proxy_user.
662 663 664 |
# File 'lib/net/http.rb', line 662 def proxy_user @proxy_user end |
Instance Attribute Details
#address ⇒ Object (readonly)
The host name to connect to.
494 495 496 |
# File 'lib/net/http.rb', line 494 def address @address end |
#close_on_empty_response ⇒ Object
Returns the value of attribute close_on_empty_response.
522 523 524 |
# File 'lib/net/http.rb', line 522 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.
502 503 504 |
# File 'lib/net/http.rb', line 502 def open_timeout @open_timeout end |
#port ⇒ Object (readonly)
The port number to connect to.
497 498 499 |
# File 'lib/net/http.rb', line 497 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.
507 508 509 |
# File 'lib/net/http.rb', line 507 def read_timeout @read_timeout end |
Class Method Details
.default_port ⇒ Object
The default port to use for HTTP requests; defaults to 80.
414 415 416 |
# File 'lib/net/http.rb', line 414 def HTTP.default_port http_default_port() end |
.get(uri_or_host, path = nil, port = nil) ⇒ Object
355 356 357 |
# File 'lib/net/http.rb', line 355 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
336 337 338 339 340 341 342 343 |
# File 'lib/net/http.rb', line 336 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
371 372 373 374 375 376 377 378 379 380 381 382 383 |
# File 'lib/net/http.rb', line 371 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.
419 420 421 |
# File 'lib/net/http.rb', line 419 def HTTP.http_default_port 80 end |
.https_default_port ⇒ Object
The default port to use for HTTPS requests; defaults to 443.
424 425 426 |
# File 'lib/net/http.rb', line 424 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.
450 451 452 453 454 455 456 |
# File 'lib/net/http.rb', line 450 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
444 |
# File 'lib/net/http.rb', line 444 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" }
400 401 402 403 404 405 406 407 |
# File 'lib/net/http.rb', line 400 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
:
}
638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 |
# File 'lib/net/http.rb', line 638 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.
656 657 658 |
# File 'lib/net/http.rb', line 656 def proxy_class? @is_proxy_class end |
.socket_type ⇒ Object
:nodoc: obsolete
428 429 430 |
# File 'lib/net/http.rb', line 428 def HTTP.socket_type #:nodoc: obsolete BufferedIO end |
.ssl_context_accessor(name) ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/net/https.rb', line 129 def self.ssl_context_accessor(name) module_eval(<<-End, __FILE__, __LINE__ + 1) def #{name} return nil unless @ssl_context @ssl_context.#{name} end def #{name}=(val) @ssl_context ||= OpenSSL::SSL::SSLContext.new @ssl_context.#{name} = val end End end |
.start(address, port = nil, p_addr = nil, p_port = nil, p_user = nil, p_pass = nil, &block) ⇒ Object
creates a new Net::HTTP object and opens its TCP connection and HTTP session. 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.
439 440 441 |
# File 'lib/net/http.rb', line 439 def HTTP.start(address, port = nil, p_addr = nil, p_port = nil, p_user = nil, p_pass = nil, &block) # :yield: +http+ new(address, port, p_addr, p_port, p_user, p_pass).start(&block) end |
.version_1_1 ⇒ Object
Turns on net/http 1.1 (ruby 1.6) features. Defaults to OFF in ruby 1.8.
300 301 302 |
# File 'lib/net/http.rb', line 300 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.
312 313 314 |
# File 'lib/net/http.rb', line 312 def HTTP.version_1_1? not @newimpl end |
.version_1_2 ⇒ Object
294 295 296 |
# File 'lib/net/http.rb', line 294 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.
306 307 308 |
# File 'lib/net/http.rb', line 306 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.
903 904 905 |
# File 'lib/net/http.rb', line 903 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.
891 892 893 |
# File 'lib/net/http.rb', line 891 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.
598 599 600 601 |
# File 'lib/net/http.rb', line 598 def finish raise IOError, 'HTTP session not yet started' unless started? do_finish end |
#get(path, initheader = nil, dest = nil, &block) ⇒ Object
Gets data from path
on the connected-to host. 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 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
}
767 768 769 770 771 772 773 774 775 776 777 778 779 |
# File 'lib/net/http.rb', line 767 def get(path, initheader = nil, dest = nil, &block) # :yield: +body_segment+ res = nil request(Get.new(path, initheader)) {|r| r.read_body dest, &block 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']
797 798 799 800 801 |
# File 'lib/net/http.rb', line 797 def head(path, initheader = nil) res = request(Head.new(path, initheader)) res.value unless @newimpl res end |
#inspect ⇒ Object
475 476 477 |
# File 'lib/net/http.rb', line 475 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.
867 868 869 |
# File 'lib/net/http.rb', line 867 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.
909 910 911 |
# File 'lib/net/http.rb', line 909 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.
897 898 899 |
# File 'lib/net/http.rb', line 897 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.
879 880 881 |
# File 'lib/net/http.rb', line 879 def (path, initheader = nil) request(Options.new(path, initheader)) end |
#peer_cert ⇒ Object
167 168 169 170 |
# File 'lib/net/https.rb', line 167 def peer_cert return nil if not use_ssl? or not @socket @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.
840 841 842 843 844 845 846 847 848 849 850 851 |
# File 'lib/net/http.rb', line 840 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.
885 886 887 |
# File 'lib/net/http.rb', line 885 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.
861 862 863 |
# File 'lib/net/http.rb', line 861 def proppatch(path, body, initheader = nil) request(Proppatch.new(path, initheader), body) end |
#proxy? ⇒ Boolean
True if self is a HTTP proxy class.
667 668 669 |
# File 'lib/net/http.rb', line 667 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.
672 673 674 |
# File 'lib/net/http.rb', line 672 def proxy_address self.class.proxy_address end |
#proxy_pass ⇒ Object
User password for accessing proxy. If self does not use a proxy, nil.
687 688 689 |
# File 'lib/net/http.rb', line 687 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.
677 678 679 |
# File 'lib/net/http.rb', line 677 def proxy_port self.class.proxy_port end |
#proxy_user ⇒ Object
User name for accessing proxy. If self does not use a proxy, nil.
682 683 684 |
# File 'lib/net/http.rb', line 682 def proxy_user self.class.proxy_user end |
#put(path, data, initheader = nil) ⇒ Object
:nodoc:
853 854 855 856 857 |
# File 'lib/net/http.rb', line 853 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.
1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 |
# File 'lib/net/http.rb', line 1030 def request(req, body = nil, &block) # :yield: +response+ unless started? start { req['connection'] ||= 'close' return request(req, body, &block) } end if proxy_user() unless use_ssl? req.proxy_basic_auth proxy_user(), proxy_pass() end end req.set_body_internal body begin_transport req req.exec @socket, @curr_http_version, edit_path(req.path) begin res = HTTPResponse.read_new(@socket) end while res.kind_of?(HTTPContinue) res.reading_body(@socket, req.response_body_permitted?) { yield res if block_given? } end_transport req, res 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
}
944 945 946 |
# File 'lib/net/http.rb', line 944 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']
958 959 960 |
# File 'lib/net/http.rb', line 958 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
}
988 989 990 |
# File 'lib/net/http.rb', line 988 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:
992 993 994 |
# File 'lib/net/http.rb', line 992 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
1012 1013 1014 1015 |
# File 'lib/net/http.rb', line 1012 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 { .... }
488 489 490 491 |
# File 'lib/net/http.rb', line 488 def set_debug_output(output) warn 'Net::HTTP#set_debug_output called after HTTP started' if started? @debug_output = output end |
#ssl_timeout ⇒ Object
152 153 154 155 |
# File 'lib/net/https.rb', line 152 def ssl_timeout return nil unless @ssl_context @ssl_context.timeout end |
#ssl_timeout=(sec) ⇒ Object Also known as: timeout=
157 158 159 160 161 162 |
# File 'lib/net/https.rb', line 157 def ssl_timeout=(sec) raise ArgumentError, 'Net::HTTP#ssl_timeout= called but use_ssl=false' \ unless use_ssl? @ssl_context ||= OpenSSL::SSL::SSLContext.new @ssl_context.timeout = sec 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.
538 539 540 541 542 543 544 545 546 547 548 549 550 |
# File 'lib/net/http.rb', line 538 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.
516 517 518 |
# File 'lib/net/http.rb', line 516 def started? @started end |
#trace(path, initheader = nil) ⇒ Object
Sends a TRACE request to the path
and gets a response, as an HTTPResponse object.
915 916 917 |
# File 'lib/net/http.rb', line 915 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.
873 874 875 |
# File 'lib/net/http.rb', line 873 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.
119 120 121 122 123 124 125 126 127 |
# File 'lib/net/https.rb', line 119 def use_ssl=(flag) flag = (flag ? true : false) raise IOError, "use_ssl value changed, but session already started" \ if started? and @use_ssl != flag if flag and not @ssl_context @ssl_context = OpenSSL::SSL::SSLContext.new end @use_ssl = flag end |
#use_ssl? ⇒ Boolean Also known as: use_ssl
returns true if use SSL/TLS with HTTP.
525 526 527 |
# File 'lib/net/http.rb', line 525 def use_ssl? false # redefined in net/https end |