* lib/open-uri.rb: dispatch code restructured to make it openable
that has `open' method. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@3448 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
5504f490a6
commit
da19d16b1c
|
@ -1,4 +1,7 @@
|
||||||
Wed Feb 5 18:54:54 2003 Tanaka Akira <akr@m17n.org>
|
Wed Feb 5 19:41:37 2003 Tanaka Akira <akr@m17n.org>
|
||||||
|
|
||||||
|
* lib/open-uri.rb: dispatch code restructured to make it openable
|
||||||
|
that has `open' method.
|
||||||
|
|
||||||
* lib/open-uri.rb: Location: field may has a relative URI.
|
* lib/open-uri.rb: Location: field may has a relative URI.
|
||||||
pointed out by erik eriksson <ee@opera.com>.
|
pointed out by erik eriksson <ee@opera.com>.
|
||||||
|
|
|
@ -58,28 +58,42 @@ require 'uri'
|
||||||
require 'stringio'
|
require 'stringio'
|
||||||
require 'time'
|
require 'time'
|
||||||
|
|
||||||
module OpenURI
|
module Kernel
|
||||||
def OpenURI.open_dispatch(name, *rest, &block) #:nodoc:
|
private
|
||||||
DispatchTable.each {|cond, meth|
|
alias open_uri_original_open open # :nodoc:
|
||||||
return meth.call(name, *rest, &block) if cond === name
|
|
||||||
}
|
|
||||||
return open_uri_original_open(name, *rest, &block)
|
|
||||||
end
|
|
||||||
|
|
||||||
def OpenURI.open_uri(name, *rest) #:nodoc:
|
# makes possible to open URIs.
|
||||||
uri = URI::Generic === name ? name : URI.parse(name)
|
# If the first argument is URI::HTTP, URI::FTP or
|
||||||
|
# String beginning with http:// or ftp://,
|
||||||
|
# the URI is opened.
|
||||||
|
# The opened file object is extended by OpenURI::Meta.
|
||||||
|
def open(name, *rest, &block)
|
||||||
|
if name.respond_to?("open")
|
||||||
|
name.open(*rest, &block)
|
||||||
|
elsif name.respond_to?("to_str") && %r{\A(http|ftp)://} =~ name
|
||||||
|
OpenURI.open_uri(name, *rest, &block)
|
||||||
|
else
|
||||||
|
open_uri_original_open(name, *rest, &block)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
module OpenURI
|
||||||
|
def OpenURI.scan_open_optional_arguments(*rest) # :nodoc:
|
||||||
if !rest.empty? && (String === rest.first || Integer === rest.first)
|
if !rest.empty? && (String === rest.first || Integer === rest.first)
|
||||||
mode = rest.shift
|
mode = rest.shift
|
||||||
if !rest.empty? && Integer === rest.first
|
if !rest.empty? && Integer === rest.first
|
||||||
perm = rest.shift
|
perm = rest.shift
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
if !rest.empty? && Hash === rest.first
|
return mode, perm, rest
|
||||||
options = rest.shift
|
end
|
||||||
end
|
|
||||||
if !rest.empty?
|
def OpenURI.open_uri(name, *rest) # :nodoc:
|
||||||
raise ArgumentError.new("extra arguments")
|
uri = URI::Generic === name ? name : URI.parse(name)
|
||||||
end
|
mode, perm, rest = OpenURI.scan_open_optional_arguments(*rest)
|
||||||
|
options = rest.shift if !rest.empty? && Hash === rest.first
|
||||||
|
raise ArgumentError.new("extra arguments") if !rest.empty?
|
||||||
|
|
||||||
unless mode == nil ||
|
unless mode == nil ||
|
||||||
mode == 'r' || mode == 'rb' ||
|
mode == 'r' || mode == 'rb' ||
|
||||||
|
@ -99,7 +113,7 @@ module OpenURI
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def OpenURI.open_loop(uri, options) #:nodoc:
|
def OpenURI.open_loop(uri, options) # :nodoc:
|
||||||
header = {}
|
header = {}
|
||||||
options.each {|k, v|
|
options.each {|k, v|
|
||||||
if String === k
|
if String === k
|
||||||
|
@ -146,13 +160,7 @@ module OpenURI
|
||||||
io
|
io
|
||||||
end
|
end
|
||||||
|
|
||||||
DispatchTable = [
|
class Redirect < StandardError # :nodoc:
|
||||||
[URI::HTTP, method(:open_uri)],
|
|
||||||
[URI::FTP, method(:open_uri)],
|
|
||||||
[%r{\A(http|ftp)://}, method(:open_uri)],
|
|
||||||
]
|
|
||||||
|
|
||||||
class Redirect < StandardError #:nodoc:
|
|
||||||
def initialize(uri)
|
def initialize(uri)
|
||||||
@uri = uri
|
@uri = uri
|
||||||
end
|
end
|
||||||
|
@ -167,7 +175,7 @@ module OpenURI
|
||||||
attr_reader :io
|
attr_reader :io
|
||||||
end
|
end
|
||||||
|
|
||||||
class Buffer #:nodoc:
|
class Buffer # :nodoc:
|
||||||
def initialize
|
def initialize
|
||||||
@io = StringIO.new
|
@io = StringIO.new
|
||||||
end
|
end
|
||||||
|
@ -192,7 +200,7 @@ module OpenURI
|
||||||
|
|
||||||
# Mixin for holding meta-information.
|
# Mixin for holding meta-information.
|
||||||
module Meta
|
module Meta
|
||||||
def Meta.init(obj, src=nil) #:nodoc:
|
def Meta.init(obj, src=nil) # :nodoc:
|
||||||
obj.extend Meta
|
obj.extend Meta
|
||||||
obj.instance_eval {
|
obj.instance_eval {
|
||||||
@base_uri = nil
|
@base_uri = nil
|
||||||
|
@ -218,7 +226,7 @@ module OpenURI
|
||||||
# The Hash keys are downcased for canonicalization.
|
# The Hash keys are downcased for canonicalization.
|
||||||
attr_reader :meta
|
attr_reader :meta
|
||||||
|
|
||||||
def meta_add_field(name, value) #:nodoc:
|
def meta_add_field(name, value) # :nodoc:
|
||||||
@meta[name.downcase] = value
|
@meta[name.downcase] = value
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -236,7 +244,7 @@ module OpenURI
|
||||||
RE_QUOTED_STRING = %r{"(?:[\r\n\t !#-\[\]-~\x80-\xff]|\\[\x00-\x7f])"}n
|
RE_QUOTED_STRING = %r{"(?:[\r\n\t !#-\[\]-~\x80-\xff]|\\[\x00-\x7f])"}n
|
||||||
RE_PARAMETERS = %r{(?:;#{RE_LWS}?#{RE_TOKEN}#{RE_LWS}?=#{RE_LWS}?(?:#{RE_TOKEN}|#{RE_QUOTED_STRING})#{RE_LWS}?)*}n
|
RE_PARAMETERS = %r{(?:;#{RE_LWS}?#{RE_TOKEN}#{RE_LWS}?=#{RE_LWS}?(?:#{RE_TOKEN}|#{RE_QUOTED_STRING})#{RE_LWS}?)*}n
|
||||||
|
|
||||||
def content_type_parse #:nodoc:
|
def content_type_parse # :nodoc:
|
||||||
v = @meta['content-type']
|
v = @meta['content-type']
|
||||||
if v && %r{\A#{RE_LWS}?(#{RE_TOKEN})#{RE_LWS}?/(#{RE_TOKEN})#{RE_LWS}?(#{RE_PARAMETERS})\z}o =~ v
|
if v && %r{\A#{RE_LWS}?(#{RE_TOKEN})#{RE_LWS}?/(#{RE_TOKEN})#{RE_LWS}?(#{RE_PARAMETERS})\z}o =~ v
|
||||||
type = $1.downcase
|
type = $1.downcase
|
||||||
|
@ -290,8 +298,8 @@ module OpenURI
|
||||||
# Mixin for URIs.
|
# Mixin for URIs.
|
||||||
module OpenRead
|
module OpenRead
|
||||||
# opens the URI.
|
# opens the URI.
|
||||||
def open(options={}, &block)
|
def open(*rest, &block)
|
||||||
OpenURI.open_uri(self, options, &block)
|
OpenURI.open_uri(self, *rest, &block)
|
||||||
end
|
end
|
||||||
|
|
||||||
# reads a content of the URI.
|
# reads a content of the URI.
|
||||||
|
@ -333,11 +341,11 @@ module URI
|
||||||
end
|
end
|
||||||
|
|
||||||
class HTTP
|
class HTTP
|
||||||
def direct_open(buf, header) #:nodoc:
|
def direct_open(buf, header) # :nodoc:
|
||||||
proxy_open(buf, request_uri, header)
|
proxy_open(buf, request_uri, header)
|
||||||
end
|
end
|
||||||
|
|
||||||
def proxy_open(buf, uri, header) #:nodoc:
|
def proxy_open(buf, uri, header) # :nodoc:
|
||||||
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}
|
||||||
|
@ -362,7 +370,7 @@ module URI
|
||||||
end
|
end
|
||||||
|
|
||||||
class FTP
|
class FTP
|
||||||
def direct_open(buf, header) #:nodoc:
|
def direct_open(buf, header) # :nodoc:
|
||||||
require 'net/ftp'
|
require 'net/ftp'
|
||||||
# xxx: header is discarded.
|
# xxx: header is discarded.
|
||||||
# todo: extract user/passwd from .netrc.
|
# todo: extract user/passwd from .netrc.
|
||||||
|
@ -380,17 +388,3 @@ module URI
|
||||||
include OpenURI::OpenRead
|
include OpenURI::OpenRead
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
module Kernel
|
|
||||||
private
|
|
||||||
alias open_uri_original_open open
|
|
||||||
|
|
||||||
# makes possible to open URIs.
|
|
||||||
# If the first argument is URI::HTTP, URI::FTP or
|
|
||||||
# String beginning with http:// or ftp://,
|
|
||||||
# the URI is opened.
|
|
||||||
# The opened file object is extended by OpenURI::Meta.
|
|
||||||
def open(name, *rest, &block)
|
|
||||||
OpenURI.open_dispatch(name, *rest, &block)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
Loading…
Reference in New Issue