* lib/net/protocol.rb: set read_timeout dynamically.

* lib/net/http.rb: @@newimpl is always true in the main trunk.
* lib/net/http.rb: HTTP.port -> default_port
* lib/net/http.rb: HTTPResponse.read_response_status -> read_status_line


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@2117 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
aamine 2002-02-22 12:10:58 +00:00
parent 9093e4a4a5
commit d3b66ccf45
3 changed files with 48 additions and 39 deletions

View File

@ -1,3 +1,14 @@
Fri Feb 22 21:20:53 2002 Minero Aoki <aamine@loveruby.net>
* lib/net/protocol.rb: set read_timeout dynamically.
* lib/net/http.rb: @@newimpl is always true in the main trunk.
* lib/net/http.rb: HTTP.port -> default_port
* lib/net/http.rb: HTTPResponse.read_response_status ->
read_status_line
Fri Feb 22 19:56:15 2002 Usaku Nakamura <usa@ruby-lang.org> Fri Feb 22 19:56:15 2002 Usaku Nakamura <usa@ruby-lang.org>
* win32/config.status.in: set LIBRUBY_SO. * win32/config.status.in: set LIBRUBY_SO.

View File

@ -460,11 +460,7 @@ module Net
# for backward compatibility # for backward compatibility
# #
if RUBY_VERSION <= '1.6' then @@newimpl = true
@@newimpl = false
else
@@newimpl = true
end
def HTTP.version_1_2 def HTTP.version_1_2
@@newimpl = true @@newimpl = true
@ -492,7 +488,7 @@ module Net
def HTTP.get( addr, path, port = nil ) def HTTP.get( addr, path, port = nil )
req = Get.new( path ) req = Get.new( path )
resp = nil resp = nil
new( addr, port || HTTP.port ).start {|http| new( addr, port || HTTP.default_port ).start {|http|
resp = http.request( req ) resp = http.request( req )
} }
resp.body resp.body
@ -776,7 +772,7 @@ module Net
private private
def addr_port def addr_port
address + (port == HTTP.port ? '' : ":#{port}") address + (port == HTTP.default_port ? '' : ":#{port}")
end end
def D( msg ) def D( msg )
@ -848,25 +844,19 @@ module Net
end end
def range def range
s = @header['range'] s = @header['range'] or return nil
s or return nil s.split(',').collect {|spec|
m = /bytes\s*=\s*(\d+)?\s*-\s*(\d+)?/i.match(spec) or
arr = [] raise HTTPHeaderSyntaxError, "wrong Range: #{spec}"
s.split(',').each do |spec| d1 = m[1].to_i
m = /bytes\s*=\s*(\d+)?\s*-\s*(\d+)?/i.match( spec ) d2 = m[2].to_i
m or raise HTTPHeaderSyntaxError, "wrong Range: #{spec}" if m[1] and m[2] then d1..d2
elsif m[1] then d1..-1
d1 = m[1].to_i elsif m[2] then -d2..-1
d2 = m[2].to_i else
if m[1] and m[2] then arr.push( d1..d2 ) raise HTTPHeaderSyntaxError, 'range is not specified'
elsif m[1] then arr.push( d1..-1 ) end
elsif m[2] then arr.push( -d2..-1 ) }
else
raise HTTPHeaderSyntaxError, 'range is not specified'
end
end
return arr
end end
def range=( r, fin = nil ) def range=( r, fin = nil )
@ -1279,19 +1269,26 @@ module Net
class << self class << self
def read_new( sock, hasbody ) def read_new( sock, hasbody )
httpv, code, msg = read_response_status(sock) httpv, code, msg = read_status_line(sock)
res = response_class(code).new( httpv, code, msg, sock, hasbody ) res = response_class(code).new( httpv, code, msg, sock, hasbody )
read_response_header sock, res each_response_header(sock) do |k,v|
if res.key? k then
res[k] << ', ' << v
else
res[k] = v
end
end
res res
end end
private private
def read_response_status( sock ) def read_status_line( sock )
str = sock.readline str = sock.readline
m = /\AHTTP(?:\/(\d+\.\d+))?\s+(\d\d\d)\s*(.*)\z/in.match(str) or m = /\AHTTP(?:\/(\d+\.\d+))?\s+(\d\d\d)\s*(.*)\z/in.match(str) or
raise HTTPBadResponse, "wrong status line: #{str.dump}" raise HTTPBadResponse, "wrong status line: #{str.dump}"
return m.to_a[1,3] m.to_a[1,3]
end end
def response_class( code ) def response_class( code )
@ -1300,7 +1297,7 @@ module Net
HTTPUnknownResponse HTTPUnknownResponse
end end
def read_response_header( sock, res ) def each_response_header( sock, res )
while true do while true do
line = sock.readuntil( "\n", true ) # ignore EOF line = sock.readuntil( "\n", true ) # ignore EOF
line.sub!( /\s+\z/, '' ) # don't use chop! line.sub!( /\s+\z/, '' ) # don't use chop!
@ -1308,13 +1305,7 @@ module Net
m = /\A([^:]+):\s*/.match(line) or m = /\A([^:]+):\s*/.match(line) or
raise HTTPBadResponse, 'wrong header line format' raise HTTPBadResponse, 'wrong header line format'
name = m[1] yield m[1], m.post_match
line = m.post_match
if res.key? name then
res[name] << ', ' << line
else
res[name] = line
end
end end
end end

View File

@ -96,7 +96,12 @@ module Net
attr_reader :socket attr_reader :socket
attr_accessor :open_timeout attr_accessor :open_timeout
attr_accessor :read_timeout attr_reader :read_timeout
def read_timeout=( sec )
@socket.read_timeout = sec if @socket
@read_timeout = sec
end
def active? def active?
@active @active
@ -378,6 +383,8 @@ module Net
@socket.addr[3] @socket.addr[3]
end end
attr_accessor :read_timeout
attr_reader :socket attr_reader :socket
def connect( otime ) def connect( otime )