1# frozen_string_literal: true 2require 'uri' 3require 'stringio' 4require 'time' 5 6module Kernel 7 private 8 alias open_uri_original_open open # :nodoc: 9 class << self 10 alias open_uri_original_open open # :nodoc: 11 end 12 13 # Allows the opening of various resources including URIs. 14 # 15 # If the first argument responds to the 'open' method, 'open' is called on 16 # it with the rest of the arguments. 17 # 18 # If the first argument is a string that begins with xxx://, it is parsed by 19 # URI.parse. If the parsed object responds to the 'open' method, 20 # 'open' is called on it with the rest of the arguments. 21 # 22 # Otherwise, the original Kernel#open is called. 23 # 24 # OpenURI::OpenRead#open provides URI::HTTP#open, URI::HTTPS#open and 25 # URI::FTP#open, Kernel#open. 26 # 27 # We can accept URIs and strings that begin with http://, https:// and 28 # ftp://. In these cases, the opened file object is extended by OpenURI::Meta. 29 def open(name, *rest, &block) # :doc: 30 if name.respond_to?(:open) 31 name.open(*rest, &block) 32 elsif name.respond_to?(:to_str) && 33 %r{\A[A-Za-z][A-Za-z0-9+\-\.]*://} =~ name && 34 (uri = URI.parse(name)).respond_to?(:open) 35 uri.open(*rest, &block) 36 else 37 open_uri_original_open(name, *rest, &block) 38 end 39 end 40 module_function :open 41end 42 43module URI #:nodoc: 44 # alias for Kernel.open defined in open-uri. 45 def self.open(name, *rest, &block) 46 Kernel.open(name, *rest, &block) 47 end 48end 49 50# OpenURI is an easy-to-use wrapper for Net::HTTP, Net::HTTPS and Net::FTP. 51# 52# == Example 53# 54# It is possible to open an http, https or ftp URL as though it were a file: 55# 56# open("http://www.ruby-lang.org/") {|f| 57# f.each_line {|line| p line} 58# } 59# 60# The opened file has several getter methods for its meta-information, as 61# follows, since it is extended by OpenURI::Meta. 62# 63# open("http://www.ruby-lang.org/en") {|f| 64# f.each_line {|line| p line} 65# p f.base_uri # <URI::HTTP:0x40e6ef2 URL:http://www.ruby-lang.org/en/> 66# p f.content_type # "text/html" 67# p f.charset # "iso-8859-1" 68# p f.content_encoding # [] 69# p f.last_modified # Thu Dec 05 02:45:02 UTC 2002 70# } 71# 72# Additional header fields can be specified by an optional hash argument. 73# 74# open("http://www.ruby-lang.org/en/", 75# "User-Agent" => "Ruby/#{RUBY_VERSION}", 76# "From" => "foo@bar.invalid", 77# "Referer" => "http://www.ruby-lang.org/") {|f| 78# # ... 79# } 80# 81# The environment variables such as http_proxy, https_proxy and ftp_proxy 82# are in effect by default. Here we disable proxy: 83# 84# open("http://www.ruby-lang.org/en/", :proxy => nil) {|f| 85# # ... 86# } 87# 88# See OpenURI::OpenRead.open and Kernel#open for more on available options. 89# 90# URI objects can be opened in a similar way. 91# 92# uri = URI.parse("http://www.ruby-lang.org/en/") 93# uri.open {|f| 94# # ... 95# } 96# 97# URI objects can be read directly. The returned string is also extended by 98# OpenURI::Meta. 99# 100# str = uri.read 101# p str.base_uri 102# 103# Author:: Tanaka Akira <akr@m17n.org> 104 105module OpenURI 106 Options = { 107 :proxy => true, 108 :proxy_http_basic_authentication => true, 109 :progress_proc => true, 110 :content_length_proc => true, 111 :http_basic_authentication => true, 112 :read_timeout => true, 113 :open_timeout => true, 114 :ssl_ca_cert => nil, 115 :ssl_verify_mode => nil, 116 :ftp_active_mode => false, 117 :redirect => true, 118 :encoding => nil, 119 } 120 121 def OpenURI.check_options(options) # :nodoc: 122 options.each {|k, v| 123 next unless Symbol === k 124 unless Options.include? k 125 raise ArgumentError, "unrecognized option: #{k}" 126 end 127 } 128 end 129 130 def OpenURI.scan_open_optional_arguments(*rest) # :nodoc: 131 if !rest.empty? && (String === rest.first || Integer === rest.first) 132 mode = rest.shift 133 if !rest.empty? && Integer === rest.first 134 perm = rest.shift 135 end 136 end 137 return mode, perm, rest 138 end 139 140 def OpenURI.open_uri(name, *rest) # :nodoc: 141 uri = URI::Generic === name ? name : URI.parse(name) 142 mode, _, rest = OpenURI.scan_open_optional_arguments(*rest) 143 options = rest.shift if !rest.empty? && Hash === rest.first 144 raise ArgumentError.new("extra arguments") if !rest.empty? 145 options ||= {} 146 OpenURI.check_options(options) 147 148 if /\Arb?(?:\Z|:([^:]+))/ =~ mode 149 encoding, = $1,Encoding.find($1) if $1 150 mode = nil 151 end 152 if options.has_key? :encoding 153 if !encoding.nil? 154 raise ArgumentError, "encoding specified twice" 155 end 156 encoding = Encoding.find(options[:encoding]) 157 end 158 159 unless mode == nil || 160 mode == 'r' || mode == 'rb' || 161 mode == File::RDONLY 162 raise ArgumentError.new("invalid access mode #{mode} (#{uri.class} resource is read only.)") 163 end 164 165 io = open_loop(uri, options) 166 io.set_encoding(encoding) if encoding 167 if block_given? 168 begin 169 yield io 170 ensure 171 if io.respond_to? :close! 172 io.close! # Tempfile 173 else 174 io.close if !io.closed? 175 end 176 end 177 else 178 io 179 end 180 end 181 182 def OpenURI.open_loop(uri, options) # :nodoc: 183 proxy_opts = [] 184 proxy_opts << :proxy_http_basic_authentication if options.include? :proxy_http_basic_authentication 185 proxy_opts << :proxy if options.include? :proxy 186 proxy_opts.compact! 187 if 1 < proxy_opts.length 188 raise ArgumentError, "multiple proxy options specified" 189 end 190 case proxy_opts.first 191 when :proxy_http_basic_authentication 192 opt_proxy, proxy_user, proxy_pass = options.fetch(:proxy_http_basic_authentication) 193 proxy_user = proxy_user.to_str 194 proxy_pass = proxy_pass.to_str 195 if opt_proxy == true 196 raise ArgumentError.new("Invalid authenticated proxy option: #{options[:proxy_http_basic_authentication].inspect}") 197 end 198 when :proxy 199 opt_proxy = options.fetch(:proxy) 200 proxy_user = nil 201 proxy_pass = nil 202 when nil 203 opt_proxy = true 204 proxy_user = nil 205 proxy_pass = nil 206 end 207 case opt_proxy 208 when true 209 find_proxy = lambda {|u| pxy = u.find_proxy; pxy ? [pxy, nil, nil] : nil} 210 when nil, false 211 find_proxy = lambda {|u| nil} 212 when String 213 opt_proxy = URI.parse(opt_proxy) 214 find_proxy = lambda {|u| [opt_proxy, proxy_user, proxy_pass]} 215 when URI::Generic 216 find_proxy = lambda {|u| [opt_proxy, proxy_user, proxy_pass]} 217 else 218 raise ArgumentError.new("Invalid proxy option: #{opt_proxy}") 219 end 220 221 uri_set = {} 222 buf = nil 223 while true 224 redirect = catch(:open_uri_redirect) { 225 buf = Buffer.new 226 uri.buffer_open(buf, find_proxy.call(uri), options) 227 nil 228 } 229 if redirect 230 if redirect.relative? 231 # Although it violates RFC2616, Location: field may have relative 232 # URI. It is converted to absolute URI using uri as a base URI. 233 redirect = uri + redirect 234 end 235 if !options.fetch(:redirect, true) 236 raise HTTPRedirect.new(buf.io.status.join(' '), buf.io, redirect) 237 end 238 unless OpenURI.redirectable?(uri, redirect) 239 raise "redirection forbidden: #{uri} -> #{redirect}" 240 end 241 if options.include? :http_basic_authentication 242 # send authentication only for the URI directly specified. 243 options = options.dup 244 options.delete :http_basic_authentication 245 end 246 uri = redirect 247 raise "HTTP redirection loop: #{uri}" if uri_set.include? uri.to_s 248 uri_set[uri.to_s] = true 249 else 250 break 251 end 252 end 253 io = buf.io 254 io.base_uri = uri 255 io 256 end 257 258 def OpenURI.redirectable?(uri1, uri2) # :nodoc: 259 # This test is intended to forbid a redirection from http://... to 260 # file:///etc/passwd, file:///dev/zero, etc. CVE-2011-1521 261 # https to http redirect is also forbidden intentionally. 262 # It avoids sending secure cookie or referer by non-secure HTTP protocol. 263 # (RFC 2109 4.3.1, RFC 2965 3.3, RFC 2616 15.1.3) 264 # However this is ad hoc. It should be extensible/configurable. 265 uri1.scheme.downcase == uri2.scheme.downcase || 266 (/\A(?:http|ftp)\z/i =~ uri1.scheme && /\A(?:https?|ftp)\z/i =~ uri2.scheme) 267 end 268 269 def OpenURI.open_http(buf, target, proxy, options) # :nodoc: 270 if proxy 271 proxy_uri, proxy_user, proxy_pass = proxy 272 raise "Non-HTTP proxy URI: #{proxy_uri}" if proxy_uri.class != URI::HTTP 273 end 274 275 if target.userinfo 276 raise ArgumentError, "userinfo not supported. [RFC3986]" 277 end 278 279 header = {} 280 options.each {|k, v| header[k] = v if String === k } 281 282 require 'net/http' 283 klass = Net::HTTP 284 if URI::HTTP === target 285 # HTTP or HTTPS 286 if proxy 287 unless proxy_user && proxy_pass 288 proxy_user, proxy_pass = proxy_uri.userinfo.split(':') if proxy_uri.userinfo 289 end 290 if proxy_user && proxy_pass 291 klass = Net::HTTP::Proxy(proxy_uri.hostname, proxy_uri.port, proxy_user, proxy_pass) 292 else 293 klass = Net::HTTP::Proxy(proxy_uri.hostname, proxy_uri.port) 294 end 295 end 296 target_host = target.hostname 297 target_port = target.port 298 request_uri = target.request_uri 299 else 300 # FTP over HTTP proxy 301 target_host = proxy_uri.hostname 302 target_port = proxy_uri.port 303 request_uri = target.to_s 304 if proxy_user && proxy_pass 305 header["Proxy-Authorization"] = 306 'Basic ' + ["#{proxy_user}:#{proxy_pass}"].pack('m0') 307 end 308 end 309 310 http = proxy ? klass.new(target_host, target_port) : klass.new(target_host, target_port, nil) 311 if target.class == URI::HTTPS 312 require 'net/https' 313 http.use_ssl = true 314 http.verify_mode = options[:ssl_verify_mode] || OpenSSL::SSL::VERIFY_PEER 315 store = OpenSSL::X509::Store.new 316 if options[:ssl_ca_cert] 317 Array(options[:ssl_ca_cert]).each do |cert| 318 if File.directory? cert 319 store.add_path cert 320 else 321 store.add_file cert 322 end 323 end 324 else 325 store.set_default_paths 326 end 327 http.cert_store = store 328 end 329 if options.include? :read_timeout 330 http.read_timeout = options[:read_timeout] 331 end 332 if options.include? :open_timeout 333 http.open_timeout = options[:open_timeout] 334 end 335 336 resp = nil 337 http.start { 338 req = Net::HTTP::Get.new(request_uri, header) 339 if options.include? :http_basic_authentication 340 user, pass = options[:http_basic_authentication] 341 req.basic_auth user, pass 342 end 343 http.request(req) {|response| 344 resp = response 345 if options[:content_length_proc] && Net::HTTPSuccess === resp 346 if resp.key?('Content-Length') 347 options[:content_length_proc].call(resp['Content-Length'].to_i) 348 else 349 options[:content_length_proc].call(nil) 350 end 351 end 352 resp.read_body {|str| 353 buf << str 354 if options[:progress_proc] && Net::HTTPSuccess === resp 355 options[:progress_proc].call(buf.size) 356 end 357 str.clear 358 } 359 } 360 } 361 io = buf.io 362 io.rewind 363 io.status = [resp.code, resp.message] 364 resp.each_name {|name| buf.io.meta_add_field2 name, resp.get_fields(name) } 365 case resp 366 when Net::HTTPSuccess 367 when Net::HTTPMovedPermanently, # 301 368 Net::HTTPFound, # 302 369 Net::HTTPSeeOther, # 303 370 Net::HTTPTemporaryRedirect # 307 371 begin 372 loc_uri = URI.parse(resp['location']) 373 rescue URI::InvalidURIError 374 raise OpenURI::HTTPError.new(io.status.join(' ') + ' (Invalid Location URI)', io) 375 end 376 throw :open_uri_redirect, loc_uri 377 else 378 raise OpenURI::HTTPError.new(io.status.join(' '), io) 379 end 380 end 381 382 class HTTPError < StandardError 383 def initialize(message, io) 384 super(message) 385 @io = io 386 end 387 attr_reader :io 388 end 389 390 # Raised on redirection, 391 # only occurs when +redirect+ option for HTTP is +false+. 392 class HTTPRedirect < HTTPError 393 def initialize(message, io, uri) 394 super(message, io) 395 @uri = uri 396 end 397 attr_reader :uri 398 end 399 400 class Buffer # :nodoc: all 401 def initialize 402 @io = StringIO.new 403 @size = 0 404 end 405 attr_reader :size 406 407 StringMax = 10240 408 def <<(str) 409 @io << str 410 @size += str.length 411 if StringIO === @io && StringMax < @size 412 require 'tempfile' 413 io = Tempfile.new('open-uri') 414 io.binmode 415 Meta.init io, @io if Meta === @io 416 io << @io.string 417 @io = io 418 end 419 end 420 421 def io 422 Meta.init @io unless Meta === @io 423 @io 424 end 425 end 426 427 # Mixin for holding meta-information. 428 module Meta 429 def Meta.init(obj, src=nil) # :nodoc: 430 obj.extend Meta 431 obj.instance_eval { 432 @base_uri = nil 433 @meta = {} # name to string. legacy. 434 @metas = {} # name to array of strings. 435 } 436 if src 437 obj.status = src.status 438 obj.base_uri = src.base_uri 439 src.metas.each {|name, values| 440 obj.meta_add_field2(name, values) 441 } 442 end 443 end 444 445 # returns an Array that consists of status code and message. 446 attr_accessor :status 447 448 # returns a URI that is the base of relative URIs in the data. 449 # It may differ from the URI supplied by a user due to redirection. 450 attr_accessor :base_uri 451 452 # returns a Hash that represents header fields. 453 # The Hash keys are downcased for canonicalization. 454 # The Hash values are a field body. 455 # If there are multiple field with same field name, 456 # the field values are concatenated with a comma. 457 attr_reader :meta 458 459 # returns a Hash that represents header fields. 460 # The Hash keys are downcased for canonicalization. 461 # The Hash value are an array of field values. 462 attr_reader :metas 463 464 def meta_setup_encoding # :nodoc: 465 charset = self.charset 466 enc = nil 467 if charset 468 begin 469 enc = Encoding.find(charset) 470 rescue ArgumentError 471 end 472 end 473 enc = Encoding::ASCII_8BIT unless enc 474 if self.respond_to? :force_encoding 475 self.force_encoding(enc) 476 elsif self.respond_to? :string 477 self.string.force_encoding(enc) 478 else # Tempfile 479 self.set_encoding enc 480 end 481 end 482 483 def meta_add_field2(name, values) # :nodoc: 484 name = name.downcase 485 @metas[name] = values 486 @meta[name] = values.join(', ') 487 meta_setup_encoding if name == 'content-type' 488 end 489 490 def meta_add_field(name, value) # :nodoc: 491 meta_add_field2(name, [value]) 492 end 493 494 # returns a Time that represents the Last-Modified field. 495 def last_modified 496 if vs = @metas['last-modified'] 497 v = vs.join(', ') 498 Time.httpdate(v) 499 else 500 nil 501 end 502 end 503 504 # :stopdoc: 505 RE_LWS = /[\r\n\t ]+/n 506 RE_TOKEN = %r{[^\x00- ()<>@,;:\\"/\[\]?={}\x7f]+}n 507 RE_QUOTED_STRING = %r{"(?:[\r\n\t !#-\[\]-~\x80-\xff]|\\[\x00-\x7f])*"}n 508 RE_PARAMETERS = %r{(?:;#{RE_LWS}?#{RE_TOKEN}#{RE_LWS}?=#{RE_LWS}?(?:#{RE_TOKEN}|#{RE_QUOTED_STRING})#{RE_LWS}?)*}n 509 # :startdoc: 510 511 def content_type_parse # :nodoc: 512 vs = @metas['content-type'] 513 # The last (?:;#{RE_LWS}?)? matches extra ";" which violates RFC2045. 514 if vs && %r{\A#{RE_LWS}?(#{RE_TOKEN})#{RE_LWS}?/(#{RE_TOKEN})#{RE_LWS}?(#{RE_PARAMETERS})(?:;#{RE_LWS}?)?\z}no =~ vs.join(', ') 515 type = $1.downcase 516 subtype = $2.downcase 517 parameters = [] 518 $3.scan(/;#{RE_LWS}?(#{RE_TOKEN})#{RE_LWS}?=#{RE_LWS}?(?:(#{RE_TOKEN})|(#{RE_QUOTED_STRING}))/no) {|att, val, qval| 519 if qval 520 val = qval[1...-1].gsub(/[\r\n\t !#-\[\]-~\x80-\xff]+|(\\[\x00-\x7f])/n) { $1 ? $1[1,1] : $& } 521 end 522 parameters << [att.downcase, val] 523 } 524 ["#{type}/#{subtype}", *parameters] 525 else 526 nil 527 end 528 end 529 530 # returns "type/subtype" which is MIME Content-Type. 531 # It is downcased for canonicalization. 532 # Content-Type parameters are stripped. 533 def content_type 534 type, *_ = content_type_parse 535 type || 'application/octet-stream' 536 end 537 538 # returns a charset parameter in Content-Type field. 539 # It is downcased for canonicalization. 540 # 541 # If charset parameter is not given but a block is given, 542 # the block is called and its result is returned. 543 # It can be used to guess charset. 544 # 545 # If charset parameter and block is not given, 546 # nil is returned except text type in HTTP. 547 # In that case, "iso-8859-1" is returned as defined by RFC2616 3.7.1. 548 def charset 549 type, *parameters = content_type_parse 550 if pair = parameters.assoc('charset') 551 pair.last.downcase 552 elsif block_given? 553 yield 554 elsif type && %r{\Atext/} =~ type && 555 @base_uri && /\Ahttp\z/i =~ @base_uri.scheme 556 "iso-8859-1" # RFC2616 3.7.1 557 else 558 nil 559 end 560 end 561 562 # Returns a list of encodings in Content-Encoding field as an array of 563 # strings. 564 # 565 # The encodings are downcased for canonicalization. 566 def content_encoding 567 vs = @metas['content-encoding'] 568 if vs && %r{\A#{RE_LWS}?#{RE_TOKEN}#{RE_LWS}?(?:,#{RE_LWS}?#{RE_TOKEN}#{RE_LWS}?)*}o =~ (v = vs.join(', ')) 569 v.scan(RE_TOKEN).map {|content_coding| content_coding.downcase} 570 else 571 [] 572 end 573 end 574 end 575 576 # Mixin for HTTP and FTP URIs. 577 module OpenRead 578 # OpenURI::OpenRead#open provides `open' for URI::HTTP and URI::FTP. 579 # 580 # OpenURI::OpenRead#open takes optional 3 arguments as: 581 # 582 # OpenURI::OpenRead#open([mode [, perm]] [, options]) [{|io| ... }] 583 # 584 # OpenURI::OpenRead#open returns an IO-like object if block is not given. 585 # Otherwise it yields the IO object and return the value of the block. 586 # The IO object is extended with OpenURI::Meta. 587 # 588 # +mode+ and +perm+ are the same as Kernel#open. 589 # 590 # However, +mode+ must be read mode because OpenURI::OpenRead#open doesn't 591 # support write mode (yet). 592 # Also +perm+ is ignored because it is meaningful only for file creation. 593 # 594 # +options+ must be a hash. 595 # 596 # Each option with a string key specifies an extra header field for HTTP. 597 # I.e., it is ignored for FTP without HTTP proxy. 598 # 599 # The hash may include other options, where keys are symbols: 600 # 601 # [:proxy] 602 # Synopsis: 603 # :proxy => "http://proxy.foo.com:8000/" 604 # :proxy => URI.parse("http://proxy.foo.com:8000/") 605 # :proxy => true 606 # :proxy => false 607 # :proxy => nil 608 # 609 # If :proxy option is specified, the value should be String, URI, 610 # boolean or nil. 611 # 612 # When String or URI is given, it is treated as proxy URI. 613 # 614 # When true is given or the option itself is not specified, 615 # environment variable `scheme_proxy' is examined. 616 # `scheme' is replaced by `http', `https' or `ftp'. 617 # 618 # When false or nil is given, the environment variables are ignored and 619 # connection will be made to a server directly. 620 # 621 # [:proxy_http_basic_authentication] 622 # Synopsis: 623 # :proxy_http_basic_authentication => 624 # ["http://proxy.foo.com:8000/", "proxy-user", "proxy-password"] 625 # :proxy_http_basic_authentication => 626 # [URI.parse("http://proxy.foo.com:8000/"), 627 # "proxy-user", "proxy-password"] 628 # 629 # If :proxy option is specified, the value should be an Array with 3 630 # elements. It should contain a proxy URI, a proxy user name and a proxy 631 # password. The proxy URI should be a String, an URI or nil. The proxy 632 # user name and password should be a String. 633 # 634 # If nil is given for the proxy URI, this option is just ignored. 635 # 636 # If :proxy and :proxy_http_basic_authentication is specified, 637 # ArgumentError is raised. 638 # 639 # [:http_basic_authentication] 640 # Synopsis: 641 # :http_basic_authentication=>[user, password] 642 # 643 # If :http_basic_authentication is specified, 644 # the value should be an array which contains 2 strings: 645 # username and password. 646 # It is used for HTTP Basic authentication defined by RFC 2617. 647 # 648 # [:content_length_proc] 649 # Synopsis: 650 # :content_length_proc => lambda {|content_length| ... } 651 # 652 # If :content_length_proc option is specified, the option value procedure 653 # is called before actual transfer is started. 654 # It takes one argument, which is expected content length in bytes. 655 # 656 # If two or more transfers are performed by HTTP redirection, the 657 # procedure is called only once for the last transfer. 658 # 659 # When expected content length is unknown, the procedure is called with 660 # nil. This happens when the HTTP response has no Content-Length header. 661 # 662 # [:progress_proc] 663 # Synopsis: 664 # :progress_proc => lambda {|size| ...} 665 # 666 # If :progress_proc option is specified, the proc is called with one 667 # argument each time when `open' gets content fragment from network. 668 # The argument +size+ is the accumulated transferred size in bytes. 669 # 670 # If two or more transfer is done by HTTP redirection, the procedure 671 # is called only one for a last transfer. 672 # 673 # :progress_proc and :content_length_proc are intended to be used for 674 # progress bar. 675 # For example, it can be implemented as follows using Ruby/ProgressBar. 676 # 677 # pbar = nil 678 # open("http://...", 679 # :content_length_proc => lambda {|t| 680 # if t && 0 < t 681 # pbar = ProgressBar.new("...", t) 682 # pbar.file_transfer_mode 683 # end 684 # }, 685 # :progress_proc => lambda {|s| 686 # pbar.set s if pbar 687 # }) {|f| ... } 688 # 689 # [:read_timeout] 690 # Synopsis: 691 # :read_timeout=>nil (no timeout) 692 # :read_timeout=>10 (10 second) 693 # 694 # :read_timeout option specifies a timeout of read for http connections. 695 # 696 # [:open_timeout] 697 # Synopsis: 698 # :open_timeout=>nil (no timeout) 699 # :open_timeout=>10 (10 second) 700 # 701 # :open_timeout option specifies a timeout of open for http connections. 702 # 703 # [:ssl_ca_cert] 704 # Synopsis: 705 # :ssl_ca_cert=>filename or an Array of filenames 706 # 707 # :ssl_ca_cert is used to specify CA certificate for SSL. 708 # If it is given, default certificates are not used. 709 # 710 # [:ssl_verify_mode] 711 # Synopsis: 712 # :ssl_verify_mode=>mode 713 # 714 # :ssl_verify_mode is used to specify openssl verify mode. 715 # 716 # [:ftp_active_mode] 717 # Synopsis: 718 # :ftp_active_mode=>bool 719 # 720 # <tt>:ftp_active_mode => true</tt> is used to make ftp active mode. 721 # Ruby 1.9 uses passive mode by default. 722 # Note that the active mode is default in Ruby 1.8 or prior. 723 # 724 # [:redirect] 725 # Synopsis: 726 # :redirect=>bool 727 # 728 # +:redirect+ is true by default. <tt>:redirect => false</tt> is used to 729 # disable all HTTP redirects. 730 # 731 # OpenURI::HTTPRedirect exception raised on redirection. 732 # Using +true+ also means that redirections between http and ftp are 733 # permitted. 734 # 735 def open(*rest, &block) 736 OpenURI.open_uri(self, *rest, &block) 737 end 738 739 # OpenURI::OpenRead#read([options]) reads a content referenced by self and 740 # returns the content as string. 741 # The string is extended with OpenURI::Meta. 742 # The argument +options+ is same as OpenURI::OpenRead#open. 743 def read(options={}) 744 self.open(options) {|f| 745 str = f.read 746 Meta.init str, f 747 str 748 } 749 end 750 end 751end 752 753module URI 754 class HTTP 755 def buffer_open(buf, proxy, options) # :nodoc: 756 OpenURI.open_http(buf, self, proxy, options) 757 end 758 759 include OpenURI::OpenRead 760 end 761 762 class FTP 763 def buffer_open(buf, proxy, options) # :nodoc: 764 if proxy 765 OpenURI.open_http(buf, self, proxy, options) 766 return 767 end 768 require 'net/ftp' 769 770 path = self.path 771 path = path.sub(%r{\A/}, '%2F') # re-encode the beginning slash because uri library decodes it. 772 directories = path.split(%r{/}, -1) 773 directories.each {|d| 774 d.gsub!(/%([0-9A-Fa-f][0-9A-Fa-f])/) { [$1].pack("H2") } 775 } 776 unless filename = directories.pop 777 raise ArgumentError, "no filename: #{self.inspect}" 778 end 779 directories.each {|d| 780 if /[\r\n]/ =~ d 781 raise ArgumentError, "invalid directory: #{d.inspect}" 782 end 783 } 784 if /[\r\n]/ =~ filename 785 raise ArgumentError, "invalid filename: #{filename.inspect}" 786 end 787 typecode = self.typecode 788 if typecode && /\A[aid]\z/ !~ typecode 789 raise ArgumentError, "invalid typecode: #{typecode.inspect}" 790 end 791 792 # The access sequence is defined by RFC 1738 793 ftp = Net::FTP.new 794 ftp.connect(self.hostname, self.port) 795 ftp.passive = !options[:ftp_active_mode] 796 # todo: extract user/passwd from .netrc. 797 user = 'anonymous' 798 passwd = nil 799 user, passwd = self.userinfo.split(/:/) if self.userinfo 800 ftp.login(user, passwd) 801 directories.each {|cwd| 802 ftp.voidcmd("CWD #{cwd}") 803 } 804 if typecode 805 # xxx: typecode D is not handled. 806 ftp.voidcmd("TYPE #{typecode.upcase}") 807 end 808 if options[:content_length_proc] 809 options[:content_length_proc].call(ftp.size(filename)) 810 end 811 ftp.retrbinary("RETR #{filename}", 4096) { |str| 812 buf << str 813 options[:progress_proc].call(buf.size) if options[:progress_proc] 814 } 815 ftp.close 816 buf.io.rewind 817 end 818 819 include OpenURI::OpenRead 820 end 821end 822