summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authoraamine <aamine@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2002-02-22 12:10:58 +0000
committeraamine <aamine@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2002-02-22 12:10:58 +0000
commitd3b66ccf45b30ad1d7210a30fd05e6aa5f12f216 (patch)
treedd23edc671484d3da8b20e98bee1bad4c4b6e7a8 /lib
parent9093e4a4a52c5a0827b1263added83f1116d8810 (diff)
* 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
Diffstat (limited to 'lib')
-rw-r--r--lib/net/http.rb67
-rw-r--r--lib/net/protocol.rb9
2 files changed, 37 insertions, 39 deletions
diff --git a/lib/net/http.rb b/lib/net/http.rb
index 536c924355..3866f115b6 100644
--- a/lib/net/http.rb
+++ b/lib/net/http.rb
@@ -460,11 +460,7 @@ module Net
# for backward compatibility
#
- if RUBY_VERSION <= '1.6' then
- @@newimpl = false
- else
- @@newimpl = true
- end
+ @@newimpl = true
def HTTP.version_1_2
@@newimpl = true
@@ -492,7 +488,7 @@ module Net
def HTTP.get( addr, path, port = nil )
req = Get.new( path )
resp = nil
- new( addr, port || HTTP.port ).start {|http|
+ new( addr, port || HTTP.default_port ).start {|http|
resp = http.request( req )
}
resp.body
@@ -776,7 +772,7 @@ module Net
private
def addr_port
- address + (port == HTTP.port ? '' : ":#{port}")
+ address + (port == HTTP.default_port ? '' : ":#{port}")
end
def D( msg )
@@ -848,25 +844,19 @@ module Net
end
def range
- s = @header['range']
- s or return nil
-
- arr = []
- s.split(',').each do |spec|
- m = /bytes\s*=\s*(\d+)?\s*-\s*(\d+)?/i.match( spec )
- m or raise HTTPHeaderSyntaxError, "wrong Range: #{spec}"
-
- d1 = m[1].to_i
- d2 = m[2].to_i
- if m[1] and m[2] then arr.push( d1..d2 )
- elsif m[1] then arr.push( d1..-1 )
- elsif m[2] then arr.push( -d2..-1 )
- else
- raise HTTPHeaderSyntaxError, 'range is not specified'
- end
- end
-
- return arr
+ s = @header['range'] or return nil
+ s.split(',').collect {|spec|
+ m = /bytes\s*=\s*(\d+)?\s*-\s*(\d+)?/i.match(spec) or
+ raise HTTPHeaderSyntaxError, "wrong Range: #{spec}"
+ d1 = m[1].to_i
+ d2 = m[2].to_i
+ if m[1] and m[2] then d1..d2
+ elsif m[1] then d1..-1
+ elsif m[2] then -d2..-1
+ else
+ raise HTTPHeaderSyntaxError, 'range is not specified'
+ end
+ }
end
def range=( r, fin = nil )
@@ -1279,19 +1269,26 @@ module Net
class << self
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 )
- 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
end
private
- def read_response_status( sock )
+ def read_status_line( sock )
str = sock.readline
m = /\AHTTP(?:\/(\d+\.\d+))?\s+(\d\d\d)\s*(.*)\z/in.match(str) or
raise HTTPBadResponse, "wrong status line: #{str.dump}"
- return m.to_a[1,3]
+ m.to_a[1,3]
end
def response_class( code )
@@ -1300,7 +1297,7 @@ module Net
HTTPUnknownResponse
end
- def read_response_header( sock, res )
+ def each_response_header( sock, res )
while true do
line = sock.readuntil( "\n", true ) # ignore EOF
line.sub!( /\s+\z/, '' ) # don't use chop!
@@ -1308,13 +1305,7 @@ module Net
m = /\A([^:]+):\s*/.match(line) or
raise HTTPBadResponse, 'wrong header line format'
- name = m[1]
- line = m.post_match
- if res.key? name then
- res[name] << ', ' << line
- else
- res[name] = line
- end
+ yield m[1], m.post_match
end
end
diff --git a/lib/net/protocol.rb b/lib/net/protocol.rb
index e0ad521f88..b95df2d2d8 100644
--- a/lib/net/protocol.rb
+++ b/lib/net/protocol.rb
@@ -96,7 +96,12 @@ module Net
attr_reader :socket
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?
@active
@@ -378,6 +383,8 @@ module Net
@socket.addr[3]
end
+ attr_accessor :read_timeout
+
attr_reader :socket
def connect( otime )