o protocol.rb, smtp.rb, pop.rb, http.rb: define all#inspect()
o protocol.rb, smtp.rb, pop.rb, http.rb: modify/add documents git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@746 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
b0ed08600a
commit
9f32fd3e9a
@ -8,16 +8,6 @@ This file is derived from "http-access.rb".
|
|||||||
This library is distributed under the terms of the Ruby license.
|
This library is distributed under the terms of the Ruby license.
|
||||||
You can freely distribute/modify this library.
|
You can freely distribute/modify this library.
|
||||||
|
|
||||||
=end
|
|
||||||
|
|
||||||
require 'net/protocol'
|
|
||||||
|
|
||||||
|
|
||||||
module Net
|
|
||||||
|
|
||||||
class HTTPBadResponse < StandardError; end
|
|
||||||
|
|
||||||
=begin
|
|
||||||
|
|
||||||
= class HTTP
|
= class HTTP
|
||||||
|
|
||||||
@ -50,7 +40,10 @@ module Net
|
|||||||
get data from "path" on connecting host.
|
get data from "path" on connecting host.
|
||||||
"header" must be a Hash like { 'Accept' => '*/*', ... }.
|
"header" must be a Hash like { 'Accept' => '*/*', ... }.
|
||||||
Data is written to "dest" by using "<<" method.
|
Data is written to "dest" by using "<<" method.
|
||||||
This method returns Net::HTTPResponse object and "dest".
|
This method returns Net::HTTPResponse object, and "dest".
|
||||||
|
|
||||||
|
# example
|
||||||
|
response, body = http.get( '/index.html' )
|
||||||
|
|
||||||
If called as iterator, give a part String of entity body.
|
If called as iterator, give a part String of entity body.
|
||||||
|
|
||||||
@ -59,15 +52,15 @@ module Net
|
|||||||
raised. At that time, you can get Response object from
|
raised. At that time, you can get Response object from
|
||||||
execption object. (same in head/post)
|
execption object. (same in head/post)
|
||||||
|
|
||||||
ex.
|
# example
|
||||||
|
|
||||||
begin
|
begin
|
||||||
head, body = http.get(...)
|
response, body = http.get(...)
|
||||||
rescue ProtoRetriableError
|
rescue Net::ProtoRetriableError
|
||||||
response = $!.data
|
response = $!.data
|
||||||
...
|
...
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
: head( path, header = nil )
|
: head( path, header = nil )
|
||||||
get only header from "path" on connecting host.
|
get only header from "path" on connecting host.
|
||||||
"header" is a Hash like { 'Accept' => '*/*', ... }.
|
"header" is a Hash like { 'Accept' => '*/*', ... }.
|
||||||
@ -94,25 +87,46 @@ module Net
|
|||||||
"header" must be a Hash like { 'Accept' => '*/*', ... }.
|
"header" must be a Hash like { 'Accept' => '*/*', ... }.
|
||||||
This method gives HTTPReadAdapter object to block.
|
This method gives HTTPReadAdapter object to block.
|
||||||
|
|
||||||
|
ex.
|
||||||
|
|
||||||
|
http.get2( '/index.html' ) do |f|
|
||||||
|
# f is a HTTPReadAdapter object
|
||||||
|
f.header
|
||||||
|
f.body
|
||||||
|
end
|
||||||
|
|
||||||
: head2( path, header = nil )
|
: head2( path, header = nil )
|
||||||
send HEAD request for "path".
|
send HEAD request for "path".
|
||||||
"header" must be a Hash like { 'Accept' => '*/*', ... }.
|
"header" must be a Hash like { 'Accept' => '*/*', ... }.
|
||||||
The difference between "head" method is that
|
The difference between "head" method is that
|
||||||
"head2" does not raise exceptions.
|
"head2" does not raise exceptions.
|
||||||
|
|
||||||
|
ex.
|
||||||
|
|
||||||
|
http.head2( '/index.html' ) do |f|
|
||||||
|
f.header
|
||||||
|
end
|
||||||
|
|
||||||
: post2( path, data, header = nil ) {|adapter| .... }
|
: post2( path, data, header = nil ) {|adapter| .... }
|
||||||
post "data"(must be String now) to "path".
|
post "data"(must be String now) to "path".
|
||||||
"header" must be a Hash like { 'Accept' => '*/*', ... }.
|
"header" must be a Hash like { 'Accept' => '*/*', ... }.
|
||||||
This method gives HTTPReadAdapter object to block.
|
This method gives HTTPReadAdapter object to block.
|
||||||
|
|
||||||
|
ex.
|
||||||
|
|
||||||
|
http.post2( '/index.html', 'data data data...' ) do |f|
|
||||||
|
f.header
|
||||||
|
f.body
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
= class HTTPResponse
|
= class HTTPResponse
|
||||||
|
|
||||||
== Methods
|
|
||||||
|
|
||||||
HTTP response object.
|
HTTP response object.
|
||||||
All "key" is case-insensitive.
|
All "key" is case-insensitive.
|
||||||
|
|
||||||
|
== Methods
|
||||||
|
|
||||||
: code
|
: code
|
||||||
HTTP result code. For example, '302'
|
HTTP result code. For example, '302'
|
||||||
|
|
||||||
@ -134,6 +148,9 @@ All "key" is case-insensitive.
|
|||||||
: each {|name,value| .... }
|
: each {|name,value| .... }
|
||||||
iterate for each field name and value pair
|
iterate for each field name and value pair
|
||||||
|
|
||||||
|
: body
|
||||||
|
"dest" argument for HTTP#get, post, put
|
||||||
|
|
||||||
|
|
||||||
= class HTTPReadAdapter
|
= class HTTPReadAdapter
|
||||||
|
|
||||||
@ -154,6 +171,14 @@ All "key" is case-insensitive.
|
|||||||
|
|
||||||
=end
|
=end
|
||||||
|
|
||||||
|
require 'net/protocol'
|
||||||
|
|
||||||
|
|
||||||
|
module Net
|
||||||
|
|
||||||
|
class HTTPBadResponse < StandardError; end
|
||||||
|
|
||||||
|
|
||||||
class HTTP < Protocol
|
class HTTP < Protocol
|
||||||
|
|
||||||
protocol_param :port, '80'
|
protocol_param :port, '80'
|
||||||
@ -320,6 +345,10 @@ All "key" is case-insensitive.
|
|||||||
@header = @body = nil
|
@header = @body = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def inspect
|
||||||
|
"#<#{type}>"
|
||||||
|
end
|
||||||
|
|
||||||
def header
|
def header
|
||||||
unless @header then
|
unless @header then
|
||||||
@header = @command.get_response
|
@header = @command.get_response
|
||||||
@ -358,6 +387,10 @@ All "key" is case-insensitive.
|
|||||||
attr_reader :http_body_exist
|
attr_reader :http_body_exist
|
||||||
attr_accessor :body
|
attr_accessor :body
|
||||||
|
|
||||||
|
def inspect
|
||||||
|
"#<Net::HTTPResponse #{code}>"
|
||||||
|
end
|
||||||
|
|
||||||
def []( key )
|
def []( key )
|
||||||
@data[ key.downcase ]
|
@data[ key.downcase ]
|
||||||
end
|
end
|
||||||
@ -463,10 +496,12 @@ All "key" is case-insensitive.
|
|||||||
super sock
|
super sock
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
attr_reader :http_version
|
attr_reader :http_version
|
||||||
|
|
||||||
|
def inspect
|
||||||
|
"#<Net::HTTPCommand>"
|
||||||
|
end
|
||||||
|
|
||||||
def get( path, u_header )
|
def get( path, u_header )
|
||||||
return unless begin_critical
|
return unless begin_critical
|
||||||
request sprintf('GET %s HTTP/%s', path, HTTPVersion), u_header
|
request sprintf('GET %s HTTP/%s', path, HTTPVersion), u_header
|
||||||
|
136
lib/net/pop.rb
136
lib/net/pop.rb
@ -7,17 +7,6 @@ written by Minero Aoki <aamine@dp.u-netsurf.ne.jp>
|
|||||||
This library is distributed under the terms of the Ruby license.
|
This library is distributed under the terms of the Ruby license.
|
||||||
You can freely distribute/modify this library.
|
You can freely distribute/modify this library.
|
||||||
|
|
||||||
=end
|
|
||||||
|
|
||||||
|
|
||||||
require 'net/protocol'
|
|
||||||
require 'md5'
|
|
||||||
|
|
||||||
|
|
||||||
module Net
|
|
||||||
|
|
||||||
|
|
||||||
=begin
|
|
||||||
|
|
||||||
== Net::POP3
|
== Net::POP3
|
||||||
|
|
||||||
@ -51,52 +40,16 @@ Net::Protocol
|
|||||||
an array of ((URL:#POPMail)).
|
an array of ((URL:#POPMail)).
|
||||||
This array is renewed when session started.
|
This array is renewed when session started.
|
||||||
|
|
||||||
=end
|
|
||||||
|
|
||||||
class POP3 < Protocol
|
== Net::APOP
|
||||||
|
|
||||||
protocol_param :port, '110'
|
This class defines no new methods.
|
||||||
protocol_param :command_type, '::Net::POP3Command'
|
Only difference from POP3 is using APOP authentification.
|
||||||
|
|
||||||
protocol_param :mail_type, '::Net::POPMail'
|
=== Super Class
|
||||||
|
|
||||||
def initialize( addr = nil, port = nil )
|
Net::POP3
|
||||||
super
|
|
||||||
@mails = [].freeze
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
attr :mails
|
|
||||||
|
|
||||||
def each
|
|
||||||
@mails.each {|m| yield m }
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
|
|
||||||
def do_start( acnt, pwd )
|
|
||||||
@command.auth( acnt, pwd )
|
|
||||||
|
|
||||||
@mails = []
|
|
||||||
t = type.mail_type
|
|
||||||
@command.list.each_with_index do |size,idx|
|
|
||||||
if size then
|
|
||||||
@mails.push t.new( idx, size, @command )
|
|
||||||
end
|
|
||||||
end
|
|
||||||
@mails.freeze
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
|
||||||
|
|
||||||
POP = POP3
|
|
||||||
POPSession = POP3
|
|
||||||
POP3Session = POP3
|
|
||||||
|
|
||||||
|
|
||||||
=begin
|
|
||||||
|
|
||||||
== Net::POPMail
|
== Net::POPMail
|
||||||
|
|
||||||
@ -107,7 +60,7 @@ A class of mail which exists on POP server.
|
|||||||
Object
|
Object
|
||||||
|
|
||||||
|
|
||||||
=== Method
|
=== Methods
|
||||||
|
|
||||||
: all( dest = '' )
|
: all( dest = '' )
|
||||||
: pop
|
: pop
|
||||||
@ -155,6 +108,54 @@ Object
|
|||||||
|
|
||||||
=end
|
=end
|
||||||
|
|
||||||
|
require 'net/protocol'
|
||||||
|
require 'md5'
|
||||||
|
|
||||||
|
|
||||||
|
module Net
|
||||||
|
|
||||||
|
class POP3 < Protocol
|
||||||
|
|
||||||
|
protocol_param :port, '110'
|
||||||
|
protocol_param :command_type, '::Net::POP3Command'
|
||||||
|
|
||||||
|
protocol_param :mail_type, '::Net::POPMail'
|
||||||
|
|
||||||
|
def initialize( addr = nil, port = nil )
|
||||||
|
super
|
||||||
|
@mails = [].freeze
|
||||||
|
end
|
||||||
|
|
||||||
|
attr :mails
|
||||||
|
|
||||||
|
def each
|
||||||
|
@mails.each {|m| yield m }
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def do_start( acnt, pwd )
|
||||||
|
@command.auth( acnt, pwd )
|
||||||
|
|
||||||
|
@mails = []
|
||||||
|
t = type.mail_type
|
||||||
|
@command.list.each_with_index do |size,idx|
|
||||||
|
if size then
|
||||||
|
@mails.push t.new( idx, size, @command )
|
||||||
|
end
|
||||||
|
end
|
||||||
|
@mails.freeze
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
POP = POP3
|
||||||
|
POPSession = POP3
|
||||||
|
POP3Session = POP3
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class POPMail
|
class POPMail
|
||||||
|
|
||||||
def initialize( n, s, cmd )
|
def initialize( n, s, cmd )
|
||||||
@ -165,9 +166,12 @@ Object
|
|||||||
@deleted = false
|
@deleted = false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
attr :size
|
attr :size
|
||||||
|
|
||||||
|
def inspect
|
||||||
|
"#<#{type} #{@num}#{@deleted ? ' deleted' : ''}>"
|
||||||
|
end
|
||||||
|
|
||||||
def all( dest = '' )
|
def all( dest = '' )
|
||||||
if iterator? then
|
if iterator? then
|
||||||
dest = ReadAdapter.new( Proc.new )
|
dest = ReadAdapter.new( Proc.new )
|
||||||
@ -202,23 +206,9 @@ Object
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
=begin
|
|
||||||
|
|
||||||
== Net::APOP
|
|
||||||
|
|
||||||
This class defines no new methods.
|
|
||||||
Only difference from POP3 is using APOP authentification.
|
|
||||||
|
|
||||||
=== Super Class
|
|
||||||
|
|
||||||
Net::POP3
|
|
||||||
|
|
||||||
=end
|
|
||||||
|
|
||||||
class APOP < POP3
|
class APOP < POP3
|
||||||
|
|
||||||
protocol_param :command_type, 'Net::APOPCommand'
|
protocol_param :command_type, 'Net::APOPCommand'
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
APOPSession = APOP
|
APOPSession = APOP
|
||||||
@ -234,7 +224,6 @@ Net::POP3
|
|||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
def auth( acnt, pass )
|
def auth( acnt, pass )
|
||||||
critical {
|
critical {
|
||||||
@socket.writeline 'USER ' + acnt
|
@socket.writeline 'USER ' + acnt
|
||||||
@ -245,7 +234,6 @@ Net::POP3
|
|||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
def list
|
def list
|
||||||
arr = []
|
arr = []
|
||||||
critical {
|
critical {
|
||||||
@ -258,7 +246,6 @@ Net::POP3
|
|||||||
arr
|
arr
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
def rset
|
def rset
|
||||||
critical {
|
critical {
|
||||||
getok 'RSET'
|
getok 'RSET'
|
||||||
@ -273,14 +260,12 @@ Net::POP3
|
|||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
def retr( num, dest = '', &block )
|
def retr( num, dest = '', &block )
|
||||||
critical {
|
critical {
|
||||||
getok sprintf( 'RETR %d', num )
|
getok sprintf( 'RETR %d', num )
|
||||||
@socket.read_pendstr( dest, &block )
|
@socket.read_pendstr( dest, &block )
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
def dele( num )
|
def dele( num )
|
||||||
critical {
|
critical {
|
||||||
@ -288,14 +273,12 @@ Net::POP3
|
|||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
def uidl( num )
|
def uidl( num )
|
||||||
critical {
|
critical {
|
||||||
getok( sprintf 'UIDL %d', num ).msg.split(' ')[1]
|
getok( sprintf 'UIDL %d', num ).msg.split(' ')[1]
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
def quit
|
def quit
|
||||||
critical {
|
critical {
|
||||||
getok 'QUIT'
|
getok 'QUIT'
|
||||||
@ -305,7 +288,6 @@ Net::POP3
|
|||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
|
||||||
def check_reply_auth
|
def check_reply_auth
|
||||||
begin
|
begin
|
||||||
cod = check_reply( SuccessCode )
|
cod = check_reply( SuccessCode )
|
||||||
@ -316,7 +298,6 @@ Net::POP3
|
|||||||
return cod
|
return cod
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
def get_reply
|
def get_reply
|
||||||
str = @socket.readline
|
str = @socket.readline
|
||||||
|
|
||||||
@ -343,7 +324,6 @@ Net::POP3
|
|||||||
@stamp = m[0]
|
@stamp = m[0]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
def auth( account, pass )
|
def auth( account, pass )
|
||||||
critical {
|
critical {
|
||||||
@socket.writeline sprintf( 'APOP %s %s',
|
@socket.writeline sprintf( 'APOP %s %s',
|
||||||
|
@ -153,10 +153,13 @@ Object
|
|||||||
@socket = nil
|
@socket = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
attr_reader :address, :port,
|
attr_reader :address, :port,
|
||||||
:command, :socket
|
:command, :socket
|
||||||
|
|
||||||
|
def inspect
|
||||||
|
"#<#{type} #{address}:#{port} open=#{active?}>"
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
def start( *args )
|
def start( *args )
|
||||||
return false if active?
|
return false if active?
|
||||||
@ -231,6 +234,10 @@ Object
|
|||||||
attr_accessor :socket
|
attr_accessor :socket
|
||||||
attr_reader :last_reply
|
attr_reader :last_reply
|
||||||
|
|
||||||
|
def inspect
|
||||||
|
"#<#{type}>"
|
||||||
|
end
|
||||||
|
|
||||||
# abstract quit
|
# abstract quit
|
||||||
|
|
||||||
|
|
||||||
@ -295,6 +302,10 @@ Object
|
|||||||
attr_reader :code_type, :code, :message
|
attr_reader :code_type, :code, :message
|
||||||
alias msg message
|
alias msg message
|
||||||
|
|
||||||
|
def inspect
|
||||||
|
"#<#{type} #{code}>"
|
||||||
|
end
|
||||||
|
|
||||||
def error!( data = nil )
|
def error!( data = nil )
|
||||||
raise code_type.error_type.new( code + ' ' + Net.quote(msg), data )
|
raise code_type.error_type.new( code + ' ' + Net.quote(msg), data )
|
||||||
end
|
end
|
||||||
@ -320,6 +331,10 @@ Object
|
|||||||
end
|
end
|
||||||
|
|
||||||
attr :data
|
attr :data
|
||||||
|
|
||||||
|
def inspect
|
||||||
|
"#<#{type}>"
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -335,6 +350,10 @@ Object
|
|||||||
|
|
||||||
attr_reader :parents
|
attr_reader :parents
|
||||||
|
|
||||||
|
def inspect
|
||||||
|
"#<#{type}>"
|
||||||
|
end
|
||||||
|
|
||||||
def error_type
|
def error_type
|
||||||
@err
|
@err
|
||||||
end
|
end
|
||||||
@ -370,6 +389,10 @@ Object
|
|||||||
@mid = mid
|
@mid = mid
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def inspect
|
||||||
|
"#<#{type}>"
|
||||||
|
end
|
||||||
|
|
||||||
def write( str )
|
def write( str )
|
||||||
@sock.__send__ @mid, str
|
@sock.__send__ @mid, str
|
||||||
end
|
end
|
||||||
@ -383,6 +406,10 @@ Object
|
|||||||
@block = block
|
@block = block
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def inspect
|
||||||
|
"#<#{type}>"
|
||||||
|
end
|
||||||
|
|
||||||
def <<( str )
|
def <<( str )
|
||||||
@block.call str
|
@block.call str
|
||||||
end
|
end
|
||||||
@ -414,6 +441,10 @@ Object
|
|||||||
alias open new
|
alias open new
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def inspect
|
||||||
|
"#<#{type} open=#{!@closed}>"
|
||||||
|
end
|
||||||
|
|
||||||
def reopen
|
def reopen
|
||||||
unless closed? then
|
unless closed? then
|
||||||
@socket.close
|
@socket.close
|
||||||
|
@ -7,17 +7,6 @@ written by Minero Aoki <aamine@dp.u-netsurf.ne.jp>
|
|||||||
This library is distributed under the terms of the Ruby license.
|
This library is distributed under the terms of the Ruby license.
|
||||||
You can freely distribute/modify this library.
|
You can freely distribute/modify this library.
|
||||||
|
|
||||||
=end
|
|
||||||
|
|
||||||
|
|
||||||
require 'net/protocol'
|
|
||||||
require 'md5'
|
|
||||||
|
|
||||||
|
|
||||||
module Net
|
|
||||||
|
|
||||||
|
|
||||||
=begin
|
|
||||||
|
|
||||||
== Net::SMTP
|
== Net::SMTP
|
||||||
|
|
||||||
@ -81,6 +70,13 @@ Net::Protocol
|
|||||||
|
|
||||||
=end
|
=end
|
||||||
|
|
||||||
|
require 'net/protocol'
|
||||||
|
require 'md5'
|
||||||
|
|
||||||
|
|
||||||
|
module Net
|
||||||
|
|
||||||
|
|
||||||
class SMTP < Protocol
|
class SMTP < Protocol
|
||||||
|
|
||||||
protocol_param :port, '25'
|
protocol_param :port, '25'
|
||||||
|
Loading…
x
Reference in New Issue
Block a user