mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* lib/net/http.rb: refine document.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@4273 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
dd15b0555b
commit
b1139297c3
1 changed files with 174 additions and 118 deletions
194
lib/net/http.rb
194
lib/net/http.rb
|
@ -1,4 +1,6 @@
|
|||
#
|
||||
# = net/http.rb
|
||||
#
|
||||
#--
|
||||
# Copyright (c) 1999-2003 Yukihiro Matsumoto
|
||||
# Copyright (c) 1999-2003 Minero Aoki
|
||||
|
@ -15,6 +17,7 @@
|
|||
#
|
||||
# $Id$
|
||||
#++
|
||||
#
|
||||
# == What Is This Library?
|
||||
#
|
||||
# This library provides your program functions to access WWW
|
||||
|
@ -145,7 +148,8 @@
|
|||
# Net::HTTP.version_1_2
|
||||
# Net::HTTP.start {|http3| ...(http3 has 1.2 features)... }
|
||||
#
|
||||
# This function is not multithread-safe.
|
||||
# This function is NOT multithread-safe.
|
||||
#
|
||||
|
||||
require 'net/protocol'
|
||||
require 'uri'
|
||||
|
@ -174,19 +178,42 @@ module Net
|
|||
|
||||
@@newimpl = true
|
||||
|
||||
# Turns on net/http 1.2 (ruby 1.8) features.
|
||||
# Defaults to ON in ruby 1.8.
|
||||
#
|
||||
# I strongly recommend to call this method always.
|
||||
#
|
||||
# require 'net/http'
|
||||
# Net::HTTP.version_1_2
|
||||
#
|
||||
def HTTP.version_1_2
|
||||
@@newimpl = true
|
||||
end
|
||||
|
||||
# Turns on net/http 1.2 (ruby 1.8) features.
|
||||
# Defaults to OFF in ruby 1.8.
|
||||
def HTTP.version_1_1
|
||||
@@newimpl = false
|
||||
end
|
||||
|
||||
def HTTP.is_version_1_2?
|
||||
# true if net/http is in version 1.2 mode.
|
||||
# Defaults to true.
|
||||
def HTTP.version_1_2?
|
||||
@@newimpl
|
||||
end
|
||||
|
||||
def HTTP.setimplversion( obj )
|
||||
# true if net/http is in version 1.1 compatible mode.
|
||||
# Defaults to true.
|
||||
def HTTP.version_1_1?
|
||||
not @@newimpl
|
||||
end
|
||||
|
||||
class << HTTP
|
||||
alias is_version_1_1? version_1_1? #:nodoc:
|
||||
alias is_version_1_2? version_1_2? #:nodoc:
|
||||
end
|
||||
|
||||
def HTTP.setimplversion( obj ) #:nodoc:
|
||||
f = @@newimpl
|
||||
obj.instance_eval { @newimpl = f }
|
||||
end
|
||||
|
@ -256,14 +283,14 @@ module Net
|
|||
end
|
||||
end
|
||||
|
||||
def HTTP.get_by_path( addr, path, port = nil )
|
||||
def HTTP.get_by_path( addr, path, port = nil ) #:nodoc:
|
||||
new(addr, port || HTTP.default_port).start {|http|
|
||||
return http.request(Get.new(path))
|
||||
}
|
||||
end
|
||||
private_class_method :get_by_path
|
||||
|
||||
def HTTP.get_by_uri( uri )
|
||||
def HTTP.get_by_uri( uri ) #:nodoc:
|
||||
# Should we allow this?
|
||||
# uri = URI.parse(uri) unless uri.respond_to?(:host)
|
||||
new(uri.host, uri.port).start {|http|
|
||||
|
@ -276,18 +303,12 @@ module Net
|
|||
# HTTP session management
|
||||
#
|
||||
|
||||
#
|
||||
# The default port to use for HTTP requests; defaults to 80.
|
||||
#
|
||||
def HTTP.default_port
|
||||
80
|
||||
end
|
||||
|
||||
#
|
||||
# The type of socket to use for HTTP connections.
|
||||
# Defaults to +InternetMessageIO+
|
||||
#
|
||||
def HTTP.socket_type
|
||||
def HTTP.socket_type #:nodoc:
|
||||
InternetMessageIO
|
||||
end
|
||||
|
||||
|
@ -299,7 +320,7 @@ module Net
|
|||
# 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.
|
||||
def start( address, port = nil, p_addr = nil, p_port = nil, p_user = nil, p_pass = nil, &block ) # :yield: +HTTP+ object
|
||||
def 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
|
||||
|
||||
|
@ -334,7 +355,7 @@ module Net
|
|||
end
|
||||
|
||||
def inspect
|
||||
"#<#{self.class} #{@address}:#{@port} open=#{active?}>"
|
||||
"#<#{self.class} #{@address}:#{@port} open=#{started?}>"
|
||||
end
|
||||
|
||||
def set_debug_output( arg ) # :nodoc:
|
||||
|
@ -343,6 +364,7 @@ module Net
|
|||
|
||||
# The host name to connect to.
|
||||
attr_reader :address
|
||||
|
||||
# The port number to connect to.
|
||||
attr_reader :port
|
||||
|
||||
|
@ -351,7 +373,7 @@ module Net
|
|||
# it raises a TimeoutError exception.
|
||||
attr_accessor :open_timeout
|
||||
|
||||
# Seconds to wait until reading one block (by one read(1) call).
|
||||
# Seconds to wait until reading one block (by one read(2) call).
|
||||
# If the HTTP object cannot open a conection in this many seconds,
|
||||
# it raises a TimeoutError exception.
|
||||
attr_reader :read_timeout
|
||||
|
@ -367,7 +389,7 @@ module Net
|
|||
@started
|
||||
end
|
||||
|
||||
alias active? started?
|
||||
alias active? started? #:nodoc:
|
||||
|
||||
attr_accessor :close_on_empty_response
|
||||
|
||||
|
@ -380,7 +402,7 @@ module Net
|
|||
# When called with a block, returns the return value of the
|
||||
# block; otherwise, returns self.
|
||||
#
|
||||
def start # :yield: Net::HTTP object
|
||||
def start # :yield: +http+
|
||||
raise IOError, 'HTTP session already opened' if @started
|
||||
if block_given?
|
||||
begin
|
||||
|
@ -499,8 +521,8 @@ module Net
|
|||
self.class.proxy_pass
|
||||
end
|
||||
|
||||
alias proxyaddr proxy_address
|
||||
alias proxyport proxy_port
|
||||
alias proxyaddr proxy_address #:nodoc:
|
||||
alias proxyport proxy_port #:nodoc:
|
||||
|
||||
private
|
||||
|
||||
|
@ -518,7 +540,7 @@ module Net
|
|||
path
|
||||
end
|
||||
|
||||
module ProxyDelta
|
||||
module ProxyDelta #:nodoc:
|
||||
private
|
||||
|
||||
# with proxy
|
||||
|
@ -545,9 +567,9 @@ module Net
|
|||
# Gets data from +path+ on the connected-to host.
|
||||
# +header+ must be a Hash like { 'Accept' => '*/*', ... }.
|
||||
#
|
||||
# In version 1.1, this method returns a pair of objects,
|
||||
# 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, this method returns a Net::HTTPResponse
|
||||
# In version 1.2 (ruby 1.8), this method returns a Net::HTTPResponse
|
||||
# object.
|
||||
#
|
||||
# If called with a block, yields each fragment of the
|
||||
|
@ -555,14 +577,8 @@ module Net
|
|||
# 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.
|
||||
# +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
|
||||
|
@ -573,7 +589,7 @@ module Net
|
|||
# # version 1.1 (bundled with Ruby 1.6)
|
||||
# response, body = http.get('/index.html')
|
||||
#
|
||||
# # version 1.2 (bundled with Ruby 1.7 or later)
|
||||
# # version 1.2 (bundled with Ruby 1.8 or later)
|
||||
# response = http.get('/index.html')
|
||||
#
|
||||
# # using block
|
||||
|
@ -582,6 +598,7 @@ module Net
|
|||
# f.write str
|
||||
# end
|
||||
# }
|
||||
#
|
||||
def get( path, initheader = nil, dest = nil, &block ) # :yield: +body_segment+
|
||||
res = nil
|
||||
request(Get.new(path, initheader)) {|r|
|
||||
|
@ -614,16 +631,16 @@ module Net
|
|||
#
|
||||
def head( path, initheader = nil )
|
||||
res = request(Head.new(path, initheader))
|
||||
@newimpl or res.value
|
||||
res.value unless @newimpl
|
||||
res
|
||||
end
|
||||
|
||||
# Posts +data+ (must be a String) to +path+. +header+ must be a Hash
|
||||
# like { 'Accept' => '*/*', ... }.
|
||||
#
|
||||
# In version 1.1, this method returns a pair of objects, a
|
||||
# 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, this method returns a Net::HTTPReponse object.
|
||||
# In version 1.2 (ruby 1.8), this method returns a Net::HTTPReponse object.
|
||||
#
|
||||
# If called with a block, yields each fragment of the
|
||||
# entity body in turn as a string as it are read from
|
||||
|
@ -656,7 +673,7 @@ module Net
|
|||
# f.write str
|
||||
# end
|
||||
# }
|
||||
def post( path, data, initheader = nil, dest = nil, &block ) # :yield: +body+
|
||||
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
|
||||
|
@ -670,7 +687,7 @@ module Net
|
|||
res
|
||||
end
|
||||
|
||||
def put( path, data, initheader = nil ) # :yield: +response+
|
||||
def put( path, data, initheader = nil ) #:nodoc:
|
||||
res = request(Put.new(path, initheader), data)
|
||||
@newimpl or res.value
|
||||
res
|
||||
|
@ -748,7 +765,7 @@ module Net
|
|||
request Post.new(path, initheader), data, &block
|
||||
end
|
||||
|
||||
def request_put( path, data, initheader = nil, &block )
|
||||
def request_put( path, data, initheader = nil, &block ) #:nodoc:
|
||||
request Put.new(path, initheader), data, &block
|
||||
end
|
||||
|
||||
|
@ -887,11 +904,11 @@ module Net
|
|||
#
|
||||
module HTTPHeader
|
||||
|
||||
def size
|
||||
def size #:nodoc:
|
||||
@header.size
|
||||
end
|
||||
|
||||
alias length size
|
||||
alias length size #:nodoc:
|
||||
|
||||
# Returns the header field corresponding to the case-insensitive key.
|
||||
# For example, a key of "Content-Type" might return "text/html"
|
||||
|
@ -911,29 +928,34 @@ module Net
|
|||
@header.fetch(key.downcase, *args, &block)
|
||||
end
|
||||
|
||||
# See Hash#each
|
||||
# Iterates for each header names and values.
|
||||
def each_header( &block ) # :yield: +key+, +value+
|
||||
@header.each(&block)
|
||||
end
|
||||
|
||||
alias each each_header
|
||||
|
||||
def each_key( &block )
|
||||
# Iterates for each header names.
|
||||
def each_key( &block ) # :yield: +key+
|
||||
@header.each_key(&block)
|
||||
end
|
||||
|
||||
def each_value( &block )
|
||||
# Iterates for each header values.
|
||||
def each_value( &block ) # :yield: +value+
|
||||
@header.each_value(&block)
|
||||
end
|
||||
|
||||
# Removes a header field.
|
||||
def delete( key )
|
||||
@header.delete(key.downcase)
|
||||
end
|
||||
|
||||
# true if +key+ header exists.
|
||||
def key?( key )
|
||||
@header.key?(key.downcase)
|
||||
end
|
||||
|
||||
# Returns a Hash consist of header names and values.
|
||||
def to_hash
|
||||
@header.dup
|
||||
end
|
||||
|
@ -1030,6 +1052,7 @@ module Net
|
|||
m[1].to_i .. m[2].to_i + 1
|
||||
end
|
||||
|
||||
# The length of the range represented in Range: header.
|
||||
def range_length
|
||||
r = self.content_range
|
||||
r and (r.end - r.begin)
|
||||
|
@ -1100,7 +1123,7 @@ module Net
|
|||
#
|
||||
|
||||
# internal use only
|
||||
def exec( sock, ver, path, body )
|
||||
def exec( sock, ver, path, body ) #:nodoc:
|
||||
if body
|
||||
check_body_permitted
|
||||
send_request_with_body sock, ver, path, body
|
||||
|
@ -1117,7 +1140,7 @@ module Net
|
|||
end
|
||||
|
||||
def send_request_with_body( sock, ver, path, body )
|
||||
@header['content-length'] = body.size.to_s
|
||||
@header['content-length'] = body.length.to_s
|
||||
@header.delete 'transfer-encoding'
|
||||
|
||||
unless @header['content-type']
|
||||
|
@ -1190,8 +1213,10 @@ module Net
|
|||
### Response
|
||||
###
|
||||
|
||||
# HTTP exception class.
|
||||
# You must use its subclasses.
|
||||
module HTTPExceptions
|
||||
def initialize( msg, res )
|
||||
def initialize( msg, res ) #:nodoc:
|
||||
super msg
|
||||
@response = res
|
||||
end
|
||||
|
@ -1274,17 +1299,17 @@ module Net
|
|||
# 505 HTTPVersionNotSupported
|
||||
#
|
||||
# xxx HTTPUnknownResponse
|
||||
#
|
||||
class HTTPResponse
|
||||
# predefine HTTPResponse class to allow inheritance
|
||||
|
||||
def self.body_permitted?
|
||||
# true if the reponse has body.
|
||||
def HTTPResponse.body_permitted?
|
||||
self::HAS_BODY
|
||||
end
|
||||
|
||||
def self.exception_type
|
||||
def HTTPResponse.exception_type # :nodoc:
|
||||
self::EXCEPTION_TYPE
|
||||
end
|
||||
end
|
||||
end # redefined after
|
||||
|
||||
class HTTPUnknownResponse < HTTPResponse
|
||||
HAS_BODY = true
|
||||
|
@ -1499,7 +1524,7 @@ module Net
|
|||
|
||||
class << self
|
||||
|
||||
def read_new( sock )
|
||||
def read_new( sock ) #:nodoc:
|
||||
httpv, code, msg = read_status_line(sock)
|
||||
res = response_class(code).new(httpv, code, msg)
|
||||
each_response_header(sock) do |k,v|
|
||||
|
@ -1546,7 +1571,7 @@ module Net
|
|||
|
||||
include HTTPHeader
|
||||
|
||||
def initialize( httpv, code, msg )
|
||||
def initialize( httpv, code, msg ) #:nodoc:
|
||||
@http_version = httpv
|
||||
@code = code
|
||||
@message = msg
|
||||
|
@ -1556,6 +1581,7 @@ module Net
|
|||
@read = false
|
||||
end
|
||||
|
||||
# The HTTP version supported by the server.
|
||||
attr_reader :http_version
|
||||
|
||||
# HTTP result code string. For example, '302'. You can also
|
||||
|
@ -1565,7 +1591,7 @@ module Net
|
|||
|
||||
# HTTP result message. For example, 'Not Found'.
|
||||
attr_reader :message
|
||||
alias msg message # for backward compatibility
|
||||
alias msg message # :nodoc:
|
||||
|
||||
def inspect
|
||||
"#<#{self.class} #{@code} readbody=#{@read}>"
|
||||
|
@ -1585,19 +1611,19 @@ module Net
|
|||
# response <-> exception relationship
|
||||
#
|
||||
|
||||
def code_type
|
||||
def code_type #:nodoc:
|
||||
self.class
|
||||
end
|
||||
|
||||
def error!
|
||||
def error! #:nodoc:
|
||||
raise error_type().new(@code + ' ' + @message.dump, self)
|
||||
end
|
||||
|
||||
def error_type
|
||||
def error_type #:nodoc:
|
||||
self.class::EXCEPTION_TYPE
|
||||
end
|
||||
|
||||
def value
|
||||
def value #:nodoc:
|
||||
error! unless HTTPSuccess === self
|
||||
end
|
||||
|
||||
|
@ -1605,12 +1631,12 @@ module Net
|
|||
# header (for backward compatibility only; DO NOT USE)
|
||||
#
|
||||
|
||||
def response
|
||||
def response #:nodoc:
|
||||
self
|
||||
end
|
||||
|
||||
alias header response
|
||||
alias read_header response
|
||||
alias header response #:nodoc:
|
||||
alias read_header response #:nodoc:
|
||||
|
||||
#
|
||||
# body
|
||||
|
@ -1627,15 +1653,28 @@ module Net
|
|||
end
|
||||
end
|
||||
|
||||
# Gets entity body and either writes it into +dest+ using "<<" method.
|
||||
# or yields it to +block+. +dest+ and +block+ cannot both be provided;
|
||||
# doing so results in an ArgumentError being raised. If neither is
|
||||
# provided, the body is read into a String and this is returned.
|
||||
# Gets entity body. If the block given, yields it to +block+.
|
||||
# The body is provided in fragments, as it is read in from the socket.
|
||||
#
|
||||
# Calling this method a second or subsequent time will return the
|
||||
# original +dest+, +block+ (non-usefully), or String the body was
|
||||
# read into.
|
||||
# already read string.
|
||||
#
|
||||
# http.request_get('/index.html') {|res|
|
||||
# puts res.read_body
|
||||
# }
|
||||
#
|
||||
# http.request_get('/index.html') {|res|
|
||||
# p res.read_body.object_id # 538149362
|
||||
# p res.read_body.object_id # 538149362
|
||||
# }
|
||||
#
|
||||
# # using iterator
|
||||
# http.request_get('/index.html') {|res|
|
||||
# res.read_body do |segment|
|
||||
# print segment
|
||||
# end
|
||||
# }
|
||||
#
|
||||
def read_body( dest = nil, &block )
|
||||
if @read
|
||||
raise IOError, "#{self.class}\#read_body called twice" if dest or block
|
||||
|
@ -1655,8 +1694,25 @@ module Net
|
|||
@body
|
||||
end
|
||||
|
||||
alias body read_body
|
||||
alias entity read_body
|
||||
# Returns the entity body.
|
||||
#
|
||||
# Calling this method a second or subsequent time will return the
|
||||
# already read string.
|
||||
#
|
||||
# http.request_get('/index.html') {|res|
|
||||
# puts res.body
|
||||
# }
|
||||
#
|
||||
# http.request_get('/index.html') {|res|
|
||||
# p res.body.object_id # 538149362
|
||||
# p res.body.object_id # 538149362
|
||||
# }
|
||||
#
|
||||
def body
|
||||
read_body()
|
||||
end
|
||||
|
||||
alias entity body #:nodoc:
|
||||
|
||||
private
|
||||
|
||||
|
@ -1718,7 +1774,7 @@ module Net
|
|||
class HTTP
|
||||
ProxyMod = ProxyDelta
|
||||
end
|
||||
module NetPrivate
|
||||
module NetPrivate #:nodoc:
|
||||
HTTPRequest = ::Net::HTTPRequest
|
||||
end
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue