mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* lib/open-uri.rb (OpenURI.open_loop, OpenURI::HTTP#proxy_open):
refactored to support options. (Buffer): maintain size by this class. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@4959 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
9e1709139d
commit
31ef5c2d5e
2 changed files with 25 additions and 17 deletions
|
@ -1,3 +1,9 @@
|
||||||
|
Sat Nov 15 10:05:40 2003 Tanaka Akira <akr@m17n.org>
|
||||||
|
|
||||||
|
* lib/open-uri.rb (OpenURI.open_loop, OpenURI::HTTP#proxy_open):
|
||||||
|
refactored to support options.
|
||||||
|
(Buffer): maintain size by this class.
|
||||||
|
|
||||||
Sat Nov 15 07:40:14 2003 Yukihiro Matsumoto <matz@ruby-lang.org>
|
Sat Nov 15 07:40:14 2003 Yukihiro Matsumoto <matz@ruby-lang.org>
|
||||||
|
|
||||||
* eval.c (rb_method_node): new API to retrieve method body.
|
* eval.c (rb_method_node): new API to retrieve method body.
|
||||||
|
|
|
@ -126,13 +126,6 @@ module OpenURI
|
||||||
end
|
end
|
||||||
|
|
||||||
def OpenURI.open_loop(uri, options) # :nodoc:
|
def OpenURI.open_loop(uri, options) # :nodoc:
|
||||||
header = {}
|
|
||||||
options.each {|k, v|
|
|
||||||
if String === k
|
|
||||||
header[k] = v
|
|
||||||
end
|
|
||||||
}
|
|
||||||
|
|
||||||
case opt_proxy = options.fetch(:proxy, true)
|
case opt_proxy = options.fetch(:proxy, true)
|
||||||
when true
|
when true
|
||||||
find_proxy = lambda {|u| u.find_proxy}
|
find_proxy = lambda {|u| u.find_proxy}
|
||||||
|
@ -151,9 +144,9 @@ module OpenURI
|
||||||
begin
|
begin
|
||||||
buf = Buffer.new
|
buf = Buffer.new
|
||||||
if proxy_uri = find_proxy.call(uri)
|
if proxy_uri = find_proxy.call(uri)
|
||||||
proxy_uri.proxy_open(buf, uri, header)
|
proxy_uri.proxy_open(buf, uri, options)
|
||||||
else
|
else
|
||||||
uri.direct_open(buf, header)
|
uri.direct_open(buf, options)
|
||||||
end
|
end
|
||||||
rescue Redirect
|
rescue Redirect
|
||||||
loc = $!.uri
|
loc = $!.uri
|
||||||
|
@ -191,12 +184,15 @@ module OpenURI
|
||||||
class Buffer # :nodoc:
|
class Buffer # :nodoc:
|
||||||
def initialize
|
def initialize
|
||||||
@io = StringIO.new
|
@io = StringIO.new
|
||||||
|
@size = 0
|
||||||
end
|
end
|
||||||
|
attr_reader :size
|
||||||
|
|
||||||
StringMax = 10240
|
StringMax = 10240
|
||||||
def <<(str)
|
def <<(str)
|
||||||
@io << str
|
@io << str
|
||||||
if StringIO === @io && StringMax < @io.size
|
@size += str.length
|
||||||
|
if StringIO === @io && StringMax < @size
|
||||||
require 'tempfile'
|
require 'tempfile'
|
||||||
io = Tempfile.new('open-uri')
|
io = Tempfile.new('open-uri')
|
||||||
Meta.init io, @io if Meta === @io
|
Meta.init io, @io if Meta === @io
|
||||||
|
@ -364,14 +360,19 @@ module URI
|
||||||
end
|
end
|
||||||
|
|
||||||
class HTTP
|
class HTTP
|
||||||
def direct_open(buf, header) # :nodoc:
|
def direct_open(buf, options) # :nodoc:
|
||||||
proxy_open(buf, request_uri, header)
|
proxy_open(buf, request_uri, options)
|
||||||
end
|
end
|
||||||
|
|
||||||
def proxy_open(buf, uri, header) # :nodoc:
|
def proxy_open(buf, uri, options) # :nodoc:
|
||||||
|
header = {}
|
||||||
|
options.each {|k, v| header[k] = v if String === k }
|
||||||
|
|
||||||
require 'net/http'
|
require 'net/http'
|
||||||
resp = Net::HTTP.start(self.host, self.port) {|http|
|
resp = Net::HTTP.start(self.host, self.port) {|http|
|
||||||
http.get(uri.to_s, header) {|str| buf << str}
|
http.get(uri.to_s, header) {|str|
|
||||||
|
buf << str
|
||||||
|
}
|
||||||
}
|
}
|
||||||
io = buf.io
|
io = buf.io
|
||||||
io.rewind
|
io.rewind
|
||||||
|
@ -393,9 +394,8 @@ module URI
|
||||||
end
|
end
|
||||||
|
|
||||||
class FTP
|
class FTP
|
||||||
def direct_open(buf, header) # :nodoc:
|
def direct_open(buf, options) # :nodoc:
|
||||||
require 'net/ftp'
|
require 'net/ftp'
|
||||||
# xxx: header is discarded.
|
|
||||||
# todo: extract user/passwd from .netrc.
|
# todo: extract user/passwd from .netrc.
|
||||||
user = 'anonymous'
|
user = 'anonymous'
|
||||||
passwd = nil
|
passwd = nil
|
||||||
|
@ -403,7 +403,9 @@ module URI
|
||||||
|
|
||||||
ftp = Net::FTP.open(self.host)
|
ftp = Net::FTP.open(self.host)
|
||||||
ftp.login(user, passwd)
|
ftp.login(user, passwd)
|
||||||
ftp.getbinaryfile(self.path, '/dev/null', Net::FTP::DEFAULT_BLOCKSIZE) {|str| buf << str}
|
ftp.getbinaryfile(self.path, '/dev/null', Net::FTP::DEFAULT_BLOCKSIZE) {|str|
|
||||||
|
buf << str
|
||||||
|
}
|
||||||
ftp.close
|
ftp.close
|
||||||
buf.io.rewind
|
buf.io.rewind
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue