rack-www 2.0.0 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c1a573de69069e3e4dba5fa155a61171e7f2a5db
4
- data.tar.gz: 1eccb6a5132455dac0d7a3e52870ef3fc8999292
3
+ metadata.gz: 67bab3314c14a6baf165bf7697f2640e290cd9f9
4
+ data.tar.gz: 39c53a98db554ad2a7d178d9c62aa0fdac9b5deb
5
5
  SHA512:
6
- metadata.gz: 793e64555ba1265be7ac95a543920aeb2156a64636153ae6f16b35bf77393b40638f886b0c38227a778c1f35f0067a44064d451eafeade87450e049332ccde64
7
- data.tar.gz: 4ae7ab8da7a192df653a069118b97db41b7b38a3f61c47c2b6f332dd4fdc9dbf3a452c1356d408d7e7d07236527c59239347bb58f0baa3a7395b6c77db3fa957
6
+ metadata.gz: 8b3c507ada98ad3a4489c21a791b660257cb67db00ade55983ca8fecb2d5e35c77042736ad8295e257a75790558d8b14c17d874e8dc2c09e4689431302d3c307
7
+ data.tar.gz: 429293155e765b0fda94c704577b80f00a990715fb3a50be42a68c444d65f563006b2e8695d45761386a9cf8b0e23a77b6f1cb5d0ffe2695be51c202b00e9402
@@ -1,3 +1,7 @@
1
+ ## 2.1
2
+
3
+ - Ignore rack-www when request host is an IP address
4
+
1
5
  ## 2.0
2
6
 
3
7
  - Update EVERYTHING
@@ -1,22 +1,79 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rack-www (2.0.0)
4
+ rack-www (2.1.0)
5
5
  rack (~> 1.0)
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
9
9
  specs:
10
+ ast (2.1.0)
11
+ astrolabe (1.3.1)
12
+ parser (~> 2.2)
13
+ coderay (1.1.0)
14
+ ffi (1.9.10)
15
+ formatador (0.2.5)
16
+ guard (2.13.0)
17
+ formatador (>= 0.2.4)
18
+ listen (>= 2.7, <= 4.0)
19
+ lumberjack (~> 1.0)
20
+ nenv (~> 0.1)
21
+ notiffany (~> 0.0)
22
+ pry (>= 0.9.12)
23
+ shellany (~> 0.0)
24
+ thor (>= 0.18.1)
25
+ guard-compat (1.2.1)
26
+ guard-minitest (2.4.4)
27
+ guard-compat (~> 1.2)
28
+ minitest (>= 3.0)
29
+ guard-rubocop (1.2.0)
30
+ guard (~> 2.0)
31
+ rubocop (~> 0.20)
32
+ listen (3.0.5)
33
+ rb-fsevent (>= 0.9.3)
34
+ rb-inotify (>= 0.9)
35
+ lumberjack (1.0.9)
36
+ method_source (0.8.2)
10
37
  minitest (5.8.3)
38
+ nenv (0.2.0)
39
+ notiffany (0.0.8)
40
+ nenv (~> 0.1)
41
+ shellany (~> 0.0)
42
+ parser (2.2.3.0)
43
+ ast (>= 1.1, < 3.0)
44
+ powerpack (0.1.1)
45
+ pry (0.10.3)
46
+ coderay (~> 1.1.0)
47
+ method_source (~> 0.8.1)
48
+ slop (~> 3.4)
11
49
  rack (1.6.4)
12
50
  rack-test (0.6.3)
13
51
  rack (>= 1.0)
52
+ rainbow (2.0.0)
14
53
  rake (10.4.2)
