summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2003-11-15 01:09:21 +0000
committerakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2003-11-15 01:09:21 +0000
commit717802b2c2bd1a99a2dcbf29d55c98d2b5863dc4 (patch)
tree2ac0ecb473bd02efef28129ce9262b7388921e5b /lib
parenta044233d228591f5de7c008f222b50c11296befe (diff)
downloadruby-717802b2c2bd1a99a2dcbf29d55c98d2b5863dc4.tar.gz
ruby-717802b2c2bd1a99a2dcbf29d55c98d2b5863dc4.tar.xz
ruby-717802b2c2bd1a99a2dcbf29d55c98d2b5863dc4.zip
* lib/open-uri.rb (OpenURI.open_loop, OpenURI::HTTP#proxy_open):
refactored to support options. (Buffer): maintain size by this class. git-svn-id: http://svn.ruby-lang.org/repos/ruby/trunk@4959 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib')
-rw-r--r--lib/open-uri.rb36
1 files changed, 19 insertions, 17 deletions
diff --git a/lib/open-uri.rb b/lib/open-uri.rb
index df6ea232d..f13f1d5ed 100644
--- a/lib/open-uri.rb
+++ b/lib/open-uri.rb
@@ -126,13 +126,6 @@ module OpenURI
end
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)
when true
find_proxy = lambda {|u| u.find_proxy}
@@ -151,9 +144,9 @@ module OpenURI
begin
buf = Buffer.new
if proxy_uri = find_proxy.call(uri)
- proxy_uri.proxy_open(buf, uri, header)
+ proxy_uri.proxy_open(buf, uri, options)
else
- uri.direct_open(buf, header)
+ uri.direct_open(buf, options)
end
rescue Redirect
loc = $!.uri
@@ -191,12 +184,15 @@ module OpenURI
class Buffer # :nodoc:
def initialize
@io = StringIO.new
+ @size = 0
end
+ attr_reader :size
StringMax = 10240
def <<(str)
@io << str
- if StringIO === @io && StringMax < @io.size
+ @size += str.length
+ if StringIO === @io && StringMax < @size
require 'tempfile'
io = Tempfile.new('open-uri')
Meta.init io, @io if Meta === @io
@@ -364,14 +360,19 @@ module URI
end
class HTTP
- def direct_open(buf, header) # :nodoc:
- proxy_open(buf, request_uri, header)
+ def direct_open(buf, options) # :nodoc:
+ proxy_open(buf, request_uri, options)
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'
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.rewind
@@ -393,9 +394,8 @@ module URI
end
class FTP
- def direct_open(buf, header) # :nodoc:
+ def direct_open(buf, options) # :nodoc:
require 'net/ftp'
- # xxx: header is discarded.
# todo: extract user/passwd from .netrc.
user = 'anonymous'
passwd = nil
@@ -403,7 +403,9 @@ module URI
ftp = Net::FTP.open(self.host)
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
buf.io.rewind
end