1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

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
This commit is contained in:
aamine 2000-03-05 10:25:53 +00:00
parent 4890f3a684
commit b014cc337e
3 changed files with 94 additions and 77 deletions

View file

@ -3,7 +3,7 @@
= net/http.rb = net/http.rb
maintained by Minero Aoki <aamine@dp.u-netsurf.ne.jp> maintained by Minero Aoki <aamine@dp.u-netsurf.ne.jp>
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. This library is distributed under the terms of the Ruby license.
You can freely distribute/modify this library. You can freely distribute/modify this library.
@ -22,7 +22,7 @@ class HTTPBadResponse < HTTPError; end
=begin =begin
= HTTP class = class HTTP
== Class Methods == Class Methods
@ -62,39 +62,58 @@ class HTTPBadResponse < HTTPError; end
def get( path, u_header = nil, ret = '' ) def get( path, u_header = nil, ret = '' )
header = connecting { u_header ||= {}
header = connecting( u_header ) {
@command.get ret, edit_path(path), u_header @command.get ret, edit_path(path), u_header
} }
return header, ret return header, ret
end end
def head( path, u_header = nil ) def head( path, u_header = nil )
connecting { u_header ||= {}
header = connecting( u_header ) {
@command.head edit_path(path), u_header @command.head edit_path(path), u_header
} }
header
end end
private private
def connecting # called when connecting
if @socket.closed? then 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 @socket.reopen
end end
header = yield
@socket.close unless keep_alive? header
header yield
unless keep_alive? u_header then
@socket.close
end
end end
def keep_alive?( header ) def keep_alive?( header )
if str = header[ 'connection' ] then if str = header['Connection'] then
if /\Aconnection:\s*keep-alive/i === str then if /\A\s*keep-alive/i === str then
return true return true
end end
else else
if @http_version == '1.1' then if @command.http_version == '1.1' then
return true return true
end end
end end
@ -102,10 +121,12 @@ class HTTPBadResponse < HTTPError; end
false false
end end
def procheader( h )
def do_finish new = {}
unless @command.error_occured or @socket.closed? then h.each do |k,v|
head '/', { 'Connection' => 'Close' } arr = k.split('-')
arr.each{|i| i.capitalize! }
new[ arr.join('-') ] = v
end end
end end
@ -148,7 +169,7 @@ class HTTPBadResponse < HTTPError; end
end end
attr :http_version attr_reader :http_version
def get( ret, path, u_header = nil ) def get( ret, path, u_header = nil )
header = get_response( header = get_response(
@ -195,12 +216,6 @@ class HTTPBadResponse < HTTPError; end
private private
def do_quit
unless @socket.closed? then
@socket.close
end
end
def get_response( line, u_header ) def get_response( line, u_header )
@socket.writeline line @socket.writeline line
write_header u_header write_header u_header

View file

@ -15,7 +15,7 @@ require 'socket'
module Net module Net
Version = '1.1.5' Version = '1.1.6'
=begin =begin
@ -100,8 +100,7 @@ Object
end end
private :connect private :connect
attr :proxyaddr attr_reader :proxyaddr, :proxyport
attr :proxyport
- -
def klass.proxy? def klass.proxy?
true true
@ -155,11 +154,8 @@ Object
end end
attr :address attr_reader :address, :port,
attr :port :command, :socket
attr :command
attr :socket
def start( *args ) def start( *args )
@ -176,23 +172,13 @@ Object
end end
def finish def finish
if @command then ret = active?
do_finish
do_finish if @command
disconnect disconnect
end
if @socket and not @socket.closed? then
@socket.close
@socket = nil
end
if active? then
@active = false @active = false
return true ret
else
return false
end
end end
def active? def active?
@ -211,6 +197,7 @@ Object
end end
def do_finish def do_finish
@command.quit
end end
@ -220,8 +207,10 @@ Object
end end
def disconnect def disconnect
@command.quit
@command = nil @command = nil
if @socket and not @socket.closed? then
@socket.close
end
@socket = nil @socket = nil
end end
@ -257,27 +246,30 @@ Object
def initialize( sock ) def initialize( sock )
@socket = sock @socket = sock
@error_occured = false @error_occured = false
@last_reply = nil
end end
attr :socket, true attr_reader :socket, :error_occured, :last_reply
attr :error_occured attr_writer :socket
def quit def quit
if @socket and not @socket.closed? then if @socket and not @socket.closed? then
begin
do_quit do_quit
ensure
@socket.close unless @socket.closed?
@socket = nil
end
@error_occured = false @error_occured = false
end end
end end
private private
def do_quit
end
# abstract get_reply()
def check_reply( *oks ) def check_reply( *oks )
reply_must( get_reply, *oks ) @last_reply = get_reply
reply_must( @last_reply, *oks )
end end
def reply_must( rep, *oks ) def reply_must( rep, *oks )
@ -310,13 +302,25 @@ Object
class ReplyCode class ReplyCode
class << self
def error_type( err )
@err = err
end
def error!( mes )
raise @err, mes
end
end
def initialize( cod, mes ) def initialize( cod, mes )
@code = cod @code = cod
@msg = mes @msg = mes
end end
attr :code attr_reader :code, :msg
attr :msg
def error!( sending ) def error!( sending )
mes = <<MES mes = <<MES
@ -328,42 +332,41 @@ writing string is:
error message from server is: error message from server is:
%s %s
MES MES
raise self.type::Error, type.error! sprintf( mes, @code, Net.quote(sending), Net.quote(@msg) )
sprintf( mes, @code, Net.quote(sending), Net.quote(@msg) )
end end
end end
class SuccessCode < ReplyCode class SuccessCode < ReplyCode
Error = ProtoUnknownError error_type ProtoUnknownError
end end
class ContinueCode < SuccessCode class ContinueCode < SuccessCode
Error = ProtoUnknownError error_type ProtoUnknownError
end end
class ErrorCode < ReplyCode class ErrorCode < ReplyCode
Error = ProtocolError error_type ProtocolError
end end
class SyntaxErrorCode < ErrorCode class SyntaxErrorCode < ErrorCode
Error = ProtoSyntaxError error_type ProtoSyntaxError
end end
class FatalErrorCode < ErrorCode class FatalErrorCode < ErrorCode
Error = ProtoFatalError error_type ProtoFatalError
end end
class ServerBusyCode < ErrorCode class ServerBusyCode < ErrorCode
Error = ProtoServerError error_type ProtoServerError
end end
class RetryCode < ReplyCode class RetryCode < ReplyCode
Error = ProtoRetryError error_type ProtoRetryError
end end
class UnknownCode < ReplyCode class UnknownCode < ReplyCode
Error = ProtoUnknownError error_type ProtoUnknownError
end end
@ -498,7 +501,6 @@ Object
@socket = TCPsocket.new( @addr, @port ) @socket = TCPsocket.new( @addr, @port )
end end
attr :socket, true attr :socket, true
def close def close
@ -515,14 +517,14 @@ Object
end end
alias addr address alias addr address
attr :port attr_reader :port
def ip_address def ip_address
@ipaddr.dup @ipaddr.dup
end end
alias ipaddr ip_address alias ipaddr ip_address
attr :sending attr_reader :sending
CRLF = "\r\n" CRLF = "\r\n"

View file

@ -36,7 +36,7 @@ Net::Protocol
This method opens TCP connection and start SMTP. This method opens TCP connection and start SMTP.
If protocol had been started, do nothing and return false. If protocol had been started, do nothing and return false.
: sendmail( mailsrc, from_domain, to_addrs ) : sendmail( mailsrc, from_addr, to_addrs )
This method sends 'mailsrc' as mail. SMTPSession read strings This method sends 'mailsrc' as mail. SMTPSession read strings
from 'mailsrc' by calling 'each' iterator, and convert them from 'mailsrc' by calling 'each' iterator, and convert them
into "\r\n" terminated string when write. into "\r\n" terminated string when write.
@ -47,7 +47,7 @@ Net::Protocol
* Net::ProtoUnknownError: unknown error * Net::ProtoUnknownError: unknown error
* Net::ProtoServerBusy: temporary error (errno.420/450) * Net::ProtoServerBusy: temporary error (errno.420/450)
: ready( from_domain, to_addrs ) {|adapter| .... } : ready( from_addr, to_addrs ) {|adapter| .... }
This method stands by the SMTP object for sending mail. This method stands by the SMTP object for sending mail.
In the block of this iterator, you can call ONLY 'write' method In the block of this iterator, you can call ONLY 'write' method
for 'adapter'. for 'adapter'.
@ -133,11 +133,11 @@ Net::Command
: mailfrom( from_addr ) : mailfrom( from_addr )
This method sends "MAIL FROM" command. This method sends "MAIL FROM" command.
from_addr is your mail address(????@????). from_addr is your mail address (xxxx@xxxx)
: rcpt( to_addrs ) : rcpt( to_addrs )
This method sends "RCPT TO" command. This method sends "RCPT TO" command.
to_addrs is array of mail address(???@???) of destination. to_addrs is array of mail address (xxxx@xxxx) of destination.
: data : data
This method sends "DATA" command. This method sends "DATA" command.