rack-host-redirect 1.1.2 → 1.2

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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/rack/host_redirect.rb +35 -20
  3. metadata +9 -9
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cdbb38de454a694add16aba10f8c4518abbffd65
4
- data.tar.gz: 3ccc2a649f0c8146b9b1eadce5554959f75b3bbc
3
+ metadata.gz: 5fb6542ad2bd4e616e14c8707e257631d5d8b41d
4
+ data.tar.gz: 9049c9f3270af53ae3ab186c10dfc22217a5a986
5
5
  SHA512:
6
- metadata.gz: 20be1733ff93683272a241f6191e95a1025a673daf2a0e9136036e61f7eebb6cb9e3c281c9e277e72a3805eaad4672b76f84f3e2a732bb76575831b2bc89540f
7
- data.tar.gz: 55fb6747c589a71c8f2723ac5766c699fb9e05a0421873943c38fe0350ccaf3e27d6f4b1a018c203b81b01258beb952eb32018e2c27432b1684758edc09a9a61
6
+ metadata.gz: bcdcc7f90caac858ee69004ced4c86834e75872624a5025129c4ad294b6ae1e59a350b38447793261206c8bb530b706574caf1a5f8816d0f277626a884919c28
7
+ data.tar.gz: e142502f621e2e72b91f5bb20552a2c0d5f691107df77e458d576ce6ffdf2e0fa154cad86a8925758fb66c5c052b42a8cec0168f08fa38caae6da74714d3929d
@@ -1,21 +1,19 @@
1
1
  require 'rack/request'
2
+ require 'uri'
2
3
 
3
4
  class Rack::HostRedirect
4
5
 
5
- def initialize(app, host_mapping = nil, &block)
6
+ def initialize(app, host_mapping)
6
7
  @app = app
7
- @host_mapping = downcase_keys(host_mapping) if host_mapping
8
- @block = block
8
+ @host_mapping = preprocess_mapping(host_mapping)
9
9
  end
10
10
 
11
11
  def call(env)
12
12
  request = Rack::Request.new(env)
13
13
  host = request.host.downcase # downcase for case-insensitive matching
14
14
 
15
- updated_host = (@host_mapping && @host_mapping[host]) || (@block && @block.call(host, env))
16
-
17
- if updated_host && updated_host.downcase != host
18
- location = replace_host(request.url, updated_host)
15
+ if updated_uri_opts = @host_mapping[host]
16
+ location = update_url(request.url, updated_uri_opts)
19
17
  [301, {'Location' => location}, []]
20
18
  else
21
19
  @app.call(env)
@@ -24,20 +22,37 @@ class Rack::HostRedirect
24
22
 
25
23
  private
26
24
 
27
- def downcase_keys hsh
28
- hsh.inject({}) {|out, (k, v)| out[k.downcase] = v; out }
25
+ def preprocess_mapping hsh
26
+ hsh.inject({}) do |out, (k, opts)|
27
+ opts = {:host => opts} if opts.is_a?(String)
28
+
29
+ if newhost = opts[:host]
30
+ opts[:host] = newhost.downcase
31
+ else
32
+ raise ArgumentError, ":host key must be specified in #{opts.inspect}"
33
+ end
34
+
35
+ [k].flatten.each do |oldhost|
36
+ oldhost = oldhost.downcase
37
+
38
+ if oldhost == opts[:host]
39
+ raise ArgumentError, "Circular redirect to #{oldhost}"
40
+ else
41
+ out[oldhost] = opts
42
+ end
43
+ end
44
+
45
+ out
46
+ end
29
47
  end
30
48
 
31
- # Captures everything in url except the host:
32
- #
33
- # REPLACE_HOST_REGEX =~ 'https://foo.com/bar?baz=qux'
34
- #
35
- # $1 == 'https://'
36
- # $2 == '/bar?baz=qux'
37
- REPLACE_HOST_REGEX = /(https?:\/\/)[^\/\?:]+(.*)/
38
-
39
- def replace_host url, host
40
- REPLACE_HOST_REGEX =~ url
41
- "#{$1}#{host}#{$2}"
49
+ def update_url url, opts
50
+ uri = URI(url)
51
+
52
+ opts.each do |k, v|
53
+ uri.send(:"#{k}=", v)
54
+ end
55
+
56
+ uri.to_s
42
57
  end
43
58
  end
metadata CHANGED
@@ -1,41 +1,41 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-host-redirect
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.2
4
+ version: '1.2'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Geoff Buesing
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-06-22 00:00:00.000000000 Z
11
+ date: 2014-02-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - '>='
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - '>='
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rack-test
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '>='
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - '>='
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  description:
@@ -56,17 +56,17 @@ require_paths:
56
56
  - lib
57
57
  required_ruby_version: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - '>='
59
+ - - ">="
60
60
  - !ruby/object:Gem::Version
61
61
  version: '0'
62
62
  required_rubygems_version: !ruby/object:Gem::Requirement
63
63
  requirements:
64
- - - '>='
64
+ - - ">="
65
65
  - !ruby/object:Gem::Version
66
66
  version: '0'
67
67
  requirements: []
68
68
  rubyforge_project:
69
- rubygems_version: 2.0.2
69
+ rubygems_version: 2.2.0
70
70
  signing_key:
71
71
  specification_version: 4
72
72
  summary: Lean and simple host redirection via Rack middleware