Project

General

Profile

Misc #15893 ยป deprecate-open-uri-kernel-open.patch

jeremyevans0 (Jeremy Evans), 06/02/2019 03:46 AM

View differences:

lib/open-uri.rb
10 10
    alias open_uri_original_open open # :nodoc:
11 11
  end
12 12

  
13
  def open(name, *rest, &block) # :nodoc:
14
    if (name.respond_to?(:open) && !name.respond_to?(:to_path)) ||
15
       (name.respond_to?(:to_str) &&
16
        %r{\A[A-Za-z][A-Za-z0-9+\-\.]*://} =~ name &&
17
        (uri = URI.parse(name)).respond_to?(:open))
18
      warn('calling URI.open via Kernel#open is deprecated, call URI.open directly', uplevel: 1)
19
      URI.open(name, *rest, &block)
20
    else
21
      open_uri_original_open(name, *rest, &block)
22
    end
23
  end
24
  module_function :open
25
end
26

  
27
module URI
13 28
  # Allows the opening of various resources including URIs.
14 29
  #
15 30
  # If the first argument responds to the 'open' method, 'open' is called on
......
26 41
  #
27 42
  # We can accept URIs and strings that begin with http://, https:// and
28 43
  # ftp://. In these cases, the opened file object is extended by OpenURI::Meta.
29
  def open(name, *rest, &block) # :doc:
44
  def self.open(name, *rest, &block)
30 45
    if name.respond_to?(:open)
31 46
      name.open(*rest, &block)
32 47
    elsif name.respond_to?(:to_str) &&
......
35 50
      uri.open(*rest, &block)
36 51
    else
37 52
      open_uri_original_open(name, *rest, &block)
53
      # After Kernel#open override is removed:
54
      #super
38 55
    end
39 56
  end
40
  module_function :open
41
end
42

  
43
module 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
48 57
end
49 58

  
50 59
# OpenURI is an easy-to-use wrapper for Net::HTTP, Net::HTTPS and Net::FTP.
test/open-uri/test_open-uri.rb
68 68
    @proxies.each_with_index {|k, i| ENV[k] = @old_proxies[i] }
69 69
  end
70 70

  
71
  def test_deprecated_kernel_open
72
    with_http {|srv, dr, url|
73
      srv.mount_proc("/foo200", lambda { |req, res| res.body = "foo200" } )
74
      assert_warning(/calling URI.open via Kernel#open is deprecated, call URI.open directly/) {
75
        open("#{url}/foo200") {|f|
76
          assert_equal("200", f.status[0])
77
          assert_equal("foo200", f.read)
78
        }
79
      }
80
    }
81
  end
82

  
71 83
  def test_200_uri_open
72 84
    with_http {|srv, dr, url|
73 85
      srv.mount_proc("/urifoo200", lambda { |req, res| res.body = "urifoo200" } )
......
81 93
  def test_200
82 94
    with_http {|srv, dr, url|
83 95
      srv.mount_proc("/foo200", lambda { |req, res| res.body = "foo200" } )
84
      open("#{url}/foo200") {|f|
96
      URI.open("#{url}/foo200") {|f|
85 97
        assert_equal("200", f.status[0])
86 98
        assert_equal("foo200", f.read)
87 99
      }
......
92 104
    with_http {|srv, dr, url|
93 105
      content = "foo200big"*10240
94 106
      srv.mount_proc("/foo200big", lambda { |req, res| res.body = content } )
95
      open("#{url}/foo200big") {|f|
107
      URI.open("#{url}/foo200big") {|f|
96 108
        assert_equal("200", f.status[0])
97 109
        assert_equal(content, f.read)
98 110
      }
......
105 117
      assert_match(%r{ERROR `/not-exist' not found}, server_log[0])
106 118
    }
107 119
    with_http(log_tester) {|srv, dr, url, server_thread, server_log|
108
      exc = assert_raise(OpenURI::HTTPError) { open("#{url}/not-exist") {} }
120
      exc = assert_raise(OpenURI::HTTPError) { URI.open("#{url}/not-exist") {} }
109 121
      assert_equal("404", exc.io.status[0])
110 122
    }
111 123
  end
......
114 126
    with_http {|srv, dr, url|
115 127
      srv.mount_proc("/foo_ou", lambda { |req, res| res.body = "foo_ou" } )
116 128
      u = URI("#{url}/foo_ou")
117
      open(u) {|f|
129
      URI.open(u) {|f|
118 130
        assert_equal("200", f.status[0])
119 131
        assert_equal("foo_ou", f.read)
120 132
      }
......
122 134
  end
123 135

  
124 136
  def test_open_too_many_arg
125
    assert_raise(ArgumentError) { open("http://192.0.2.1/tma", "r", 0666, :extra) {} }
137
    assert_raise(ArgumentError) { URI.open("http://192.0.2.1/tma", "r", 0666, :extra) {} }
126 138
  end
127 139

  
128 140
  def test_read_timeout
......
169 181
  end
170 182

  
171 183
  def test_invalid_option
172
    assert_raise(ArgumentError) { open("http://127.0.0.1/", :invalid_option=>true) {} }
184
    assert_raise(ArgumentError) { URI.open("http://127.0.0.1/", :invalid_option=>true) {} }
173 185
  end
174 186

  
175 187
  def test_mode
176 188
    with_http {|srv, dr, url|
177 189
      srv.mount_proc("/mode", lambda { |req, res| res.body = "mode" } )
178
      open("#{url}/mode", "r") {|f|
190
      URI.open("#{url}/mode", "r") {|f|
179 191
        assert_equal("200", f.status[0])
180 192
        assert_equal("mode", f.read)
181 193
      }
182
      open("#{url}/mode", "r", 0600) {|f|
194
      URI.open("#{url}/mode", "r", 0600) {|f|
183 195
        assert_equal("200", f.status[0])
184 196
        assert_equal("mode", f.read)
185 197
      }
186
      assert_raise(ArgumentError) { open("#{url}/mode", "a") {} }
187
      open("#{url}/mode", "r:us-ascii") {|f|
198
      assert_raise(ArgumentError) { URI.open("#{url}/mode", "a") {} }
199
      URI.open("#{url}/mode", "r:us-ascii") {|f|
188 200
        assert_equal(Encoding::US_ASCII, f.read.encoding)
189 201
      }
190
      open("#{url}/mode", "r:utf-8") {|f|
202
      URI.open("#{url}/mode", "r:utf-8") {|f|
191 203
        assert_equal(Encoding::UTF_8, f.read.encoding)
192 204
      }
193
      assert_raise(ArgumentError) { open("#{url}/mode", "r:invalid-encoding") {} }
205
      assert_raise(ArgumentError) { URI.open("#{url}/mode", "r:invalid-encoding") {} }
194 206
    }
195 207
  end
196 208

  
......
198 210
    with_http {|srv, dr, url|
199 211
      srv.mount_proc("/without_block", lambda { |req, res| res.body = "without_block" } )
200 212
      begin
201
        f = open("#{url}/without_block")
213
        f = URI.open("#{url}/without_block")
202 214
        assert_equal("200", f.status[0])
203 215
        assert_equal("without_block", f.read)
204 216
      ensure
......
211 223
    with_http {|srv, dr, url|
212 224
      srv.mount_proc("/close200", lambda { |req, res| res.body = "close200" } )
213 225
      assert_nothing_raised {
214
        open("#{url}/close200") {|f|
226
        URI.open("#{url}/close200") {|f|
215 227
          f.close
216 228
        }
217 229
      }
......
223 235
      content = "close200big"*10240
224 236
      srv.mount_proc("/close200big", lambda { |req, res| res.body = content } )
225 237
      assert_nothing_raised {
226
        open("#{url}/close200big") {|f|
238
        URI.open("#{url}/close200big") {|f|
227 239
          f.close
228 240
        }
229 241
      }
......
235 247
    myheader2 = nil
236 248
    with_http {|srv, dr, url|
237 249
      srv.mount_proc("/h/") {|req, res| myheader2 = req['myheader']; res.body = "foo" }
238
      open("#{url}/h/", 'MyHeader'=>myheader1) {|f|
250
      URI.open("#{url}/h/", 'MyHeader'=>myheader1) {|f|
239 251
        assert_equal("foo", f.read)
240 252
        assert_equal(myheader1, myheader2)
241 253
      }
......
244 256

  
245 257
  def test_multi_proxy_opt
246 258
    assert_raise(ArgumentError) {
247
      open("http://127.0.0.1/", :proxy_http_basic_authentication=>true, :proxy=>true) {}
259
      URI.open("http://127.0.0.1/", :proxy_http_basic_authentication=>true, :proxy=>true) {}
248 260
    }
249 261
  end
250 262

  
251 263
  def test_non_http_proxy
252 264
    assert_raise(RuntimeError) {
253
      open("http://127.0.0.1/", :proxy=>URI("ftp://127.0.0.1/")) {}
265
      URI.open("http://127.0.0.1/", :proxy=>URI("ftp://127.0.0.1/")) {}
254 266
    }
255 267
  end
256 268

  
......
273 285
      begin
274 286
        proxy_thread = proxy.start
275 287
        srv.mount_proc("/proxy", lambda { |req, res| res.body = "proxy" } )
276
        open("#{url}/proxy", :proxy=>proxy_url) {|f|
288
        URI.open("#{url}/proxy", :proxy=>proxy_url) {|f|
277 289
          assert_equal("200", f.status[0])
278 290
          assert_equal("proxy", f.read)
279 291
        }
280 292
        assert_match(/#{Regexp.quote url}/, proxy_auth_log); proxy_auth_log.clear
281
        open("#{url}/proxy", :proxy=>URI(proxy_url)) {|f|
293
        URI.open("#{url}/proxy", :proxy=>URI(proxy_url)) {|f|
282 294
          assert_equal("200", f.status[0])
283 295
          assert_equal("proxy", f.read)
284 296
        }
285 297
        assert_match(/#{Regexp.quote url}/, proxy_auth_log); proxy_auth_log.clear
286
        open("#{url}/proxy", :proxy=>nil) {|f|
298
        URI.open("#{url}/proxy", :proxy=>nil) {|f|
287 299
          assert_equal("200", f.status[0])
288 300
          assert_equal("proxy", f.read)
289 301
        }
290 302
        assert_equal("", proxy_auth_log); proxy_auth_log.clear
291 303
        assert_raise(ArgumentError) {
292
          open("#{url}/proxy", :proxy=>:invalid) {}
304
          URI.open("#{url}/proxy", :proxy=>:invalid) {}
293 305
        }
294 306
        assert_equal("", proxy_auth_log); proxy_auth_log.clear
295 307
        with_env("http_proxy"=>proxy_url) {
296 308
          # should not use proxy for 127.0.0.0/8.
297
          open("#{url}/proxy") {|f|
309
          URI.open("#{url}/proxy") {|f|
298 310
            assert_equal("200", f.status[0])
299 311
            assert_equal("proxy", f.read)
300 312
          }
......
330 342
      begin
331 343
        th = proxy.start
332 344
        srv.mount_proc("/proxy", lambda { |req, res| res.body = "proxy" } )
333
        exc = assert_raise(OpenURI::HTTPError) { open("#{url}/proxy", :proxy=>proxy_url) {} }
345
        exc = assert_raise(OpenURI::HTTPError) { URI.open("#{url}/proxy", :proxy=>proxy_url) {} }
334 346
        assert_equal("407", exc.io.status[0])
335 347
        assert_match(/#{Regexp.quote url}/, proxy_auth_log); proxy_auth_log.clear
336 348
      ensure
......
363 375
      begin
364 376
        th = proxy.start
365 377
        srv.mount_proc("/proxy", lambda { |req, res| res.body = "proxy" } )
366
        open("#{url}/proxy",
378
        URI.open("#{url}/proxy",
367 379
            :proxy_http_basic_authentication=>[proxy_url, "user", "pass"]) {|f|
368 380
          assert_equal("200", f.status[0])
369 381
          assert_equal("proxy", f.read)
370 382
        }
371 383
        assert_match(/#{Regexp.quote url}/, proxy_auth_log); proxy_auth_log.clear
372 384
        assert_raise(ArgumentError) {
373
          open("#{url}/proxy",
385
          URI.open("#{url}/proxy",
374 386
              :proxy_http_basic_authentication=>[true, "user", "pass"]) {}
375 387
        }
376 388
        assert_equal("", proxy_auth_log); proxy_auth_log.clear
......
404 416
      begin
405 417
        th = proxy.start
406 418
        srv.mount_proc("/proxy", lambda { |req, res| res.body = "proxy" } )
407
        open("#{url}/proxy", :proxy => proxy_url) {|f|
419
        URI.open("#{url}/proxy", :proxy => proxy_url) {|f|
408 420
          assert_equal("200", f.status[0])
409 421
          assert_equal("proxy", f.read)
410 422
        }
......
423 435
      srv.mount_proc("/r1/") {|req, res| res.status = 301; res["location"] = "#{url}/r2"; res.body = "r1" }
424 436
      srv.mount_proc("/r2/") {|req, res| res.body = "r2" }
425 437
      srv.mount_proc("/to-file/") {|req, res| res.status = 301; res["location"] = "file:///foo" }
426
      open("#{url}/r1/") {|f|
438
      URI.open("#{url}/r1/") {|f|
427 439
        assert_equal("#{url}/r2", f.base_uri.to_s)
428 440
        assert_equal("r2", f.read)
429 441
      }
430
      assert_raise(OpenURI::HTTPRedirect) { open("#{url}/r1/", :redirect=>false) {} }
431
      assert_raise(RuntimeError) { open("#{url}/to-file/") {} }
442
      assert_raise(OpenURI::HTTPRedirect) { URI.open("#{url}/r1/", :redirect=>false) {} }
443
      assert_raise(RuntimeError) { URI.open("#{url}/to-file/") {} }
432 444
    }
433 445
  end
434 446

  
......
436 448
    with_http {|srv, dr, url|
437 449
      srv.mount_proc("/r1/") {|req, res| res.status = 301; res["location"] = "#{url}/r2"; res.body = "r1" }
438 450
      srv.mount_proc("/r2/") {|req, res| res.status = 301; res["location"] = "#{url}/r1"; res.body = "r2" }
439
      assert_raise(RuntimeError) { open("#{url}/r1/") {} }
451
      assert_raise(RuntimeError) { URI.open("#{url}/r1/") {} }
440 452
    }
441 453
  end
442 454

  
......
515 527
  def test_redirect_auth_success
516 528
    with_http {|srv, dr, url|
517 529
      setup_redirect_auth(srv, url)
518
      open("#{url}/r2/", :http_basic_authentication=>['user', 'pass']) {|f|
530
      URI.open("#{url}/r2/", :http_basic_authentication=>['user', 'pass']) {|f|
519 531
        assert_equal("r2", f.read)
520 532
      }
521 533
    }
......
528 540
    }
529 541
    with_http(log_tester) {|srv, dr, url, server_thread, server_log|
530 542
      setup_redirect_auth(srv, url)
531
      exc = assert_raise(OpenURI::HTTPError) { open("#{url}/r2/") {} }
543
      exc = assert_raise(OpenURI::HTTPError) { URI.open("#{url}/r2/") {} }
532 544
      assert_equal("401", exc.io.status[0])
533 545
    }
534 546
  end
......
540 552
    }
541 553
    with_http(log_tester) {|srv, dr, url, server_thread, server_log|
542 554
      setup_redirect_auth(srv, url)
543
      exc = assert_raise(OpenURI::HTTPError) { open("#{url}/r1/", :http_basic_authentication=>['user', 'pass']) {} }
555
      exc = assert_raise(OpenURI::HTTPError) { URI.open("#{url}/r1/", :http_basic_authentication=>['user', 'pass']) {} }
544 556
      assert_equal("401", exc.io.status[0])
545 557
    }
546 558
  end
547 559

  
548 560
  def test_userinfo
549
    assert_raise(ArgumentError) { open("http://user:pass@127.0.0.1/") {} }
561
    assert_raise(ArgumentError) { URI.open("http://user:pass@127.0.0.1/") {} }
550 562
  end
551 563

  
552 564
  def test_progress
......
555 567
      srv.mount_proc("/data/") {|req, res| res.body = content }
556 568
      length = []
557 569
      progress = []
558
      open("#{url}/data/",
570
      URI.open("#{url}/data/",
559 571
           :content_length_proc => lambda {|n| length << n },
560 572
           :progress_proc => lambda {|n| progress << n }
561 573
          ) {|f|
......
575 587
      srv.mount_proc("/data/") {|req, res| res.body = content; res.chunked = true }
576 588
      length = []
577 589
      progress = []
578
      open("#{url}/data/",
590
      URI.open("#{url}/data/",
579 591
           :content_length_proc => lambda {|n| length << n },
580 592
           :progress_proc => lambda {|n| progress << n }
581 593
          ) {|f|
......
605 617
      srv.mount_proc("/u8/") {|req, res| res.body = content_u8; res['content-type'] = 'text/plain; charset=utf-8' }
606 618
      srv.mount_proc("/ej/") {|req, res| res.body = content_ej; res['content-type'] = 'TEXT/PLAIN; charset=EUC-JP' }
607 619
      srv.mount_proc("/nc/") {|req, res| res.body = "aa"; res['content-type'] = 'Text/Plain' }
608
      open("#{url}/u8/") {|f|
620
      URI.open("#{url}/u8/") {|f|
609 621
        assert_equal(content_u8, f.read)
610 622
        assert_equal("text/plain", f.content_type)
611 623
        assert_equal("utf-8", f.charset)
612 624
      }
613
      open("#{url}/ej/") {|f|
625
      URI.open("#{url}/ej/") {|f|
614 626
        assert_equal(content_ej, f.read)
615 627
        assert_equal("text/plain", f.content_type)
616 628
        assert_equal("euc-jp", f.charset)
617 629
        assert_equal(Encoding::EUC_JP, f.read.encoding)
618 630
      }
619
      open("#{url}/ej/", 'r:utf-8') {|f|
631
      URI.open("#{url}/ej/", 'r:utf-8') {|f|
620 632
        # override charset with encoding option
621 633
        assert_equal(content_ej.dup.force_encoding('utf-8'), f.read)
622 634
        assert_equal("text/plain", f.content_type)
623 635
        assert_equal("euc-jp", f.charset)
624 636
        assert_equal(Encoding::UTF_8, f.read.encoding)
625 637
      }
626
      open("#{url}/ej/", :encoding=>'utf-8') {|f|
638
      URI.open("#{url}/ej/", :encoding=>'utf-8') {|f|
627 639
        # override charset with encoding option
628 640
        assert_equal(content_ej.dup.force_encoding('utf-8'), f.read)
629 641
        assert_equal("text/plain", f.content_type)
......
631 643
        assert_equal(Encoding::UTF_8, f.read.encoding)
632 644
      }
633 645
      assert_raise(ArgumentError) {
634
        open("#{url}/ej/", 'r:utf-8', :encoding=>'utf-8') {|f| }
646
        URI.open("#{url}/ej/", 'r:utf-8', :encoding=>'utf-8') {|f| }
635 647
      }
636
      open("#{url}/nc/") {|f|
648
      URI.open("#{url}/nc/") {|f|
637 649
        assert_equal("aa", f.read)
638 650
        assert_equal("text/plain", f.content_type)
639 651
        assert_equal("iso-8859-1", f.charset)
......
646 658
    with_http {|srv, dr, url|
647 659
      content_u8 = "\u3042"
648 660
      srv.mount_proc("/qu8/") {|req, res| res.body = content_u8; res['content-type'] = 'text/plain; charset="utf\-8"' }
649
      open("#{url}/qu8/") {|f|
661
      URI.open("#{url}/qu8/") {|f|
650 662
        assert_equal(content_u8, f.read)
651 663
        assert_equal("text/plain", f.content_type)
652 664
        assert_equal("utf-8", f.charset)
......
657 669
  def test_last_modified
658 670
    with_http {|srv, dr, url|
659 671
      srv.mount_proc("/data/") {|req, res| res.body = "foo"; res['last-modified'] = 'Fri, 07 Aug 2009 06:05:04 GMT' }
660
      open("#{url}/data/") {|f|
672
      URI.open("#{url}/data/") {|f|
661 673
        assert_equal("foo", f.read)
662 674
        assert_equal(Time.utc(2009,8,7,6,5,4), f.last_modified)
663 675
      }
......
671 683
      srv.mount_proc("/data/") {|req, res| res.body = content_gz; res['content-encoding'] = 'gzip' }
672 684
      srv.mount_proc("/data2/") {|req, res| res.body = content_gz; res['content-encoding'] = 'gzip'; res.chunked = true }
673 685
      srv.mount_proc("/noce/") {|req, res| res.body = content_gz }
674
      open("#{url}/data/") {|f|
686
      URI.open("#{url}/data/") {|f|
675 687
        assert_equal [], f.content_encoding
676 688
        assert_equal(content, f.read)
677 689
      }
678
      open("#{url}/data2/") {|f|
690
      URI.open("#{url}/data2/") {|f|
679 691
        assert_equal [], f.content_encoding
680 692
        assert_equal(content, f.read)
681 693
      }
682
      open("#{url}/noce/") {|f|
694
      URI.open("#{url}/noce/") {|f|
683 695
        assert_equal [], f.content_encoding
684 696
        assert_equal(content_gz, f.read.force_encoding("ascii-8bit"))
685 697
      }
......
693 705
        res.cookies << "name2=value2; blabla"
694 706
        res.body = "foo"
695 707
      }
696
      open("#{url}/mcookie/") {|f|
708
      URI.open("#{url}/mcookie/") {|f|
697 709
        assert_equal("foo", f.read)
698 710
        assert_equal(["name1=value1; blabla", "name2=value2; blabla"],
699 711
                     f.metas['set-cookie'].sort)
test/open-uri/test_ssl.rb
67 67

  
68 68
  def setup_validation(srv, dr)
69 69
    cacert_filename = "#{dr}/cacert.pem"
70
    open(cacert_filename, "w") {|f| f << CA_CERT }
70
    URI.open(cacert_filename, "w") {|f| f << CA_CERT }
71 71
    srv.mount_proc("/data", lambda { |req, res| res.body = "ddd" } )
72 72
    cacert_filename
73 73
  end
......
75 75
  def test_validation_success
76 76
    with_https {|srv, dr, url|
77 77
      cacert_filename = setup_validation(srv, dr)
78
      open("#{url}/data", :ssl_ca_cert => cacert_filename) {|f|
78
      URI.open("#{url}/data", :ssl_ca_cert => cacert_filename) {|f|
79 79
        assert_equal("200", f.status[0])
80 80
        assert_equal("ddd", f.read)
81 81
      }
......
85 85
  def test_validation_noverify
86 86
    with_https {|srv, dr, url|
87 87
      setup_validation(srv, dr)
88
      open("#{url}/data", :ssl_verify_mode => OpenSSL::SSL::VERIFY_NONE) {|f|
88
      URI.open("#{url}/data", :ssl_verify_mode => OpenSSL::SSL::VERIFY_NONE) {|f|
89 89
        assert_equal("200", f.status[0])
90 90
        assert_equal("ddd", f.read)
91 91
      }
......
103 103
    end
104 104
    with_https(log_tester) {|srv, dr, url, server_thread, server_log|
105 105
      setup_validation(srv, dr)
106
      assert_raise(OpenSSL::SSL::SSLError) { open("#{url}/data") {} }
106
      assert_raise(OpenSSL::SSL::SSLError) { URI.open("#{url}/data") {} }
107 107
    }
108 108
  end
109 109

  
......
149 149
    }
150 150
    with_https_proxy(proxy_log_tester) {|srv, dr, url_, cacert_filename, cacert_directory, proxy_host, proxy_port|
151 151
      url = url_
152
      open("#{url}/proxy", :proxy=>"http://#{proxy_host}:#{proxy_port}/", :ssl_ca_cert => cacert_filename) {|f|
152
      URI.open("#{url}/proxy", :proxy=>"http://#{proxy_host}:#{proxy_port}/", :ssl_ca_cert => cacert_filename) {|f|
153 153
        assert_equal("200", f.status[0])
154 154
        assert_equal("proxy", f.read)
155 155
      }
......
165 165
    }
166 166
    with_https_proxy(proxy_log_tester) {|srv, dr, url_, cacert_filename, cacert_directory, proxy_host, proxy_port|
167 167
      url = url_
168
      open("#{url}/proxy", :proxy=>"http://#{proxy_host}:#{proxy_port}/", :ssl_ca_cert => cacert_directory) {|f|
168
      URI.open("#{url}/proxy", :proxy=>"http://#{proxy_host}:#{proxy_port}/", :ssl_ca_cert => cacert_directory) {|f|
169 169
        assert_equal("200", f.status[0])
170 170
        assert_equal("proxy", f.read)
171 171
      }
172
-