58
58
require 'stringio'
59
59
require 'time'
60
60
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
67
78
end
79
+ end
68
80
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:
71
83
if !rest . empty? && ( String === rest . first || Integer === rest . first )
72
84
mode = rest . shift
73
85
if !rest . empty? && Integer === rest . first
74
86
perm = rest . shift
75
87
end
76
88
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?
83
97
84
98
unless mode == nil ||
85
99
mode == 'r' || mode == 'rb' ||
@@ -99,7 +113,7 @@ def OpenURI.open_uri(name, *rest) #:nodoc:
99
113
end
100
114
end
101
115
102
- def OpenURI . open_loop ( uri , options ) #:nodoc:
116
+ def OpenURI . open_loop ( uri , options ) # :nodoc:
103
117
header = { }
104
118
options . each { |k , v |
105
119
if String === k
@@ -146,13 +160,7 @@ def OpenURI.open_loop(uri, options) #:nodoc:
146
160
io
147
161
end
148
162
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:
156
164
def initialize ( uri )
157
165
@uri = uri
158
166
end
@@ -167,7 +175,7 @@ def initialize(message, io)
167
175
attr_reader :io
168
176
end
169
177
170
- class Buffer #:nodoc:
178
+ class Buffer # :nodoc:
171
179
def initialize
172
180
@io = StringIO . new
173
181
end
@@ -192,7 +200,7 @@ def io
192
200
193
201
# Mixin for holding meta-information.
194
202
module Meta
195
- def Meta . init ( obj , src = nil ) #:nodoc:
203
+ def Meta . init ( obj , src = nil ) # :nodoc:
196
204
obj . extend Meta
197
205
obj . instance_eval {
198
206
@base_uri = nil
@@ -218,7 +226,7 @@ def Meta.init(obj, src=nil) #:nodoc:
218
226
# The Hash keys are downcased for canonicalization.
219
227
attr_reader :meta
220
228
221
- def meta_add_field ( name , value ) #:nodoc:
229
+ def meta_add_field ( name , value ) # :nodoc:
222
230
@meta [ name . downcase ] = value
223
231
end
224
232
@@ -236,7 +244,7 @@ def last_modified
236
244
RE_QUOTED_STRING = %r{"(?:[\r \n \t !#-\[ \] -~\x80 -\xff ]|\\ [\x00 -\x7f ])"}n
237
245
RE_PARAMETERS = %r{(?:;#{ RE_LWS } ?#{ RE_TOKEN } #{ RE_LWS } ?=#{ RE_LWS } ?(?:#{ RE_TOKEN } |#{ RE_QUOTED_STRING } )#{ RE_LWS } ?)*}n
238
246
239
- def content_type_parse #:nodoc:
247
+ def content_type_parse # :nodoc:
240
248
v = @meta [ 'content-type' ]
241
249
if v && %r{\A #{ RE_LWS } ?(#{ RE_TOKEN } )#{ RE_LWS } ?/(#{ RE_TOKEN } )#{ RE_LWS } ?(#{ RE_PARAMETERS } )\z }o =~ v
242
250
type = $1. downcase
@@ -290,8 +298,8 @@ def content_encoding
290
298
# Mixin for URIs.
291
299
module OpenRead
292
300
# 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 )
295
303
end
296
304
297
305
# reads a content of the URI.
@@ -333,11 +341,11 @@ def find_proxy
333
341
end
334
342
335
343
class HTTP
336
- def direct_open ( buf , header ) #:nodoc:
344
+ def direct_open ( buf , header ) # :nodoc:
337
345
proxy_open ( buf , request_uri , header )
338
346
end
339
347
340
- def proxy_open ( buf , uri , header ) #:nodoc:
348
+ def proxy_open ( buf , uri , header ) # :nodoc:
341
349
require 'net/http'
342
350
resp = Net ::HTTP . start ( self . host , self . port ) { |http |
343
351
http . get ( uri . to_s , header ) { |str | buf << str }
@@ -362,7 +370,7 @@ def proxy_open(buf, uri, header) #:nodoc:
362
370
end
363
371
364
372
class FTP
365
- def direct_open ( buf , header ) #:nodoc:
373
+ def direct_open ( buf , header ) # :nodoc:
366
374
require 'net/ftp'
367
375
# xxx: header is discarded.
368
376
# todo: extract user/passwd from .netrc.
@@ -380,17 +388,3 @@ def direct_open(buf, header) #:nodoc:
380
388
include OpenURI ::OpenRead
381
389
end
382
390
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