* lib/net/protocol.rb, smtp.rb, pop.rb, http.rb: 1.1.31
* lib/net/http.rb: initializes header in HTTP, not HTTPCommand
* lib/net/protocol.rb, http.rb: rewrites proxy code


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@1035 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
aamine 2000-11-10 23:31:32 +00:00
parent ae5cb89708
commit f5ba5e64d5
5 changed files with 194 additions and 108 deletions

View File

@ -1,3 +1,11 @@
Sat Nov 11 08:34:18 2000 Minero Aoki <aamine@dp.u-netsurf.ne.jp>
* lib/net/protocol.rb, smtp.rb, pop.rb, http.rb: 1.1.31
* lib/net/http.rb: initializes header in HTTP, not HTTPCommand
* lib/net/protocol.rb, http.rb: rewrites proxy code
Fri Nov 10 16:15:53 2000 Yukihiro Matsumoto <matz@ruby-lang.org> Fri Nov 10 16:15:53 2000 Yukihiro Matsumoto <matz@ruby-lang.org>
* error.c: T_SYMBOL was misplaced my T_UNDEF. * error.c: T_SYMBOL was misplaced my T_UNDEF.

View File

@ -1,6 +1,6 @@
=begin =begin
= net/http.rb version 1.1.30 = net/http.rb version 1.1.31
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".
@ -27,8 +27,25 @@ You can get it from RAA
: start( address = 'localhost', port = 80, proxy_addr = nil, proxy_port = nil ) {|http| .... } : start( address = 'localhost', port = 80, proxy_addr = nil, proxy_port = nil ) {|http| .... }
is equals to Net::HTTP.new( address, port, proxy_addr, proxy_port ).start(&block) is equals to Net::HTTP.new( address, port, proxy_addr, proxy_port ).start(&block)
: Proxy( address, port )
creates a HTTP proxy class.
Arguments are address/port of proxy host.
You can replace HTTP class by this proxy class.
# example
proxy_http = HTTP::Proxy( 'proxy.foo.org', 8080 )
:
proxy_http.start( 'www.ruby-lang.org' ) do |http|
# connecting proxy.foo.org:8080
:
end
: proxy_class?
If self is HTTP, false.
If self is a class which was created by HTTP::Proxy(), true.
: port : port
HTTP default port, 80 HTTP default port (80).
== Methods == Methods
@ -40,6 +57,15 @@ You can get it from RAA
When this method is called with 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.
: proxy?
true if self is a HTTP proxy class
: proxy_address
address of proxy host. If self is not a proxy, nil.
: proxy_port
port number of proxy host. If self is not a proxy, nil.
: get( path, header = nil, dest = '' ) : get( path, header = nil, dest = '' )
: get( path, header = nil ) {|str| .... } : get( path, header = nil ) {|str| .... }
get data from "path" on connecting host. get data from "path" on connecting host.
@ -249,8 +275,18 @@ module Net
protocol_param :port, '80' protocol_param :port, '80'
protocol_param :command_type, '::Net::NetPrivate::HTTPCommand' protocol_param :command_type, '::Net::NetPrivate::HTTPCommand'
###
### proxy
###
class << self class << self
def Proxy( p_addr, p_port = nil )
::Net::NetPrivate::HTTPProxy.create_proxy_class(
p_addr, p_port || self.port )
end
alias orig_new new alias orig_new new
def new( address = nil, port = nil, p_addr = nil, p_port = nil ) def new( address = nil, port = nil, p_addr = nil, p_port = nil )
@ -261,50 +297,92 @@ module Net
new( address, port, p_addr, p_port ).start( &block ) new( address, port, p_addr, p_port ).start( &block )
end end
def proxy_class?
false
end
def proxy_address
nil
end
def proxy_port
nil
end
end end
def proxy?
false
end
def proxy_address
nil
end
def proxy_port
nil
end
def edit_path( path )
path
end
###
### 1.2 implementation
###
@new_impl = false @new_impl = false
def HTTP.new_implementation class << self
return if @new_impl
@new_impl = true
module_eval %^
undef head def new_implementation
alias head head2 return if @new_impl
@new_impl = true
module_eval %^
undef get undef head
alias head head2
def get( path, u_header = nil, dest = nil, &block ) undef get
get2( path, u_header ) {|f| f.body( dest, &block ) }
def get( path, u_header = nil, dest = nil, &block )
get2( path, u_header ) {|f| f.body( dest, &block ) }
end
undef post
def post( path, data, u_header = nil, dest = nil, &block )
post2( path, data, u_header ) {|f| f.body( dest, &block ) }
end
undef put
def put( path, src, u_header = nil )
put2( path, src, u_header ) {|f| f.body }
end
^
end end
undef post def old_implementation
if @new_impl then
def post( path, data, u_header = nil, dest = nil, &block ) raise RuntimeError,
post2( path, data, u_header ) {|f| f.body( dest, &block ) } 'http.rb is already switched to new implementation'
end
end end
undef put
def put( path, src, u_header = nil )
put2( path, src, u_header ) {|f| f.body }
end
^
end end
def HTTP.old_implementation
if @new_impl then
raise RuntimeError, "http.rb is already switched to new implementation"
end
end
###
### http operations
###
def get( path, u_header = nil, dest = nil, &block ) def get( path, u_header = nil, dest = nil, &block )
resp = get2( path, u_header ) {|f| dest = f.body( dest, &block ) } resp = get2( path, u_header ) {|f| f.body( dest, &block ) }
resp.value resp.value
return resp, dest return resp, resp.body
end end
def get2( path, u_header = nil, &block ) def get2( path, u_header = nil, &block )
@ -330,10 +408,9 @@ module Net
def post( path, data, u_header = nil, dest = nil, &block ) def post( path, data, u_header = nil, dest = nil, &block )
resp = post2( path, data, u_header ) {|f| resp = post2( path, data, u_header ) {|f| f.body( dest, &block ) }
dest = f.body( dest, &block ) }
resp.value resp.value
return resp, dest return resp, resp.body
end end
def post2( path, data, u_header = nil, &block ) def post2( path, data, u_header = nil, &block )
@ -403,13 +480,20 @@ module Net
end end
def procheader( h ) def procheader( h )
return( {} ) unless h ret = {}
new = {} ret[ 'Host' ] = address +
((port == HTTP.port) ? '' : ":#{port}")
ret[ 'Connection' ] = 'Keep-Alive'
ret[ 'Accept' ] = '*/*'
return ret unless h
h.each do |k,v| h.each do |k,v|
arr = k.split('-') arr = k.split('-')
arr.each{|i| i.capitalize! } arr.each {|i| i.capitalize! }
new[ arr.join('-') ] = v ret[ arr.join('-') ] = v
end end
ret
end end
@ -428,26 +512,69 @@ module Net
end end
end end
end
HTTPSession = HTTP
module NetPrivate
module HTTPProxy
class << self
def create_proxy_class( p_addr, p_port )
klass = Class.new( HTTP )
klass.module_eval {
include HTTPProxy
@proxy_address = p_addr
@proxy_port = p_port
}
def klass.proxy_class?
true
end
def klass.proxy_address
@proxy_address
end
def klass.proxy_port
@proxy_port
end
klass
end
def edit_path( path )
path
end end
def HTTP.Proxy( p_addr, p_port = nil )
klass = super def initialize( addr, port )
klass.module_eval( <<'SRC', 'http.rb', __LINE__ + 1 ) super
def edit_path( path ) @proxy_address = type.proxy_address
'http://' + address + @proxy_port = type.proxy_port
(@port == HTTP.port ? '' : ":#{@port}") + end
path
end attr_reader :proxy_address, :proxy_port
SRC
klass alias proxyaddr proxy_address
alias proxyport proxy_port
def proxy?
true
end
def connect( addr = nil, port = nil )
super @proxy_address, @proxy_port
end
def edit_path( path )
'http://' + address + (port == HTTP.port ? '' : ":#{port}") + path
end end
end end
HTTPSession = HTTP end # net private
class HTTPResponseReceiver class HTTPResponseReceiver
@ -653,13 +780,6 @@ SRC
def initialize( sock ) def initialize( sock )
@http_version = HTTPVersion @http_version = HTTPVersion
@in_header = {}
@in_header[ 'Host' ] = sock.addr +
((sock.port == HTTP.port) ? '' : ":#{sock.port}")
@in_header[ 'Connection' ] = 'Keep-Alive'
@in_header[ 'Accept' ] = '*/*'
super sock super sock
end end
@ -713,12 +833,7 @@ SRC
def request( req, u_header ) def request( req, u_header )
@socket.writeline req @socket.writeline req
if u_header then u_header.each do |n,v|
header = @in_header.dup.update( u_header )
else
header = @in_header
end
header.each do |n,v|
@socket.writeline n + ': ' + v @socket.writeline n + ': ' + v
end end
@socket.writeline '' @socket.writeline ''

