summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoraamine <aamine@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2000-03-15 13:30:27 +0000
committeraamine <aamine@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2000-03-15 13:30:27 +0000
commitfcea6fbdbbe07d65bdcd740b59906981aa304737 (patch)
treee3f544a4065ff5d31b5a1184148e5ab7932bc269
parentf3763005763f48e0eaf2aff72e8565660e433266 (diff)
version 1.1.7 for ruby-1.4
protocol.rb smtp.rb pop.rb http.rb git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_4@643 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--lib/net/http.rb75
-rw-r--r--lib/net/pop.rb35
-rw-r--r--lib/net/smtp.rb66
3 files changed, 134 insertions, 42 deletions
diff --git a/lib/net/http.rb b/lib/net/http.rb
index 4847f88bf7..b908b8ae79 100644
--- a/lib/net/http.rb
+++ b/lib/net/http.rb
@@ -3,14 +3,14 @@
= 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.
=end
-require 'net/session'
+require 'net/protocol'
module Net
@@ -22,7 +22,7 @@ class HTTPBadResponse < HTTPError; end
=begin
-= HTTP class
+= class HTTP
== Class Methods
@@ -62,39 +62,60 @@ 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
+
+ unless keep_alive? u_header then
+ @socket.close
+ end
header
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 +123,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 +171,7 @@ class HTTPBadResponse < HTTPError; end
end
- attr :http_version
+ attr_reader :http_version
def get( ret, path, u_header = nil )
header = get_response(
@@ -175,7 +198,15 @@ class HTTPBadResponse < HTTPError; end
end
- # def put
+ # not work
+ def post( path, u_header = nil )
+ get_response sprintf( 'POST %s HTTP/%s', path, HTTPVersion ), u_header
+ end
+
+ # not work
+ def put( path, u_header = nil )
+ get_response sprintf( 'PUT %s HTTP/%s', path, HTTPVersion ), u_header
+ end
# def delete
@@ -187,12 +218,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
@@ -223,7 +248,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/pop.rb b/lib/net/pop.rb
index 0ec2481557..440f894f57 100644
--- a/lib/net/pop.rb
+++ b/lib/net/pop.rb
@@ -10,7 +10,7 @@ You can freely distribute/modify this library.
=end
-require 'net/session'
+require 'net/protocol'
require 'md5'
@@ -101,10 +101,33 @@ Object
=== Method
-: all
+: all( dest = '' )
: pop
: mail
- This method fetches a mail and return it.
+ This method fetches a mail and write to 'dest' using '<<' method.
+
+ # usage example
+
+ mailarr = []
+ POP3.start( 'localhost', 110 ) do |pop|
+ pop.each do |popm|
+ mailarr.push popm.pop # all() returns 'dest' (this time, string)
+ # or, you can also
+ # popm.pop( $stdout ) # write mail to stdout
+ end
+ end
+
+: all {|str| .... }
+ You can use all/pop/mail as the iterator.
+ argument 'str' is a read string (a part of mail).
+
+ # usage example
+
+ POP3.start( 'localhost', 110 ) do |pop|
+ pop.mails[0].pop do |str| # pop only first mail...
+ _do_anything_( str )
+ end
+ end
: header
This method fetches only mail header.
@@ -138,6 +161,9 @@ Object
attr :size
def all( dest = '' )
+ if iterator? then
+ dest = ReadAdapter.new( Proc.new )
+ end
@command.retr( @num, dest )
end
alias pop all
@@ -172,7 +198,8 @@ Object
== Net::APOP
-This class has no new methods. Only way of authetication is changed.
+This class defines no new methods.
+Only difference from POP3 is using APOP authentification.
=== Super Class
diff --git a/lib/net/smtp.rb b/lib/net/smtp.rb
index 7a04aa2aa2..2c387db43c 100644
--- a/lib/net/smtp.rb
+++ b/lib/net/smtp.rb
@@ -10,7 +10,7 @@ You can freely distribute/modify this library.
=end
-require 'net/session'
+require 'net/protocol'
module Net
@@ -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,6 +47,21 @@ Net::Protocol
* Net::ProtoUnknownError: unknown error
* Net::ProtoServerBusy: temporary error (errno.420/450)
+: 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'.
+
+ # usage example
+
+ SMTP.start( 'localhost', 25 ) do |smtp|
+ smtp.ready( from, to ) do |adapter|
+ adapter.write str1
+ adapter.write str2
+ adapter.write str3
+ end
+ end
+
: finish
This method ends SMTP.
If protocol had not started, do nothind and return false.
@@ -59,14 +74,21 @@ Net::Protocol
protocol_param :command_type, '::Net::SMTPCommand'
- def sendmail( mailsrc, fromaddr, toaddrs )
+ def sendmail( mailsrc, fromaddr, toaddrs, &block )
@command.mailfrom fromaddr
@command.rcpt toaddrs
@command.data
- @command.sendmail mailsrc
+ @command.write_mail( mailsrc, &block )
+ end
+
+ def ready( fromaddr, toaddrs, &block )
+ sendmail nil, fromaddr, toaddrs, &block
end
+ attr :esmtp
+
+
private
@@ -74,7 +96,14 @@ Net::Protocol
unless helodom then
raise ArgumentError, "cannot get hostname"
end
- @command.helo helodom
+
+ @esmtp = false
+ begin
+ @command.ehlo helodom
+ @esmtp = true
+ rescue ProtocolError
+ @command.helo helodom
+ end
end
end
@@ -104,15 +133,21 @@ 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.
-: data( mailsrc )
- This method send 'mailsrc' as mail. SMTP reads strings from 'mailsrc'
- by calling 'each' iterator.
+: write_mail( mailsrc )
+: write_mail {|socket| ... }
+ send 'mailsrc' as mail.
+ SMTPCommand reads strings from 'mailsrc' by calling 'each' iterator.
+ When iterator, SMTPCommand only stand by socket and pass it.
+ (The socket will accepts only 'in_write' method in the block)
: quit
This method sends "QUIT" command and ends SMTP session.
@@ -132,6 +167,11 @@ Net::Command
end
+ def ehlo( fromdom )
+ getok sprintf( 'EHLO %s', fromdom )
+ end
+
+
def mailfrom( fromaddr )
getok sprintf( 'MAIL FROM:<%s>', fromaddr )
end
@@ -149,11 +189,11 @@ Net::Command
end
- def writemail( mailsrc )
- @socket.write_pendstr mailsrc
+ def write_mail( mailsrc, &block )
+ @socket.write_pendstr mailsrc, &block
check_reply SuccessCode
end
- alias sendmail writemail
+ alias sendmail write_mail
private