1999-10-13 11:29:15 +04:00
|
|
|
=begin
|
|
|
|
|
2001-12-13 22:15:21 +03:00
|
|
|
= net/protocol.rb
|
1999-10-13 11:29:15 +04:00
|
|
|
|
2002-02-19 15:33:52 +03:00
|
|
|
Copyright (c) 1999-2002 Yukihiro Matsumoto
|
1999-10-13 11:29:15 +04:00
|
|
|
|
2001-06-27 03:49:21 +04:00
|
|
|
written & maintained by Minero Aoki <aamine@loveruby.net>
|
1999-10-13 11:29:15 +04:00
|
|
|
|
2001-06-27 03:49:21 +04:00
|
|
|
This program is free software. You can re-distribute and/or
|
|
|
|
modify this program under the same terms as Ruby itself,
|
|
|
|
Ruby Distribute License or GNU General Public License.
|
2001-02-24 07:53:50 +03:00
|
|
|
|
2001-12-07 13:04:25 +03:00
|
|
|
NOTE: You can find Japanese version of this document in
|
|
|
|
the doc/net directory of the standard ruby interpreter package.
|
1999-10-13 11:29:15 +04:00
|
|
|
|
2001-12-13 22:15:21 +03:00
|
|
|
$Id$
|
|
|
|
|
1999-10-13 11:29:15 +04:00
|
|
|
=end
|
|
|
|
|
2000-06-16 17:47:38 +04:00
|
|
|
require 'socket'
|
2001-02-06 14:14:51 +03:00
|
|
|
require 'timeout'
|
2000-06-16 17:47:38 +04:00
|
|
|
|
|
|
|
|
|
|
|
module Net
|
|
|
|
|
1999-12-29 14:14:04 +03:00
|
|
|
class Protocol
|
1999-12-17 18:00:13 +03:00
|
|
|
|
2001-07-08 11:00:23 +04:00
|
|
|
Version = '1.2.3'
|
2001-12-13 22:15:21 +03:00
|
|
|
Revision = %q$Revision$.split(/\s+/)[1]
|
2000-02-21 18:27:49 +03:00
|
|
|
|
1999-12-17 18:00:13 +03:00
|
|
|
|
2001-12-30 22:18:45 +03:00
|
|
|
class << self
|
1999-12-17 18:00:13 +03:00
|
|
|
|
2002-02-19 15:33:52 +03:00
|
|
|
def port
|
|
|
|
default_port
|
|
|
|
end
|
|
|
|
|
1999-12-17 18:00:13 +03:00
|
|
|
private
|
|
|
|
|
1999-12-29 14:14:04 +03:00
|
|
|
def protocol_param( name, val )
|
2001-12-30 22:18:45 +03:00
|
|
|
module_eval <<-End, __FILE__, __LINE__ + 1
|
|
|
|
def self.#{name.id2name}
|
|
|
|
#{val}
|
|
|
|
end
|
|
|
|
End
|
1999-12-17 18:00:13 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
#
|
2001-06-27 03:49:21 +04:00
|
|
|
# --- Configuration Staffs for Sub Classes ---
|
|
|
|
#
|
2002-02-19 15:33:52 +03:00
|
|
|
# class method default_port
|
|
|
|
# class method command_type
|
|
|
|
# class method socket_type
|
1999-12-17 18:00:13 +03:00
|
|
|
#
|
2001-12-30 22:18:45 +03:00
|
|
|
# private method do_start
|
|
|
|
# private method do_finish
|
1999-12-17 18:00:13 +03:00
|
|
|
#
|
2001-12-30 22:18:45 +03:00
|
|
|
# private method conn_address
|
|
|
|
# private method conn_port
|
1999-12-17 18:00:13 +03:00
|
|
|
#
|
|
|
|
|
|
|
|
|
2001-12-30 22:18:45 +03:00
|
|
|
def Protocol.start( address, port = nil, *args )
|
2002-02-19 15:33:52 +03:00
|
|
|
instance = new(address, port)
|
2001-12-30 22:18:45 +03:00
|
|
|
|
|
|
|
if block_given? then
|
2002-02-19 15:33:52 +03:00
|
|
|
instance.start(*args) { return yield(instance) }
|
2001-12-30 22:18:45 +03:00
|
|
|
else
|
2002-02-19 15:33:52 +03:00
|
|
|
instance.start(*args)
|
2001-12-30 22:18:45 +03:00
|
|
|
instance
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2001-08-17 07:08:45 +04:00
|
|
|
def initialize( addr, port = nil )
|
2001-12-07 13:04:25 +03:00
|
|
|
@address = addr
|
2002-02-19 15:33:52 +03:00
|
|
|
@port = port || type.default_port
|
1999-12-17 18:00:13 +03:00
|
|
|
|
1999-12-29 14:14:04 +03:00
|
|
|
@command = nil
|
1999-12-17 18:00:13 +03:00
|
|
|
@socket = nil
|
2000-12-22 21:40:55 +03:00
|
|
|
|
2001-02-06 14:14:51 +03:00
|
|
|
@active = false
|
|
|
|
|
2001-12-30 22:18:45 +03:00
|
|
|
@open_timeout = 30
|
|
|
|
@read_timeout = 60
|
2001-02-06 14:14:51 +03:00
|
|
|
|
2001-02-07 20:17:51 +03:00
|
|
|
@dout = nil
|
1999-12-17 18:00:13 +03:00
|
|
|
end
|
|
|
|
|
2000-12-22 21:40:55 +03:00
|
|
|
attr_reader :address
|
|
|
|
attr_reader :port
|
|
|
|
|
|
|
|
attr_reader :command
|
|
|
|
attr_reader :socket
|
1999-12-17 18:00:13 +03:00
|
|
|
|
2001-02-06 14:14:51 +03:00
|
|
|
attr_accessor :open_timeout
|
2002-02-22 15:10:58 +03:00
|
|
|
attr_reader :read_timeout
|
|
|
|
|
|
|
|
def read_timeout=( sec )
|
|
|
|
@socket.read_timeout = sec if @socket
|
|
|
|
@read_timeout = sec
|
|
|
|
end
|
2001-02-06 14:14:51 +03:00
|
|
|
|
|
|
|
def active?
|
|
|
|
@active
|
|
|
|
end
|
|
|
|
|
2001-02-07 20:17:51 +03:00
|
|
|
def set_debug_output( arg ) # un-documented
|
|
|
|
@dout = arg
|
2001-02-06 14:14:51 +03:00
|
|
|
end
|
|
|
|
|
2001-02-07 20:17:51 +03:00
|
|
|
alias set_pipe set_debug_output
|
|
|
|
|
2000-06-12 20:42:46 +04:00
|
|
|
def inspect
|
|
|
|
"#<#{type} #{address}:#{port} open=#{active?}>"
|
|
|
|
end
|
|
|
|
|
2001-02-06 14:14:51 +03:00
|
|
|
#
|
2001-12-30 22:18:45 +03:00
|
|
|
# open
|
2001-02-06 14:14:51 +03:00
|
|
|
#
|
1999-12-17 18:00:13 +03:00
|
|
|
|
1999-09-22 11:32:33 +04:00
|
|
|
def start( *args )
|
2001-12-30 22:18:45 +03:00
|
|
|
@active and raise IOError, 'protocol has been opened already'
|
1999-09-22 11:32:33 +04:00
|
|
|
|
2000-12-22 21:40:55 +03:00
|
|
|
if block_given? then
|
|
|
|
begin
|
2001-12-30 22:18:45 +03:00
|
|
|
do_start( *args )
|
|
|
|
@active = true
|
|
|
|
return yield(self)
|
2000-12-22 21:40:55 +03:00
|
|
|
ensure
|
2001-12-30 22:18:45 +03:00
|
|
|
finish if @active
|
2000-12-22 21:40:55 +03:00
|
|
|
end
|
1999-09-22 11:32:33 +04:00
|
|
|
end
|
2001-12-30 22:18:45 +03:00
|
|
|
|
|
|
|
do_start( *args )
|
|
|
|
@active = true
|
2002-03-22 08:24:00 +03:00
|
|
|
self
|
1999-09-22 11:32:33 +04:00
|
|
|
end
|
|
|
|
|
2001-02-06 14:14:51 +03:00
|
|
|
private
|
|
|
|
|
2001-12-30 22:18:45 +03:00
|
|
|
# abstract do_start()
|
2000-12-22 21:40:55 +03:00
|
|
|
|
2001-12-30 22:18:45 +03:00
|
|
|
def conn_socket
|
|
|
|
@socket = type.socket_type.open(
|
|
|
|
conn_address(), conn_port(),
|
|
|
|
@open_timeout, @read_timeout, @dout )
|
2001-02-06 14:14:51 +03:00
|
|
|
on_connect
|
|
|
|
end
|
1999-12-17 18:00:13 +03:00
|
|
|
|
2001-12-30 22:18:45 +03:00
|
|
|
alias conn_address address
|
|
|
|
alias conn_port port
|
|
|
|
|
|
|
|
def reconn_socket
|
2001-02-07 20:17:51 +03:00
|
|
|
@socket.reopen @open_timeout
|
|
|
|
on_connect
|
|
|
|
end
|
|
|
|
|
2001-12-30 22:18:45 +03:00
|
|
|
def conn_command
|
|
|
|
@command = type.command_type.new(@socket)
|
1999-12-17 18:00:13 +03:00
|
|
|
end
|
1999-09-22 11:32:33 +04:00
|
|
|
|
2001-02-06 14:14:51 +03:00
|
|
|
def on_connect
|
1999-10-13 11:29:15 +04:00
|
|
|
end
|
|
|
|
|
2001-02-06 14:14:51 +03:00
|
|
|
#
|
2001-12-30 22:18:45 +03:00
|
|
|
# close
|
2001-02-06 14:14:51 +03:00
|
|
|
#
|
1999-12-17 18:00:13 +03:00
|
|
|
|
2001-02-06 14:14:51 +03:00
|
|
|
public
|
1999-12-17 18:00:13 +03:00
|
|
|
|
2001-02-06 14:14:51 +03:00
|
|
|
def finish
|
2001-12-30 22:18:45 +03:00
|
|
|
active? or raise IOError, 'closing already closed protocol'
|
|
|
|
do_finish
|
2001-02-06 14:14:51 +03:00
|
|
|
@active = false
|
2001-07-03 23:03:16 +04:00
|
|
|
nil
|
1999-12-17 18:00:13 +03:00
|
|
|
end
|
|
|
|
|
2001-02-06 14:14:51 +03:00
|
|
|
private
|
1999-12-17 18:00:13 +03:00
|
|
|
|
2001-12-30 22:18:45 +03:00
|
|
|
# abstract do_finish()
|
1999-12-17 18:00:13 +03:00
|
|
|
|
2001-12-30 22:18:45 +03:00
|
|
|
def disconn_command
|
|
|
|
@command.quit if @command and not @command.critical?
|
1999-12-29 14:14:04 +03:00
|
|
|
@command = nil
|
2001-12-30 22:18:45 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def disconn_socket
|
2000-03-05 13:25:53 +03:00
|
|
|
if @socket and not @socket.closed? then
|
|
|
|
@socket.close
|
|
|
|
end
|
2001-02-06 14:14:51 +03:00
|
|
|
@socket = nil
|
1999-12-17 18:00:13 +03:00
|
|
|
end
|
2000-03-27 19:52:27 +04:00
|
|
|
|
1999-09-22 11:32:33 +04:00
|
|
|
end
|
|
|
|
|
1999-12-29 14:14:04 +03:00
|
|
|
Session = Protocol
|
|
|
|
|
1999-09-22 11:32:33 +04:00
|
|
|
|
2000-03-31 17:02:40 +04:00
|
|
|
class Response
|
1999-09-22 11:32:33 +04:00
|
|
|
|
2001-12-07 13:04:25 +03:00
|
|
|
def initialize( ctype, code, msg )
|
2000-03-31 17:02:40 +04:00
|
|
|
@code_type = ctype
|
2001-12-07 13:04:25 +03:00
|
|
|
@code = code
|
2000-03-31 17:02:40 +04:00
|
|
|
@message = msg
|
|
|
|
super()
|
2000-03-26 12:48:15 +04:00
|
|
|
end
|
|
|
|
|
2000-03-31 17:02:40 +04:00
|
|
|
attr_reader :code_type, :code, :message
|
|
|
|
alias msg message
|
1999-09-22 11:32:33 +04:00
|
|
|
|
2000-06-12 20:42:46 +04:00
|
|
|
def inspect
|
|
|
|
"#<#{type} #{code}>"
|
|
|
|
end
|
|
|
|
|
2001-06-27 03:49:21 +04:00
|
|
|
def error!
|
2001-12-30 22:18:45 +03:00
|
|
|
raise @code_type.error_type.new( code + ' ' + msg.dump, self )
|
1999-09-22 11:32:33 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2000-03-31 17:02:40 +04:00
|
|
|
class ProtocolError < StandardError; end
|
|
|
|
class ProtoSyntaxError < ProtocolError; end
|
|
|
|
class ProtoFatalError < ProtocolError; end
|
|
|
|
class ProtoUnknownError < ProtocolError; end
|
|
|
|
class ProtoServerError < ProtocolError; end
|
|
|
|
class ProtoAuthError < ProtocolError; end
|
|
|
|
class ProtoCommandError < ProtocolError; end
|
|
|
|
class ProtoRetriableError < ProtocolError; end
|
|
|
|
ProtocRetryError = ProtoRetriableError
|
1999-12-17 18:00:13 +03:00
|
|
|
|
2000-06-09 11:53:59 +04:00
|
|
|
class ProtocolError
|
|
|
|
|
2001-06-27 03:49:21 +04:00
|
|
|
def initialize( msg, resp )
|
2000-06-09 11:53:59 +04:00
|
|
|
super msg
|
2001-06-27 03:49:21 +04:00
|
|
|
@response = resp
|
2000-06-09 11:53:59 +04:00
|
|
|
end
|
|
|
|
|
2002-02-19 15:33:52 +03:00
|
|
|
attr_reader :response
|
2001-06-27 03:49:21 +04:00
|
|
|
alias data response
|
2000-06-12 20:42:46 +04:00
|
|
|
|
|
|
|
def inspect
|
|
|
|
"#<#{type}>"
|
|
|
|
end
|
2000-06-09 11:53:59 +04:00
|
|
|
|
|
|
|
end
|
|
|
|
|
1999-12-17 18:00:13 +03:00
|
|
|
|
2000-03-31 17:02:40 +04:00
|
|
|
class Code
|
1999-12-17 18:00:13 +03:00
|
|
|
|
2000-03-31 17:02:40 +04:00
|
|
|
def initialize( paren, err )
|
2001-12-13 22:15:21 +03:00
|
|
|
@parents = [self] + paren
|
2000-03-31 17:02:40 +04:00
|
|
|
@err = err
|
|
|
|
end
|
1999-12-17 18:00:13 +03:00
|
|
|
|
2001-12-13 22:15:21 +03:00
|
|
|
def parents
|
|
|
|
@parents.dup
|
|
|
|
end
|
2000-03-31 17:02:40 +04:00
|
|
|
|
2000-06-12 20:42:46 +04:00
|
|
|
def inspect
|
2001-12-13 22:15:21 +03:00
|
|
|
"#<#{type} #{sprintf '0x%x', __id__}>"
|
2000-06-12 20:42:46 +04:00
|
|
|
end
|
|
|
|
|
2000-03-31 17:02:40 +04:00
|
|
|
def error_type
|
|
|
|
@err
|
|
|
|
end
|
|
|
|
|
|
|
|
def ===( response )
|
2001-12-13 22:15:21 +03:00
|
|
|
response.code_type.parents.each {|c| return true if c == self }
|
2000-03-31 17:02:40 +04:00
|
|
|
false
|
|
|
|
end
|
1999-12-22 16:49:13 +03:00
|
|
|
|
2000-03-31 17:02:40 +04:00
|
|
|
def mkchild( err = nil )
|
2001-12-13 22:15:21 +03:00
|
|
|
type.new( @parents, err || @err )
|
2000-03-31 17:02:40 +04:00
|
|
|
end
|
|
|
|
|
1999-09-22 11:32:33 +04:00
|
|
|
end
|
2000-03-31 17:02:40 +04:00
|
|
|
|
|
|
|
ReplyCode = Code.new( [], ProtoUnknownError )
|
2000-10-11 09:27:56 +04:00
|
|
|
InformationCode = ReplyCode.mkchild( ProtoUnknownError )
|
2000-03-31 17:02:40 +04:00
|
|
|
SuccessCode = ReplyCode.mkchild( ProtoUnknownError )
|
|
|
|
ContinueCode = ReplyCode.mkchild( ProtoUnknownError )
|
|
|
|
ErrorCode = ReplyCode.mkchild( ProtocolError )
|
|
|
|
SyntaxErrorCode = ErrorCode.mkchild( ProtoSyntaxError )
|
|
|
|
FatalErrorCode = ErrorCode.mkchild( ProtoFatalError )
|
|
|
|
ServerErrorCode = ErrorCode.mkchild( ProtoServerError )
|
2000-04-25 13:23:21 +04:00
|
|
|
AuthErrorCode = ErrorCode.mkchild( ProtoAuthError )
|
2000-03-31 17:02:40 +04:00
|
|
|
RetriableCode = ReplyCode.mkchild( ProtoRetriableError )
|
|
|
|
UnknownCode = ReplyCode.mkchild( ProtoUnknownError )
|
1999-09-22 11:32:33 +04:00
|
|
|
|
|
|
|
|
aamine
* lib/net/protocol.rb, smtp.rb, pop.rb, http.rb: 1.1.26.
* lib/net/protocol.rb, smtp.rb, pop.rb, http.rb:
add module Net::NetPrivate and its inner classes
{Read,Write}Adapter, Command, Socket,
SMTPCommand, POP3Command, APOPCommand, HTTPCommand
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@826 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2000-07-12 10:04:40 +04:00
|
|
|
class Command
|
|
|
|
|
|
|
|
def initialize( sock )
|
|
|
|
@socket = sock
|
|
|
|
@last_reply = nil
|
2001-12-30 22:18:45 +03:00
|
|
|
@atomic = false
|
aamine
* lib/net/protocol.rb, smtp.rb, pop.rb, http.rb: 1.1.26.
* lib/net/protocol.rb, smtp.rb, pop.rb, http.rb:
add module Net::NetPrivate and its inner classes
{Read,Write}Adapter, Command, Socket,
SMTPCommand, POP3Command, APOPCommand, HTTPCommand
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@826 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2000-07-12 10:04:40 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
attr_accessor :socket
|
|
|
|
attr_reader :last_reply
|
|
|
|
|
|
|
|
def inspect
|
|
|
|
"#<#{type}>"
|
|
|
|
end
|
|
|
|
|
2001-12-30 22:18:45 +03:00
|
|
|
# abstract quit()
|
aamine
* lib/net/protocol.rb, smtp.rb, pop.rb, http.rb: 1.1.26.
* lib/net/protocol.rb, smtp.rb, pop.rb, http.rb:
add module Net::NetPrivate and its inner classes
{Read,Write}Adapter, Command, Socket,
SMTPCommand, POP3Command, APOPCommand, HTTPCommand
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@826 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2000-07-12 10:04:40 +04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def check_reply( *oks )
|
2001-12-30 22:18:45 +03:00
|
|
|
@last_reply = get_reply()
|
|
|
|
reply_must @last_reply, *oks
|
aamine
* lib/net/protocol.rb, smtp.rb, pop.rb, http.rb: 1.1.26.
* lib/net/protocol.rb, smtp.rb, pop.rb, http.rb:
add module Net::NetPrivate and its inner classes
{Read,Write}Adapter, Command, Socket,
SMTPCommand, POP3Command, APOPCommand, HTTPCommand
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@826 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2000-07-12 10:04:40 +04:00
|
|
|
end
|
|
|
|
|
2001-12-30 22:18:45 +03:00
|
|
|
# abstract get_reply()
|
|
|
|
|
aamine
* lib/net/protocol.rb, smtp.rb, pop.rb, http.rb: 1.1.26.
* lib/net/protocol.rb, smtp.rb, pop.rb, http.rb:
add module Net::NetPrivate and its inner classes
{Read,Write}Adapter, Command, Socket,
SMTPCommand, POP3Command, APOPCommand, HTTPCommand
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@826 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2000-07-12 10:04:40 +04:00
|
|
|
def reply_must( rep, *oks )
|
|
|
|
oks.each do |i|
|
2001-12-30 22:18:45 +03:00
|
|
|
return rep if i === rep
|
aamine
* lib/net/protocol.rb, smtp.rb, pop.rb, http.rb: 1.1.26.
* lib/net/protocol.rb, smtp.rb, pop.rb, http.rb:
add module Net::NetPrivate and its inner classes
{Read,Write}Adapter, Command, Socket,
SMTPCommand, POP3Command, APOPCommand, HTTPCommand
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@826 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2000-07-12 10:04:40 +04:00
|
|
|
end
|
|
|
|
rep.error!
|
|
|
|
end
|
|
|
|
|
2000-12-22 21:40:55 +03:00
|
|
|
def getok( line, expect = SuccessCode )
|
aamine
* lib/net/protocol.rb, smtp.rb, pop.rb, http.rb: 1.1.26.
* lib/net/protocol.rb, smtp.rb, pop.rb, http.rb:
add module Net::NetPrivate and its inner classes
{Read,Write}Adapter, Command, Socket,
SMTPCommand, POP3Command, APOPCommand, HTTPCommand
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@826 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2000-07-12 10:04:40 +04:00
|
|
|
@socket.writeline line
|
2000-12-22 21:40:55 +03:00
|
|
|
check_reply expect
|
aamine
* lib/net/protocol.rb, smtp.rb, pop.rb, http.rb: 1.1.26.
* lib/net/protocol.rb, smtp.rb, pop.rb, http.rb:
add module Net::NetPrivate and its inner classes
{Read,Write}Adapter, Command, Socket,
SMTPCommand, POP3Command, APOPCommand, HTTPCommand
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@826 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2000-07-12 10:04:40 +04:00
|
|
|
end
|
|
|
|
|
2000-12-22 21:40:55 +03:00
|
|
|
#
|
|
|
|
# error handle
|
|
|
|
#
|
|
|
|
|
|
|
|
public
|
|
|
|
|
|
|
|
def critical?
|
2001-12-30 22:18:45 +03:00
|
|
|
@atomic
|
2000-12-22 21:40:55 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def error_ok
|
2001-12-30 22:18:45 +03:00
|
|
|
@atomic = false
|
2000-12-22 21:40:55 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2001-12-30 22:18:45 +03:00
|
|
|
def atomic
|
|
|
|
@atomic = true
|
2000-10-25 21:40:30 +04:00
|
|
|
ret = yield
|
2001-12-30 22:18:45 +03:00
|
|
|
@atomic = false
|
2000-10-25 21:40:30 +04:00
|
|
|
ret
|
aamine
* lib/net/protocol.rb, smtp.rb, pop.rb, http.rb: 1.1.26.
* lib/net/protocol.rb, smtp.rb, pop.rb, http.rb:
add module Net::NetPrivate and its inner classes
{Read,Write}Adapter, Command, Socket,
SMTPCommand, POP3Command, APOPCommand, HTTPCommand
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@826 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2000-07-12 10:04:40 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2002-02-19 15:33:52 +03:00
|
|
|
class InternetMessageIO
|
1999-12-17 18:00:13 +03:00
|
|
|
|
2001-12-30 22:18:45 +03:00
|
|
|
class << self
|
|
|
|
alias open new
|
|
|
|
end
|
2001-02-06 14:14:51 +03:00
|
|
|
|
2001-12-30 22:18:45 +03:00
|
|
|
def initialize( addr, port, otime = nil, rtime = nil, dout = nil )
|
|
|
|
@address = addr
|
|
|
|
@port = port
|
2001-02-06 14:14:51 +03:00
|
|
|
@read_timeout = rtime
|
2001-02-07 20:17:51 +03:00
|
|
|
@debugout = dout
|
1999-12-17 18:00:13 +03:00
|
|
|
|
2001-12-30 22:18:45 +03:00
|
|
|
@socket = nil
|
|
|
|
@rbuf = nil
|
1999-12-17 18:00:13 +03:00
|
|
|
|
2001-02-23 02:23:57 +03:00
|
|
|
connect otime
|
|
|
|
D 'opened'
|
|
|
|
end
|
|
|
|
|
2001-12-30 22:18:45 +03:00
|
|
|
attr_reader :address
|
|
|
|
attr_reader :port
|
1999-12-17 18:00:13 +03:00
|
|
|
|
2001-12-30 22:18:45 +03:00
|
|
|
def ip_address
|
|
|
|
@socket or return ''
|
|
|
|
@socket.addr[3]
|
1999-12-17 18:00:13 +03:00
|
|
|
end
|
|
|
|
|
2002-02-22 15:10:58 +03:00
|
|
|
attr_accessor :read_timeout
|
|
|
|
|
2001-12-30 22:18:45 +03:00
|
|
|
attr_reader :socket
|
2000-06-12 20:42:46 +04:00
|
|
|
|
2001-12-30 22:18:45 +03:00
|
|
|
def connect( otime )
|
|
|
|
D "opening connection to #{@address}..."
|
|
|
|
timeout( otime ) {
|
|
|
|
@socket = TCPsocket.new( @address, @port )
|
|
|
|
}
|
|
|
|
@rbuf = ''
|
1999-12-17 18:00:13 +03:00
|
|
|
end
|
2001-12-30 22:18:45 +03:00
|
|
|
private :connect
|
1999-12-17 18:00:13 +03:00
|
|
|
|
|
|
|
def close
|
2001-02-23 02:23:57 +03:00
|
|
|
if @socket then
|
|
|
|
@socket.close
|
|
|
|
D 'closed'
|
|
|
|
else
|
|
|
|
D 'close call for already closed socket'
|
|
|
|
end
|
|
|
|
@socket = nil
|
2001-12-13 22:15:21 +03:00
|
|
|
@rbuf = ''
|
1999-12-17 18:00:13 +03:00
|
|
|
end
|
|
|
|
|
2001-12-30 22:18:45 +03:00
|
|
|
def reopen( otime = nil )
|
|
|
|
D 'reopening...'
|
|
|
|
close
|
|
|
|
connect otime
|
|
|
|
D 'reopened'
|
1999-12-17 18:00:13 +03:00
|
|
|
end
|
|
|
|
|
2001-12-30 22:18:45 +03:00
|
|
|
def closed?
|
|
|
|
not @socket
|
1999-12-17 18:00:13 +03:00
|
|
|
end
|
2000-11-16 17:03:20 +03:00
|
|
|
|
2001-12-30 22:18:45 +03:00
|
|
|
def inspect
|
|
|
|
"#<#{type} #{closed? ? 'closed' : 'opened'}>"
|
1999-12-17 18:00:13 +03:00
|
|
|
end
|
2000-11-16 17:03:20 +03:00
|
|
|
|
2001-12-30 22:18:45 +03:00
|
|
|
###
|
|
|
|
### READ
|
|
|
|
###
|
1999-12-17 18:00:13 +03:00
|
|
|
|
2001-02-06 14:14:51 +03:00
|
|
|
public
|
2000-11-16 17:03:20 +03:00
|
|
|
|
2001-12-30 22:18:45 +03:00
|
|
|
def read( len, dest = '', ignore = false )
|
2001-02-23 02:23:57 +03:00
|
|
|
D_off "reading #{len} bytes..."
|
1999-12-17 18:00:13 +03:00
|
|
|
|
1999-09-22 11:32:33 +04:00
|
|
|
rsize = 0
|
2001-01-16 10:57:43 +03:00
|
|
|
begin
|
2001-12-13 22:15:21 +03:00
|
|
|
while rsize + @rbuf.size < len do
|
2001-12-30 22:18:45 +03:00
|
|
|
rsize += rbuf_moveto(dest, @rbuf.size)
|
2001-02-07 20:17:51 +03:00
|
|
|
rbuf_fill
|
2001-01-16 10:57:43 +03:00
|
|
|
end
|
2001-02-07 20:17:51 +03:00
|
|
|
rbuf_moveto dest, len - rsize
|
2001-01-16 10:57:43 +03:00
|
|
|
rescue EOFError
|
2001-12-30 22:18:45 +03:00
|
|
|
raise unless ignore
|
1999-09-22 11:32:33 +04:00
|
|
|
end
|
|
|
|
|
2001-02-23 02:23:57 +03:00
|
|
|
D_on "read #{len} bytes"
|
2000-03-31 17:02:40 +04:00
|
|
|
dest
|
1999-09-22 11:32:33 +04:00
|
|
|
end
|
|
|
|
|
2000-03-31 17:02:40 +04:00
|
|
|
def read_all( dest = '' )
|
2001-02-23 02:23:57 +03:00
|
|
|
D_off 'reading all...'
|
2000-01-21 15:52:24 +03:00
|
|
|
|
|
|
|
rsize = 0
|
|
|
|
begin
|
|
|
|
while true do
|
2001-12-30 22:18:45 +03:00
|
|
|
rsize += rbuf_moveto(dest, @rbuf.size)
|
2001-02-07 20:17:51 +03:00
|
|
|
rbuf_fill
|
2000-01-21 15:52:24 +03:00
|
|
|
end
|
|
|
|
rescue EOFError
|
|
|
|
;
|
|
|
|
end
|
|
|
|
|
2001-02-23 02:23:57 +03:00
|
|
|
D_on "read #{rsize} bytes"
|
2000-03-31 17:02:40 +04:00
|
|
|
dest
|
2000-01-21 15:52:24 +03:00
|
|
|
end
|
|
|
|
|
2001-12-30 22:18:45 +03:00
|
|
|
def readuntil( target, ignore = false )
|
2000-03-31 17:02:40 +04:00
|
|
|
dest = ''
|
2001-01-16 10:57:43 +03:00
|
|
|
begin
|
|
|
|
while true do
|
2001-12-30 22:18:45 +03:00
|
|
|
idx = @rbuf.index(target)
|
2001-01-16 10:57:43 +03:00
|
|
|
break if idx
|
2001-02-07 20:17:51 +03:00
|
|
|
rbuf_fill
|
2001-01-16 10:57:43 +03:00
|
|
|
end
|
2001-02-07 20:17:51 +03:00
|
|
|
rbuf_moveto dest, idx + target.size
|
2001-01-16 10:57:43 +03:00
|
|
|
rescue EOFError
|
2001-12-30 22:18:45 +03:00
|
|
|
raise unless ignore
|
2001-12-13 22:15:21 +03:00
|
|
|
rbuf_moveto dest, @rbuf.size
|
2001-01-16 10:57:43 +03:00
|
|
|
end
|
2000-03-31 17:02:40 +04:00
|
|
|
dest
|
1999-09-22 11:32:33 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
def readline
|
2001-12-30 22:18:45 +03:00
|
|
|
ret = readuntil("\n")
|
1999-09-22 11:32:33 +04:00
|
|
|
ret.chop!
|
1999-12-17 18:00:13 +03:00
|
|
|
ret
|
1999-09-22 11:32:33 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2002-02-19 15:33:52 +03:00
|
|
|
BLOCK_SIZE = 1024
|
1999-09-22 11:32:33 +04:00
|
|
|
|
2001-02-07 20:17:51 +03:00
|
|
|
def rbuf_fill
|
2002-02-07 23:22:39 +03:00
|
|
|
until IO.select [@socket], nil, nil, @read_timeout do
|
2001-02-06 14:14:51 +03:00
|
|
|
on_read_timeout
|
|
|
|
end
|
2001-12-13 22:15:21 +03:00
|
|
|
@rbuf << @socket.sysread(BLOCK_SIZE)
|
2001-02-06 14:14:51 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def on_read_timeout
|
|
|
|
raise TimeoutError, "socket read timeout (#{@read_timeout} sec)"
|
1999-09-22 11:32:33 +04:00
|
|
|
end
|
|
|
|
|
2001-02-07 20:17:51 +03:00
|
|
|
def rbuf_moveto( dest, len )
|
2001-12-13 22:15:21 +03:00
|
|
|
dest << (s = @rbuf.slice!(0, len))
|
2001-12-30 22:18:45 +03:00
|
|
|
@debugout << %Q[-> #{s.dump}\n] if @debugout
|
2000-01-21 15:52:24 +03:00
|
|
|
len
|
1999-12-17 18:00:13 +03:00
|
|
|
end
|
1999-09-22 11:32:33 +04:00
|
|
|
|
2002-02-19 15:33:52 +03:00
|
|
|
#
|
|
|
|
# message read
|
|
|
|
#
|
|
|
|
|
|
|
|
public
|
|
|
|
|
|
|
|
def read_message_to( dest )
|
|
|
|
D_off 'reading text...'
|
|
|
|
|
|
|
|
rsize = 0
|
|
|
|
while (str = readuntil("\r\n")) != ".\r\n" do
|
|
|
|
rsize += str.size
|
|
|
|
dest << str.sub(/\A\./, '')
|
|
|
|
end
|
|
|
|
|
|
|
|
D_on "read #{rsize} bytes"
|
|
|
|
dest
|
|
|
|
end
|
|
|
|
|
|
|
|
# private use only (cannot handle 'break')
|
|
|
|
def each_list_item
|
|
|
|
while (str = readuntil("\r\n")) != ".\r\n" do
|
|
|
|
yield str.chop
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
1999-09-22 11:32:33 +04:00
|
|
|
|
2001-12-30 22:18:45 +03:00
|
|
|
###
|
|
|
|
### WRITE
|
|
|
|
###
|
|
|
|
|
2001-02-06 14:14:51 +03:00
|
|
|
#
|
2002-02-19 15:33:52 +03:00
|
|
|
# basic write
|
2001-02-06 14:14:51 +03:00
|
|
|
#
|
2000-11-16 17:03:20 +03:00
|
|
|
|
1999-09-22 11:32:33 +04:00
|
|
|
public
|
|
|
|
|
2000-03-26 12:48:15 +04:00
|
|
|
def write( str )
|
2000-06-27 17:36:17 +04:00
|
|
|
writing {
|
2001-12-30 22:18:45 +03:00
|
|
|
do_write str
|
2000-06-27 17:36:17 +04:00
|
|
|
}
|
1999-09-22 11:32:33 +04:00
|
|
|
end
|
|
|
|
|
2000-03-26 12:48:15 +04:00
|
|
|
def writeline( str )
|
2000-06-27 17:36:17 +04:00
|
|
|
writing {
|
2001-12-30 22:18:45 +03:00
|
|
|
do_write str + "\r\n"
|
2000-06-27 17:36:17 +04:00
|
|
|
}
|
1999-09-22 11:32:33 +04:00
|
|
|
end
|
|
|
|
|
2002-02-19 15:33:52 +03:00
|
|
|
private
|
|
|
|
|
|
|
|
def writing
|
|
|
|
@writtensize = 0
|
|
|
|
@debugout << '<- ' if @debugout
|
|
|
|
yield
|
|
|
|
@socket.flush
|
|
|
|
@debugout << "\n" if @debugout
|
|
|
|
@writtensize
|
|
|
|
end
|
|
|
|
|
|
|
|
def do_write( str )
|
|
|
|
@debugout << str.dump if @debugout
|
|
|
|
@writtensize += (n = @socket.write(str))
|
|
|
|
n
|
1999-09-22 11:32:33 +04:00
|
|
|
end
|
|
|
|
|
2001-12-30 22:18:45 +03:00
|
|
|
#
|
2002-02-19 15:33:52 +03:00
|
|
|
# message write
|
2001-12-30 22:18:45 +03:00
|
|
|
#
|
|
|
|
|
|
|
|
public
|
|
|
|
|
2002-02-19 15:33:52 +03:00
|
|
|
def write_message( src )
|
2001-02-23 02:23:57 +03:00
|
|
|
D_off "writing text from #{src.type}"
|
1999-09-22 11:32:33 +04:00
|
|
|
|
2001-12-13 22:15:21 +03:00
|
|
|
wsize = using_each_crlf_line {
|
2002-02-19 15:33:52 +03:00
|
|
|
wpend_in src
|
|
|
|
}
|
|
|
|
|
|
|
|
D_on "wrote #{wsize} bytes text"
|
|
|
|
wsize
|
|
|
|
end
|
|
|
|
|
|
|
|
def through_message
|
|
|
|
D_off 'writing text from block'
|
|
|
|
|
|
|
|
wsize = using_each_crlf_line {
|
|
|
|
yield WriteAdapter.new(self, :wpend_in)
|
2000-06-27 17:36:17 +04:00
|
|
|
}
|
1999-09-22 11:32:33 +04:00
|
|
|
|
2001-02-23 02:23:57 +03:00
|
|
|
D_on "wrote #{wsize} bytes text"
|
1999-12-17 18:00:13 +03:00
|
|
|
wsize
|
1999-09-22 11:32:33 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2000-06-27 17:36:17 +04:00
|
|
|
def wpend_in( src )
|
|
|
|
line = nil
|
2000-08-16 23:26:07 +04:00
|
|
|
pre = @writtensize
|
2000-06-27 17:36:17 +04:00
|
|
|
each_crlf_line( src ) do |line|
|
|
|
|
do_write '.' if line[0] == ?.
|
|
|
|
do_write line
|
|
|
|
end
|
2000-08-16 23:26:07 +04:00
|
|
|
|
|
|
|
@writtensize - pre
|
2000-02-21 18:27:49 +03:00
|
|
|
end
|
|
|
|
|
2001-12-13 22:15:21 +03:00
|
|
|
def using_each_crlf_line
|
2000-06-27 17:36:17 +04:00
|
|
|
writing {
|
2001-12-30 22:18:45 +03:00
|
|
|
@wbuf = ''
|
2000-02-21 18:27:49 +03:00
|
|
|
|
2001-12-30 22:18:45 +03:00
|
|
|
yield
|
2000-02-21 18:27:49 +03:00
|
|
|
|
2001-12-30 22:18:45 +03:00
|
|
|
if not @wbuf.empty? then # unterminated last line
|
|
|
|
if @wbuf[-1] == ?\r then
|
|
|
|
@wbuf.chop!
|
|
|
|
end
|
|
|
|
@wbuf.concat "\r\n"
|
|
|
|
do_write @wbuf
|
|
|
|
elsif @writtensize == 0 then # empty src
|
|
|
|
do_write "\r\n"
|
2000-06-27 17:36:17 +04:00
|
|
|
end
|
2001-12-30 22:18:45 +03:00
|
|
|
do_write ".\r\n"
|
2000-02-21 18:27:49 +03:00
|
|
|
|
2001-12-30 22:18:45 +03:00
|
|
|
@wbuf = nil
|
2000-06-27 17:36:17 +04:00
|
|
|
}
|
|
|
|
end
|
1999-09-22 11:32:33 +04:00
|
|
|
|
2000-06-27 17:36:17 +04:00
|
|
|
def each_crlf_line( src )
|
2000-07-01 22:28:24 +04:00
|
|
|
str = m = beg = nil
|
1999-09-22 11:32:33 +04:00
|
|
|
|
2000-06-27 17:36:17 +04:00
|
|
|
adding( src ) do
|
2000-07-01 22:28:24 +04:00
|
|
|
beg = 0
|
2000-06-27 17:36:17 +04:00
|
|
|
buf = @wbuf
|
|
|
|
while buf.index( /\n|\r\n|\r/, beg ) do
|
2001-02-23 02:23:57 +03:00
|
|
|
m = Regexp.last_match
|
2000-06-27 17:36:17 +04:00
|
|
|
if m.begin(0) == buf.size - 1 and buf[-1] == ?\r then
|
2000-06-16 17:47:38 +04:00
|
|
|
# "...\r" : can follow "\n..."
|
|
|
|
break
|
|
|
|
end
|
2001-02-23 02:23:57 +03:00
|
|
|
str = buf[ beg ... m.begin(0) ]
|
2000-06-16 17:47:38 +04:00
|
|
|
str.concat "\r\n"
|
2000-06-27 17:36:17 +04:00
|
|
|
yield str
|
|
|
|
beg = m.end(0)
|
1999-09-22 11:32:33 +04:00
|
|
|
end
|
2001-02-23 02:23:57 +03:00
|
|
|
@wbuf = buf[ beg ... buf.size ]
|
1999-09-22 11:32:33 +04:00
|
|
|
end
|
2000-02-21 18:27:49 +03:00
|
|
|
end
|
|
|
|
|
2000-06-27 17:36:17 +04:00
|
|
|
def adding( src )
|
2000-02-21 18:27:49 +03:00
|
|
|
i = nil
|
|
|
|
|
|
|
|
case src
|
|
|
|
when String
|
2000-06-27 17:36:17 +04:00
|
|
|
0.step( src.size - 1, 2048 ) do |i|
|
|
|
|
@wbuf << src[i,2048]
|
2000-02-21 18:27:49 +03:00
|
|
|
yield
|
|
|
|
end
|
|
|
|
|
|
|
|
when File
|
2000-03-31 17:02:40 +04:00
|
|
|
while true do
|
2001-12-20 08:00:20 +03:00
|
|
|
i = src.read(2048)
|
2000-03-31 17:02:40 +04:00
|
|
|
break unless i
|
2000-06-27 17:36:17 +04:00
|
|
|
i[0,0] = @wbuf
|
|
|
|
@wbuf = i
|
2000-02-21 18:27:49 +03:00
|
|
|
yield
|
|
|
|
end
|
|
|
|
|
|
|
|
else
|
2000-06-27 17:36:17 +04:00
|
|
|
src.each do |i|
|
|
|
|
@wbuf << i
|
|
|
|
if @wbuf.size > 2048 then
|
|
|
|
yield
|
|
|
|
end
|
2000-02-21 18:27:49 +03:00
|
|
|
end
|
2001-01-13 22:07:15 +03:00
|
|
|
yield unless @wbuf.empty?
|
2000-02-21 18:27:49 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2001-12-30 22:18:45 +03:00
|
|
|
###
|
|
|
|
### DEBUG
|
|
|
|
###
|
|
|
|
|
|
|
|
private
|
2000-03-31 17:02:40 +04:00
|
|
|
|
2001-02-07 20:17:51 +03:00
|
|
|
def D_off( msg )
|
2001-02-23 02:23:57 +03:00
|
|
|
D msg
|
2001-02-07 20:17:51 +03:00
|
|
|
@savedo, @debugout = @debugout, nil
|
2000-03-31 17:02:40 +04:00
|
|
|
end
|
|
|
|
|
2001-02-07 20:17:51 +03:00
|
|
|
def D_on( msg )
|
|
|
|
@debugout = @savedo
|
2001-02-23 02:23:57 +03:00
|
|
|
D msg
|
|
|
|
end
|
|
|
|
|
|
|
|
def D( msg )
|
|
|
|
@debugout or return
|
|
|
|
@debugout << msg
|
|
|
|
@debugout << "\n"
|
2000-03-31 17:02:40 +04:00
|
|
|
end
|
2002-02-19 15:33:52 +03:00
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
class WriteAdapter
|
|
|
|
|
|
|
|
def initialize( sock, mid )
|
|
|
|
@socket = sock
|
|
|
|
@mid = mid
|
|
|
|
end
|
|
|
|
|
|
|
|
def inspect
|
|
|
|
"#<#{type} socket=#{@socket.inspect}>"
|
|
|
|
end
|
|
|
|
|
|
|
|
def write( str )
|
|
|
|
@socket.__send__ @mid, str
|
|
|
|
end
|
|
|
|
|
|
|
|
alias print write
|
|
|
|
|
|
|
|
def <<( str )
|
|
|
|
write str
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
|
|
|
def puts( str = '' )
|
|
|
|
write str.sub(/\n?/, "\n")
|
|
|
|
end
|
|
|
|
|
|
|
|
def printf( *args )
|
|
|
|
write sprintf(*args)
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
class ReadAdapter
|
|
|
|
|
|
|
|
def initialize( block )
|
|
|
|
@block = block
|
|
|
|
end
|
2000-03-31 17:02:40 +04:00
|
|
|
|
2002-02-19 15:33:52 +03:00
|
|
|
def inspect
|
|
|
|
"#<#{type}>"
|
|
|
|
end
|
|
|
|
|
|
|
|
def <<( str )
|
|
|
|
call_block str, &@block if @block
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def call_block( str )
|
|
|
|
yield str
|
|
|
|
end
|
|
|
|
|
1999-12-17 18:00:13 +03:00
|
|
|
end
|
1999-09-22 11:32:33 +04:00
|
|
|
|
aamine
* lib/net/protocol.rb, smtp.rb, pop.rb, http.rb: 1.1.26.
* lib/net/protocol.rb, smtp.rb, pop.rb, http.rb:
add module Net::NetPrivate and its inner classes
{Read,Write}Adapter, Command, Socket,
SMTPCommand, POP3Command, APOPCommand, HTTPCommand
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@826 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2000-07-12 10:04:40 +04:00
|
|
|
|
2001-12-13 22:15:21 +03:00
|
|
|
# for backward compatibility
|
|
|
|
module NetPrivate
|
2002-02-19 15:33:52 +03:00
|
|
|
Response = ::Net::Response
|
|
|
|
Command = ::Net::Command
|
|
|
|
Socket = ::Net::InternetMessageIO
|
|
|
|
BufferedSocket = ::Net::InternetMessageIO
|
|
|
|
WriteAdapter = ::Net::WriteAdapter
|
|
|
|
ReadAdapter = ::Net::ReadAdapter
|
2001-12-13 22:15:21 +03:00
|
|
|
end
|
2002-02-19 15:33:52 +03:00
|
|
|
BufferedSocket = ::Net::InternetMessageIO
|
2001-12-13 22:15:21 +03:00
|
|
|
|
1999-12-17 18:00:13 +03:00
|
|
|
end # module Net
|