mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
aamine
* lib/net/protocol.rb: set @closed false in Socket#reopen. * lib/net/pop.rb: add POP3.foreach, delete_all. * lib/net/pop.rb: add POP3#delete_all. * lib/net/http.rb: add HTTP.version_1_1, version_1_2 * lib/net/http.rb: refactoring. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@1071 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
e6bf7809f3
commit
79633d3bb8
5 changed files with 179 additions and 62 deletions
12
ChangeLog
12
ChangeLog
|
@ -1,3 +1,15 @@
|
||||||
|
Sat Dec 23 03:44:16 2000 Minero Aoki <aamine@dp.u-netsurf.ne.jp>
|
||||||
|
|
||||||
|
* lib/net/protocol.rb: set @closed false in Socket#reopen.
|
||||||
|
|
||||||
|
* lib/net/pop.rb: add POP3.foreach, delete_all.
|
||||||
|
|
||||||
|
* lib/net/pop.rb: add POP3#delete_all.
|
||||||
|
|
||||||
|
* lib/net/http.rb: add HTTP.version_1_1, version_1_2
|
||||||
|
|
||||||
|
* lib/net/http.rb: refactoring.
|
||||||
|
|
||||||
Fri Dec 22 17:59:30 2000 Yukihiro Matsumoto <matz@ruby-lang.org>
|
Fri Dec 22 17:59:30 2000 Yukihiro Matsumoto <matz@ruby-lang.org>
|
||||||
|
|
||||||
* stable version 1.6.2 released.
|
* stable version 1.6.2 released.
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
=begin
|
=begin
|
||||||
|
|
||||||
= net/http.rb version 1.1.31
|
= net/http.rb version 1.1.32
|
||||||
|
|
||||||
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".
|
||||||
|
@ -55,8 +55,8 @@ You can get it from RAA
|
||||||
: start {|http| .... }
|
: start {|http| .... }
|
||||||
creates a new Net::HTTP object and starts HTTP session.
|
creates a new Net::HTTP object and starts HTTP session.
|
||||||
|
|
||||||
When this method is called with block, gives HTTP object to block
|
When this method is called with block, gives a HTTP object to block
|
||||||
and close HTTP session after block call finished.
|
and close HTTP session after returning from the block.
|
||||||
|
|
||||||
: proxy?
|
: proxy?
|
||||||
true if self is a HTTP proxy class
|
true if self is a HTTP proxy class
|
||||||
|
@ -233,16 +233,16 @@ All "key" is case-insensitive.
|
||||||
|
|
||||||
= http.rb version 1.2 features
|
= http.rb version 1.2 features
|
||||||
|
|
||||||
You can use 1.2 features by calling HTTP.new_implementation. And
|
You can use 1.2 features by calling HTTP.version_1_2. And
|
||||||
calling Net::HTTP.old_implementation allows to use 1.1 features.
|
calling Net::HTTP.version_1_1 allows to use 1.1 features.
|
||||||
|
|
||||||
# example
|
# example
|
||||||
HTTP.start {|http1| ...(http1 has 1.1 features)... }
|
HTTP.start {|http1| ...(http1 has 1.1 features)... }
|
||||||
|
|
||||||
HTTP.new_implementation
|
HTTP.version_1_2
|
||||||
HTTP.start {|http2| ...(http2 has 1.2 features)... }
|
HTTP.start {|http2| ...(http2 has 1.2 features)... }
|
||||||
|
|
||||||
HTTP.old_implementation
|
HTTP.version_1_1
|
||||||
HTTP.start {|http3| ...(http3 has 1.1 features)... }
|
HTTP.start {|http3| ...(http3 has 1.1 features)... }
|
||||||
|
|
||||||
== Method (only diff to 1.1)
|
== Method (only diff to 1.1)
|
||||||
|
@ -293,7 +293,7 @@ module Net
|
||||||
def new( address = nil, port = nil, p_addr = nil, p_port = nil )
|
def new( address = nil, port = nil, p_addr = nil, p_port = nil )
|
||||||
c = p_addr ? self::Proxy(p_addr, p_port) : self
|
c = p_addr ? self::Proxy(p_addr, p_port) : self
|
||||||
i = c.orig_new( address, port )
|
i = c.orig_new( address, port )
|
||||||
setvar i
|
setimplv i
|
||||||
i
|
i
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -340,17 +340,17 @@ module Net
|
||||||
|
|
||||||
#class << self
|
#class << self
|
||||||
|
|
||||||
def self.new_implementation
|
def self.version_1_2
|
||||||
@@newimpl = true
|
@@newimpl = true
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.old_implementation
|
def self.version_1_1
|
||||||
@@newimpl = false
|
@@newimpl = false
|
||||||
end
|
end
|
||||||
|
|
||||||
#private
|
#private
|
||||||
|
|
||||||
def self.setvar( obj )
|
def self.setimplv( obj )
|
||||||
f = @@newimpl
|
f = @@newimpl
|
||||||
obj.instance_eval { @newimpl = f }
|
obj.instance_eval { @newimpl = f }
|
||||||
end
|
end
|
||||||
|
@ -575,7 +575,7 @@ module Net
|
||||||
end
|
end
|
||||||
|
|
||||||
def edit_path( path )
|
def edit_path( path )
|
||||||
'http://' + address + (port == HTTP.port ? '' : ":#{port}") + path
|
'http://' + address + (port == type.port ? '' : ":#{port}") + path
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
128
lib/net/pop.rb
128
lib/net/pop.rb
|
@ -1,6 +1,6 @@
|
||||||
=begin
|
=begin
|
||||||
|
|
||||||
= net/pop.rb version 1.1.31
|
= net/pop.rb version 1.1.32
|
||||||
|
|
||||||
written by Minero Aoki <aamine@dp.u-netsurf.ne.jp>
|
written by Minero Aoki <aamine@dp.u-netsurf.ne.jp>
|
||||||
|
|
||||||
|
@ -25,9 +25,45 @@ Net::Protocol
|
||||||
creates a new Net::POP3 object.
|
creates a new Net::POP3 object.
|
||||||
This method does not open TCP connection yet.
|
This method does not open TCP connection yet.
|
||||||
|
|
||||||
: start( address = 'localhost', port = 110, *protoargs )
|
: start( address = 'localhost', port = 110, account, password )
|
||||||
: start( address = 'localhost', port = 110, *protoargs ) {|pop| .... }
|
: start( address = 'localhost', port = 110, account, password ) {|pop| .... }
|
||||||
equals to Net::POP3.new( address, port ).start( *protoargs )
|
equals to Net::POP3.new( address, port ).start( account, password )
|
||||||
|
|
||||||
|
# typical usage
|
||||||
|
Net::POP3.start( addr, port, acnt, pass ) do |pop|
|
||||||
|
pop.each_mail do |m|
|
||||||
|
any_file.write m.pop
|
||||||
|
m.delete
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
: foreach( address = 'localhost', port = 110, account, password ) {|mail| .... }
|
||||||
|
starts protocol and iterate for each POPMail object.
|
||||||
|
This method equals to
|
||||||
|
|
||||||
|
Net::POP3.start( address, port, account, password ) do |pop|
|
||||||
|
pop.each do |m|
|
||||||
|
yield m
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
.
|
||||||
|
|
||||||
|
# typical usage
|
||||||
|
Net::POP3.foreach( addr, nil, acnt, pass ) do |m|
|
||||||
|
m.pop file
|
||||||
|
m.delete
|
||||||
|
end
|
||||||
|
|
||||||
|
: delete_all( address = 'localhost', port = 110, account, password )
|
||||||
|
: delete_all( address = 'localhost', port = 110, account, password ) {|mail| .... }
|
||||||
|
starts POP3 session and delete all mails.
|
||||||
|
If block is given, iterates for each POPMail object before delete.
|
||||||
|
|
||||||
|
# typical usage
|
||||||
|
Net::POP3.delete_all( addr, nil, acnt, pass ) do |m|
|
||||||
|
m.pop file
|
||||||
|
end
|
||||||
|
|
||||||
=== Methods
|
=== Methods
|
||||||
|
|
||||||
|
@ -35,16 +71,36 @@ Net::Protocol
|
||||||
: start( account, password ) {|pop| .... }
|
: start( account, password ) {|pop| .... }
|
||||||
starts POP3 session.
|
starts POP3 session.
|
||||||
|
|
||||||
When called with block, give a POP3 object to block and
|
When called with block, gives a POP3 object to block and
|
||||||
close session after block call is finished.
|
closes the session after block call finish.
|
||||||
|
|
||||||
: each {|popmail| .... }
|
|
||||||
This method is equals to "pop3.mails.each"
|
|
||||||
|
|
||||||
: mails
|
: mails
|
||||||
an array of ((URL:#POPMail)).
|
an array of ((URL:#POPMail)).
|
||||||
This array is renewed when session started.
|
This array is renewed when session started.
|
||||||
|
|
||||||
|
: each_mail {|popmail| .... }
|
||||||
|
: each {|popmail| .... }
|
||||||
|
is equals to "pop3.mails.each"
|
||||||
|
|
||||||
|
: delete_all
|
||||||
|
: delete_all {|popmail| .... }
|
||||||
|
deletes all mails.
|
||||||
|
If called with block, gives mails to the block before deleting.
|
||||||
|
|
||||||
|
# example 1
|
||||||
|
# pop and delete all mails
|
||||||
|
n = 1
|
||||||
|
pop.delete_all do |m|
|
||||||
|
File.open("inbox/#{n}") {|f| f.write m.pop }
|
||||||
|
n += 1
|
||||||
|
end
|
||||||
|
|
||||||
|
# example 2
|
||||||
|
# clear all mails on server
|
||||||
|
Net::POP3.start( addr, port, acc, pass ) do |pop|
|
||||||
|
pop.delete_all
|
||||||
|
end
|
||||||
|
|
||||||
: reset
|
: reset
|
||||||
reset the session. All "deleted mark" are removed.
|
reset the session. All "deleted mark" are removed.
|
||||||
|
|
||||||
|
@ -70,31 +126,33 @@ Object
|
||||||
|
|
||||||
=== Methods
|
=== Methods
|
||||||
|
|
||||||
: all( dest = '' )
|
: pop( dest = '' )
|
||||||
: pop
|
|
||||||
: mail
|
|
||||||
This method fetches a mail and write to 'dest' using '<<' method.
|
This method fetches a mail and write to 'dest' using '<<' method.
|
||||||
|
|
||||||
# usage example
|
# usage example
|
||||||
|
|
||||||
mailarr = []
|
mailarr = []
|
||||||
POP3.start( 'localhost', 110 ) do |pop|
|
POP3.start( 'localhost', 110 ) do |pop|
|
||||||
pop.each do |popm|
|
pop.each_mail do |popm|
|
||||||
mailarr.push popm.pop # all() returns 'dest' (this time, string)
|
mailarr.push popm.pop # all() returns 'dest' (this time, string)
|
||||||
# or, you can also
|
# or, you can also
|
||||||
# popm.pop( $stdout ) # write mail to stdout
|
# popm.pop( $stdout ) # write mail to stdout
|
||||||
|
|
||||||
|
# maybe you also want to delete mail after popping
|
||||||
|
popm.delete
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
: all {|str| .... }
|
: pop {|str| .... }
|
||||||
You can call all/pop/mail with block.
|
If pop() is called with block, it gives the block part strings of a mail.
|
||||||
argument 'str' is a read string (a part of mail).
|
|
||||||
|
|
||||||
# usage example
|
# usage example
|
||||||
|
|
||||||
POP3.start( 'localhost', 110 ) do |pop|
|
POP3.start( 'localhost', 110 ) do |pop3|
|
||||||
pop.mails[0].pop do |str| # pop only first mail...
|
pop3.each_mail do |m|
|
||||||
_do_anything_( str )
|
m.pop do |str|
|
||||||
|
# do anything
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -129,6 +187,25 @@ module Net
|
||||||
|
|
||||||
protocol_param :mail_type, '::Net::POPMail'
|
protocol_param :mail_type, '::Net::POPMail'
|
||||||
|
|
||||||
|
class << self
|
||||||
|
|
||||||
|
def foreach( address = nil, port = nil,
|
||||||
|
account = nil, password = nil, &block )
|
||||||
|
start( address, port, account, password ) do |pop|
|
||||||
|
pop.each_mail( &block )
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def delete_all( address = nil, port = nil,
|
||||||
|
account = nil, password = nil, &block )
|
||||||
|
start( address, port, account, password ) do |pop|
|
||||||
|
pop.delete_all( &block )
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
def initialize( addr = nil, port = nil )
|
def initialize( addr = nil, port = nil )
|
||||||
super
|
super
|
||||||
@mails = nil
|
@mails = nil
|
||||||
|
@ -136,9 +213,18 @@ module Net
|
||||||
|
|
||||||
attr :mails
|
attr :mails
|
||||||
|
|
||||||
def each( &block )
|
def each_mail( &block )
|
||||||
io_check
|
io_check
|
||||||
@mails.each &block
|
@mails.each( &block )
|
||||||
|
end
|
||||||
|
|
||||||
|
alias each each_mail
|
||||||
|
|
||||||
|
def delete_all
|
||||||
|
@mails.each do |m|
|
||||||
|
yield m if block_given?
|
||||||
|
m.delete unless m.deleted?
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def reset
|
def reset
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
=begin
|
=begin
|
||||||
|
|
||||||
= net/protocol.rb version 1.1.31
|
= net/protocol.rb version 1.1.32
|
||||||
|
|
||||||
written by Minero Aoki <aamine@dp.u-netsurf.ne.jp>
|
written by Minero Aoki <aamine@dp.u-netsurf.ne.jp>
|
||||||
|
|
||||||
|
@ -65,7 +65,7 @@ module Net
|
||||||
|
|
||||||
class Protocol
|
class Protocol
|
||||||
|
|
||||||
Version = '1.1.31'
|
Version = '1.1.32'
|
||||||
|
|
||||||
|
|
||||||
class << self
|
class << self
|
||||||
|
@ -113,15 +113,18 @@ module Net
|
||||||
@address = addr || 'localhost'
|
@address = addr || 'localhost'
|
||||||
@port = port || type.port
|
@port = port || type.port
|
||||||
|
|
||||||
@active = false
|
|
||||||
@pipe = nil
|
|
||||||
|
|
||||||
@command = nil
|
@command = nil
|
||||||
@socket = nil
|
@socket = nil
|
||||||
|
|
||||||
|
@active = false
|
||||||
|
@pipe = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
attr_reader :address, :port,
|
attr_reader :address
|
||||||
:command, :socket
|
attr_reader :port
|
||||||
|
|
||||||
|
attr_reader :command
|
||||||
|
attr_reader :socket
|
||||||
|
|
||||||
def inspect
|
def inspect
|
||||||
"#<#{type} #{address}:#{port} open=#{active?}>"
|
"#<#{type} #{address}:#{port} open=#{active?}>"
|
||||||
|
@ -131,20 +134,29 @@ module Net
|
||||||
def start( *args )
|
def start( *args )
|
||||||
return false if active?
|
return false if active?
|
||||||
|
|
||||||
begin
|
if block_given? then
|
||||||
connect
|
begin
|
||||||
do_start( *args )
|
_start args
|
||||||
@active = true
|
yield self
|
||||||
yield self if block_given?
|
ensure
|
||||||
ensure
|
finish
|
||||||
finish if block_given?
|
end
|
||||||
|
else
|
||||||
|
_start args
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def _start( args )
|
||||||
|
connect
|
||||||
|
do_start( *args )
|
||||||
|
@active = true
|
||||||
|
end
|
||||||
|
private :_start
|
||||||
|
|
||||||
def finish
|
def finish
|
||||||
return false unless active?
|
return false unless active?
|
||||||
|
|
||||||
do_finish
|
do_finish unless @command.critical?
|
||||||
disconnect
|
disconnect
|
||||||
@active = false
|
@active = false
|
||||||
true
|
true
|
||||||
|
@ -375,24 +387,35 @@ module Net
|
||||||
rep.error!
|
rep.error!
|
||||||
end
|
end
|
||||||
|
|
||||||
def getok( line, ok = SuccessCode )
|
def getok( line, expect = SuccessCode )
|
||||||
@socket.writeline line
|
@socket.writeline line
|
||||||
check_reply ok
|
check_reply expect
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
#
|
||||||
|
# error handle
|
||||||
|
#
|
||||||
|
|
||||||
|
public
|
||||||
|
|
||||||
|
def critical?
|
||||||
|
@critical
|
||||||
|
end
|
||||||
|
|
||||||
|
def error_ok
|
||||||
|
@critical = false
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
def critical
|
def critical
|
||||||
return if @critical
|
|
||||||
@critical = true
|
@critical = true
|
||||||
ret = yield
|
ret = yield
|
||||||
@critical = false
|
@critical = false
|
||||||
ret
|
ret
|
||||||
end
|
end
|
||||||
|
|
||||||
def critical?
|
|
||||||
@critical
|
|
||||||
end
|
|
||||||
|
|
||||||
def begin_critical
|
def begin_critical
|
||||||
ret = @critical
|
ret = @critical
|
||||||
@critical = true
|
@critical = true
|
||||||
|
@ -403,11 +426,6 @@ module Net
|
||||||
@critical = false
|
@critical = false
|
||||||
end
|
end
|
||||||
|
|
||||||
def error_ok
|
|
||||||
@critical = false
|
|
||||||
end
|
|
||||||
public :error_ok
|
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
@ -441,10 +459,11 @@ module Net
|
||||||
|
|
||||||
def reopen
|
def reopen
|
||||||
unless closed? then
|
unless closed? then
|
||||||
@socket.close
|
close
|
||||||
@buffer = ''
|
@buffer = ''
|
||||||
end
|
end
|
||||||
@socket = TCPsocket.new( @addr, @port )
|
@socket = TCPsocket.new( @addr, @port )
|
||||||
|
@closed = false
|
||||||
end
|
end
|
||||||
|
|
||||||
attr :socket, true
|
attr :socket, true
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
=begin
|
=begin
|
||||||
|
|
||||||
= net/smtp.rb version 1.1.31
|
= net/smtp.rb version 1.1.32
|
||||||
|
|
||||||
written by Minero Aoki <aamine@dp.u-netsurf.ne.jp>
|
written by Minero Aoki <aamine@dp.u-netsurf.ne.jp>
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue