open-uri 0.4.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ba843c62b6eeedc0ba41feb9a8539cbbaff5f363cd6f960f7c004153e32b2fbf
4
- data.tar.gz: 8de6ddfea24c2b7596ae35a4094b3bbf30cae1ed92d142b1a7b0772c9200b456
3
+ metadata.gz: d13353141f32e54cbb98e25fe19197d3ad8224d3c661fba933442db5b18b3b2e
4
+ data.tar.gz: ef41b498379d50506d963d85993281d29b9c67723ac791bfeb6221a395141c9d
5
5
  SHA512:
6
- metadata.gz: a48baeacc9043f013c631ba9f07e4ac9eadff5d04a207dbe87d428d2f0393feb605281f91335ae74bedc2351b238f96858b3cad97e311e36a257026993106c8a
7
- data.tar.gz: f8f47f6f52bdf4cc73a2d3cf4c898bebe8ccbe8329f53e68b9532b658fdbbe976a5a4d0a6c0d069722b6f260b707a20b2f6ab49aa200db800a2d7e29b0cd4814
6
+ metadata.gz: e520fd4b3d3ede380bf840470bf28a2d529f7bc2658a6e8645436b53c0dd2808cefd899e5eff73391a54974760decb29511795c50348a29d02b598e23fed2433
7
+ data.tar.gz: e6bff48cd004d12658c238ed6a2979be0a4a12ef6c670e99cb36f6cb6cb88359ff1ca0b4968c5d428185c02a7c055af3290deeacc4d9bb42c2ad20249dc5f3f7
@@ -4,10 +4,10 @@ Redistribution and use in source and binary forms, with or without
4
4
  modification, are permitted provided that the following conditions
5
5
  are met:
6
6
  1. Redistributions of source code must retain the above copyright
7
- notice, this list of conditions and the following disclaimer.
7
+ notice, this list of conditions and the following disclaimer.
8
8
  2. Redistributions in binary form must reproduce the above copyright
9
- notice, this list of conditions and the following disclaimer in the
10
- documentation and/or other materials provided with the distribution.
9
+ notice, this list of conditions and the following disclaimer in the
10
+ documentation and/or other materials provided with the distribution.
11
11
 
12
12
  THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
13
13
  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
data/COPYING ADDED
@@ -0,0 +1,56 @@
1
+ Ruby is copyrighted free software by Yukihiro Matsumoto <matz@netlab.jp>.
2
+ You can redistribute it and/or modify it under either the terms of the
3
+ 2-clause BSDL (see the file BSDL), or the conditions below:
4
+
5
+ 1. You may make and give away verbatim copies of the source form of the
6
+ software without restriction, provided that you duplicate all of the
7
+ original copyright notices and associated disclaimers.
8
+
9
+ 2. You may modify your copy of the software in any way, provided that
10
+ you do at least ONE of the following:
11
+
12
+ a. place your modifications in the Public Domain or otherwise
13
+ make them Freely Available, such as by posting said
14
+ modifications to Usenet or an equivalent medium, or by allowing
15
+ the author to include your modifications in the software.
16
+
17
+ b. use the modified software only within your corporation or
18
+ organization.
19
+
20
+ c. give non-standard binaries non-standard names, with
21
+ instructions on where to get the original software distribution.
22
+
23
+ d. make other distribution arrangements with the author.
24
+
25
+ 3. You may distribute the software in object code or binary form,
26
+ provided that you do at least ONE of the following:
27
+
28
+ a. distribute the binaries and library files of the software,
29
+ together with instructions (in the manual page or equivalent)
30
+ on where to get the original distribution.
31
+
32
+ b. accompany the distribution with the machine-readable source of
33
+ the software.
34
+
35
+ c. give non-standard binaries non-standard names, with
36
+ instructions on where to get the original software distribution.
37
+
38
+ d. make other distribution arrangements with the author.
39
+
40
+ 4. You may modify and include the part of the software into any other
41
+ software (possibly commercial). But some files in the distribution
42
+ are not written by the author, so that they are not under these terms.
43
+
44
+ For the list of those files and their copying conditions, see the
45
+ file LEGAL.
46
+
47
+ 5. The scripts and library files supplied as input to or produced as
48
+ output from the software do not automatically fall under the
49
+ copyright of the software, but belong to whomever generated them,
50
+ and may be sold commercially, and may be aggregated with this
51
+ software.
52
+
53
+ 6. THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
54
+ IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
55
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
56
+ PURPOSE.
data/lib/open-uri.rb CHANGED
@@ -91,7 +91,7 @@ end
91
91
 
92
92
  module OpenURI
93
93
 
94
- VERSION = "0.4.0"
94
+ VERSION = "0.5.0"
95
95
 
96
96
  Options = {
97
97
  :proxy => true,
@@ -108,6 +108,8 @@ module OpenURI
108
108
  :ftp_active_mode => false,
109
109
  :redirect => true,
110
110
  :encoding => nil,
111
+ :max_redirects => 64,
112
+ :request_specific_fields => nil,
111
113
  }
112
114
 
113
115
  def OpenURI.check_options(options) # :nodoc:
@@ -147,7 +149,11 @@ module OpenURI
147
149
  end
148
150
  encoding = Encoding.find(options[:encoding])
149
151
  end
