open_uri_redirections 0.1.4 → 0.2.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 +4 -4
- data/.travis.yml +14 -4
- data/LICENSE.txt +2 -1
- data/README.md +27 -16
- data/lib/open-uri/redirections_patch.rb +27 -20
- data/lib/open_uri_redirections/version.rb +1 -1
- data/open_uri_redirections.gemspec +3 -3
- data/spec/redirections_spec.rb +48 -4
- metadata +18 -17
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 700a295a62e1dcaa24dbb77e2c73cdfb99b25f26
|
4
|
+
data.tar.gz: 9e4e1ce91c9443fbb229ee34b6de90bdc9aba6b5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 60ceb82cdbded3b4af61108efa78342a8e9971bcebc2a3ddc718769e5abe2b653c846c1759a740c35a633ffab84d54049d95b1890338ef83d0363b2142801567
|
7
|
+
data.tar.gz: d166c07b860618625f4dd0a67aeb251d9cf4f73bbe54642553c56cda007a2d6b3381f31b98342ab4316aa8dda4908373864e01018d58403d63464f1dc2179eef
|
data/.travis.yml
CHANGED
data/LICENSE.txt
CHANGED
data/README.md
CHANGED
@@ -2,25 +2,29 @@
|
|
2
2
|
|
3
3
|
This gem applies a patch to OpenURI to optionally allow redirections from HTTP to HTTPS, or from HTTPS to HTTP.
|
4
4
|
|
5
|
-
This is based on [this patch](http://bugs.ruby-lang.org/issues/859), and modified to allow redirections in both directions.
|
5
|
+
This is based on [this patch](http://bugs.ruby-lang.org/issues/859) and [this gem](https://github.com/obfusk/open_uri_w_redirect_to_https), and modified to allow redirections in both directions.
|
6
6
|
|
7
7
|
Here is the problem it tries to solve:
|
8
8
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
9
|
+
```sh
|
10
|
+
$ irb
|
11
|
+
1.9.2p320 :001 > require 'open-uri'
|
12
|
+
=> true
|
13
|
+
1.9.2p320 :002 > open('http://github.com')
|
14
|
+
RuntimeError: redirection forbidden: http://github.com -> https://github.com/
|
15
|
+
```
|
14
16
|
|
15
17
|
And here is how you can use this patch to follow the redirections:
|
16
18
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
19
|
+
```sh
|
20
|
+
$ irb
|
21
|
+
1.9.2p320 :001 > require 'open-uri'
|
22
|
+
=> true
|
23
|
+
> require 'open_uri_redirections'
|
24
|
+
=> true
|
25
|
+
1.9.2p320 :002 > open('http://github.com', :allow_redirections => :safe)
|
26
|
+
=> #<File:/var/folders/...>
|
27
|
+
```
|
24
28
|
|
25
29
|
The patch contained in this gem adds the :allow_redirections option to `OpenURI#open`:
|
26
30
|
|
@@ -35,11 +39,14 @@ Before using this gem, read this:
|
|
35
39
|
[https://gist.github.com/1271420](https://gist.github.com/1271420)
|
36
40
|
|
37
41
|
### Relevant issue:
|
38
|
-
[
|
42
|
+
[https://bugs.ruby-lang.org/issues/3719](https://bugs.ruby-lang.org/issues/3719)
|
39
43
|
|
40
44
|
### Source here:
|
41
45
|
[https://github.com/ruby/ruby/blob/trunk/lib/open-uri.rb](https://github.com/ruby/ruby/blob/trunk/lib/open-uri.rb)
|
42
46
|
|
47
|
+
### Thread-safe implementation adapted from this gem:
|
48
|
+
[https://github.com/obfusk/open_uri_w_redirect_to_https](https://github.com/obfusk/open_uri_w_redirect_to_https)
|
49
|
+
|
43
50
|
Use it at your own risk!
|
44
51
|
|
45
52
|
## Installation
|
@@ -50,11 +57,15 @@ Add this line to your application's Gemfile:
|
|
50
57
|
|
51
58
|
And then execute:
|
52
59
|
|
53
|
-
|
60
|
+
```sh
|
61
|
+
$ bundle install
|
62
|
+
```
|
54
63
|
|
55
64
|
Or install it yourself as:
|
56
65
|
|
57
|
-
|
66
|
+
```sh
|
67
|
+
$ gem install open_uri_redirections
|
68
|
+
```
|
58
69
|
|
59
70
|
## Contributing
|
60
71
|
|
@@ -7,14 +7,33 @@
|
|
7
7
|
#
|
8
8
|
# Source here:
|
9
9
|
# https://github.com/ruby/ruby/blob/trunk/lib/open-uri.rb
|
10
|
+
#
|
11
|
+
# Thread-safe implementation adapted from:
|
12
|
+
# https://github.com/obfusk/open_uri_w_redirect_to_https
|
10
13
|
|
11
14
|
module OpenURI
|
12
15
|
class <<self
|
13
16
|
alias_method :open_uri_original, :open_uri
|
14
17
|
alias_method :redirectable_cautious?, :redirectable?
|
15
18
|
|
19
|
+
def redirectable?(uri1, uri2)
|
20
|
+
allow_redirections = Thread.current[:__open_uri_redirections__]
|
21
|
+
|
22
|
+
# clear to prevent leaking (e.g. to block)
|
23
|
+
Thread.current[:__open_uri_redirections__] = nil
|
24
|
+
|
25
|
+
case allow_redirections
|
26
|
+
when :safe
|
27
|
+
redirectable_safe? uri1, uri2
|
28
|
+
when :all
|
29
|
+
redirectable_all? uri1, uri2
|
30
|
+
else
|
31
|
+
redirectable_cautious? uri1, uri2
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
16
35
|
def redirectable_safe?(uri1, uri2)
|
17
|
-
uri1
|
36
|
+
redirectable_cautious?(uri1, uri2) || (uri1.scheme.downcase == "http" && uri2.scheme.downcase == "https")
|
18
37
|
end
|
19
38
|
|
20
39
|
def redirectable_all?(uri1, uri2)
|
@@ -29,27 +48,15 @@ module OpenURI
|
|
29
48
|
#
|
30
49
|
def self.open_uri(name, *rest, &block)
|
31
50
|
options = self.first_hash_argument(rest)
|
32
|
-
|
33
51
|
allow_redirections = options.delete :allow_redirections if options
|
34
|
-
|
35
|
-
when :safe
|
36
|
-
class << self
|
37
|
-
remove_method :redirectable?
|
38
|
-
alias_method :redirectable?, :redirectable_safe?
|
39
|
-
end
|
40
|
-
when :all
|
41
|
-
class << self
|
42
|
-
remove_method :redirectable?
|
43
|
-
alias_method :redirectable?, :redirectable_all?
|
44
|
-
end
|
45
|
-
else
|
46
|
-
class << self
|
47
|
-
remove_method :redirectable?
|
48
|
-
alias_method :redirectable?, :redirectable_cautious?
|
49
|
-
end
|
50
|
-
end
|
52
|
+
Thread.current[:__open_uri_redirections__] = allow_redirections
|
51
53
|
|
52
|
-
|
54
|
+
begin
|
55
|
+
self.open_uri_original name, *rest, &block
|
56
|
+
ensure
|
57
|
+
# clear (redirectable? might not be called due to an exception)
|
58
|
+
Thread.current[:__open_uri_redirections__] = nil
|
59
|
+
end
|
53
60
|
end
|
54
61
|
|
55
62
|
private
|
@@ -6,7 +6,7 @@ require 'open_uri_redirections/version'
|
|
6
6
|
Gem::Specification.new do |gem|
|
7
7
|
gem.name = "open_uri_redirections"
|
8
8
|
gem.version = OpenUriRedirections::VERSION
|
9
|
-
gem.authors = ["Jaime Iniesta", "Gabriel Cebrian"]
|
9
|
+
gem.authors = ["Jaime Iniesta", "Gabriel Cebrian", "Felix C. Stegerman"]
|
10
10
|
gem.email = ["jaimeiniesta@gmail.com"]
|
11
11
|
gem.description = %q{OpenURI patch to allow redirections between HTTP and HTTPS}
|
12
12
|
gem.summary = %q{OpenURI patch to allow redirections between HTTP and HTTPS}
|
@@ -17,7 +17,7 @@ Gem::Specification.new do |gem|
|
|
17
17
|
gem.test_files = gem.files.grep(%r{^(spec)/})
|
18
18
|
gem.require_paths = ["lib"]
|
19
19
|
|
20
|
-
gem.add_development_dependency 'rspec', '~>
|
20
|
+
gem.add_development_dependency 'rspec', '~> 3.1.0'
|
21
21
|
gem.add_development_dependency 'fakeweb', '~> 1.3.0'
|
22
|
-
gem.add_development_dependency 'rake', '~> 10.
|
22
|
+
gem.add_development_dependency 'rake', '~> 10.3.2'
|
23
23
|
end
|
data/spec/redirections_spec.rb
CHANGED
@@ -2,6 +2,10 @@
|
|
2
2
|
|
3
3
|
require File.join(File.dirname(__FILE__), "/spec_helper")
|
4
4
|
|
5
|
+
class << OpenURI
|
6
|
+
alias_method :open_uri_original__, :open_uri_original
|
7
|
+
end
|
8
|
+
|
5
9
|
describe "OpenURI" do
|
6
10
|
describe "#open" do
|
7
11
|
describe "Default settings" do
|
@@ -32,7 +36,9 @@ describe "OpenURI" do
|
|
32
36
|
end
|
33
37
|
|
34
38
|
it "should follow safe redirections" do
|
35
|
-
|
39
|
+
expect(
|
40
|
+
open("http://safe.com", :allow_redirections => :safe).read
|
41
|
+
).to eq("Hello, this is Safe.")
|
36
42
|
end
|
37
43
|
|
38
44
|
it "should follow safe redirections with block" do
|
@@ -56,11 +62,15 @@ describe "OpenURI" do
|
|
56
62
|
end
|
57
63
|
|
58
64
|
it "should follow safe redirections" do
|
59
|
-
|
65
|
+
expect(
|
66
|
+
open("http://safe.com", :allow_redirections => :all).read
|
67
|
+
).to eq("Hello, this is Safe.")
|
60
68
|
end
|
61
69
|
|
62
70
|
it "should follow unsafe redirections" do
|
63
|
-
|
71
|
+
expect(
|
72
|
+
open("https://unsafe.com", :allow_redirections => :all).read
|
73
|
+
).to eq("Hello, this is Unsafe.")
|
64
74
|
end
|
65
75
|
|
66
76
|
it "should follow safe redirections with block" do
|
@@ -90,10 +100,44 @@ describe "OpenURI" do
|
|
90
100
|
end
|
91
101
|
|
92
102
|
it "should pass the arguments down the stack" do
|
93
|
-
OpenURI.
|
103
|
+
expect(OpenURI).to receive(:open_uri_original).with(an_instance_of(URI::HTTP), "r", 0444, { "User-Agent" => "Mozilla/5.0" })
|
94
104
|
|
95
105
|
open("http://safe.com", 'r', 0444, "User-Agent" => "Mozilla/5.0", :allow_redirections => :safe)
|
96
106
|
end
|
97
107
|
end
|
108
|
+
|
109
|
+
describe "threads" do
|
110
|
+
it "works" do
|
111
|
+
allow(OpenURI).to receive(:open_uri_original) { |*a,&b| sleep rand; OpenURI.open_uri_original__ *a, &b }
|
112
|
+
ts = []
|
113
|
+
Thread.abort_on_exception = true
|
114
|
+
begin
|
115
|
+
100.times {
|
116
|
+
ts << Thread.new {
|
117
|
+
expect {
|
118
|
+
open("http://safe.com")
|
119
|
+
}.to raise_error(RuntimeError, "redirection forbidden: http://safe.com -> https://safe.com/")
|
120
|
+
}
|
121
|
+
ts << Thread.new {
|
122
|
+
expect {
|
123
|
+
open("http://safe.com", :allow_redirections => :safe)
|
124
|
+
}.to_not raise_error
|
125
|
+
}
|
126
|
+
ts << Thread.new {
|
127
|
+
expect {
|
128
|
+
open("https://unsafe.com")
|
129
|
+
}.to raise_error(RuntimeError, "redirection forbidden: https://unsafe.com -> http://unsafe.com/")
|
130
|
+
}
|
131
|
+
ts << Thread.new {
|
132
|
+
expect {
|
133
|
+
open("https://unsafe.com", :allow_redirections => :safe)
|
134
|
+
}.to raise_error(RuntimeError, "redirection forbidden: https://unsafe.com -> http://unsafe.com/")
|
135
|
+
}
|
136
|
+
}
|
137
|
+
ensure
|
138
|
+
ts.each(&:join)
|
139
|
+
end
|
140
|
+
end
|
141
|
+
end
|
98
142
|
end
|
99
143
|
end
|
metadata
CHANGED
@@ -1,58 +1,59 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: open_uri_redirections
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jaime Iniesta
|
8
8
|
- Gabriel Cebrian
|
9
|
+
- Felix C. Stegerman
|
9
10
|
autorequire:
|
10
11
|
bindir: bin
|
11
12
|
cert_chain: []
|
12
|
-
date:
|
13
|
+
date: 2014-11-24 00:00:00.000000000 Z
|
13
14
|
dependencies:
|
14
15
|
- !ruby/object:Gem::Dependency
|
15
16
|
name: rspec
|
16
17
|
requirement: !ruby/object:Gem::Requirement
|
17
18
|
requirements:
|
18
|
-
- - ~>
|
19
|
+
- - "~>"
|
19
20
|
- !ruby/object:Gem::Version
|
20
|
-
version:
|
21
|
+
version: 3.1.0
|
21
22
|
type: :development
|
22
23
|
prerelease: false
|
23
24
|
version_requirements: !ruby/object:Gem::Requirement
|
24
25
|
requirements:
|
25
|
-
- - ~>
|
26
|
+
- - "~>"
|
26
27
|
- !ruby/object:Gem::Version
|
27
|
-
version:
|
28
|
+
version: 3.1.0
|
28
29
|
- !ruby/object:Gem::Dependency
|
29
30
|
name: fakeweb
|
30
31
|
requirement: !ruby/object:Gem::Requirement
|
31
32
|
requirements:
|
32
|
-
- - ~>
|
33
|
+
- - "~>"
|
33
34
|
- !ruby/object:Gem::Version
|
34
35
|
version: 1.3.0
|
35
36
|
type: :development
|
36
37
|
prerelease: false
|
37
38
|
version_requirements: !ruby/object:Gem::Requirement
|
38
39
|
requirements:
|
39
|
-
- - ~>
|
40
|
+
- - "~>"
|
40
41
|
- !ruby/object:Gem::Version
|
41
42
|
version: 1.3.0
|
42
43
|
- !ruby/object:Gem::Dependency
|
43
44
|
name: rake
|
44
45
|
requirement: !ruby/object:Gem::Requirement
|
45
46
|
requirements:
|
46
|
-
- - ~>
|
47
|
+
- - "~>"
|
47
48
|
- !ruby/object:Gem::Version
|
48
|
-
version: 10.
|
49
|
+
version: 10.3.2
|
49
50
|
type: :development
|
50
51
|
prerelease: false
|
51
52
|
version_requirements: !ruby/object:Gem::Requirement
|
52
53
|
requirements:
|
53
|
-
- - ~>
|
54
|
+
- - "~>"
|
54
55
|
- !ruby/object:Gem::Version
|
55
|
-
version: 10.
|
56
|
+
version: 10.3.2
|
56
57
|
description: OpenURI patch to allow redirections between HTTP and HTTPS
|
57
58
|
email:
|
58
59
|
- jaimeiniesta@gmail.com
|
@@ -60,8 +61,8 @@ executables: []
|
|
60
61
|
extensions: []
|
61
62
|
extra_rdoc_files: []
|
62
63
|
files:
|
63
|
-
- .gitignore
|
64
|
-
- .travis.yml
|
64
|
+
- ".gitignore"
|
65
|
+
- ".travis.yml"
|
65
66
|
- Gemfile
|
66
67
|
- LICENSE.txt
|
67
68
|
- README.md
|
@@ -86,17 +87,17 @@ require_paths:
|
|
86
87
|
- lib
|
87
88
|
required_ruby_version: !ruby/object:Gem::Requirement
|
88
89
|
requirements:
|
89
|
-
- -
|
90
|
+
- - ">="
|
90
91
|
- !ruby/object:Gem::Version
|
91
92
|
version: '0'
|
92
93
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
93
94
|
requirements:
|
94
|
-
- -
|
95
|
+
- - ">="
|
95
96
|
- !ruby/object:Gem::Version
|
96
97
|
version: '0'
|
97
98
|
requirements: []
|
98
99
|
rubyforge_project:
|
99
|
-
rubygems_version: 2.
|
100
|
+
rubygems_version: 2.2.2
|
100
101
|
signing_key:
|
101
102
|
specification_version: 4
|
102
103
|
summary: OpenURI patch to allow redirections between HTTP and HTTPS
|