8000 * lib/open-uri.rb: dispatch code restructured to make it openable · documenting-ruby/ruby@da19d16 · GitHub
[go: up one dir, main page]

Skip to content

Commit da19d16

Browse files
committed
* 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
1 parent 5504f49 commit da19d16

File tree

2 files changed

+43
-46
lines changed

2 files changed

+43
-46
lines changed

ChangeLog

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
Wed Feb 5 18:54:54 2003 Tanaka Akira <akr@m17n.org>
1+
Wed Feb 5 19:41:37 2003 Tanaka Akira <akr@m17n.org>
2+
3+
* lib/open-uri.rb: dispatch code restructured to make it openable
4+
that has `open' method.
25

36
* lib/open-uri.rb: Location: field may has a relative URI.
47
pointed out by erik eriksson <ee@opera.com>.

lib/open-uri.rb

Lines changed: 39 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -58,28 +58,42 @@
5858
require 'stringio'
5959
require 'time'
6060

61-
module OpenURI
62-
def OpenURI.open_dispatch(name, *rest, &block) #:nodoc:
63-
DispatchTable.each {|cond, meth|
64-
return meth.call(name, *rest, &block) if cond === name
65-
}
66-
return open_uri_original_open(name, *rest, &block)
61+
module Kernel
62+
private
63+
alias open_uri_original_open open # :nodoc:
64+
65+
# makes possible to open URIs.
66+
# If the first argument is URI::HTTP, URI::FTP or
67+
# String beginning with http:// or ftp://,
68+
# the URI is opened.
69+
# The opened file object is extended by OpenURI::Meta.
70+
def open(name, *rest, &block)
71+
if name.respond_to?("open")
72+
name.open(*rest, &block)
73+
elsif name.respond_to?("to_str") && %r{\A(http|ftp)://} =~ name
74+
OpenURI.open_uri(name, *rest, &block)
75+
else
76+
open_uri_original_open(name, *rest, &block)
77+
end
6778
end
79+
end
6880

69-
def OpenURI.open_uri(name, *rest) #:nodoc:
70-
uri = URI::Generic === name ? name : URI.parse(name)
81+
module OpenURI
82+
def OpenURI.scan_open_optional_arguments(*rest) # :nodoc:
7183
if !rest.empty? && (String === rest.first || Integer === rest.first)
7284
mode = rest.shift
7385
if !rest.empty? && Integer === rest.first
7486
perm = rest.shift
7587
end
7688
end
77-
if !rest.empty? && Hash === rest.first
78-
options = rest.shift
79-
end
80-
if !rest.empty?
81-
raise ArgumentError.new("extra arguments")
82-
end
89+
return mode, perm, rest
90+
end
91+
92+
def OpenURI.open_uri(name, *rest) # :nodoc:
93+
uri = URI::Generic === name ? name : URI.parse(name)
94+
mode, perm, rest = OpenURI.scan_open_optional_arguments(*rest)
95+
options = rest.shift if !rest.empty? && Hash === rest.first
96+
raise ArgumentError.new("extra arguments") if !rest.empty?
8397

8498
unless mode == nil ||
8599
mode == 'r' || mode == 'rb' ||
@@ -99,7 +113,7 @@ def OpenURI.open_uri(name, *rest) #:nodoc:
99113
end
100114
end
101115

102-
def OpenURI.open_loop(uri, options) #:nodoc:
116+
def OpenURI.open_loop(uri, options) # :nodoc:
103117
header = {}
104118
options.each {|k, v|
105119
if String === k
@@ -146,13 +160,7 @@ def OpenURI.open_loop(uri, options) #:nodoc:
146160
io
147161
end
148162

149-
DispatchTable = [
150-
[URI::HTTP, method(:open_uri)],
151-
[URI::FTP, method(:open_uri)],
152-
[%r{\A(http|ftp)://}, method(:open_uri)],
153-
]
154-
155-
class Redirect < StandardError #:nodoc:
163+
class Redirect < StandardError # :nodoc:
156164
def initialize(uri)
157165
@uri = uri
158166
end
@@ -167,7 +175,7 @@ def initialize(message, io)
167175
attr_reader :io
168176
end
169177

170-
class Buffer #:nodoc:
178+
class Buffer # :nodoc:
171179
def initialize
172180
@io = StringIO.new
173181
end
@@ -192,7 +200,7 @@ def io
192200

193201
# Mixin for holding meta-information.
194202
module Meta
195-
def Meta.init(obj, src=nil) #:nodoc:
203+
def Meta.init(obj, src=nil) # :nodoc:
196204
obj.extend Meta
197205
obj.instance_eval {
198206
@base_uri = nil
@@ -218,7 +226,7 @@ def Meta.init(obj, src=nil) #:nodoc:
218226
# The Hash keys are downcased for canonicalization.
219227
attr_reader :meta
220228

221-
def meta_add_field(name, value) #:nodoc:
229+
def meta_add_field(name, value) # :nodoc:
222230
@meta[name.downcase] = value
223231
end
224232

@@ -236,7 +244,7 @@ def last_modified
236244
RE_QUOTED_STRING = %r{"(?:[\r\n\t !#-\[\]-~\x80-\xff]|\\[\x00-\x7f])"}n
237245
RE_PARAMETERS = %r{(?:;#{RE_LWS}?#{RE_TOKEN}#{RE_LWS}?=#{RE_LWS}?(?:#{RE_TOKEN}|#{RE_QUOTED_STRING})#{RE_LWS}?)*}n
238246

239-
def content_type_parse #:nodoc:
247+
def content_type_parse # :nodoc:
240248
v = @meta['content-type']
241249
if v && %r{\A#{RE_LWS}?(#{RE_TOKEN})#{RE_LWS}?/(#{RE_TOKEN})#{RE_LWS}?(#{RE_PARAMETERS})\z}o =~ v
242250
type = $1.downcase
@@ -290,8 +298,8 @@ def content_encoding
290298
# Mixin for URIs.
291299
module OpenRead
292300
# opens the URI.
293-
def open(options={}, &block)
294-
OpenURI.open_uri(self, options, &block)
301+
def open(*rest, &block)
302+
OpenURI.open_uri(self, *rest, &block)
295303
end
296304

297305
# reads a content of the URI.
@@ -333,11 +341,11 @@ def find_proxy
333341
end
334342

335343
class HTTP
336-
def direct_open(buf, header) #:nodoc:
344+
def direct_open(buf, header) # :nodoc:
337345
proxy_open(buf, request_uri, header)
338346
end
339347

340-
def proxy_open(buf, uri, header) #:nodoc:
348+
def proxy_open(buf, uri, header) # :nodoc:
341349
require 'net/http'
342350
resp = Net::HTTP.start(self.host, self.port) {|http|
343351
http.get(uri.to_s, header) {|str| buf << str}
@@ -362,7 +370,7 @@ def proxy_open(buf, uri, header) #:nodoc:
362370
end
363371

364372
class FTP
365-
def direct_open(buf, header) #:nodoc:
373+
def direct_open(buf, header) # :nodoc:
366374
require 'net/ftp'
367375
# xxx: header is discarded.
368376
# todo: extract user/passwd from .netrc.
@@ -380,17 +388,3 @@ def direct_open(buf, header) #:nodoc:
380388
include OpenURI::OpenRead
381389
end
382390
end
383-
384-
module Kernel
385-
private
386-
alias open_uri_original_open open
387-
388-
# makes possible to open URIs.
389-
# If the first argument is URI::HTTP, URI::FTP or
390-
# String beginning with http:// or ftp://,
391-
# the URI is opened.
392-
# The opened file object is extended by OpenURI::Meta.
393-
def open(name, *rest, &block)
394-
OpenURI.open_dispatch(name, *rest, &block)
395-
end
396-
end

0 commit comments

Comments
 (0)
0