From b014cc337ef28498311f58a7b28bdfffebc53f00 Mon Sep 17 00:00:00 2001 From: aamine Date: Sun, 5 Mar 2000 10:25:53 +0000 Subject: Version 1.1.6 o all: use 'attr_reader/writer' instead of 'attr' o http.rb: get/head allow implicit 'start' o http.rb: change connection state algorithm o http.rb: process user header before write o protocol.rb: refine start/finish o protocol.rb: Command#last_reply o protocol.rb: ReplyCode.error! git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@631 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- lib/net/http.rb | 65 +++++++++++++++++++++-------------- lib/net/protocol.rb | 98 +++++++++++++++++++++++++++-------------------------- lib/net/smtp.rb | 8 ++--- 3 files changed, 94 insertions(+), 77 deletions(-) (limited to 'lib') diff --git a/lib/net/http.rb b/lib/net/http.rb index 4ae5349202..104dca9681 100644 --- a/lib/net/http.rb +++ b/lib/net/http.rb @@ -3,7 +3,7 @@ = net/http.rb maintained by Minero Aoki -This file is derived from http-access.rb +This file is derived from "http-access.rb". This library is distributed under the terms of the Ruby license. You can freely distribute/modify this library. @@ -22,7 +22,7 @@ class HTTPBadResponse < HTTPError; end =begin -= HTTP class += class HTTP == Class Methods @@ -62,39 +62,58 @@ class HTTPBadResponse < HTTPError; end def get( path, u_header = nil, ret = '' ) - header = connecting { + u_header ||= {} + header = connecting( u_header ) { @command.get ret, edit_path(path), u_header } + return header, ret end def head( path, u_header = nil ) - connecting { + u_header ||= {} + header = connecting( u_header ) { @command.head edit_path(path), u_header } + + header end private - def connecting - if @socket.closed? then + # called when connecting + def do_finish + unless @socket.closed? then + @command.head '/', { 'Connection' => 'Close' } + end + end + + def connecting( u_header ) + u_header = procheader( u_header ) + + if not @socket then + u_header['Connection'] = 'Close' + start + elsif @socket.closed? then @socket.reopen end - header = yield - @socket.close unless keep_alive? header - header + yield + + unless keep_alive? u_header then + @socket.close + end end def keep_alive?( header ) - if str = header[ 'connection' ] then - if /\Aconnection:\s*keep-alive/i === str then + if str = header['Connection'] then + if /\A\s*keep-alive/i === str then return true end else - if @http_version == '1.1' then + if @command.http_version == '1.1' then return true end end @@ -102,14 +121,16 @@ class HTTPBadResponse < HTTPError; end false end - - def do_finish - unless @command.error_occured or @socket.closed? then - head '/', { 'Connection' => 'Close' } + def procheader( h ) + new = {} + h.each do |k,v| + arr = k.split('-') + arr.each{|i| i.capitalize! } + new[ arr.join('-') ] = v end end - + def edit_path( path ) path end @@ -148,7 +169,7 @@ class HTTPBadResponse < HTTPError; end end - attr :http_version + attr_reader :http_version def get( ret, path, u_header = nil ) header = get_response( @@ -195,12 +216,6 @@ class HTTPBadResponse < HTTPError; end private - def do_quit - unless @socket.closed? then - @socket.close - end - end - def get_response( line, u_header ) @socket.writeline line write_header u_header @@ -231,7 +246,7 @@ class HTTPBadResponse < HTTPError; end when ?3 then RetryCode when ?4 then ServerBusyCode when ?5 then FatalErrorCode - else UnknownCode + else UnknownCode end klass.new( status, discrip ) end diff --git a/lib/net/protocol.rb b/lib/net/protocol.rb index aa8a007adf..6d2e34e127 100644 --- a/lib/net/protocol.rb +++ b/lib/net/protocol.rb @@ -15,7 +15,7 @@ require 'socket' module Net - Version = '1.1.5' + Version = '1.1.6' =begin @@ -100,8 +100,7 @@ Object end private :connect - attr :proxyaddr - attr :proxyport + attr_reader :proxyaddr, :proxyport - def klass.proxy? true @@ -155,11 +154,8 @@ Object end - attr :address - attr :port - - attr :command - attr :socket + attr_reader :address, :port, + :command, :socket def start( *args ) @@ -176,23 +172,13 @@ Object end def finish - if @command then - do_finish - disconnect - end - - if @socket and not @socket.closed? then - @socket.close - @socket = nil - end + ret = active? - if active? then - @active = false + do_finish if @command + disconnect + @active = false - return true - else - return false - end + ret end def active? @@ -211,6 +197,7 @@ Object end def do_finish + @command.quit end @@ -220,8 +207,10 @@ Object end def disconnect - @command.quit @command = nil + if @socket and not @socket.closed? then + @socket.close + end @socket = nil end @@ -257,27 +246,30 @@ Object def initialize( sock ) @socket = sock @error_occured = false + @last_reply = nil end - attr :socket, true - attr :error_occured + attr_reader :socket, :error_occured, :last_reply + attr_writer :socket def quit if @socket and not @socket.closed? then - begin - do_quit - ensure - @socket.close unless @socket.closed? - @socket = nil - end + do_quit @error_occured = false end end + private + def do_quit + end + + # abstract get_reply() + def check_reply( *oks ) - reply_must( get_reply, *oks ) + @last_reply = get_reply + reply_must( @last_reply, *oks ) end def reply_must( rep, *oks ) @@ -310,13 +302,25 @@ Object class ReplyCode + class << self + + def error_type( err ) + @err = err + end + + def error!( mes ) + raise @err, mes + end + + end + def initialize( cod, mes ) @code = cod @msg = mes end - attr :code - attr :msg + attr_reader :code, :msg + def error!( sending ) mes = <