54
+ rb-fsevent (0.9.6)
55
+ rb-inotify (0.9.5)
56
+ ffi (>= 0.5.0)
57
+ rubocop (0.35.1)
58
+ astrolabe (~> 1.3)
59
+ parser (>= 2.2.3.0, < 3.0)
60
+ powerpack (~> 0.1)
61
+ rainbow (>= 1.99.1, < 3.0)
62
+ ruby-progressbar (~> 1.7)
63
+ tins (<= 1.6.0)
64
+ ruby-progressbar (1.7.5)
65
+ shellany (0.0.1)
66
+ slop (3.6.0)
67
+ thor (0.19.1)
68
+ tins (1.6.0)
15
69
 
16
70
  PLATFORMS
17
71
  ruby
18
72
 
19
73
  DEPENDENCIES
74
+ guard (~> 2.13)
75
+ guard-minitest (~> 2.4)
76
+ guard-rubocop (~> 1.2)
20
77
  minitest (~> 5.8)
21
78
  rack-test (~> 0.6)
22
79
  rack-www!
@@ -0,0 +1,11 @@
1
+ guard :minitest do
2
+ # with Minitest::Unit
3
+ watch(%r{^test/(.*)\/?test_(.*)\.rb$})
4
+ watch(%r{^lib/(.*/)?([^/]+)\.rb$}) { |m| "test/#{m[1]}test_#{m[2]}.rb" }
5
+ watch(%r{^test/test_helper\.rb$}) { 'test' }
6
+ end
7
+
8
+ guard :rubocop do
9
+ watch(/.+\.rb$/)
10
+ watch(%r{(?:.+/)?\.rubocop\.yml$}) { |m| File.dirname(m[0]) }
11
+ end
data/Rakefile CHANGED
@@ -1,9 +1,9 @@
1
1
  require 'rake/testtask'
2
2
 
3
- task :default => :test
3
+ task default: :test
4
4
 
5
5
  Rake::TestTask.new do |t|
6
- t.libs << "test"
7
- t.pattern = "test/**/*_test.rb"
6
+ t.libs << 'test'
7
+ t.pattern = 'test/**/*_test.rb'
8
8
  t.warning = true
9
9
  end
@@ -1,13 +1,15 @@
1
1
  require 'rack'
2
2
  require 'rack/request'
3
+ require 'ipaddr'
3
4
 
4
5
  module Rack
6
+ # Rack::WWW
5
7
  class WWW
6
8
  def initialize(app, options = {})
7
- @options = {:subdomain => "www"}.merge(options)
9
+ @options = { subdomain: 'www' }.merge(options)
8
10
  @app = app
9
11
 
10
- @redirect = @options[:www] != nil ? @options[:www] : true
12
+ @redirect = !@options[:www].nil? ? @options[:www] : true
11
13
  @message = @options[:message]
12
14
  @subdomain = @options[:subdomain]
13
15
  @predicate = @options[:predicate]
@@ -22,9 +24,10 @@ module Rack
22
24
  end
23
25
 
24
26
  private
27
+
25
28
  def redirect(env)
26
29
  url = prepare_url(env)
27
- headers = {"Content-Type" => "text/html", "location" => url}
30
+ headers = { 'Content-Type' => 'text/html', 'location' => url }
28
31
 
29
32
  if @message.respond_to?(:each)
30
33
  message = @message
@@ -35,7 +38,7 @@ module Rack
35
38
  end
36
39
 
37
40
  def redirect?(env)
38
- predicate?(env) && change_subdomain?(env)
41
+ predicate?(env) && change_subdomain?(env) && !ip_request?(env)
39
42
  end
40
43
 
41
44
  def predicate?(env)
@@ -51,27 +54,36 @@ module Rack
51
54
  !@redirect && already_subdomain?(env)
52
55
  end
53
56
 
