Class: Net::HTTP
- Defined in:
- lib/mechanize/net-overrides/net/http.rb,
lib/mechanize/net-overrides/net/http.rb,
lib/mechanize/net-overrides/net/http.rb,
lib/mechanize/net-overrides/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: 1.123 $.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(arg1, arg2 = nil, arg3 = nil) ⇒ Object
Send a GET request to the target and return the response as a string.
-
.get_print(arg1, arg2 = nil, port = nil) ⇒ Object
Get body from target and output it to $stdout.
-
.get_response(arg1, arg2 = nil, arg3 = nil) ⇒ 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
-
.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
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 = nil, 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
-
#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.
- #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
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
(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.
348 349 350 351 352 353 354 355 356 357 358 359 360 361 |
# File 'lib/mechanize/net-overrides/net/http.rb', line 348 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 = 30 @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.
542 543 544 |
# File 'lib/mechanize/net-overrides/net/http.rb', line 542 def proxy_address @proxy_address end |
.proxy_pass ⇒ Object (readonly)
Returns the value of attribute proxy_pass.
545 546 547 |
# File 'lib/mechanize/net-overrides/net/http.rb', line 545 def proxy_pass @proxy_pass end |
.proxy_port ⇒ Object (readonly)
Returns the value of attribute proxy_port.
543 544 545 |
# File 'lib/mechanize/net-overrides/net/http.rb', line 543 def proxy_port @proxy_port end |
.proxy_user ⇒ Object (readonly)
Returns the value of attribute proxy_user.
544 545 546 |
# File 'lib/mechanize/net-overrides/net/http.rb', line 544 def proxy_user @proxy_user end |
Instance Attribute Details
#address ⇒ Object (readonly)
The host name to connect to.
382 383 384 |
# File 'lib/mechanize/net-overrides/net/http.rb', line 382 def address @address end |
#close_on_empty_response ⇒ Object
Returns the value of attribute close_on_empty_response.
410 411 412 |
# File 'lib/mechanize/net-overrides/net/http.rb', line 410 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.
390 391 392 |
# File 'lib/mechanize/net-overrides/net/http.rb', line 390 def open_timeout @open_timeout end |
#port ⇒ Object (readonly)
The port number to connect to.
385 386 387 |
# File 'lib/mechanize/net-overrides/net/http.rb', line 385 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.
395 396 397 |
# File 'lib/mechanize/net-overrides/net/http.rb', line 395 def read_timeout @read_timeout end |
Class Method Details
.default_port ⇒ Object
The default port to use for HTTP requests; defaults to 80.
302 303 304 |
# File 'lib/mechanize/net-overrides/net/http.rb', line 302 def HTTP.default_port http_default_port() end |
.get(arg1, arg2 = nil, arg3 = nil) ⇒ Object
257 258 259 |
# File 'lib/mechanize/net-overrides/net/http.rb', line 257 def HTTP.get(arg1, arg2 = nil, arg3 = nil) get_response(arg1,arg2,arg3).body end |
.get_print(arg1, arg2 = nil, port = nil) ⇒ Object
232 233 234 235 236 237 238 239 240 241 242 243 244 245 |
# File 'lib/mechanize/net-overrides/net/http.rb', line 232 def HTTP.get_print(arg1, arg2 = nil, port = nil) if arg2 addr, path = arg1, arg2 else uri = arg1 addr = uri.host path = uri.request_uri port = uri.port end new(addr, port || HTTP.default_port).start {|http| http.get path, nil, $stdout } nil end |
.get_response(arg1, arg2 = nil, arg3 = nil) ⇒ 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
273 274 275 276 277 278 279 |
# File 'lib/mechanize/net-overrides/net/http.rb', line 273 def HTTP.get_response(arg1, arg2 = nil, arg3 = nil) if arg2 get_by_path(arg1, arg2, arg3) else get_by_uri(arg1) end end |
.http_default_port ⇒ Object
The default port to use for HTTP requests; defaults to 80.
307 308 309 |
# File 'lib/mechanize/net-overrides/net/http.rb', line 307 def HTTP.http_default_port 80 end |
.https_default_port ⇒ Object
The default port to use for HTTPS requests; defaults to 443.
312 313 314 |
# File 'lib/mechanize/net-overrides/net/http.rb', line 312 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.
338 339 340 341 342 343 344 |
# File 'lib/mechanize/net-overrides/net/http.rb', line 338 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
332 |
# File 'lib/mechanize/net-overrides/net/http.rb', line 332 alias newobj new |
.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
:
}
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 |
# File 'lib/mechanize/net-overrides/net/http.rb', line 520 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.
538 539 540 |
# File 'lib/mechanize/net-overrides/net/http.rb', line 538 def proxy_class? @is_proxy_class end |
.socket_type ⇒ Object
:nodoc: obsolete
316 317 318 |
# File 'lib/mechanize/net-overrides/net/http.rb', line 316 def HTTP.socket_type #:nodoc: obsolete InternetMessageIO end |
.ssl_context_accessor(name) ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/mechanize/net-overrides/net/https.rb', line 128 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.
327 328 329 |
# File 'lib/mechanize/net-overrides/net/http.rb', line 327 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.
196 197 198 |
# File 'lib/mechanize/net-overrides/net/http.rb', line 196 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.
208 209 210 |
# File 'lib/mechanize/net-overrides/net/http.rb', line 208 def HTTP.version_1_1? not @newimpl end |
.version_1_2 ⇒ Object
190 191 192 |
# File 'lib/mechanize/net-overrides/net/http.rb', line 190 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.
202 203 204 |
# File 'lib/mechanize/net-overrides/net/http.rb', line 202 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.
792 793 794 |
# File 'lib/mechanize/net-overrides/net/http.rb', line 792 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.
780 781 782 |
# File 'lib/mechanize/net-overrides/net/http.rb', line 780 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.
480 481 482 483 |
# File 'lib/mechanize/net-overrides/net/http.rb', line 480 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
}
653 654 655 656 657 658 659 660 661 662 663 664 665 |
# File 'lib/mechanize/net-overrides/net/http.rb', line 653 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']
683 684 685 686 687 |
# File 'lib/mechanize/net-overrides/net/http.rb', line 683 def head(path, initheader = nil) res = request(Head.new(path, initheader)) res.value unless @newimpl res end |
#inspect ⇒ Object
363 364 365 |
# File 'lib/mechanize/net-overrides/net/http.rb', line 363 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.
756 757 758 |
# File 'lib/mechanize/net-overrides/net/http.rb', line 756 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.
798 799 800 |
# File 'lib/mechanize/net-overrides/net/http.rb', line 798 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.
786 787 788 |
# File 'lib/mechanize/net-overrides/net/http.rb', line 786 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.
768 769 770 |
# File 'lib/mechanize/net-overrides/net/http.rb', line 768 def (path, initheader = nil) request(Options.new(path, initheader)) end |
#peer_cert ⇒ Object
165 166 167 168 |
# File 'lib/mechanize/net-overrides/net/https.rb', line 165 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 is an alternative method of collecting the body. It must be an object responding to the “<<” operator (such as a String or an Array). Each fragment of the entity body will be “<<”-ed in turn onto dest if provided, and it will also become the body of the returned response object.
You must not provide both dest and a block; doing so will result in an ArgumentError.
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
}
728 729 730 731 732 733 734 735 736 737 738 739 740 |
# File 'lib/mechanize/net-overrides/net/http.rb', line 728 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.
774 775 776 |
# File 'lib/mechanize/net-overrides/net/http.rb', line 774 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.
750 751 752 |
# File 'lib/mechanize/net-overrides/net/http.rb', line 750 def proppatch(path, body, initheader = nil) request(Proppatch.new(path, initheader), body) end |
#proxy? ⇒ Boolean
True if self is a HTTP proxy class.
549 550 551 |
# File 'lib/mechanize/net-overrides/net/http.rb', line 549 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.
554 555 556 |
# File 'lib/mechanize/net-overrides/net/http.rb', line 554 def proxy_address self.class.proxy_address end |
#proxy_pass ⇒ Object
User password for accessing proxy. If self does not use a proxy, nil.
569 570 571 |
# File 'lib/mechanize/net-overrides/net/http.rb', line 569 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.
559 560 561 |
# File 'lib/mechanize/net-overrides/net/http.rb', line 559 def proxy_port self.class.proxy_port end |
#proxy_user ⇒ Object
User name for accessing proxy. If self does not use a proxy, nil.
564 565 566 |
# File 'lib/mechanize/net-overrides/net/http.rb', line 564 def proxy_user self.class.proxy_user end |
#put(path, data, initheader = nil) ⇒ Object
:nodoc:
742 743 744 745 746 |
# File 'lib/mechanize/net-overrides/net/http.rb', line 742 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.
919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 |
# File 'lib/mechanize/net-overrides/net/http.rb', line 919 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() 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
}
833 834 835 |
# File 'lib/mechanize/net-overrides/net/http.rb', line 833 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']
847 848 849 |
# File 'lib/mechanize/net-overrides/net/http.rb', line 847 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
}
877 878 879 |
# File 'lib/mechanize/net-overrides/net/http.rb', line 877 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:
881 882 883 |
# File 'lib/mechanize/net-overrides/net/http.rb', line 881 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
901 902 903 904 |
# File 'lib/mechanize/net-overrides/net/http.rb', line 901 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 { .... }
376 377 378 379 |
# File 'lib/mechanize/net-overrides/net/http.rb', line 376 def set_debug_output(output) warn 'Net::HTTP#set_debug_output called after HTTP started' if started? @debug_output = output end |
#ssl_timeout ⇒ Object
151 152 153 154 |
# File 'lib/mechanize/net-overrides/net/https.rb', line 151 def ssl_timeout return nil unless @ssl_context @ssl_context.timeout end |
#ssl_timeout=(sec) ⇒ Object Also known as: timeout=
156 157 158 159 160 161 |
# File 'lib/mechanize/net-overrides/net/https.rb', line 156 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.
426 427 428 429 430 431 432 433 434 435 436 437 438 |
# File 'lib/mechanize/net-overrides/net/http.rb', line 426 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.
404 405 406 |
# File 'lib/mechanize/net-overrides/net/http.rb', line 404 def started? @started end |
#trace(path, initheader = nil) ⇒ Object
Sends a TRACE request to the path and gets a response, as an HTTPResponse object.
804 805 806 |
# File 'lib/mechanize/net-overrides/net/http.rb', line 804 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.
762 763 764 |
# File 'lib/mechanize/net-overrides/net/http.rb', line 762 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.
118 119 120 121 122 123 124 125 126 |
# File 'lib/mechanize/net-overrides/net/https.rb', line 118 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.
413 414 415 |
# File 'lib/mechanize/net-overrides/net/http.rb', line 413 def use_ssl? false # redefined in net/https end |