open_uri_redirections 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +4 -0
- data/README.md +8 -6
- data/Rakefile +7 -1
- data/lib/open-uri/redirections_patch.rb +14 -9
- data/lib/open_uri_redirections/version.rb +1 -1
- data/open_uri_redirections.gemspec +4 -3
- data/spec/redirections_spec.rb +30 -24
- metadata +24 -7
data/.travis.yml
ADDED
data/README.md
CHANGED
@@ -1,7 +1,9 @@
|
|
1
|
-
# OpenUriRedirections
|
1
|
+
# OpenUriRedirections [![Build Status](https://secure.travis-ci.org/jaimeiniesta/open_uri_redirections.png)](http://travis-ci.org/jaimeiniesta/open_uri_redirections) [![Dependency Status](https://gemnasium.com/jaimeiniesta/open_uri_redirections.png)](https://gemnasium.com/jaimeiniesta/open_uri_redirections)
|
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.
|
6
|
+
|
5
7
|
Here is the problem it tries to solve:
|
6
8
|
|
7
9
|
$ irb
|
@@ -17,13 +19,13 @@ And here is how you can use this patch to follow the redirections:
|
|
17
19
|
=> true
|
18
20
|
> require 'open_uri_redirections'
|
19
21
|
=> true
|
20
|
-
1.9.2p320 :002 > open('http://github.com', :
|
22
|
+
1.9.2p320 :002 > open('http://github.com', :allow_redirections => :safe)
|
21
23
|
=> #<File:/var/folders/...>
|
22
24
|
|
23
|
-
The patch contained in this gem adds
|
25
|
+
The patch contained in this gem adds the :allow_redirections option to `OpenURI#open`:
|
24
26
|
|
25
|
-
* `:
|
26
|
-
* `:
|
27
|
+
* `:allow_redirections => :safe` will allow HTTP => HTTPS redirections.
|
28
|
+
* `:allow_redirections => :all` will allow HTTP => HTTPS redirections and HTTPS => HTTP redirections.
|
27
29
|
|
28
30
|
## Understand what you're doing
|
29
31
|
|
@@ -48,7 +50,7 @@ Add this line to your application's Gemfile:
|
|
48
50
|
|
49
51
|
And then execute:
|
50
52
|
|
51
|
-
$ bundle
|
53
|
+
$ bundle install
|
52
54
|
|
53
55
|
Or install it yourself as:
|
54
56
|
|
data/Rakefile
CHANGED
@@ -17,31 +17,36 @@ module OpenURI
|
|
17
17
|
uri1.scheme.downcase == uri2.scheme.downcase || (uri1.scheme.downcase == "http" && uri2.scheme.downcase == "https")
|
18
18
|
end
|
19
19
|
|
20
|
-
def
|
21
|
-
|
20
|
+
def redirectable_all?(uri1, uri2)
|
21
|
+
redirectable_safe?(uri1, uri2) || (uri1.scheme.downcase == "https" && uri2.scheme.downcase == "http")
|
22
22
|
end
|
23
23
|
end
|
24
24
|
|
25
|
+
# Patches the original open_uri method to accept the :allow_redirections option
|
26
|
+
#
|
27
|
+
# :allow_redirections => :safe will allow HTTP => HTTPS redirections.
|
28
|
+
# :allow_redirections => :all will allow HTTP => HTTPS and HTTPS => HTTP redirections.
|
29
|
+
#
|
25
30
|
# The original open_uri takes *args but then doesn't do anything with them.
|
26
31
|
# Assume we can only handle a hash.
|
27
32
|
def self.open_uri(name, options = {})
|
28
|
-
|
29
|
-
redirectable_safe = options.delete :allow_safe_redirections
|
33
|
+
allow_redirections = options.delete :allow_redirections
|
30
34
|
|
31
|
-
|
35
|
+
case allow_redirections
|
36
|
+
when :safe
|
32
37
|
class <<self
|
33
38
|
remove_method :redirectable?
|
34
|
-
alias_method
|
39
|
+
alias_method :redirectable?, :redirectable_safe?
|
35
40
|
end
|
36
|
-
|
41
|
+
when :all
|
37
42
|
class <<self
|
38
43
|
remove_method :redirectable?
|
39
|
-
alias_method
|
44
|
+
alias_method :redirectable?, :redirectable_all?
|
40
45
|
end
|
41
46
|
else
|
42
47
|
class <<self
|
43
48
|
remove_method :redirectable?
|
44
|
-
alias_method
|
49
|
+
alias_method :redirectable?, :redirectable_cautious?
|
45
50
|
end
|
46
51
|
end
|
47
52
|
|
@@ -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
|
9
|
+
gem.authors = ["Jaime Iniesta", "Gabriel Cebrian"]
|
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}
|
@@ -16,6 +16,7 @@ Gem::Specification.new do |gem|
|
|
16
16
|
gem.test_files = gem.files.grep(%r{^(spec)/})
|
17
17
|
gem.require_paths = ["lib"]
|
18
18
|
|
19
|
-
gem.add_development_dependency 'rspec',
|
20
|
-
gem.add_development_dependency 'fakeweb', '1.3.0'
|
19
|
+
gem.add_development_dependency 'rspec', '~> 2.12.0'
|
20
|
+
gem.add_development_dependency 'fakeweb', '~> 1.3.0'
|
21
|
+
gem.add_development_dependency 'rake', '~> 10.0.3'
|
21
22
|
end
|
data/spec/redirections_spec.rb
CHANGED
@@ -4,51 +4,57 @@ require File.join(File.dirname(__FILE__), "/spec_helper")
|
|
4
4
|
|
5
5
|
describe "OpenURI" do
|
6
6
|
describe "#open" do
|
7
|
-
describe "
|
8
|
-
it "should
|
7
|
+
describe "Default settings" do
|
8
|
+
it "should disallow HTTP => HTTPS redirections" do
|
9
9
|
expect {
|
10
|
-
open("http://safe.com"
|
11
|
-
}.
|
10
|
+
open("http://safe.com")
|
11
|
+
}.to raise_error(RuntimeError, "redirection forbidden: http://safe.com -> https://safe.com/")
|
12
12
|
end
|
13
13
|
|
14
|
-
it "should disallow
|
14
|
+
it "should disallow HTTPS => HTTP redirections" do
|
15
15
|
expect {
|
16
|
-
open("
|
17
|
-
}.to raise_error(RuntimeError, "redirection forbidden:
|
16
|
+
open("https://unsafe.com")
|
17
|
+
}.to raise_error(RuntimeError, "redirection forbidden: https://unsafe.com -> http://unsafe.com/")
|
18
18
|
end
|
19
|
+
end
|
19
20
|
|
20
|
-
|
21
|
+
describe ":allow_redirections => :safe" do
|
22
|
+
it "should allow HTTP => HTTPS redirections" do
|
21
23
|
expect {
|
22
|
-
open("http://safe.com")
|
23
|
-
}.
|
24
|
+
open("http://safe.com", :allow_redirections => :safe)
|
25
|
+
}.to_not raise_error
|
24
26
|
end
|
25
27
|
|
26
|
-
it "should
|
27
|
-
|
28
|
+
it "should disallow HTTPS => HTTP redirections" do
|
29
|
+
expect {
|
30
|
+
open("https://unsafe.com", :allow_redirections => :safe)
|
31
|
+
}.to raise_error(RuntimeError, "redirection forbidden: https://unsafe.com -> http://unsafe.com/")
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should follow safe redirections" do
|
35
|
+
open("http://safe.com", :allow_redirections => :safe).read.should == "Hello, this is Safe."
|
28
36
|
end
|
29
37
|
end
|
30
38
|
|
31
|
-
describe ":
|
32
|
-
it "should allow
|
39
|
+
describe ":allow_redirections => :all" do
|
40
|
+
it "should allow HTTP => HTTPS redirections" do
|
33
41
|
expect {
|
34
|
-
open("
|
42
|
+
open("http://safe.com", :allow_redirections => :all)
|
35
43
|
}.to_not raise_error
|
36
44
|
end
|
37
45
|
|
38
|
-
it "should
|
46
|
+
it "should allow HTTPS => HTTP redirections" do
|
39
47
|
expect {
|
40
|
-
open("https://unsafe.com", :
|
41
|
-
}.
|
48
|
+
open("https://unsafe.com", :allow_redirections => :all)
|
49
|
+
}.to_not raise_error
|
42
50
|
end
|
43
51
|
|
44
|
-
it "should
|
45
|
-
|
46
|
-
open("https://unsafe.com")
|
47
|
-
}.to raise_error(RuntimeError, "redirection forbidden: https://unsafe.com -> http://unsafe.com/")
|
52
|
+
it "should follow safe redirections" do
|
53
|
+
open("http://safe.com", :allow_redirections => :all).read.should == "Hello, this is Safe."
|
48
54
|
end
|
49
55
|
|
50
|
-
it "should follow unsafe
|
51
|
-
open("https://unsafe.com", :
|
56
|
+
it "should follow unsafe redirections" do
|
57
|
+
open("https://unsafe.com", :allow_redirections => :all).read.should == "Hello, this is Unsafe."
|
52
58
|
end
|
53
59
|
end
|
54
60
|
end
|
metadata
CHANGED
@@ -1,22 +1,22 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: open_uri_redirections
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 27
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
- 0
|
9
8
|
- 1
|
10
|
-
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Jaime Iniesta
|
14
|
-
-
|
14
|
+
- Gabriel Cebrian
|
15
15
|
autorequire:
|
16
16
|
bindir: bin
|
17
17
|
cert_chain: []
|
18
18
|
|
19
|
-
date:
|
19
|
+
date: 2013-01-19 00:00:00 Z
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
22
22
|
name: rspec
|
@@ -24,7 +24,7 @@ dependencies:
|
|
24
24
|
requirement: &id001 !ruby/object:Gem::Requirement
|
25
25
|
none: false
|
26
26
|
requirements:
|
27
|
-
- -
|
27
|
+
- - ~>
|
28
28
|
- !ruby/object:Gem::Version
|
29
29
|
hash: 63
|
30
30
|
segments:
|
@@ -40,7 +40,7 @@ dependencies:
|
|
40
40
|
requirement: &id002 !ruby/object:Gem::Requirement
|
41
41
|
none: false
|
42
42
|
requirements:
|
43
|
-
- -
|
43
|
+
- - ~>
|
44
44
|
- !ruby/object:Gem::Version
|
45
45
|
hash: 27
|
46
46
|
segments:
|
@@ -50,6 +50,22 @@ dependencies:
|
|
50
50
|
version: 1.3.0
|
51
51
|
type: :development
|
52
52
|
version_requirements: *id002
|
53
|
+
- !ruby/object:Gem::Dependency
|
54
|
+
name: rake
|
55
|
+
prerelease: false
|
56
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
hash: 73
|
62
|
+
segments:
|
63
|
+
- 10
|
64
|
+
- 0
|
65
|
+
- 3
|
66
|
+
version: 10.0.3
|
67
|
+
type: :development
|
68
|
+
version_requirements: *id003
|
53
69
|
description: OpenURI patch to allow redirections between HTTP and HTTPS
|
54
70
|
email:
|
55
71
|
- jaimeiniesta@gmail.com
|
@@ -61,6 +77,7 @@ extra_rdoc_files: []
|
|
61
77
|
|
62
78
|
files:
|
63
79
|
- .gitignore
|
80
|
+
- .travis.yml
|
64
81
|
- Gemfile
|
65
82
|
- LICENSE.txt
|
66
83
|
- README.md
|