open_uri_redirections 0.1.3 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/LICENSE.txt +1 -1
- data/README.md +3 -3
- data/lib/open-uri/redirections_patch.rb +12 -4
- data/lib/open_uri_redirections/version.rb +1 -1
- data/spec/redirections_spec.rb +12 -6
- data/spec/samples/https_unsafe.response +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dc4b17aba7d8dceb5ba1ddc29af49955c6e91b58
|
4
|
+
data.tar.gz: 236a88b135504f1d26594073b0749f2ff7a3a825
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fa7c16dfd95c36a7ac291a2b6505eb7f576b8bf0610616352866507bf8fd51b591bbd7eb71475ab9b058db2799c996884a3744ea96619e8b6fa1f71bb90e9a58
|
7
|
+
data.tar.gz: 1b2ea3d8b764a3b16610417f3e55bc4cad8e00dd43d51fc9f3f3976229187f78eba3bd8b814dc040bd6ce11768061db1e53b67c9f146c5d4502ed74f8b35c775
|
data/LICENSE.txt
CHANGED
@@ -19,4 +19,4 @@ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
19
19
|
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
20
|
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
21
|
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
-
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
CHANGED
@@ -8,7 +8,7 @@ Here is the problem it tries to solve:
|
|
8
8
|
|
9
9
|
$ irb
|
10
10
|
1.9.2p320 :001 > require 'open-uri'
|
11
|
-
=> true
|
11
|
+
=> true
|
12
12
|
1.9.2p320 :002 > open('http://github.com')
|
13
13
|
RuntimeError: redirection forbidden: http://github.com -> https://github.com/
|
14
14
|
|
@@ -16,9 +16,9 @@ And here is how you can use this patch to follow the redirections:
|
|
16
16
|
|
17
17
|
$ irb
|
18
18
|
1.9.2p320 :001 > require 'open-uri'
|
19
|
-
=> true
|
19
|
+
=> true
|
20
20
|
> require 'open_uri_redirections'
|
21
|
-
=> true
|
21
|
+
=> true
|
22
22
|
1.9.2p320 :002 > open('http://github.com', :allow_redirections => :safe)
|
23
23
|
=> #<File:/var/folders/...>
|
24
24
|
|
@@ -27,11 +27,8 @@ module OpenURI
|
|
27
27
|
# :allow_redirections => :safe will allow HTTP => HTTPS redirections.
|
28
28
|
# :allow_redirections => :all will allow HTTP => HTTPS and HTTPS => HTTP redirections.
|
29
29
|
#
|
30
|
-
# The original open_uri takes *args but then doesn't do anything with them.
|
31
|
-
# Assume we can only handle a hash.
|
32
30
|
def self.open_uri(name, *rest, &block)
|
33
|
-
|
34
|
-
options = rest.first if !rest.empty? && Hash === rest.first
|
31
|
+
options = self.first_hash_argument(rest)
|
35
32
|
|
36
33
|
allow_redirections = options.delete :allow_redirections if options
|
37
34
|
case allow_redirections
|
@@ -54,4 +51,15 @@ module OpenURI
|
|
54
51
|
|
55
52
|
self.open_uri_original name, *rest, &block
|
56
53
|
end
|
54
|
+
|
55
|
+
private
|
56
|
+
|
57
|
+
# OpenURI::open can receive different kinds of arguments, like a string for the mode
|
58
|
+
# or an integer for the permissions, and then a hash with options like UserAgent, etc.
|
59
|
+
#
|
60
|
+
# This method helps us find this options hash, as it is where our :allow_redirections
|
61
|
+
# option will reside.
|
62
|
+
def self.first_hash_argument(arguments)
|
63
|
+
arguments.select {|arg| Hash === arg}.first
|
64
|
+
end
|
57
65
|
end
|
data/spec/redirections_spec.rb
CHANGED
@@ -36,7 +36,7 @@ describe "OpenURI" do
|
|
36
36
|
end
|
37
37
|
|
38
38
|
it "should follow safe redirections with block" do
|
39
|
-
expect { |b|
|
39
|
+
expect { |b|
|
40
40
|
open("http://safe.com", :allow_redirections => :safe, &b)
|
41
41
|
}.to yield_control
|
42
42
|
end
|
@@ -64,30 +64,36 @@ describe "OpenURI" do
|
|
64
64
|
end
|
65
65
|
|
66
66
|
it "should follow safe redirections with block" do
|
67
|
-
expect { |b|
|
67
|
+
expect { |b|
|
68
68
|
open("http://safe.com", :allow_redirections => :all, &b)
|
69
69
|
}.to yield_control
|
70
70
|
end
|
71
71
|
|
72
72
|
it "should follow unsafe redirections with block" do
|
73
|
-
expect { |b|
|
73
|
+
expect { |b|
|
74
74
|
open("https://unsafe.com", :allow_redirections => :all, &b)
|
75
75
|
}.to yield_control
|
76
76
|
end
|
77
77
|
end
|
78
78
|
|
79
|
-
describe "
|
79
|
+
describe "passing arguments down the stack" do
|
80
80
|
it "should disallow HTTP => HTTPS redirections" do
|
81
81
|
expect {
|
82
|
-
open("http://safe.com", 'r')
|
82
|
+
open("http://safe.com", 'r', 0444, "User-Agent" => "Mozilla/5.0")
|
83
83
|
}.to raise_error(RuntimeError, "redirection forbidden: http://safe.com -> https://safe.com/")
|
84
84
|
end
|
85
85
|
|
86
86
|
it "should allow HTTP => HTTPS redirections" do
|
87
87
|
expect {
|
88
|
-
open("http://safe.com", 'r', :allow_redirections => :safe)
|
88
|
+
open("http://safe.com", 'r', 0444, "User-Agent" => "Mozilla/5.0", :allow_redirections => :safe)
|
89
89
|
}.to_not raise_error
|
90
90
|
end
|
91
|
+
|
92
|
+
it "should pass the arguments down the stack" do
|
93
|
+
OpenURI.should_receive(:open_uri_original).with(an_instance_of(URI::HTTP), "r", 0444, { "User-Agent" => "Mozilla/5.0" })
|
94
|
+
|
95
|
+
open("http://safe.com", 'r', 0444, "User-Agent" => "Mozilla/5.0", :allow_redirections => :safe)
|
96
|
+
end
|
91
97
|
end
|
92
98
|
end
|
93
99
|
end
|