rack-cname_request 0.1.1 → 0.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f2c45ae4f0970d558960667f9730829e21f6d9ca
4
- data.tar.gz: 4aba8bc690a5af39cde5187487d3fbf94f3f9ad2
3
+ metadata.gz: 76b6ee695dfd46b74650f95e38362e587c5cd7c9
4
+ data.tar.gz: a4d4b43f7ef474e871a27c0e7574a2cdbbbcb72c
5
5
  SHA512:
6
- metadata.gz: 6a2bae2aa4de9d71a356ab547ee30764e99fe4de0c9cc854b5deb7b5598b7fe79d492bcdfb8cae55b5716ba64c9da384965c7f91a1c1d4f340cf38220634b3b6
7
- data.tar.gz: 8501f8ce43de9f3e404b00e98086dd5ac8bd78ffd48bc959ddd0a8df4d05ac7ca3220ebb9779b09e00810986aee8eb1cc0942f1c936f08dc2df8e075b9b36a3d
6
+ metadata.gz: 8d4f73aa1cb54d1704f5f6e28c0895bd0afa05d2895b8bfeb864d0a4509a070578820901fc4623fd4299aa106c61020eea27a5fc946fed33b1a03052e68c94bc
7
+ data.tar.gz: f86d4a2af0575e7d229a9339b63a047b116530291de5ad636348e4649a25ba9511e77c7f91079d30dfa737f7e70789f8b4f76daeb34f1c4329e40ae3c3b5a63c
@@ -3,26 +3,55 @@ require 'rack/cname_request/version'
3
3
  module Rack
4
4
  class CnameRequest
5
5
 
6
- attr_reader :http_cname_header_key
6
+ attr_reader :http_cname_header_key, :host_whitelist
7
7
 
8
- def initialize(app, cname_header_key: nil)
8
+ def initialize(app, cname_header_key: nil, only: [])
9
9
  @app = app
10
10
  @http_cname_header_key = "HTTP_#{cname_header_key}"
11
+ @host_whitelist = only || []
11
12
  end
12
13
 
13
14
  def call(env)
14
15
  status, headers, body = @app.call env
15
-
16
- if headers['Location'] && env[http_cname_header_key]
17
- uri = URI(headers['Location'])
18
- uri.host = env[http_cname_header_key]
19
- headers['Location'] = uri.to_s
16
+ original_location = headers['Location']
17
+ if original_location
18
+ modifier = LocationModifier.new(original_location, env[http_cname_header_key], host_whitelist: host_whitelist)
19
+ headers['Location'] = modifier.modified_location if modifier.should_modify?
20
20
  end
21
21
 
22
22
  [status, headers, body]
23
23
  end
24
24
 
25
+ end
26
+
27
+ class LocationModifier
28
+
29
+ attr_reader :location_uri, :cname_header_value, :host_whitelist
30
+
31
+ def initialize(location, cname_header_value, host_whitelist: [])
32
+ @location_uri = URI(location) if location
33
+ @cname_header_value = cname_header_value
34
+ @host_whitelist = host_whitelist || []
35
+ end
36
+
37
+ def should_modify?
38
+ comes_from_cname_proxy? && location_host_in_white_list?
39
+ end
40
+
41
+ def modified_location
42
+ location_uri.host = cname_header_value
43
+ location_uri.to_s
44
+ end
45
+
25
46
  private
26
47
 
48
+ def comes_from_cname_proxy?
49
+ cname_header_value
50
+ end
51
+
52
+ def location_host_in_white_list?
53
+ host_whitelist.include?(location_uri.host)
54
+ end
55
+
27
56
  end
28
57
  end
@@ -1,5 +1,5 @@
1
1
  module Rack
2
2
  class CnameRequest
3
- VERSION = '0.1.1'
3
+ VERSION = '0.1.2'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-cname_request
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - FENG Zhichao
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-01-03 00:00:00.000000000 Z
11
+ date: 2018-01-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler