1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00
* lib/net/protocol.rb, smtp.rb, pop.rb, http.rb: 1.1.28.
* lib/net/http.rb: HTTPReadAdapter -> HTTPResponseReceiver
* lib/net/http.rb (connecting): response is got in receive()


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@951 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
aamine 2000-09-21 06:58:01 +00:00
parent 13243e5ad1
commit 616c1cd971
5 changed files with 153 additions and 83 deletions

View file

@ -1,3 +1,11 @@
Thu Sep 21 15:59:23 2000 Minero Aoki <aamine@dp.u-netsurf.ne.jp>
* lib/net/protocol.rb, smtp.rb, pop.rb, http.rb: 1.1.28.
* lib/net/http.rb: HTTPReadAdapter -> HTTPResponseReceiver
* lib/net/http.rb (connecting): response is got in receive()
Wed Sep 20 23:21:38 2000 Yukihiro Matsumoto <matz@ruby-lang.org> Wed Sep 20 23:21:38 2000 Yukihiro Matsumoto <matz@ruby-lang.org>
* ruby.c (load_file): two Ctrl-D was required to stop ruby at the * ruby.c (load_file): two Ctrl-D was required to stop ruby at the

View file

@ -1,6 +1,6 @@
=begin =begin
= net/http.rb version 1.1.28 = net/http.rb version 1.1.29
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".
@ -9,6 +9,10 @@ This program is free software.
You can distribute/modify this program under You can distribute/modify this program under
the terms of the Ruby Distribute License. the terms of the Ruby Distribute License.
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).
= class HTTP = class HTTP
@ -33,7 +37,7 @@ the terms of the Ruby Distribute License.
: 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 a block, gives HTTP object to block When this method is called with block, gives HTTP object to block
and close HTTP session after block call finished. and close HTTP session after block call finished.
: get( path, header = nil, dest = '' ) : get( path, header = nil, dest = '' )
@ -46,22 +50,21 @@ the terms of the Ruby Distribute License.
# example # example
response, body = http.get( '/index.html' ) response, body = http.get( '/index.html' )
If called with a block, give a part String of entity body. If called with block, give a part String of entity body.
Note: Note:
If status is not 2xx(success), ProtocolError exception is If status is not 2xx(success), ProtocolError exception is
raised. At that time, you can get Response object from raised. At that time, you can get HTTPResponse object from
execption object. (same in head/post) execption object. (same in head/post)
# example # example
begin begin
response, body = http.get(...) response, body = http.get( '/index.html' )
rescue Net::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' => '*/*', ... }.
@ -81,45 +84,75 @@ the terms of the Ruby Distribute License.
"header" must be a Hash like { 'Accept' => '*/*', ... }. "header" must be a Hash like { 'Accept' => '*/*', ... }.
This method returns Net::HTTPResponse object and "dest". This method returns Net::HTTPResponse object and "dest".
If called with a block, gives a part String of entity body. If called with block, gives a part String of entity body.
: get2( path, header = nil ) {|adapter| .... } : get2( path, header = nil )
: get2( path, header = nil ) {|recv| .... }
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. If this method is called with block, one gives
a HTTPResponseReceiver object to block.
ex. # example
http.get2( '/index.html' ) do |recv|
http.get2( '/index.html' ) do |f| # "recv" is a HTTPResponseReceiver object
# f is a HTTPReadAdapter object recv.header
f.header recv.body
f.body end
# another way
response = http.get2( '/index.html' )
response['content-type']
response.body
# this is wrong
http.get2( '/index.html' ) do |recv|
print recv.header.body # body is not read yet!!!
end
# but this is ok
http.get2( '/index.html' ) do |recv|
recv.body # read body and set recv.header.body
print recv.header.body # ref
end end
: head2( path, header = nil ) : head2( path, header = nil )
: head2( path, header = nil ) {|recv| .... }
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. If this method is called with block, one gives
a HTTPResponseReceiver object to block.
http.head2( '/index.html' ) do |f|
f.header # example
response = http.head2( '/index.html' )
# another way
http.head2( '/index.html' ) do |recv|
recv.response
end end
: post2( path, data, header = nil ) {|adapter| .... } : post2( path, data, header = nil )
: post2( path, data, header = nil ) {|recv| .... }
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. If this method is called with block, one gives
a HTTPResponseReceiver object to block.
ex. # example
http.post2( '/anycgi.rb', 'data data data...' ) do |recv|
http.post2( '/index.html', 'data data data...' ) do |adapter| # "recv" is a HTTPResponseReceiver object
adapter.header recv.header
adapter.body recv.body
end end
# another way
response = http.post2( '/anycgi.rb', 'important data' )
response['content-type']
response.body
= class HTTPResponse = class HTTPResponse
@ -152,7 +185,8 @@ All "key" is case-insensitive.
: message : message
HTTP result message. For example, 'Not Found' HTTP result message. For example, 'Not Found'
= class HTTPReadAdapter
= class HTTPResponseReceiver
== Methods == Methods
@ -165,8 +199,8 @@ All "key" is case-insensitive.
entity body. A body is written to "dest" using "<<" method. entity body. A body is written to "dest" using "<<" method.
: body {|str| ... } : body {|str| ... }
get entity body by using block. gets entity body with block.
If this method is called twice, block is not called and If this method is called twice, block is not executed and
returns first "dest". returns first "dest".
@ -283,8 +317,9 @@ module Net
end end
def get2( path, u_header = nil, &block ) def get2( path, u_header = nil, &block )
connecting( u_header, block ) {|uh| connecting( u_header ) {|uh|
@command.get edit_path(path), uh @command.get edit_path(path), uh
receive true, block
} }
end end
@ -295,10 +330,10 @@ module Net
resp resp
end end
def head2( path, u_header = nil ) def head2( path, u_header = nil, &block )
connecting( u_header, nil ) {|uh| connecting( u_header ) {|uh|
@command.head edit_path(path), uh @command.head edit_path(path), uh
@command.get_response_no_body receive false, block
} }
end end
@ -311,8 +346,9 @@ module Net
end end
def post2( path, data, u_header = nil, &block ) def post2( path, data, u_header = nil, &block )
connecting( u_header, block ) {|uh| connecting( u_header ) {|uh|
@command.post edit_path(path), uh, data @command.post edit_path(path), uh, data
receive true, block
} }
end end
@ -325,8 +361,9 @@ module Net
end end
def put2( path, src, u_header = nil, &block ) def put2( path, src, u_header = nil, &block )
connecting( u_header, block ) {|uh| connecting( u_header ) {|uh|
@command.put path, uh, src @command.put path, uh, src
receive true, block
} }
end end
@ -334,14 +371,7 @@ module Net
private private
# called when connecting def connecting( u_header )
def do_finish
unless @socket.closed? then
head2 '/', { 'Connection' => 'close' }
end
end
def connecting( u_header, ublock )
u_header = procheader( u_header ) u_header = procheader( u_header )
if not @socket then if not @socket then
u_header['Connection'] = 'close' u_header['Connection'] = 'close'
@ -351,12 +381,7 @@ module Net
end end
resp = yield( u_header ) resp = yield( u_header )
if ublock then
adapter = HTTPReadAdapter.new( @command )
ublock.call adapter
resp = adapter.off
end
unless keep_alive? u_header, resp then unless keep_alive? u_header, resp then
@socket.close @socket.close
end end
@ -396,22 +421,37 @@ module Net
end end
end end
def receive( body_exist, block )
recv = HTTPResponseReceiver.new( @command, body_exist )
block.call recv if block
recv.terminate
recv.header
end
# called when connecting
def do_finish
unless @socket.closed? then
head2 '/', { 'Connection' => 'close' }
end
end
def edit_path( path ) def edit_path( path )
path path
end end
class << self def HTTP.Proxy( p_addr, p_port = nil )
def Proxy( p_addr, p_port ) klass = super
klass = super klass.module_eval %-
klass.module_eval %- def edit_path( path )
def edit_path( path ) 'http://' + address +
'http://' + address + (@port == HTTP.port ? '' : ":#{@port}") +
(@port == #{self.port} ? '' : ':' + @port.to_s) + path path
end end
- -
klass klass
end
end end
end end
@ -521,10 +561,11 @@ module Net
HTTPVersionNotSupported = HTTPFatalErrorCode.mkchild HTTPVersionNotSupported = HTTPFatalErrorCode.mkchild
class HTTPReadAdapter class HTTPResponseReceiver
def initialize( command ) def initialize( command, body_exist )
@command = command @command = command
@body_exist = body_exist
@header = @body = nil @header = @body = nil
end end
@ -534,7 +575,9 @@ module Net
def header def header
unless @header then unless @header then
@header = @command.get_response stream_check
@header = @body_exist ? @command.get_response :
@command.get_response_no_body
end end
@header @header
end end
@ -543,20 +586,31 @@ module Net
def body( dest = nil, &block ) def body( dest = nil, &block )
dest, ret = HTTP.procdest( dest, block ) dest, ret = HTTP.procdest( dest, block )
unless @body then unless @body then
@body = @command.get_body( response, dest ) stream_check
@body = @command.get_body( header, dest )
end end
@body @body
end end
alias entity body alias entity body
def off def terminate
body header
body if @body_exist
@command = nil @command = nil
@header end
private
def stream_check
unless @command then
raise IOError, 'receiver was used out of block'
end
end end
end end
HTTPReadAdapter = HTTPResponseReceiver
module NetPrivate module NetPrivate
@ -570,11 +624,8 @@ module Net
@http_version = HTTPVersion @http_version = HTTPVersion
@in_header = {} @in_header = {}
if sock.port == HTTP.port @in_header[ 'Host' ] = sock.addr +
@in_header[ 'Host' ] = sock.addr ((sock.port == HTTP.port) ? '' : ":#{sock.port}")
else
@in_header[ 'Host' ] = sock.addr + ':' + sock.port
end
@in_header[ 'Connection' ] = 'Keep-Alive' @in_header[ 'Connection' ] = 'Keep-Alive'
@in_header[ 'Accept' ] = '*/*' @in_header[ 'Accept' ] = '*/*'

View file

@ -1,6 +1,6 @@
=begin =begin
= net/pop.rb version 1.1.28 = net/pop.rb version 1.1.29
written by Minero Aoki <aamine@dp.u-netsurf.ne.jp> written by Minero Aoki <aamine@dp.u-netsurf.ne.jp>
@ -8,6 +8,10 @@ This program is free software.
You can distribute/modify this program under You can distribute/modify this program under
the terms of the Ruby Distribute License. the terms of the Ruby Distribute License.
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).
== Net::POP3 == Net::POP3
@ -31,7 +35,7 @@ Net::Protocol
: start( account, password ) {|pop| .... } : start( account, password ) {|pop| .... }
starts POP3 session. starts POP3 session.
When called with a block, give a POP3 object to block and When called with block, give a POP3 object to block and
close session after block call is finished. close session after block call is finished.
: each {|popmail| .... } : each {|popmail| .... }
@ -80,7 +84,7 @@ Object
end end
: all {|str| .... } : all {|str| .... }
You can use all/pop/mail with a block. You can call all/pop/mail with block.
argument 'str' is a read string (a part of mail). argument 'str' is a read string (a part of mail).
# usage example # usage example

View file

@ -1,6 +1,6 @@
=begin =begin
= net/protocol.rb version 1.1.28 = net/protocol.rb version 1.1.29
written by Minero Aoki <aamine@dp.u-netsurf.ne.jp> written by Minero Aoki <aamine@dp.u-netsurf.ne.jp>
@ -8,6 +8,10 @@ This program is free software.
You can distribute/modify this program under You can distribute/modify this program under
the terms of the Ruby Distribute License. the terms of the Ruby Distribute License.
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).
== Net::Protocol == Net::Protocol
@ -46,7 +50,7 @@ Object
'*args' are specified in subclasses. '*args' are specified in subclasses.
When is called with a block, gives Protocol object to block and When is called with block, gives Protocol object to block and
close session when block finished. close session when block finished.
: finish : finish
@ -65,7 +69,7 @@ module Net
class Protocol class Protocol
Version = '1.1.28' Version = '1.1.29'
class << self class << self

View file

@ -1,6 +1,6 @@
=begin =begin
= net/smtp.rb version 1.1.28 = net/smtp.rb version 1.1.29
written by Minero Aoki <aamine@dp.u-netsurf.ne.jp> written by Minero Aoki <aamine@dp.u-netsurf.ne.jp>
@ -8,6 +8,10 @@ This program is free software.
You can distribute/modify this program under You can distribute/modify this program under
the terms of the Ruby Distribute License. the terms of the Ruby Distribute License.
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).
== Net::SMTP == Net::SMTP
@ -33,7 +37,7 @@ Net::Protocol
opens TCP connection and starts SMTP session. opens TCP connection and starts SMTP session.
If protocol had been started, do nothing and return false. If protocol had been started, do nothing and return false.
When this methods is called with a block, give a SMTP object to block and When this methods is called with block, give a SMTP object to block and
close session after block call finished. close session after block call finished.
If account and password are given, is trying to get authentication If account and password are given, is trying to get authentication
@ -42,7 +46,7 @@ Net::Protocol
: send_mail( mailsrc, from_addr, *to_addrs ) : send_mail( mailsrc, from_addr, *to_addrs )
: sendmail( mailsrc, from_addr, *to_addrs ) : sendmail( mailsrc, from_addr, *to_addrs )
This method sends 'mailsrc' as mail. SMTP read strings This method sends 'mailsrc' as mail. SMTP read strings
from 'mailsrc' by calling 'each' method, and convert them from 'mailsrc' by calling 'each' iterator, and convert them
into "\r\n" terminated string when write. into "\r\n" terminated string when write.
from_addr must be String. from_addr must be String.
@ -62,8 +66,7 @@ Net::Protocol
: ready( from_addr, to_addrs ) {|adapter| .... } : ready( from_addr, to_addrs ) {|adapter| .... }
This method stands by the SMTP object for sending mail. This method stands by the SMTP object for sending mail.
In the block of this method, you can call ONLY 'write' method "adapter" object accepts only "write" method.
for 'adapter'.
# usage example # usage example