57
+ def ip_request?(env)
58
+ IPAddr.new(env['SERVER_NAME'].to_s
59
+ .gsub(/^(#{@subdomain}\.)/, '')
60
+ .gsub(/^(www\.)/, ''))
61
+ rescue IPAddr::InvalidAddressError
62
+ false
63
+ end
64
+
54
65
  def already_subdomain?(env)
55
- env["HTTP_HOST"].to_s.downcase =~ /^(#{@subdomain}\.)/
66
+ env['HTTP_HOST'].to_s.downcase =~ /^(#{@subdomain}\.)/
56
67
  end
57
68
 
58
69
  def prepare_url(env)
59
- scheme = env["rack.url_scheme"]
60
- host = env["SERVER_NAME"].to_s.gsub(/^(#{@subdomain}\.)/, "").gsub(/^(www\.)/, "")
61
- port = env['SERVER_PORT'] == '80' ? '' : ":#{env['SERVER_PORT']}"
62
- path = env["PATH_INFO"]
63
-
64
- unless env["QUERY_STRING"].empty?
65
- query_string = "?" + env["QUERY_STRING"]
66
- end
70
+ scheme = env['rack.url_scheme']
71
+ host, port, path, query_string = extract_host(env)
67
72
 
68
73
  if @redirect == true
69
74
  host = "://#{@subdomain}." + host
70
75
  else
71
- host = "://" + host
76
+ host = '://' + host
72
77
  end
73
78
 
74
79
  "#{scheme}#{host}#{port}#{path}#{query_string}"
75
80
  end
81
+
82
+ def extract_host(env)
83
+ [env['SERVER_NAME'].to_s.gsub(/^(#{@subdomain}\.|www\.)/, ''),
84
+ env['SERVER_PORT'] == '80' ? '' : ":#{env['SERVER_PORT']}",
85
+ env['PATH_INFO'],
86
+ env['QUERY_STRING'].empty? ? '' : '?' + env['QUERY_STRING'].to_s]
87
+ end
76
88
  end
77
89
  end
File without changes
@@ -1,21 +1,24 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'rack-www'
3
- s.version = '2.0.0'
3
+ s.version = '2.1.0'
4
4
  s.date = '2015-12-02'
5
- s.homepage = "https://github.com/stjhimy/rack-www"
6
- s.email = "stjhimy@gmail.com"
7
- s.summary = "Force redirects to a any given subdomain, example: www or www2"
8
- s.description = "Rack middleware to force subdomain redirects."
9
- s.licenses = ["MIT"]
10
- s.authors = ["Jhimy Fernandes Villar"]
5
+ s.homepage = 'https://github.com/stjhimy/rack-www'
6
+ s.email = 'stjhimy@gmail.com'
7
+ s.summary = 'Force redirects to a any given subdomain.'
8
+ s.description = 'Rack middleware to force subdomain redirects.'
9
+ s.licenses = ['MIT']
10
+ s.authors = ['Jhimy Fernandes Villar']
11
11
 
12
12
  s.files = `git ls-files`.split("\n")
13
13
  s.test_files = `git ls-files -- test/*`.split("\n")
14
- s.require_paths = ["lib"]
14
+ s.require_paths = ['lib']
15
15
  s.required_ruby_version = '>= 1.9.3'
16
16
 
17
17
  s.add_runtime_dependency 'rack', '~> 1.0'
18
18
  s.add_development_dependency 'rake', '~> 10.4'
19
19
  s.add_development_dependency 'rack-test', '~> 0.6'
20
20
  s.add_development_dependency 'minitest', '~> 5.8'
21
+ s.add_development_dependency 'guard', '~> 2.13'
22
+ s.add_development_dependency 'guard-minitest', '~> 2.4'
23
+ s.add_development_dependency 'guard-rubocop', '~> 1.2'
21
24
  end
@@ -1,4 +1,4 @@
1
- require "rack/www"
1
+ require 'rack/www'
2
2
  require 'minitest/autorun'
3
3
  require 'rack/test'
4
4
 
@@ -6,11 +6,10 @@ class TestClass < MiniTest::Test
6
6
  include Rack::Test::Methods
7
7
 
8
8
  def default_app
9
- lambda { |env|
10
- headers = {'Content-Type' => "text/html"}
11
- headers['Set-Cookie'] = "id=1; path=/\ntoken=abc; path=/; secure; HttpOnly"
12
- [200, headers, ["default body"]]
13
- }
9
+ lambda do |_env|
10
+ headers = { 'Content-Type' => 'text/html' }
11
+ [200, headers, ['default body']]
12
+ end
14
13
  end
15
14
 
16
15
  def app
@@ -1,20 +1,27 @@
1
- require "helper"
1
+ require 'helper'
2
2
 
3
3
  class PredicateTest < TestClass
4
4
  def test_predicate_false
5
- self.app = Rack::WWW.new(default_app, :predicate => lambda { |env| false } )
6
- get_example
5
+ self.app = Rack::WWW.new(default_app, predicate: ->(_env) { false })
6
+ example
7
7
  assert_equal last_response.status, 200
8
8
  end
9
9
 
10
10
  def test_predicate_true
11
- self.app = Rack::WWW.new(default_app, :predicate => lambda { |env| true } )
12
- get_example
13
- assert_equal "http://www.example.com/", last_response.headers['Location']
11
+ self.app = Rack::WWW.new(default_app, predicate: ->(_env) { true })
12
+ example
13
+ assert_equal 'http://www.example.com/', last_response.headers['Location']
14
+ end
15
+
16
+ def test_ignore_www_when_ip_request
17
+ get 'http://111.111.111.111/'
18
+ assert_equal last_response.status, 200
19
+ assert_equal '', last_response.headers['Location'].to_s
14
20
  end
15
21
 
16
22
  private
17
- def get_example
18
- get "http://example.com/"
23
+
24
+ def example
25
+ get 'http://example.com/'
19
26
  end
20
27
  end
@@ -1,43 +1,56 @@
1
- require "helper"
1
+ require 'helper'
2
2
 
3
3
  class SubdomainTest < TestClass
4
4
  def setup
5
- self.app = Rack::WWW.new(default_app, :subdomain => "secure")
5
+ self.app = Rack::WWW.new(default_app, subdomain: 's')
6
6
  end
7
7
 
8
8
  def test_if_allow_custom_subdomain
9
9
  get 'http://example.com'
10
- assert_equal 'http://secure.example.com/', last_response.headers['Location']
10
+ assert_equal 'http://s.example.com/',
11
+ last_response.headers['Location']
11
12
  end
12
13
 
13
14
  def test_custom_subdomain_with_path
14
- get "http://example.com/path/1"
15
- assert_equal "http://secure.example.com/path/1", last_response.headers['Location']
15
+ get 'http://example.com/path/1'
16
+ assert_equal 'http://s.example.com/path/1',
17
+ last_response.headers['Location']
16
18
  end
17
19
 
18
20
  def test_custom_subdomain_with_path_and_www
19
- get "http://www.example.com/path/1"
20
- assert_equal "http://secure.example.com/path/1", last_response.headers['Location']
21
+ get 'http://www.example.com/path/1'
22
+ assert_equal 'http://s.example.com/path/1',
23
+ last_response.headers['Location']
21
24
  end
22
25
 
23
26
  def test_custom_subdomain_with_query
24
- get "http://example.com/path/1?test=true"
25
- assert_equal "http://secure.example.com/path/1?test=true", last_response.headers['Location']
27
+ get 'http://example.com/path/1?test=true'
28
+ assert_equal 'http://s.example.com/path/1?test=true',
29
+ last_response.headers['Location']
26
30
  end
27
31
 
28
32
  def test_custom_subdomain_with_query_and_www
29
- get "http://www.example.com/path/1?test=true"
30
- assert_equal "http://secure.example.com/path/1?test=true", last_response.headers['Location']
33
+ get 'http://www.example.com/path/1?test=true'
34
+ assert_equal 'http://s.example.com/path/1?test=true',
35
+ last_response.headers['Location']
31
36
  end
32
37
 
33
38
  def test_body_with_custom_subdomain_and_message
34
- self.app = Rack::WWW.new(default_app, :subdomain => "secure", :message => "redirecting now!")
35
- get "http://example.com/"
36
- assert_equal last_response.body, "redirecting now!"
39
+ self.app = Rack::WWW.new(default_app,
40
+ subdomain: 'secure',
41
+ message: 'redirecting now!')
42
+ get 'http://example.com/'
43
+ assert_equal last_response.body, 'redirecting now!'
37
44
  end
38
45
 
39
46
  def test_body_with_custom_subdomain_without_message
40
- get "http://example.com/"
41
- assert_equal last_response.body, ""
47
+ get 'http://example.com/'
48
+ assert_equal last_response.body, ''
49
+ end
50
+
51
+ def test_ignore_www_when_ip_request
52
+ get 'http://111.111.111.111/'
53
+ assert_equal last_response.status, 200
54
+ assert_equal '', last_response.headers['Location'].to_s
42
55
  end
43
56
  end
@@ -1,27 +1,36 @@
1
- require "helper"
1
+ require 'helper'
2
2
 
3
3
  class FalseWWW < TestClass
4
4
  def setup
5
- self.app = Rack::WWW.new(default_app, :www => false)
5
+ self.app = Rack::WWW.new(default_app, www: false)
6
6
  end
7
7
 
8
8
  def test_www_false
9
- get "http://www.example.com/"
10
- assert_equal "http://example.com/", last_response.headers['Location']
9
+ get 'http://www.example.com/'
10
+ assert_equal 'http://example.com/',
11
+ last_response.headers['Location']
11
12
  end
12
13
 
13
14
  def test_www_false_with_path
14
- get "http://www.example.com/path/1"
15
- assert_equal "http://example.com/path/1", last_response.headers['Location']
15
+ get 'http://www.example.com/path/1'
16
+ assert_equal 'http://example.com/path/1',
17
+ last_response.headers['Location']
16
18
  end
17
19
 
18
20
  def test_www_false_with_query
19
- get "http://www.example.com/path/1?param=test"
20
- assert_equal "http://example.com/path/1?param=test", last_response.headers['Location']
21
+ get 'http://www.example.com/path/1?param=test'
22
+ assert_equal 'http://example.com/path/1?param=test',
23
+ last_response.headers['Location']
21
24
  end
22
25
 
23
26
  def test_www_false_non_www
24
- get "http://example.com/"
27
+ get 'http://example.com/'
25
28
  assert last_response.ok?
26
29
  end
30
+
31
+ def test_ignore_www_when_ip_request
32
+ get 'http://111.111.111.111/'
33
+ assert_equal last_response.status, 200
34
+ assert_equal '', last_response.headers['Location'].to_s
35
+ end
27
36
  end
@@ -1,58 +1,70 @@
1
- require "helper"
1
+ require 'helper'
2
2
 
3
3
  class TrueWWW < TestClass
4
4
  def setup
5
- self.app = Rack::WWW.new(default_app, :www => true)
5
+ self.app = Rack::WWW.new(default_app, www: true)
6
6
  end
7
7
 
8
8
  def test_response_200
9
- get "http://www.example.com"
9
+ get 'http://www.example.com'
10
10
  assert_equal last_response.status, 200
11
11
  end
12
12
 
13
13
  def test_response_301
14
- get "http://example.com"
14
+ get 'http://example.com'
15
15
  assert_equal last_response.status, 301
16
16
  end
17
17
 
18
18
  def test_redirects
19
- get "http://example.com/"
20
- assert_equal "http://www.example.com/", last_response.headers['Location']
19
+ get 'http://example.com/'
20
+ assert_equal 'http://www.example.com/', last_response.headers['Location']
21
21
  end
22
22
 
23
23
  def test_www_true
24
- get "http://example.com/"
25
- assert_equal "http://www.example.com/", last_response.headers['Location']
24
+ get 'http://example.com/'
25
+ assert_equal 'http://www.example.com/', last_response.headers['Location']
26
26
  end
27
27
 
28
28
  def test_www_true_with_path
29
- get "http://example.com/path/1"
30
- assert_equal "http://www.example.com/path/1", last_response.headers['Location']
29
+ get 'http://example.com/path/1'
30
+ assert_equal 'http://www.example.com/path/1',
31
+ last_response.headers['Location']
31
32
  end
32
33
 
33
34
  def test_www_true_with_query
34
- get "http://example.com/path/1?param=test"
35
- assert_equal "http://www.example.com/path/1?param=test", last_response.headers['Location']
35
+ get 'http://example.com/path/1?param=test'
36
+ assert_equal 'http://www.example.com/path/1?param=test',
37
+ last_response.headers['Location']
36
38
  end
37
39
 
38
40
  def test_body_with_message
39
- self.app = Rack::WWW.new(default_app, :www => true, :message => "redirecting now!")
40
- get "http://example.com/"
41
- assert_equal last_response.body, "redirecting now!"
41
+ self.app = Rack::WWW.new(default_app,
42
+ www: true,
43
+ message: 'redirecting now!')
44
+ get 'http://example.com/'
45
+ assert_equal last_response.body, 'redirecting now!'
42
46
  end
43
47
 
44
48
  def test_body_without_message
45
- get "http://example.com/"
46
- assert_equal last_response.body, ""
49
+ get 'http://example.com/'
50
+ assert_equal last_response.body, ''
47
51
  end
48
52
 
49
53
  def test_server_port
50
- get "http://example.com:8080/"
51
- assert_equal "http://www.example.com:8080/",last_response.headers['Location']
54
+ get 'http://example.com:8080/'
55
+ assert_equal 'http://www.example.com:8080/',
56
+ last_response.headers['Location']
52
57
  end
53
58
 
54
59
  def test_server_port_80
55
- get "http://example.com:80/"
56
- assert_equal "http://www.example.com/",last_response.headers['Location']
60
+ get 'http://example.com:80/'
61
+ assert_equal 'http://www.example.com/',
62
+ last_response.headers['Location']
63
+ end
64
+
65
+ def test_ignore_www_when_ip_request
66
+ get 'http://111.111.111.111/'
67
+ assert_equal last_response.status, 200
68
+ assert_equal '', last_response.headers['Location'].to_s
57
69
  end
58
70
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-www
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jhimy Fernandes Villar
@@ -66,6 +66,48 @@ dependencies:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: '5.8'
69
+ - !ruby/object:Gem::Dependency
70
+ name: guard
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '2.13'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '2.13'
83
+ - !ruby/object:Gem::Dependency
84
+ name: guard-minitest
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '2.4'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '2.4'
97
+ - !ruby/object:Gem::Dependency
98
+ name: guard-rubocop
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '1.2'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '1.2'
69
111
  description: Rack middleware to force subdomain redirects.
70
112
  email: stjhimy@gmail.com
71
113
  executables: []
@@ -78,11 +120,12 @@ files:
78
120
  - CHANGELOG.md
79
121
  - Gemfile
80
122
  - Gemfile.lock
123
+ - Guardfile
81
124
  - LICENSE
82
125
  - README.md
83
126
  - Rakefile
84
- - lib/rack-www.rb
85
127
  - lib/rack/www.rb
128
+ - lib/rack_www.rb
86
129
  - rack-www.gemspec
87
130
  - test/helper.rb
88
131
  - test/predicate_test.rb
@@ -112,7 +155,7 @@ rubyforge_project:
112
155
  rubygems_version: 2.4.5.1
113
156
  signing_key:
114
157
  specification_version: 4
115
- summary: 'Force redirects to a any given subdomain, example: www or www2'
158
+ summary: Force redirects to a any given subdomain.
116
159
  test_files:
117
160
  - test/helper.rb
118
161
  - test/predicate_test.rb