summaryrefslogtreecommitdiff
path: root/lib/net
diff options
context:
space:
mode:
authoraamine <aamine@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2000-03-05 10:25:53 +0000
committeraamine <aamine@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2000-03-05 10:25:53 +0000
commitb014cc337ef28498311f58a7b28bdfffebc53f00 (patch)
tree3cd5ad06eca04c69ae860e1421a06d0f73b281f4 /lib/net
parent4890f3a684aff94d93a5cc3f68fa8c67c1da6c19 (diff)
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
Diffstat (limited to 'lib/net')
-rw-r--r--lib/net/http.rb65
-rw-r--r--lib/net/protocol.rb98
-rw-r--r--lib/net/smtp.rb8
3 files changed, 94 insertions, 77 deletions
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 <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.
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 = <<MES
@@ -328,42 +332,41 @@ writing string is:
error message from server is:
%s
MES
- raise self.type::Error,
- sprintf( mes, @code, Net.quote(sending), Net.quote(@msg) )
+ type.error! sprintf( mes, @code, Net.quote(sending), Net.quote(@msg) )
end
end
class SuccessCode < ReplyCode
- Error = ProtoUnknownError
+ error_type ProtoUnknownError
end
class ContinueCode < SuccessCode
- Error = ProtoUnknownError
+ error_type ProtoUnknownError
end
class ErrorCode < ReplyCode
- Error = ProtocolError
+ error_type ProtocolError
end
class SyntaxErrorCode < ErrorCode
- Error = ProtoSyntaxError
+ error_type ProtoSyntaxError
end
class FatalErrorCode < ErrorCode
- Error = ProtoFatalError
+ error_type ProtoFatalError
end
class ServerBusyCode < ErrorCode
- Error = ProtoServerError
+ error_type ProtoServerError
end
class RetryCode < ReplyCode
- Error = ProtoRetryError
+ error_type ProtoRetryError
end
class UnknownCode < ReplyCode
- Error = ProtoUnknownError
+ error_type ProtoUnknownError
end
@@ -498,7 +501,6 @@ Object
@socket = TCPsocket.new( @addr, @port )
end
-
attr :socket, true
def close
@@ -515,14 +517,14 @@ Object
end
alias addr address
- attr :port
+ attr_reader :port
def ip_address
@ipaddr.dup
end
alias ipaddr ip_address
- attr :sending
+ attr_reader :sending
CRLF = "\r\n"
diff --git a/lib/net/smtp.rb b/lib/net/smtp.rb
index 4b2986dea2..2c387db43c 100644
--- a/lib/net/smtp.rb
+++ b/lib/net/smtp.rb
@@ -36,7 +36,7 @@ Net::Protocol
This method opens TCP connection and start SMTP.
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
from 'mailsrc' by calling 'each' iterator, and convert them
into "\r\n" terminated string when write.
@@ -47,7 +47,7 @@ Net::Protocol
* Net::ProtoUnknownError: unknown error
* 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.
In the block of this iterator, you can call ONLY 'write' method
for 'adapter'.
@@ -133,11 +133,11 @@ Net::Command
: mailfrom( from_addr )
This method sends "MAIL FROM" command.
- from_addr is your mail address(????@????).
+ from_addr is your mail address (xxxx@xxxx)
: rcpt( to_addrs )
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
This method sends "DATA" command.