1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

{protocol,smtp,pop,http}.rb for ruby 1.4 branch

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_4@673 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
aamine 2000-04-18 09:39:41 +00:00
parent cf520dda16
commit 54588fb3f1
3 changed files with 82 additions and 72 deletions

View file

@ -65,12 +65,12 @@ module Net
If called as iterator, gives a part String of entity body. If called as iterator, gives a part String of entity body.
: get2( path, header = nil ) {|writer| .... } : get2( path, header = nil ) {|adapter| .... }
send GET request for "path". send GET request for "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.
: post2( path, data, header = nil ) {|writer| .... } : 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.
@ -84,10 +84,10 @@ HTTP response object.
All "key" is case-insensitive. All "key" is case-insensitive.
: code : code
HTTP result code. ex. '302' HTTP result code. For example, '302'
: message : message
HTTP result message. ex. 'Not Found' HTTP result message. For example, 'Not Found'
: self[ key ] : self[ key ]
returns header field for "key". returns header field for "key".
@ -101,6 +101,9 @@ All "key" is case-insensitive.
: key?( key ) : key?( key )
true if key is exist true if key is exist
: each {|name,value| .... }
iterate for each field name and value pair
= class HTTPReadAdapter = class HTTPReadAdapter
@ -110,13 +113,14 @@ All "key" is case-insensitive.
: response : response
Net::HTTPResponse object Net::HTTPResponse object
: entity( dest = '' )
: body( dest = '' ) : body( dest = '' )
entity body : entity( dest = '' )
entity body. A body is written to "dest" using "<<" method.
: entity {|str| ... } : body {|str| ... }
get entity body by using iterator. get entity body by using iterator.
If this method is called twice, block is not called. If this method is called twice, block is not called and
returns first "dest".
=end =end
@ -136,30 +140,32 @@ All "key" is case-insensitive.
def get( path, u_header = nil, dest = nil, &block ) def get( path, u_header = nil, dest = nil, &block )
u_header = procheader( u_header ) resp = get2( path, u_header ) {|f| dest = f.entity( dest, &block ) }
dest, ret = HTTP.procdest( dest, block ) resp.value
resp = nil return resp, dest
connecting( u_header ) {
@command.get edit_path(path), u_header
resp = @command.get_response
@command.get_body( resp, dest )
}
return resp, ret
end end
def get2( path, u_header = nil ) def get2( path, u_header = nil )
u_header = procheader( u_header ) u_header = procheader( u_header )
resp = nil
connecting( u_header ) { connecting( u_header ) {
@command.get edit_path(path), u_header @command.get edit_path(path), u_header
tmp = HTTPReadAdapter.new( @command ) tmp = HTTPReadAdapter.new( @command )
yield tmp yield tmp
tmp.off resp = tmp.off
} }
resp
end end
def head( path, u_header = nil ) def head( path, u_header = nil )
resp = head2( path, u_header )
resp.value
resp
end
def head2( path, u_header = nil )
u_header = procheader( u_header ) u_header = procheader( u_header )
resp = nil resp = nil
connecting( u_header ) { connecting( u_header ) {
@ -172,41 +178,46 @@ All "key" is case-insensitive.
def post( path, data, u_header = nil, dest = nil, &block ) def post( path, data, u_header = nil, dest = nil, &block )
u_header = procheader( u_header ) resp = post2( path, data, u_header ) {|f|
dest, ret = HTTP.procdest( dest, block ) dest = f.entity( dest, &block ) }
resp = nil resp.value
connecting( u_header ) { return resp, dest
@command.post edit_path(path), u_header, data
resp = @command.get_response
@command.get_body( resp, dest )
}
return resp, ret
end end
def post2( path, data, u_header = nil ) def post2( path, data, u_header = nil )
u_header = procheader( u_header ) u_header = procheader( u_header )
resp = nil
connecting( u_header ) { connecting( u_header ) {
@command.post edit_path(path), u_header, data @command.post edit_path(path), u_header, data
tmp = HTTPReadAdapter.new( @command ) tmp = HTTPReadAdapter.new( @command )
yield tmp yield tmp
tmp.off resp = tmp.off
} }
resp
end end
# not tested because I could not setup apache (__;;; # not tested because I could not setup apache (__;;;
def put( path, src, u_header = nil ) def put( path, src, u_header = nil )
ret = nil
resp = put2( path, src, u_header ) {|f| ret = f.entity }
resp.value
return resp, ret
end
def put2( path, src, u_header = nil )
u_header = procheader( u_header ) u_header = procheader( u_header )
ret = '' ret = ''
resp = nil resp = nil
connecting( u_header ) { connecting( u_header ) {
@command.put path, u_header, src, dest @command.put path, u_header, src, dest
resp = @comman.get_response tmp = HTTPReadAdapter.new( @command )
@command.get_body( resp, ret ) yield tmp
resp = tmp.off
} }
return resp, ret resp
end end
@ -319,6 +330,7 @@ All "key" is case-insensitive.
def off def off
body body
@command = nil @command = nil
@header
end end
end end
@ -326,13 +338,13 @@ All "key" is case-insensitive.
class HTTPResponse < Response class HTTPResponse < Response
def initialize( code_type, code, msg ) def initialize( code_type, bexist, code, msg )
super super( code_type, code, msg )
@data = {} @data = {}
@http_body_exist = true @http_body_exist = bexist
end end
attr_accessor :http_body_exist attr_reader :http_body_exist
def []( key ) def []( key )
@data[ key.downcase ] @data[ key.downcase ]
@ -366,6 +378,10 @@ All "key" is case-insensitive.
@data.dup @data.dup
end end
def value
error! unless SuccessCode === self
end
end end
@ -445,6 +461,7 @@ All "key" is case-insensitive.
def post( path, u_header, data ) def post( path, u_header, data )
return unless begin_critical return unless begin_critical
u_header[ 'Content-Length' ] = data.size.to_s
request sprintf('POST %s HTTP/%s', path, HTTPVersion), u_header request sprintf('POST %s HTTP/%s', path, HTTPVersion), u_header
@socket.write data @socket.write data
end end
@ -489,10 +506,6 @@ All "key" is case-insensitive.
resp resp
end end
def check_response( resp )
reply_must resp, SuccessCode
end
def get_body( resp, dest ) def get_body( resp, dest )
if resp.http_body_exist then if resp.http_body_exist then
if chunked? resp then if chunked? resp then
@ -516,7 +529,6 @@ All "key" is case-insensitive.
end end
end end
end_critical end_critical
reply_must resp, SuccessCode
dest dest
end end
@ -524,7 +536,6 @@ All "key" is case-insensitive.
def get_response_no_body def get_response_no_body
resp = get_response resp = get_response
end_critical end_critical
reply_must resp, SuccessCode
resp resp
end end
@ -576,13 +587,13 @@ All "key" is case-insensitive.
'408' => [HTTPRequestTimeOut, true], '408' => [HTTPRequestTimeOut, true],
'409' => [HTTPConflict, true], '409' => [HTTPConflict, true],
'410' => [HTTPGone, true], '410' => [HTTPGone, true],
'411' => [FatalErrorCode, true], '411' => [HTTPFatalErrorCode, true],
'412' => [HTTPPreconditionFailed, true], '412' => [HTTPPreconditionFailed, true],
'413' => [HTTPRequestEntityTooLarge, true], '413' => [HTTPRequestEntityTooLarge, true],
'414' => [HTTPRequestURITooLarge, true], '414' => [HTTPRequestURITooLarge, true],
'415' => [HTTPUnsupportedMediaType, true], '415' => [HTTPUnsupportedMediaType, true],
'500' => [FatalErrorCode, true], '500' => [HTTPFatalErrorCode, true],
'501' => [HTTPNotImplemented, true], '501' => [HTTPNotImplemented, true],
'502' => [HTTPBadGateway, true], '502' => [HTTPBadGateway, true],
'503' => [HTTPServiceUnavailable, true], '503' => [HTTPServiceUnavailable, true],
@ -601,9 +612,7 @@ All "key" is case-insensitive.
discrip = m[3] discrip = m[3]
klass, bodyexist = HTTPCODE_TO_OBJ[status] || [UnknownCode, true] klass, bodyexist = HTTPCODE_TO_OBJ[status] || [UnknownCode, true]
resp = HTTPResponse.new( klass, status, discrip ) HTTPResponse.new( klass, bodyexist, status, discrip )
resp.http_body_exist = bodyexist
resp
end end
def read_chunked( ret, header ) def read_chunked( ret, header )

View file

@ -15,7 +15,7 @@ require 'socket'
module Net module Net
Version = '1.1.13' Version = '1.1.14'
=begin =begin
@ -224,13 +224,12 @@ Object
def initialize( sock ) def initialize( sock )
@socket = sock @socket = sock
@error_occured = false
@last_reply = nil @last_reply = nil
@critical = false @critical = false
end end
attr_reader :socket, :error_occured, :last_reply attr_accessor :socket
attr_writer :socket attr_reader :last_reply
# abstract quit # abstract quit
@ -250,9 +249,7 @@ Object
return rep return rep
end end
end end
rep.error!
@error_occured = true
rep.error! @socket.sending
end end
def getok( line, ok = SuccessCode ) def getok( line, ok = SuccessCode )
@ -298,17 +295,8 @@ Object
attr_reader :code_type, :code, :message attr_reader :code_type, :code, :message
alias msg message alias msg message
def error!( sending ) def error!
raise @code_type.error_type, raise @code_type.error_type, @code + ' ' + Net.quote(@message)
sprintf( <<MSG, @code, Net.quote(sending), Net.quote(@message) )
status %s
writing string is:
%s
error message from server is:
%s
MSG
end end
end end

View file

@ -74,6 +74,14 @@ Net::Protocol
protocol_param :command_type, '::Net::SMTPCommand' protocol_param :command_type, '::Net::SMTPCommand'
def initialize( addr = nil, port = nil )
super
@esmtp = true
end
attr :esmtp
def sendmail( mailsrc, fromaddr, toaddrs ) def sendmail( mailsrc, fromaddr, toaddrs )
do_ready fromaddr, toaddrs do_ready fromaddr, toaddrs
@command.write_mail mailsrc, nil @command.write_mail mailsrc, nil
@ -85,9 +93,6 @@ Net::Protocol
end end
attr :esmtp
private private
@ -104,10 +109,18 @@ Net::Protocol
@esmtp = false @esmtp = false
begin begin
@command.ehlo helodom if @esmtp then
@esmtp = true @command.ehlo helodom
else
@command.helo helodom
end
rescue ProtocolError rescue ProtocolError
@command.helo helodom if @esmtp then
@esmtp = false
retry
else
raise
end
end end
end end