150
-
152
+ if options.has_key? :request_specific_fields
153
+ if !(options[:request_specific_fields].is_a?(Hash) || options[:request_specific_fields].is_a?(Proc))
154
+ raise ArgumentError, "Invalid request_specific_fields option: #{options[:request_specific_fields].inspect}"
155
+ end
156
+ end
151
157
  unless mode == nil ||
152
158
  mode == 'r' || mode == 'rb' ||
153
159
  mode == File::RDONLY
@@ -211,11 +217,20 @@ module OpenURI
211
217
  end
212
218
 
213
219
  uri_set = {}
220
+ max_redirects = options[:max_redirects] || Options.fetch(:max_redirects)
214
221
  buf = nil
215
222
  while true
223
+ request_specific_fields = {}
224
+ if options.has_key? :request_specific_fields
225
+ request_specific_fields = if options[:request_specific_fields].is_a?(Hash)
226
+ options[:request_specific_fields]
227
+ else options[:request_specific_fields].is_a?(Proc)
228
+ options[:request_specific_fields].call(uri)
229
+ end
230
+ end
216
231
  redirect = catch(:open_uri_redirect) {
217
232
  buf = Buffer.new
218
- uri.buffer_open(buf, find_proxy.call(uri), options)
233
+ uri.buffer_open(buf, find_proxy.call(uri), options.merge(request_specific_fields))
219
234
  nil
220
235
  }
221
236
  if redirect
@@ -235,9 +250,14 @@ module OpenURI
235
250
  options = options.dup
236
251
  options.delete :http_basic_authentication
237
252
  end
253
+ if options.include?(:request_specific_fields) && options[:request_specific_fields].is_a?(Hash)
254
+ # Send request specific headers only for the initial request.
255
+ options.delete :request_specific_fields
256
+ end
238
257
  uri = redirect
239
258
  raise "HTTP redirection loop: #{uri}" if uri_set.include? uri.to_s
240
259
  uri_set[uri.to_s] = true
260
+ raise TooManyRedirects.new("Too many redirects", buf.io) if max_redirects && uri_set.size > max_redirects
241
261
  else
242
262
  break
243
263
  end
@@ -392,6 +412,9 @@ module OpenURI
392
412
  attr_reader :uri
393
413
  end
394
414
 
415
+ class TooManyRedirects < HTTPError
416
+ end
417
+
395
418
  class Buffer # :nodoc: all
396
419
  def initialize
397
420
  @io = StringIO.new
@@ -740,6 +763,44 @@ module OpenURI
740
763
  # Using +true+ also means that redirections between http and ftp are
741
764
  # permitted.
742
765
  #
766
+ # [:max_redirects]
767
+ # Synopsis:
768
+ # :max_redirects=>int
769
+ #
770
+ # Number of HTTP redirects allowed before OpenURI::TooManyRedirects is raised.
771
+ # The default is 64.
772
+ #
773
+ # [:request_specific_fields]
774
+ # Synopsis:
775
+ # :request_specific_fields => {}
776
+ # :request_specific_fields => lambda {|url| ...}
777
+ #
778
+ # :request_specific_fields option allows specifying custom header fields that
779
+ # are sent with the HTTP request. It can be passed as a Hash or a Proc that
780
+ # gets evaluated on each request and returns a Hash of header fields.
781
+ #
782
+ # If a Hash is provided, it specifies the headers only for the initial
783
+ # request and these headers will not be sent on redirects.
784
+ #
785
+ # If a Proc is provided, it will be executed for each request including
786
+ # redirects, allowing dynamic header customization based on the request URL.
787
+ # It is important that the Proc returns a Hash. And this Hash specifies the
788
+ # headers to be sent with the request.
789
+ #
790
+ # For Example with Hash
791
+ # URI.open("http://...",
792
+ # request_specific_fields: {"Authorization" => "token dummy"}) {|f| ... }
793
+ #
794
+ # For Example with Proc:
795
+ # URI.open("http://...",
796
+ # request_specific_fields: lambda { |uri|
797
+ # if uri.host == "example.com"
798
+ # {"Authorization" => "token dummy"}
799
+ # else
800
+ # {}
801
+ # end
802
+ # }) {|f| ... }
803
+ #
743
804
  def open(*rest, &block)
744
805
  OpenURI.open_uri(self, *rest, &block)
745
806
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: open-uri
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tanaka Akira
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-11-07 00:00:00.000000000 Z
11
+ date: 2024-11-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: uri
@@ -59,7 +59,8 @@ executables: []
59
59
  extensions: []
60
60
  extra_rdoc_files: []
61
61
  files:
62
- - LICENSE.txt
62
+ - BSDL
63
+ - COPYING
63
64
  - README.md
64
65
  - lib/open-uri.rb
65
66
  homepage: https://github.com/ruby/open-uri
@@ -69,7 +70,7 @@ licenses:
69
70
  metadata:
70
71
  homepage_uri: https://github.com/ruby/open-uri
71
72
  source_code_uri: https://github.com/ruby/open-uri
72
- post_install_message:
73
+ post_install_message:
73
74
  rdoc_options: []
74
75
  require_paths:
75
76
  - lib
@@ -84,8 +85,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
84
85
  - !ruby/object:Gem::Version
85
86
  version: '0'
86
87
  requirements: []
87
- rubygems_version: 3.5.0.dev
88
- signing_key:
88
+ rubygems_version: 3.5.11
89
+ signing_key:
89
90
  specification_version: 4
90
91
  summary: An easy-to-use wrapper for Net::HTTP, Net::HTTPS and Net::FTP.
91
92
  test_files: []