From b576f57aa02c4983eeb081f625c45b9445c50538 Mon Sep 17 00:00:00 2001 From: aamine Date: Wed, 7 Feb 2001 17:17:51 +0000 Subject: 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 --- ChangeLog | 9 ++++++ lib/net/http.rb | 42 +++++++++---------------- lib/net/protocol.rb | 90 ++++++++++++++++++++++++++++------------------------- 3 files changed, 72 insertions(+), 69 deletions(-) diff --git a/ChangeLog b/ChangeLog index 6bcb2c45d3..9f333c92f5 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,12 @@ +Thu Feb 8 02:22:09 2001 Minero Aoki + + * 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 * lib/net/http.rb: split HTTPResponse into HTTPReadResponse diff --git a/lib/net/http.rb b/lib/net/http.rb index 2f5d76cc26..4c39bb74ba 100644 --- a/lib/net/http.rb +++ b/lib/net/http.rb @@ -221,11 +221,6 @@ module Net def do_finish end - def re_connect - @socket.reopen @open_timeout - on_connect - end - ### ### proxy @@ -438,7 +433,7 @@ module Net yield req 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 @socket.closed? then @@ -579,12 +574,10 @@ module Net @socket = nil @response = nil - @http_version = nil end attr_reader :path attr_reader :response - attr_reader :http_version def inspect "\#<#{type}>" @@ -633,6 +626,7 @@ module Net ready( sock, ihead ) {|header| request ver, path, header } + @response end def ready( sock, ihead ) @@ -663,12 +657,6 @@ module Net resp end - end - - module HTTPReadResponse - - private - def read_response resp = get_resline @@ -697,20 +685,16 @@ module Net unless m then raise HTTPBadResponse, "wrong status line: #{str}" end - @http_version = m[1] + httpver = m[1] status = m[2] discrip = m[3] ::Net::NetPrivate::HTTPResponse.new( - status, discrip, @socket, type::HAS_BODY ) + status, discrip, @socket, type::HAS_BODY, httpver ) end end - class HTTPRequest - include ::Net::NetPrivate::HTTPReadResponse - end - class HTTPRequestWithData < HTTPRequest @@ -733,6 +717,7 @@ module Net request ver, path, header @socket.write data } + @response end def check_arg( data, blkp ) @@ -792,7 +777,7 @@ module Net METHOD = 'PUT' end - end # HTTP:: + end @@ -804,7 +789,7 @@ module Net class HTTPResponse < Response - HTTPCODE_CLASS_TO_OBJ = { + CODE_CLASS_TO_OBJ = { '1' => HTTPInformationCode, '2' => HTTPSuccessCode, '3' => HTTPRedirectionCode, @@ -812,7 +797,7 @@ module Net '5' => HTTPServerErrorCode } - HTTPCODE_TO_OBJ = { + CODE_TO_OBJ = { '100' => ContinueCode, '101' => HTTPSwitchProtocol, @@ -857,19 +842,22 @@ module Net } - def initialize( status, msg, sock, be ) - code = HTTPCODE_TO_OBJ[status] || - HTTPCODE_CLASS_TO_OBJ[status[0,1]] || + def initialize( stat, msg, sock, be, hv ) + code = CODE_TO_OBJ[stat] || + CODE_CLASS_TO_OBJ[stat[0,1]] || UnknownCode - super code, status, msg + super code, stat, msg @socket = sock @body_exist = be + @http_version = hv @header = {} @body = nil @read = false end + attr_reader :http_version + def inspect "#<#{type} #{code}>" end diff --git a/lib/net/protocol.rb b/lib/net/protocol.rb index 7eb38cf993..f936e5c6d6 100644 --- a/lib/net/protocol.rb +++ b/lib/net/protocol.rb @@ -130,7 +130,7 @@ module Net @open_timeout = nil @read_timeout = nil - @pipe = nil + @dout = nil end attr_reader :address @@ -146,10 +146,12 @@ module Net @active end - def set_pipe( arg ) # un-documented - @pipe = arg + def set_debug_output( arg ) # un-documented + @dout = arg end + alias set_pipe set_debug_output + def inspect "#<#{type} #{address}:#{port} open=#{active?}>" end @@ -187,9 +189,14 @@ module Net on_connect end + def re_connect + @socket.reopen @open_timeout + on_connect + end + def conn_socket( addr, port ) @socket = type.socket_type.open( - addr, port, @open_timeout, @read_timeout, @pipe ) + addr, port, @open_timeout, @read_timeout, @dout ) end def conn_command( sock ) @@ -475,14 +482,13 @@ module Net class Socket - def initialize( addr, port, otime = nil, rtime = nil, pipe = nil ) + def initialize( addr, port, otime = nil, rtime = nil, dout = nil ) @addr = addr @port = port @read_timeout = rtime - @pipe = pipe - @prepipe = nil + @debugout = dout @closed = true @ipaddr = '' @@ -554,37 +560,37 @@ module Net CRLF = "\r\n" def read( len, dest = '', ignerr = false ) - @pipe << "reading #{len} bytes...\n" if @pipe; pipeoff + D_off "reading #{len} bytes...\n" rsize = 0 begin while rsize + @buffer.size < len do - rsize += writeinto( dest, @buffer.size ) - fill_rbuf + rsize += rbuf_moveto( dest, @buffer.size ) + rbuf_fill end - writeinto( dest, len - rsize ) + rbuf_moveto dest, len - rsize rescue EOFError raise unless igneof end - @pipe << "read #{len} bytes\n" if pipeon + D_on "read #{len} bytes\n" dest end def read_all( dest = '' ) - @pipe << "reading all...\n" if @pipe; pipeoff + D_off "reading all...\n" rsize = 0 begin while true do - rsize += writeinto( dest, @buffer.size ) - fill_rbuf + rsize += rbuf_moveto( dest, @buffer.size ) + rbuf_fill end rescue EOFError ; end - @pipe << "read #{rsize} bytes\n" if pipeon + D_on "read #{rsize} bytes\n" dest end @@ -594,12 +600,12 @@ module Net while true do idx = @buffer.index( target ) break if idx - fill_rbuf + rbuf_fill end - writeinto( dest, idx + target.size ) + rbuf_moveto dest, idx + target.size rescue EOFError raise unless igneof - writeinto( dest, @buffer.size ) + rbuf_moveto dest, @buffer.size end dest end @@ -611,7 +617,7 @@ module Net end def read_pendstr( dest ) - @pipe << "reading text...\n" if @pipe; pipeoff + D_off "reading text...\n" rsize = 0 while (str = readuntil("\r\n")) != ".\r\n" do @@ -620,13 +626,13 @@ module Net dest << str end - @pipe << "read #{rsize} bytes\n" if pipeon + D_on "read #{rsize} bytes\n" dest end # private use only (can not handle 'break') def read_pendlist - @pipe << "reading list...\n" if @pipe; pipeoff + D_off "reading list...\n" str = nil i = 0 @@ -636,7 +642,7 @@ module Net yield str end - @pipe << "read #{i} items\n" if pipeon + D_on "read #{i} items\n" end @@ -645,7 +651,7 @@ module Net READ_SIZE = 1024 * 4 - def fill_rbuf + def rbuf_fill unless IO.select [@socket], nil, nil, @read_timeout then on_read_timeout end @@ -656,12 +662,13 @@ module Net raise TimeoutError, "socket read timeout (#{@read_timeout} sec)" end - def writeinto( dest, len ) + def rbuf_moveto( dest, len ) bsi = @buffer.size - dest << @buffer[ 0, len ] + s = @buffer[ 0, len ] + dest << s @buffer = @buffer[ len, bsi - len ] - @pipe << %{read "#{Net.quote dest}"\n} if @pipe + @debugout << % if @debugout len end @@ -698,7 +705,7 @@ module Net end 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 { if block then @@ -708,7 +715,7 @@ module Net end } - @pipe << "wrote #{wsize} bytes text\n" if pipeon + D_on "wrote #{wsize} bytes text\n" wsize end @@ -806,17 +813,17 @@ module Net yield - if @pipe then - @pipe << 'write "' - @pipe << @sending - @pipe << "\"\n" + if @debugout then + @debugout << 'write "' + @debugout << @sending + @debugout << "\"\n" end @socket.flush @writtensize end def do_write( arg ) - if @pipe or @sending.size < 128 then + if @debugout or @sending.size < 128 then @sending << Net.quote( arg ) else @sending << '...' unless @sending[-1] == ?. @@ -828,16 +835,15 @@ module Net end - def pipeoff - @prepipe = @pipe - @pipe = nil - @prepipe + def D_off( msg ) + @debugout << msg if @debugout + @savedo, @debugout = @debugout, nil end - def pipeon - @pipe = @prepipe - @prepipe = nil - @pipe + def D_on( msg ) + @debugout = @savedo + @savedo = nil + @debugout << msg if @debugout end end -- cgit v1.2.3