View File

@ -1,6 +1,6 @@
=begin =begin
= net/pop.rb version 1.1.30 = net/pop.rb version 1.1.31
written by Minero Aoki <aamine@dp.u-netsurf.ne.jp> written by Minero Aoki <aamine@dp.u-netsurf.ne.jp>

View File

@ -1,6 +1,6 @@
=begin =begin
= net/protocol.rb version 1.1.30 = net/protocol.rb version 1.1.31
written by Minero Aoki <aamine@dp.u-netsurf.ne.jp> written by Minero Aoki <aamine@dp.u-netsurf.ne.jp>
@ -31,10 +31,6 @@ Object
This method creates a new Protocol object and opens a session. This method creates a new Protocol object and opens a session.
equals to Net::Protocol.new( address, port ).start( *protoargs ) equals to Net::Protocol.new( address, port ).start( *protoargs )
: Proxy( address, port )
This method creates a proxy class of its protocol.
Arguments are address/port of proxy host.
=== Methods === Methods
: address : address
@ -69,7 +65,7 @@ module Net
class Protocol class Protocol
Version = '1.1.30' Version = '1.1.31'
class << self class << self
@ -85,38 +81,6 @@ module Net
end end
end end
def Proxy( p_addr, p_port = nil )
p_port ||= self.port
klass = Class.new( self )
klass.module_eval %-
def initialize( addr, port )
@proxyaddr = '#{p_addr}'
@proxyport = '#{p_port}'
super @proxyaddr, @proxyport
@address = addr
@port = port
end
def connect( addr = nil, port = nil )
super @proxyaddr, @proxyport
end
private :connect
attr_reader :proxyaddr, :proxyport
-
def klass.proxy?
true
end
klass
end
def proxy?
false
end
private private
def protocol_param( name, val ) def protocol_param( name, val )
@ -225,7 +189,6 @@ module Net
class Response class Response
def initialize( ctype, cno, msg ) def initialize( ctype, cno, msg )

View File

@ -1,6 +1,6 @@
=begin =begin
= net/smtp.rb version 1.1.30 = net/smtp.rb version 1.1.31
written by Minero Aoki <aamine@dp.u-netsurf.ne.jp> written by Minero Aoki <aamine@dp.u-netsurf.ne.jp>