aamine
* lib/net/http.rb: join HTTPReadResponse into HTTPResponse again. * lib/net/http.rb: move http_version() from HTTPRequest to HTTPResponse. * lib/net/protocol.rb: refactoring. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@1165 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
7e5f9d5a13
commit
b576f57aa0
@ -1,3 +1,12 @@
|
|||||||
|
Thu Feb 8 02:22:09 2001 Minero Aoki <aamine@dp.u-netsurf.ne.jp>
|
||||||
|
|
||||||
|
* lib/net/http.rb: join HTTPReadResponse into HTTPResponse again.
|
||||||
|
|
||||||
|
* lib/net/http.rb: move http_version() from HTTPRequest to
|
||||||
|
HTTPResponse.
|
||||||
|
|
||||||
|
* lib/net/protocol.rb: refactoring.
|
||||||
|
|
||||||
Wed Feb 7 16:27:27 2001 Minero Aoki <aamine@dp.u-netsurf.ne.jp>
|
Wed Feb 7 16:27:27 2001 Minero Aoki <aamine@dp.u-netsurf.ne.jp>
|
||||||
|
|
||||||
* lib/net/http.rb: split HTTPResponse into HTTPReadResponse
|
* lib/net/http.rb: split HTTPResponse into HTTPReadResponse
|
||||||
|
@ -221,11 +221,6 @@ module Net
|
|||||||
def do_finish
|
def do_finish
|
||||||
end
|
end
|
||||||
|
|
||||||
def re_connect
|
|
||||||
@socket.reopen @open_timeout
|
|
||||||
on_connect
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
###
|
###
|
||||||
### proxy
|
### proxy
|
||||||
@ -438,7 +433,7 @@ module Net
|
|||||||
|
|
||||||
yield req
|
yield req
|
||||||
req.response.__send__ :terminate
|
req.response.__send__ :terminate
|
||||||
@curr_http_version = req.http_version
|
@curr_http_version = req.response.http_version
|
||||||
|
|
||||||
if keep_alive? req, req.response then
|
if keep_alive? req, req.response then
|
||||||
if @socket.closed? then
|
if @socket.closed? then
|
||||||
@ -579,12 +574,10 @@ module Net
|
|||||||
|
|
||||||
@socket = nil
|
@socket = nil
|
||||||
@response = nil
|
@response = nil
|
||||||
@http_version = nil
|
|
||||||
end
|
end
|
||||||
|
|
||||||
attr_reader :path
|
attr_reader :path
|
||||||
attr_reader :response
|
attr_reader :response
|
||||||
attr_reader :http_version
|
|
||||||
|
|
||||||
def inspect
|
def inspect
|
||||||
"\#<#{type}>"
|
"\#<#{type}>"
|
||||||
@ -633,6 +626,7 @@ module Net
|
|||||||
ready( sock, ihead ) {|header|
|
ready( sock, ihead ) {|header|
|
||||||
request ver, path, header
|
request ver, path, header
|
||||||
}
|
}
|
||||||
|
@response
|
||||||
end
|
end
|
||||||
|
|
||||||
def ready( sock, ihead )
|
def ready( sock, ihead )
|
||||||
@ -663,12 +657,6 @@ module Net
|
|||||||
resp
|
resp
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
|
||||||
|
|
||||||
module HTTPReadResponse
|
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
def read_response
|
def read_response
|
||||||
resp = get_resline
|
resp = get_resline
|
||||||
|
|
||||||
@ -697,20 +685,16 @@ module Net
|
|||||||
unless m then
|
unless m then
|
||||||
raise HTTPBadResponse, "wrong status line: #{str}"
|
raise HTTPBadResponse, "wrong status line: #{str}"
|
||||||
end
|
end
|
||||||
@http_version = m[1]
|
httpver = m[1]
|
||||||
status = m[2]
|
status = m[2]
|
||||||
discrip = m[3]
|
discrip = m[3]
|
||||||
|
|
||||||
::Net::NetPrivate::HTTPResponse.new(
|
::Net::NetPrivate::HTTPResponse.new(
|
||||||
status, discrip, @socket, type::HAS_BODY )
|
status, discrip, @socket, type::HAS_BODY, httpver )
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
class HTTPRequest
|
|
||||||
include ::Net::NetPrivate::HTTPReadResponse
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
class HTTPRequestWithData < HTTPRequest
|
class HTTPRequestWithData < HTTPRequest
|
||||||
|
|
||||||
@ -733,6 +717,7 @@ module Net
|
|||||||
request ver, path, header
|
request ver, path, header
|
||||||
@socket.write data
|
@socket.write data
|
||||||
}
|
}
|
||||||
|
@response
|
||||||
end
|
end
|
||||||
|
|
||||||
def check_arg( data, blkp )
|
def check_arg( data, blkp )
|
||||||
@ -792,7 +777,7 @@ module Net
|
|||||||
METHOD = 'PUT'
|
METHOD = 'PUT'
|
||||||
end
|
end
|
||||||
|
|
||||||
end # HTTP::
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -804,7 +789,7 @@ module Net
|
|||||||
|
|
||||||
class HTTPResponse < Response
|
class HTTPResponse < Response
|
||||||
|
|
||||||
HTTPCODE_CLASS_TO_OBJ = {
|
CODE_CLASS_TO_OBJ = {
|
||||||
'1' => HTTPInformationCode,
|
'1' => HTTPInformationCode,
|
||||||
'2' => HTTPSuccessCode,
|
'2' => HTTPSuccessCode,
|
||||||
'3' => HTTPRedirectionCode,
|
'3' => HTTPRedirectionCode,
|
||||||
@ -812,7 +797,7 @@ module Net
|
|||||||
'5' => HTTPServerErrorCode
|
'5' => HTTPServerErrorCode
|
||||||
}
|
}
|
||||||
|
|
||||||
HTTPCODE_TO_OBJ = {
|
CODE_TO_OBJ = {
|
||||||
'100' => ContinueCode,
|
'100' => ContinueCode,
|
||||||
'101' => HTTPSwitchProtocol,
|
'101' => HTTPSwitchProtocol,
|
||||||
|
|
||||||
@ -857,19 +842,22 @@ module Net
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def initialize( status, msg, sock, be )
|
def initialize( stat, msg, sock, be, hv )
|
||||||
code = HTTPCODE_TO_OBJ[status] ||
|
code = CODE_TO_OBJ[stat] ||
|
||||||
HTTPCODE_CLASS_TO_OBJ[status[0,1]] ||
|
CODE_CLASS_TO_OBJ[stat[0,1]] ||
|
||||||
UnknownCode
|
UnknownCode
|
||||||
super code, status, msg
|
super code, stat, msg
|
||||||
@socket = sock
|
@socket = sock
|
||||||
@body_exist = be
|
@body_exist = be
|
||||||
|
@http_version = hv
|
||||||
|
|
||||||
@header = {}
|
@header = {}
|
||||||
@body = nil
|
@body = nil
|
||||||
@read = false
|
@read = false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
attr_reader :http_version
|
||||||
|
|
||||||
def inspect
|
def inspect
|
||||||
"#<#{type} #{code}>"
|
"#<#{type} #{code}>"
|
||||||
end
|
end
|
||||||
|
@ -130,7 +130,7 @@ module Net
|
|||||||
@open_timeout = nil
|
@open_timeout = nil
|
||||||
@read_timeout = nil
|
@read_timeout = nil
|
||||||
|
|
||||||
@pipe = nil
|
@dout = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
attr_reader :address
|
attr_reader :address
|
||||||
@ -146,10 +146,12 @@ module Net
|
|||||||
@active
|
@active
|
||||||
end
|
end
|
||||||
|
|
||||||
def set_pipe( arg ) # un-documented
|
def set_debug_output( arg ) # un-documented
|
||||||
@pipe = arg
|
@dout = arg
|
||||||
end
|
end
|
||||||
|
|
||||||
|
alias set_pipe set_debug_output
|
||||||
|
|
||||||
def inspect
|
def inspect
|
||||||
"#<#{type} #{address}:#{port} open=#{active?}>"
|
"#<#{type} #{address}:#{port} open=#{active?}>"
|
||||||
end
|
end
|
||||||
@ -187,9 +189,14 @@ module Net
|
|||||||
on_connect
|
on_connect
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def re_connect
|
||||||
|
@socket.reopen @open_timeout
|
||||||
|
on_connect
|
||||||
|
end
|
||||||
|
|
||||||
def conn_socket( addr, port )
|
def conn_socket( addr, port )
|
||||||
@socket = type.socket_type.open(
|
@socket = type.socket_type.open(
|
||||||
addr, port, @open_timeout, @read_timeout, @pipe )
|
addr, port, @open_timeout, @read_timeout, @dout )
|
||||||
end
|
end
|
||||||
|
|
||||||
def conn_command( sock )
|
def conn_command( sock )
|
||||||
@ -475,14 +482,13 @@ module Net
|
|||||||
|
|
||||||
class Socket
|
class Socket
|
||||||
|
|
||||||
def initialize( addr, port, otime = nil, rtime = nil, pipe = nil )
|
def initialize( addr, port, otime = nil, rtime = nil, dout = nil )
|
||||||
@addr = addr
|
@addr = addr
|
||||||
@port = port
|
@port = port
|
||||||
|
|
||||||
@read_timeout = rtime
|
@read_timeout = rtime
|
||||||
|
|
||||||
@pipe = pipe
|
@debugout = dout
|
||||||
@prepipe = nil
|
|
||||||
|
|
||||||
@closed = true
|
@closed = true
|
||||||
@ipaddr = ''
|
@ipaddr = ''
|
||||||
@ -554,37 +560,37 @@ module Net
|
|||||||
CRLF = "\r\n"
|
CRLF = "\r\n"
|
||||||
|
|
||||||
def read( len, dest = '', ignerr = false )
|
def read( len, dest = '', ignerr = false )
|
||||||
@pipe << "reading #{len} bytes...\n" if @pipe; pipeoff
|
D_off "reading #{len} bytes...\n"
|
||||||
|
|
||||||
rsize = 0
|
rsize = 0
|
||||||
begin
|
begin
|
||||||
while rsize + @buffer.size < len do
|
while rsize + @buffer.size < len do
|
||||||
rsize += writeinto( dest, @buffer.size )
|
rsize += rbuf_moveto( dest, @buffer.size )
|
||||||
fill_rbuf
|
rbuf_fill
|
||||||
end
|
end
|
||||||
writeinto( dest, len - rsize )
|
rbuf_moveto dest, len - rsize
|
||||||
rescue EOFError
|
rescue EOFError
|
||||||
raise unless igneof
|
raise unless igneof
|
||||||
end
|
end
|
||||||
|
|
||||||
@pipe << "read #{len} bytes\n" if pipeon
|
D_on "read #{len} bytes\n"
|
||||||
dest
|
dest
|
||||||
end
|
end
|
||||||
|
|
||||||
def read_all( dest = '' )
|
def read_all( dest = '' )
|
||||||
@pipe << "reading all...\n" if @pipe; pipeoff
|
D_off "reading all...\n"
|
||||||
|
|
||||||
rsize = 0
|
rsize = 0
|
||||||
begin
|
begin
|
||||||
while true do
|
while true do
|
||||||
rsize += writeinto( dest, @buffer.size )
|
rsize += rbuf_moveto( dest, @buffer.size )
|
||||||
fill_rbuf
|
rbuf_fill
|
||||||
end
|
end
|
||||||
rescue EOFError
|
rescue EOFError
|
||||||
;
|
;
|
||||||
end
|
end
|
||||||
|
|
||||||
@pipe << "read #{rsize} bytes\n" if pipeon
|
D_on "read #{rsize} bytes\n"
|
||||||
dest
|
dest
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -594,12 +600,12 @@ module Net
|
|||||||
while true do
|
while true do
|
||||||
idx = @buffer.index( target )
|
idx = @buffer.index( target )
|
||||||
break if idx
|
break if idx
|
||||||
fill_rbuf
|
rbuf_fill
|
||||||
end
|
end
|
||||||
writeinto( dest, idx + target.size )
|
rbuf_moveto dest, idx + target.size
|
||||||
rescue EOFError
|
rescue EOFError
|
||||||
raise unless igneof
|
raise unless igneof
|
||||||
writeinto( dest, @buffer.size )
|
rbuf_moveto dest, @buffer.size
|
||||||
end
|
end
|
||||||
dest
|
dest
|
||||||
end
|
end
|
||||||
@ -611,7 +617,7 @@ module Net
|
|||||||
end
|
end
|
||||||
|
|
||||||
def read_pendstr( dest )
|
def read_pendstr( dest )
|
||||||
@pipe << "reading text...\n" if @pipe; pipeoff
|
D_off "reading text...\n"
|
||||||
|
|
||||||
rsize = 0
|
rsize = 0
|
||||||
while (str = readuntil("\r\n")) != ".\r\n" do
|
while (str = readuntil("\r\n")) != ".\r\n" do
|
||||||
@ -620,13 +626,13 @@ module Net
|
|||||||
dest << str
|
dest << str
|
||||||
end
|
end
|
||||||
|
|
||||||
@pipe << "read #{rsize} bytes\n" if pipeon
|
D_on "read #{rsize} bytes\n"
|
||||||
dest
|
dest
|
||||||
end
|
end
|
||||||
|
|
||||||
# private use only (can not handle 'break')
|
# private use only (can not handle 'break')
|
||||||
def read_pendlist
|
def read_pendlist
|
||||||
@pipe << "reading list...\n" if @pipe; pipeoff
|
D_off "reading list...\n"
|
||||||
|
|
||||||
str = nil
|
str = nil
|
||||||
i = 0
|
i = 0
|
||||||
@ -636,7 +642,7 @@ module Net
|
|||||||
yield str
|
yield str
|
||||||
end
|
end
|
||||||
|
|
||||||
@pipe << "read #{i} items\n" if pipeon
|
D_on "read #{i} items\n"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
@ -645,7 +651,7 @@ module Net
|
|||||||
|
|
||||||
READ_SIZE = 1024 * 4
|
READ_SIZE = 1024 * 4
|
||||||
|
|
||||||
def fill_rbuf
|
def rbuf_fill
|
||||||
unless IO.select [@socket], nil, nil, @read_timeout then
|
unless IO.select [@socket], nil, nil, @read_timeout then
|
||||||
on_read_timeout
|
on_read_timeout
|
||||||
end
|
end
|
||||||
@ -656,12 +662,13 @@ module Net
|
|||||||
raise TimeoutError, "socket read timeout (#{@read_timeout} sec)"
|
raise TimeoutError, "socket read timeout (#{@read_timeout} sec)"
|
||||||
end
|
end
|
||||||
|
|
||||||
def writeinto( dest, len )
|
def rbuf_moveto( dest, len )
|
||||||
bsi = @buffer.size
|
bsi = @buffer.size
|
||||||
dest << @buffer[ 0, len ]
|
s = @buffer[ 0, len ]
|
||||||
|
dest << s
|
||||||
@buffer = @buffer[ len, bsi - len ]
|
@buffer = @buffer[ len, bsi - len ]
|
||||||
|
|
||||||
@pipe << %{read "#{Net.quote dest}"\n} if @pipe
|
@debugout << %<read "#{Net.quote s}"\n> if @debugout
|
||||||
len
|
len
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -698,7 +705,7 @@ module Net
|
|||||||
end
|
end
|
||||||
|
|
||||||
def write_pendstr( src, block )
|
def write_pendstr( src, block )
|
||||||
@pipe << "writing text from #{src.type}\n" if @pipe; pipeoff
|
D_off "writing text from #{src.type}\n"
|
||||||
|
|
||||||
wsize = use_each_crlf_line {
|
wsize = use_each_crlf_line {
|
||||||
if block then
|
if block then
|
||||||
@ -708,7 +715,7 @@ module Net
|
|||||||
end
|
end
|
||||||
}
|
}
|
||||||
|
|
||||||
@pipe << "wrote #{wsize} bytes text\n" if pipeon
|
D_on "wrote #{wsize} bytes text\n"
|
||||||
wsize
|
wsize
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -806,17 +813,17 @@ module Net
|
|||||||
|
|
||||||
yield
|
yield
|
||||||
|
|
||||||
if @pipe then
|
if @debugout then
|
||||||
@pipe << 'write "'
|
@debugout << 'write "'
|
||||||
@pipe << @sending
|
@debugout << @sending
|
||||||
@pipe << "\"\n"
|
@debugout << "\"\n"
|
||||||
end
|
end
|
||||||
@socket.flush
|
@socket.flush
|
||||||
@writtensize
|
@writtensize
|
||||||
end
|
end
|
||||||
|
|
||||||
def do_write( arg )
|
def do_write( arg )
|
||||||
if @pipe or @sending.size < 128 then
|
if @debugout or @sending.size < 128 then
|
||||||
@sending << Net.quote( arg )
|
@sending << Net.quote( arg )
|
||||||
else
|
else
|
||||||
@sending << '...' unless @sending[-1] == ?.
|
@sending << '...' unless @sending[-1] == ?.
|
||||||
@ -828,16 +835,15 @@ module Net
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
def pipeoff
|
def D_off( msg )
|
||||||
@prepipe = @pipe
|
@debugout << msg if @debugout
|
||||||
@pipe = nil
|
@savedo, @debugout = @debugout, nil
|
||||||
@prepipe
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def pipeon
|
def D_on( msg )
|
||||||
@pipe = @prepipe
|
@debugout = @savedo
|
||||||
@prepipe = nil
|
@savedo = nil
|
||||||
@pipe
|
@debugout << msg if @debugout
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user