1999-10-13 11:29:15 +04:00
|
|
|
=begin
|
|
|
|
|
2000-11-11 02:31:32 +03:00
|
|
|
= net/protocol.rb version 1.1.31
|
1999-10-13 11:29:15 +04:00
|
|
|
|
1999-12-17 18:00:13 +03:00
|
|
|
written by Minero Aoki <aamine@dp.u-netsurf.ne.jp>
|
1999-10-13 11:29:15 +04:00
|
|
|
|
2000-08-16 23:26:07 +04:00
|
|
|
This program is free software.
|
|
|
|
You can distribute/modify this program under
|
|
|
|
the terms of the Ruby Distribute License.
|
1999-10-13 11:29:15 +04:00
|
|
|
|
2000-09-21 10:58:01 +04:00
|
|
|
Japanese version of this document is in "net" full package.
|
|
|
|
You can get it from RAA
|
|
|
|
(Ruby Application Archive: http://www.ruby-lang.org/en/raa.html).
|
|
|
|
|
1999-09-22 11:32:33 +04:00
|
|
|
|
1999-12-29 14:14:04 +03:00
|
|
|
== Net::Protocol
|
1999-10-13 11:29:15 +04:00
|
|
|
|
2000-09-23 22:00:28 +04:00
|
|
|
the abstract class for Internet protocol
|
1999-10-13 11:29:15 +04:00
|
|
|
|
|
|
|
=== Super Class
|
|
|
|
|
|
|
|
Object
|
|
|
|
|
|
|
|
=== Class Methods
|
|
|
|
|
|
|
|
: new( address = 'localhost', port = nil )
|
2000-02-21 18:27:49 +03:00
|
|
|
This method Creates a new protocol object.
|
1999-10-13 11:29:15 +04:00
|
|
|
|
2000-05-18 12:57:37 +04:00
|
|
|
: start( address = 'localhost', port = nil, *protoargs )
|
|
|
|
: start( address = 'localhost', port = nil, *protoargs ) {|proto| .... }
|
|
|
|
This method creates a new Protocol object and opens a session.
|
|
|
|
equals to Net::Protocol.new( address, port ).start( *protoargs )
|
1999-10-13 11:29:15 +04:00
|
|
|
|
|
|
|
=== Methods
|
|
|
|
|
|
|
|
: address
|
|
|
|
the address of connecting server (FQDN).
|
|
|
|
|
|
|
|
: port
|
|
|
|
connecting port number
|
|
|
|
|
|
|
|
: start( *args )
|
2000-05-18 12:57:37 +04:00
|
|
|
: start( *args ) {|proto| .... }
|
|
|
|
This method starts protocol. If protocol was already started,
|
|
|
|
do nothing and returns false.
|
1999-10-13 11:29:15 +04:00
|
|
|
|
|
|
|
'*args' are specified in subclasses.
|
|
|
|
|
2000-09-21 10:58:01 +04:00
|
|
|
When is called with block, gives Protocol object to block and
|
2000-05-18 12:57:37 +04:00
|
|
|
close session when block finished.
|
|
|
|
|
1999-10-13 11:29:15 +04:00
|
|
|
: finish
|
1999-12-29 14:14:04 +03:00
|
|
|
This method ends protocol. If you call this method before protocol starts,
|
1999-10-13 11:29:15 +04:00
|
|
|
it only return false without doing anything.
|
|
|
|
|
|
|
|
: active?
|
|
|
|
true if session have been started
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2000-06-16 17:47:38 +04:00
|
|
|
require 'socket'
|
|
|
|
|
|
|
|
|
|
|
|
module Net
|
|
|
|
|
1999-12-29 14:14:04 +03:00
|
|
|
class Protocol
|
1999-12-17 18:00:13 +03:00
|
|
|
|
2000-11-11 02:31:32 +03:00
|
|
|
Version = '1.1.31'
|
2000-06-16 17:47:38 +04:00
|
|
|
|
2000-02-21 18:27:49 +03:00
|
|
|
|
1999-12-17 18:00:13 +03:00
|
|
|
class << self
|
|
|
|
|
|
|
|
def start( address = 'localhost', port = nil, *args )
|
1999-12-29 14:14:04 +03:00
|
|
|
instance = new( address, port )
|
1999-12-17 18:00:13 +03:00
|
|
|
|
2000-09-12 09:37:38 +04:00
|
|
|
if block_given? then
|
1999-12-29 14:14:04 +03:00
|
|
|
instance.start( *args ) { yield instance }
|
1999-12-17 18:00:13 +03:00
|
|
|
else
|
2000-08-16 23:26:07 +04:00
|
|
|
instance.start( *args )
|
1999-12-29 14:14:04 +03:00
|
|
|
instance
|
1999-12-17 18:00:13 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
1999-12-29 14:14:04 +03:00
|
|
|
def protocol_param( name, val )
|
1999-12-17 18:00:13 +03:00
|
|
|
module_eval %-
|
|
|
|
def self.#{name.id2name}
|
|
|
|
#{val}
|
|
|
|
end
|
|
|
|
-
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
# sub-class requirements
|
|
|
|
#
|
1999-12-29 14:14:04 +03:00
|
|
|
# protocol_param command_type
|
|
|
|
# protocol_param port
|
1999-12-17 18:00:13 +03:00
|
|
|
#
|
|
|
|
# private method do_start (optional)
|
|
|
|
# private method do_finish (optional)
|
|
|
|
#
|
|
|
|
|
1999-12-29 14:14:04 +03:00
|
|
|
protocol_param :port, 'nil'
|
|
|
|
protocol_param :command_type, 'nil'
|
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
|
|
|
protocol_param :socket_type, '::Net::NetPrivate::Socket'
|
1999-12-17 18:00:13 +03:00
|
|
|
|
|
|
|
|
1999-12-29 14:14:04 +03:00
|
|
|
def initialize( addr = nil, port = nil )
|
|
|
|
@address = addr || 'localhost'
|
2000-03-27 19:52:27 +04:00
|
|
|
@port = port || type.port
|
1999-12-17 18:00:13 +03:00
|
|
|
|
|
|
|
@active = false
|
|
|
|
@pipe = nil
|
|
|
|
|
1999-12-29 14:14:04 +03:00
|
|
|
@command = nil
|
1999-12-17 18:00:13 +03:00
|
|
|
@socket = nil
|
|
|
|
end
|
|
|
|
|
2000-03-05 13:25:53 +03:00
|
|
|
attr_reader :address, :port,
|
|
|
|
:command, :socket
|
1999-12-17 18:00:13 +03:00
|
|
|
|
2000-06-12 20:42:46 +04:00
|
|
|
def inspect
|
|
|
|
"#<#{type} #{address}:#{port} open=#{active?}>"
|
|
|
|
end
|
|
|
|
|
1999-12-17 18:00:13 +03:00
|
|
|
|
1999-09-22 11:32:33 +04:00
|
|
|
def start( *args )
|
|
|
|
return false if active?
|
|
|
|
|
1999-12-17 18:00:13 +03:00
|
|
|
begin
|
2000-03-27 19:52:27 +04:00
|
|
|
connect
|
2000-08-16 23:26:07 +04:00
|
|
|
do_start( *args )
|
2000-03-27 19:52:27 +04:00
|
|
|
@active = true
|
2000-09-12 09:37:38 +04:00
|
|
|
yield self if block_given?
|
1999-12-17 18:00:13 +03:00
|
|
|
ensure
|
2000-09-12 09:37:38 +04:00
|
|
|
finish if block_given?
|
1999-09-22 11:32:33 +04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def finish
|
2000-07-03 13:20:35 +04:00
|
|
|
return false unless active?
|
1999-12-17 18:00:13 +03:00
|
|
|
|
2000-03-27 19:52:27 +04:00
|
|
|
do_finish
|
2000-03-05 13:25:53 +03:00
|
|
|
disconnect
|
|
|
|
@active = false
|
2000-07-03 13:20:35 +04:00
|
|
|
true
|
1999-09-22 11:32:33 +04:00
|
|
|
end
|
|
|
|
|
1999-12-17 18:00:13 +03:00
|
|
|
def active?
|
|
|
|
@active
|
|
|
|
end
|
1999-09-22 11:32:33 +04:00
|
|
|
|
1999-12-17 18:00:13 +03:00
|
|
|
def set_pipe( arg ) # un-documented
|
1999-10-13 11:29:15 +04:00
|
|
|
@pipe = arg
|
|
|
|
end
|
|
|
|
|
1999-12-17 18:00:13 +03:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
|
|
|
|
def do_start
|
|
|
|
end
|
|
|
|
|
|
|
|
def do_finish
|
2000-03-05 13:25:53 +03:00
|
|
|
@command.quit
|
1999-12-17 18:00:13 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2000-03-27 19:52:27 +04:00
|
|
|
def connect( addr = @address, port = @port )
|
|
|
|
@socket = type.socket_type.open( addr, port, @pipe )
|
|
|
|
@command = type.command_type.new( @socket )
|
1999-12-17 18:00:13 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def disconnect
|
1999-12-29 14:14:04 +03:00
|
|
|
@command = nil
|
2000-03-05 13:25:53 +03:00
|
|
|
if @socket and not @socket.closed? then
|
|
|
|
@socket.close
|
|
|
|
end
|
1999-12-29 14:14:04 +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
|
|
|
|
2000-03-31 17:02:40 +04:00
|
|
|
def initialize( ctype, cno, msg )
|
|
|
|
@code_type = ctype
|
|
|
|
@code = cno
|
|
|
|
@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
|
|
|
|
|
2000-06-09 11:53:59 +04:00
|
|
|
def error!( data = nil )
|
|
|
|
raise code_type.error_type.new( code + ' ' + Net.quote(msg), data )
|
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
|
|
|
|
|
2000-10-11 09:27:56 +04:00
|
|
|
def initialize( msg, data = nil )
|
2000-06-09 11:53:59 +04:00
|
|
|
super msg
|
|
|
|
@data = data
|
|
|
|
end
|
|
|
|
|
|
|
|
attr :data
|
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 )
|
|
|
|
@parents = paren
|
|
|
|
@err = err
|
1999-12-17 18:00:13 +03:00
|
|
|
|
2000-03-31 17:02:40 +04:00
|
|
|
@parents.push self
|
|
|
|
end
|
1999-12-17 18:00:13 +03:00
|
|
|
|
2000-03-31 17:02:40 +04:00
|
|
|
attr_reader :parents
|
|
|
|
|
2000-06-12 20:42:46 +04:00
|
|
|
def inspect
|
|
|
|
"#<#{type}>"
|
|
|
|
end
|
|
|
|
|
2000-03-31 17:02:40 +04:00
|
|
|
def error_type
|
|
|
|
@err
|
|
|
|
end
|
|
|
|
|
|
|
|
def ===( response )
|
|
|
|
response.code_type.parents.reverse_each {|i| return true if i == self }
|
|
|
|
false
|
|
|
|
end
|
1999-12-22 16:49:13 +03:00
|
|
|
|
2000-03-31 17:02:40 +04:00
|
|
|
def mkchild( err = nil )
|
|
|
|
type.new( @parents + [self], err || @err )
|
|
|
|
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
|
|
|
|
|
|
|
|
2000-02-21 18:27:49 +03: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
|
|
|
module NetPrivate
|
|
|
|
|
|
|
|
|
2000-02-21 18:27:49 +03:00
|
|
|
class WriteAdapter
|
|
|
|
|
|
|
|
def initialize( sock, mid )
|
|
|
|
@sock = sock
|
|
|
|
@mid = mid
|
|
|
|
end
|
|
|
|
|
2000-06-12 20:42:46 +04:00
|
|
|
def inspect
|
|
|
|
"#<#{type}>"
|
|
|
|
end
|
|
|
|
|
2000-02-21 18:27:49 +03:00
|
|
|
def write( str )
|
|
|
|
@sock.__send__ @mid, str
|
|
|
|
end
|
|
|
|
alias << write
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
class ReadAdapter
|
|
|
|
|
|
|
|
def initialize( block )
|
|
|
|
@block = block
|
|
|
|
end
|
|
|
|
|
2000-06-12 20:42:46 +04:00
|
|
|
def inspect
|
|
|
|
"#<#{type}>"
|
|
|
|
end
|
|
|
|
|
2000-02-21 18:27:49 +03:00
|
|
|
def <<( str )
|
2000-11-16 17:03:20 +03:00
|
|
|
callblock( str, &@block ) if @block
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def callblock( str )
|
|
|
|
begin
|
|
|
|
user_break = true
|
|
|
|
yield str
|
|
|
|
user_break = false
|
|
|
|
rescue Exception
|
|
|
|
user_break = false
|
|
|
|
raise
|
|
|
|
ensure
|
|
|
|
if user_break then
|
|
|
|
@block = nil
|
|
|
|
return # stop break
|
|
|
|
end
|
|
|
|
end
|
2000-02-21 18:27:49 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2000-11-16 17:03:20 +03: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
|
|
|
|
@critical = false
|
|
|
|
end
|
|
|
|
|
|
|
|
attr_accessor :socket
|
|
|
|
attr_reader :last_reply
|
|
|
|
|
|
|
|
def inspect
|
|
|
|
"#<#{type}>"
|
|
|
|
end
|
|
|
|
|
|
|
|
# abstract quit
|
|
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
# abstract get_reply()
|
|
|
|
|
|
|
|
def check_reply( *oks )
|
|
|
|
@last_reply = get_reply
|
|
|
|
reply_must( @last_reply, *oks )
|
|
|
|
end
|
|
|
|
|
|
|
|
def reply_must( rep, *oks )
|
|
|
|
oks.each do |i|
|
|
|
|
if i === rep then
|
|
|
|
return rep
|
|
|
|
end
|
|
|
|
end
|
|
|
|
rep.error!
|
|
|
|
end
|
|
|
|
|
|
|
|
def getok( line, ok = SuccessCode )
|
|
|
|
@socket.writeline line
|
|
|
|
check_reply ok
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def critical
|
|
|
|
return if @critical
|
|
|
|
@critical = true
|
2000-10-25 21:40:30 +04:00
|
|
|
ret = yield
|
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
|
|
|
@critical = 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
|
|
|
|
|
|
|
|
def critical?
|
|
|
|
@critical
|
|
|
|
end
|
|
|
|
|
|
|
|
def begin_critical
|
|
|
|
ret = @critical
|
|
|
|
@critical = true
|
|
|
|
not ret
|
|
|
|
end
|
|
|
|
|
|
|
|
def end_critical
|
|
|
|
@critical = false
|
|
|
|
end
|
|
|
|
|
2000-11-07 14:27:16 +03:00
|
|
|
def error_ok
|
2000-10-25 21:40:30 +04:00
|
|
|
@critical = false
|
|
|
|
end
|
2000-11-07 14:27:16 +03:00
|
|
|
public :error_ok
|
2000-10-25 21:40:30 +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
|
|
|
end
|
|
|
|
|
|
|
|
|
2000-02-21 18:27:49 +03:00
|
|
|
class Socket
|
1999-12-17 18:00:13 +03:00
|
|
|
|
|
|
|
def initialize( addr, port, pipe = nil )
|
|
|
|
@addr = addr
|
|
|
|
@port = port
|
|
|
|
@pipe = pipe
|
2000-03-31 17:02:40 +04:00
|
|
|
@prepipe = nil
|
1999-12-17 18:00:13 +03:00
|
|
|
|
|
|
|
@closed = true
|
|
|
|
@ipaddr = ''
|
|
|
|
@sending = ''
|
|
|
|
@buffer = ''
|
|
|
|
|
|
|
|
@socket = TCPsocket.new( addr, port )
|
|
|
|
@closed = false
|
|
|
|
@ipaddr = @socket.addr[3]
|
|
|
|
end
|
|
|
|
|
|
|
|
attr :pipe, true
|
|
|
|
|
|
|
|
class << self
|
|
|
|
alias open new
|
|
|
|
end
|
|
|
|
|
2000-06-12 20:42:46 +04:00
|
|
|
def inspect
|
|
|
|
"#<#{type} open=#{!@closed}>"
|
|
|
|
end
|
|
|
|
|
1999-12-17 18:00:13 +03:00
|
|
|
def reopen
|
|
|
|
unless closed? then
|
|
|
|
@socket.close
|
2000-01-21 15:52:24 +03:00
|
|
|
@buffer = ''
|
1999-12-17 18:00:13 +03:00
|
|
|
end
|
|
|
|
@socket = TCPsocket.new( @addr, @port )
|
|
|
|
end
|
|
|
|
|
|
|
|
attr :socket, true
|
|
|
|
|
|
|
|
def close
|
|
|
|
@socket.close
|
|
|
|
@closed = true
|
|
|
|
end
|
|
|
|
|
|
|
|
def closed?
|
|
|
|
@closed
|
|
|
|
end
|
|
|
|
|
|
|
|
def address
|
|
|
|
@addr.dup
|
|
|
|
end
|
2000-11-16 17:03:20 +03:00
|
|
|
|
1999-12-17 18:00:13 +03:00
|
|
|
alias addr address
|
|
|
|
|
2000-03-05 13:25:53 +03:00
|
|
|
attr_reader :port
|
1999-12-17 18:00:13 +03:00
|
|
|
|
|
|
|
def ip_address
|
|
|
|
@ipaddr.dup
|
|
|
|
end
|
2000-11-16 17:03:20 +03:00
|
|
|
|
1999-12-17 18:00:13 +03:00
|
|
|
alias ipaddr ip_address
|
|
|
|
|
2000-03-05 13:25:53 +03:00
|
|
|
attr_reader :sending
|
1999-12-17 18:00:13 +03:00
|
|
|
|
|
|
|
|
2000-11-16 17:03:20 +03:00
|
|
|
###
|
|
|
|
### read
|
|
|
|
###
|
|
|
|
|
|
|
|
CRLF = "\r\n"
|
1999-12-17 18:00:13 +03:00
|
|
|
|
2000-03-31 17:02:40 +04:00
|
|
|
def read( len, dest = '' )
|
|
|
|
@pipe << "reading #{len} bytes...\n" if @pipe; pipeoff
|
1999-12-17 18:00:13 +03:00
|
|
|
|
1999-09-22 11:32:33 +04:00
|
|
|
rsize = 0
|
|
|
|
while rsize + @buffer.size < len do
|
2000-03-31 17:02:40 +04:00
|
|
|
rsize += writeinto( dest, @buffer.size )
|
1999-09-22 11:32:33 +04:00
|
|
|
fill_rbuf
|
|
|
|
end
|
2000-03-31 17:02:40 +04:00
|
|
|
writeinto( dest, len - rsize )
|
1999-09-22 11:32:33 +04:00
|
|
|
|
2000-03-31 17:02:40 +04:00
|
|
|
@pipe << "read #{len} bytes\n" if pipeon
|
|
|
|
dest
|
1999-09-22 11:32:33 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2000-03-31 17:02:40 +04:00
|
|
|
def read_all( dest = '' )
|
|
|
|
@pipe << "reading all...\n" if @pipe; pipeoff
|
2000-01-21 15:52:24 +03:00
|
|
|
|
|
|
|
rsize = 0
|
|
|
|
begin
|
|
|
|
while true do
|
2000-03-31 17:02:40 +04:00
|
|
|
rsize += writeinto( dest, @buffer.size )
|
2000-01-21 15:52:24 +03:00
|
|
|
fill_rbuf
|
|
|
|
end
|
|
|
|
rescue EOFError
|
|
|
|
;
|
|
|
|
end
|
|
|
|
|
2000-03-31 17:02:40 +04:00
|
|
|
@pipe << "read #{rsize} bytes\n" if pipeon
|
|
|
|
dest
|
2000-01-21 15:52:24 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
|
1999-09-22 11:32:33 +04:00
|
|
|
def readuntil( target )
|
2000-03-31 17:02:40 +04:00
|
|
|
while true do
|
|
|
|
idx = @buffer.index( target )
|
|
|
|
break if idx
|
1999-09-22 11:32:33 +04:00
|
|
|
fill_rbuf
|
|
|
|
end
|
|
|
|
|
2000-03-31 17:02:40 +04:00
|
|
|
dest = ''
|
|
|
|
writeinto( dest, idx + target.size )
|
|
|
|
dest
|
1999-09-22 11:32:33 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def readline
|
2000-06-16 17:47:38 +04:00
|
|
|
ret = readuntil( "\r\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
|
|
|
|
|
|
|
|
|
2000-03-31 17:02:40 +04:00
|
|
|
def read_pendstr( dest )
|
|
|
|
@pipe << "reading text...\n" if @pipe; pipeoff
|
1999-09-22 11:32:33 +04:00
|
|
|
|
|
|
|
rsize = 0
|
|
|
|
|
2000-11-16 17:03:20 +03:00
|
|
|
while (str = readuntil("\r\n")) != ".\r\n" do
|
1999-09-22 11:32:33 +04:00
|
|
|
rsize += str.size
|
2000-06-16 17:47:38 +04:00
|
|
|
str.gsub!( /\A\./, '' )
|
1999-09-22 11:32:33 +04:00
|
|
|
dest << str
|
|
|
|
end
|
|
|
|
|
2000-03-31 17:02:40 +04:00
|
|
|
@pipe << "read #{rsize} bytes\n" if pipeon
|
1999-12-17 18:00:13 +03:00
|
|
|
dest
|
1999-09-22 11:32:33 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2000-11-16 17:03:20 +03:00
|
|
|
# private use only (can not handle 'break')
|
1999-09-22 11:32:33 +04:00
|
|
|
def read_pendlist
|
2000-03-31 17:02:40 +04:00
|
|
|
@pipe << "reading list...\n" if @pipe; pipeoff
|
1999-10-13 11:29:15 +04:00
|
|
|
|
1999-09-22 11:32:33 +04:00
|
|
|
str = nil
|
2000-11-16 17:03:20 +03:00
|
|
|
i = 0
|
|
|
|
while (str = readuntil("\r\n")) != ".\r\n" do
|
1999-09-22 11:32:33 +04:00
|
|
|
str.chop!
|
2000-11-16 17:03:20 +03:00
|
|
|
yield str
|
|
|
|
i += 1
|
1999-09-22 11:32:33 +04:00
|
|
|
end
|
|
|
|
|
2000-11-16 17:03:20 +03:00
|
|
|
@pipe << "read #{i} items\n" if pipeon
|
1999-09-22 11:32:33 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
|
|
|
|
READ_BLOCK = 1024 * 8
|
|
|
|
|
|
|
|
def fill_rbuf
|
|
|
|
@buffer << @socket.sysread( READ_BLOCK )
|
|
|
|
end
|
|
|
|
|
2000-03-31 17:02:40 +04:00
|
|
|
def writeinto( dest, len )
|
1999-09-22 11:32:33 +04:00
|
|
|
bsi = @buffer.size
|
2000-03-31 17:02:40 +04:00
|
|
|
dest << @buffer[ 0, len ]
|
1999-09-22 11:32:33 +04:00
|
|
|
@buffer = @buffer[ len, bsi - len ]
|
|
|
|
|
2000-03-31 17:02:40 +04:00
|
|
|
@pipe << %{read "#{Net.quote dest}"\n} if @pipe
|
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
|
|
|
|
|
|
|
|
2000-11-16 17:03:20 +03:00
|
|
|
###
|
|
|
|
### write
|
|
|
|
###
|
|
|
|
|
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 {
|
|
|
|
do_write str
|
|
|
|
}
|
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 {
|
|
|
|
do_write str
|
|
|
|
do_write "\r\n"
|
|
|
|
}
|
1999-09-22 11:32:33 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2000-03-31 17:02:40 +04:00
|
|
|
def write_bin( src, block )
|
2000-06-27 17:36:17 +04:00
|
|
|
writing {
|
|
|
|
if block then
|
|
|
|
block.call WriteAdapter.new( self, :do_write )
|
|
|
|
else
|
|
|
|
src.each do |bin|
|
|
|
|
do_write bin
|
|
|
|
end
|
2000-03-26 12:48:15 +04:00
|
|
|
end
|
2000-06-27 17:36:17 +04:00
|
|
|
}
|
1999-09-22 11:32:33 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2000-03-31 17:02:40 +04:00
|
|
|
def write_pendstr( src, block )
|
|
|
|
@pipe << "writing text from #{src.type}\n" if @pipe; pipeoff
|
1999-09-22 11:32:33 +04:00
|
|
|
|
2000-06-27 17:36:17 +04:00
|
|
|
wsize = use_each_crlf_line {
|
|
|
|
if block then
|
|
|
|
block.call WriteAdapter.new( self, :wpend_in )
|
|
|
|
else
|
|
|
|
wpend_in src
|
|
|
|
end
|
|
|
|
}
|
1999-09-22 11:32:33 +04:00
|
|
|
|
2000-05-17 22:39:43 +04:00
|
|
|
@pipe << "wrote #{wsize} bytes text\n" if pipeon
|
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
|
|
|
|
|
2000-06-27 17:36:17 +04:00
|
|
|
def use_each_crlf_line
|
|
|
|
writing {
|
|
|
|
@wbuf = ''
|
2000-02-21 18:27:49 +03:00
|
|
|
|
2000-06-27 17:36:17 +04:00
|
|
|
yield
|
2000-02-21 18:27:49 +03:00
|
|
|
|
2000-06-27 17:36:17 +04:00
|
|
|
if not @wbuf.empty? then # un-terminated 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"
|
|
|
|
end
|
|
|
|
do_write ".\r\n"
|
2000-02-21 18:27:49 +03:00
|
|
|
|
2000-06-27 17:36:17 +04:00
|
|
|
@wbuf = nil
|
|
|
|
}
|
|
|
|
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
|
|
|
|
m = $~
|
|
|
|
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
|
2000-06-27 17:36:17 +04:00
|
|
|
str = buf[ beg, m.begin(0) - beg ]
|
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
|
2000-06-27 17:36:17 +04:00
|
|
|
@wbuf = buf[ beg, buf.size - beg ]
|
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
|
2000-06-27 17:36:17 +04: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
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
1999-09-22 11:32:33 +04:00
|
|
|
|
2000-06-27 17:36:17 +04:00
|
|
|
def writing
|
1999-09-22 11:32:33 +04:00
|
|
|
@writtensize = 0
|
|
|
|
@sending = ''
|
2000-06-27 17:36:17 +04:00
|
|
|
|
|
|
|
yield
|
|
|
|
|
|
|
|
if @pipe then
|
|
|
|
@pipe << 'write "'
|
|
|
|
@pipe << @sending
|
|
|
|
@pipe << "\"\n"
|
|
|
|
end
|
|
|
|
@socket.flush
|
|
|
|
@writtensize
|
1999-09-22 11:32:33 +04:00
|
|
|
end
|
|
|
|
|
2000-06-27 17:36:17 +04:00
|
|
|
def do_write( arg )
|
1999-12-17 18:00:13 +03:00
|
|
|
if @pipe or @sending.size < 128 then
|
|
|
|
@sending << Net.quote( arg )
|
1999-09-22 11:32:33 +04:00
|
|
|
else
|
|
|
|
@sending << '...' unless @sending[-1] == ?.
|
|
|
|
end
|
1999-10-13 11:29:15 +04:00
|
|
|
|
1999-09-22 11:32:33 +04:00
|
|
|
s = @socket.write( arg )
|
|
|
|
@writtensize += s
|
1999-12-17 18:00:13 +03:00
|
|
|
s
|
1999-09-22 11:32:33 +04:00
|
|
|
end
|
|
|
|
|
2000-03-31 17:02:40 +04:00
|
|
|
|
|
|
|
def pipeoff
|
|
|
|
@prepipe = @pipe
|
|
|
|
@pipe = nil
|
|
|
|
@prepipe
|
|
|
|
end
|
|
|
|
|
|
|
|
def pipeon
|
|
|
|
@pipe = @prepipe
|
|
|
|
@prepipe = nil
|
|
|
|
@pipe
|
|
|
|
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
|
|
|
end # module Net::NetPrivate
|
|
|
|
|
|
|
|
|
1999-12-17 18:00:13 +03:00
|
|
|
def Net.quote( str )
|
|
|
|
str = str.gsub( "\n", '\\n' )
|
|
|
|
str.gsub!( "\r", '\\r' )
|
|
|
|
str.gsub!( "\t", '\\t' )
|
|
|
|
str
|
1999-09-22 11:32:33 +04:00
|
|
|
end
|
|
|
|
|
1999-12-17 18:00:13 +03:00
|
|
|
end # module Net
|