rack-reverse-proxy 0.8.1 → 0.9.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: d51045cd47dd0cee3f5afb010a2915f9dd6367ba
4
- data.tar.gz: c675c804802eb806d6fca45b471630ed0d980268
3
+ metadata.gz: c4e87644e758f5a1fa0a3d4fcefaad9876b7b798
4
+ data.tar.gz: aee6c2fd853ba1c1ce552058a63c9130ea1f6e74
5
5
  SHA512:
6
- metadata.gz: dada6c70259a95d63705ffad9ad59f54d492b721b98d2496322ba33cec38d1005a778830091dfe44c2f5a2b23a9d4b91ff002d761f90a93a37dc32f4de38c7ad
7
- data.tar.gz: 7aa447d71a827b37b3a53818723b426c8c0135b90b7c7dcdc2b850dcd1717531e5dddd99afc82ffdcee57f6ebc4a761bb4fba320529f793ad239e21b08a99c3b
6
+ metadata.gz: a372f0bd493d50b7d411d1c1abc59382cd22ad7db6fa6a3192a93e37acd52e6f037bc6aae76d359fb6d095557f58adeecbfe87d27b94f6c2602c76417fd0a354
7
+ data.tar.gz: ec4efcc102cdd5fc88cd7e77e47df6450486e9be23729b0a3605ad540eb9ced832e4c864fd3dc2f7dc79b3bc46c13ef43a9215b0cde46793e3265472528abc9a
data/README.md CHANGED
@@ -1,5 +1,5 @@
1
1
  # A Reverse Proxy for Rack
2
- [![TravisCI](https://secure.travis-ci.org/pex/rack-reverse-proxy.png "Build Status")](http://travis-ci.org/pex/rack-reverse-proxy "Build Status")
2
+ [![TravisCI](https://secure.travis-ci.org/waterlink/rack-reverse-proxy.svg "Build Status")](http://travis-ci.org/waterlink/rack-reverse-proxy "Build Status")
3
3
 
4
4
  This is a simple reverse proxy for Rack that pretty heavily rips off Rack Forwarder. It is not meant for production systems (although it may work), as the webserver fronting your app is generally much better at this sort of thing.
5
5
 
@@ -59,6 +59,7 @@ reverse_proxy_options sets global options for all reverse proxies. Available opt
59
59
  (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
60
60
  * Send me a pull request. Bonus points for topic branches.
61
61
 
62
- ## Copyright
62
+ ## Contributors
63
63
 
64
- Copyright (c) 2010 Jon Swope. See LICENSE for details.
64
+ - Jon Swope, creator
65
+ - Oleksii Fedorov, maintainer
@@ -3,6 +3,7 @@ require 'net/https'
3
3
  require "rack-proxy"
4
4
  require "rack/reverse_proxy_matcher"
5
5
  require "rack/exception"
6
+ require "rack/reverse_proxy/http_streaming_response"
6
7
 
7
8
  module Rack
8
9
  class ReverseProxy
@@ -12,12 +13,12 @@ module Rack
12
13
  @app = app || lambda {|env| [404, [], []] }
13
14
  @matchers = []
14
15
  @global_options = {:preserve_host => true, :x_forwarded_host => true, :matching => :all, :replace_response_host => false}
15
- instance_eval &b if block_given?
16
+ instance_eval(&b) if block_given?
16
17
  end
17
18
 
18
19
  def call(env)
19
20
  rackreq = Rack::Request.new(env)
20
- matcher = get_matcher(rackreq.fullpath, extract_http_request_headers(rackreq.env), rackreq)
21
+ matcher = get_matcher(rackreq.fullpath, Proxy.extract_http_request_headers(rackreq.env), rackreq)
21
22
  return @app.call(env) if matcher.nil?
22
23
 
23
24
  if @global_options[:newrelic_instrumentation]
@@ -43,10 +44,14 @@ module Rack
43
44
  target_request = Net::HTTP.const_get(source_request.request_method.capitalize).new(uri.request_uri)
44
45
 
45
46
  # Setup headers
46
- target_request_headers = extract_http_request_headers(source_request.env)
47
+ target_request_headers = Proxy.extract_http_request_headers(source_request.env)
47
48
 
48
49
  if options[:preserve_host]
49
- target_request_headers['HOST'] = "#{uri.host}:#{uri.port}"
50
+ if uri.port == uri.default_port
51
+ target_request_headers['HOST'] = uri.host
52
+ else
53
+ target_request_headers['HOST'] = "#{uri.host}:#{uri.port}"
54
+ end
50
55
  end
51
56
 
52
57
  if options[:x_forwarded_host]
@@ -71,42 +76,26 @@ module Rack
71
76
  # Create a streaming response (the actual network communication is deferred, a.k.a. streamed)
72
77
  target_response = HttpStreamingResponse.new(target_request, uri.host, uri.port)
73
78
 
79
+ # pass the timeout configuration through
80
+ target_response.set_read_timeout(options[:timeout]) if options[:timeout].to_i > 0
81
+
74
82
  target_response.use_ssl = "https" == uri.scheme
75
83
 
76
84
  # Let rack set the transfer-encoding header
77
- response_headers = target_response.headers
78
- response_headers.delete('transfer-encoding')
85
+ response_headers = Rack::Utils::HeaderHash.new Proxy.normalize_headers(format_headers(target_response.headers))
86
+ response_headers.delete('Transfer-Encoding')
79
87
 
80
88
  # Replace the location header with the proxy domain
81
- if response_headers['location'] && options[:replace_response_host]
82
- response_location = URI(response_headers['location'][0])
89
+ if response_headers['Location'] && options[:replace_response_host]
90
+ response_location = URI(response_headers['location'])
83
91
  response_location.host = source_request.host
84
- response_headers['location'] = response_location.to_s
92
+ response_location.port = source_request.port
93
+ response_headers['Location'] = response_location.to_s
85
94
  end
86
95
 
87
96
  [target_response.status, response_headers, target_response.body]
88
97
  end
89
98
 
90
- def extract_http_request_headers(env)
91
- headers = env.reject do |k, v|
92
- !(/^HTTP_[A-Z_]+$/ === k) || v.nil?
93
- end.map do |k, v|
94
- [reconstruct_header_name(k), v]
95
- end.inject(Utils::HeaderHash.new) do |hash, k_v|
96
- k, v = k_v
97
- hash[k] = v
98
- hash
99
- end
100
-
101
- x_forwarded_for = (headers["X-Forwarded-For"].to_s.split(/, +/) << env["REMOTE_ADDR"]).join(", ")
102
-
103
- headers.merge!("X-Forwarded-For" => x_forwarded_for)
104
- end
105
-
106
- def reconstruct_header_name(name)
107
- name.sub(/^HTTP_/, "").gsub("_", "-")
108
- end
109
-
110
99
  def get_matcher(path, headers, rackreq)
111
100
  matches = @matchers.select do |matcher|
112
101
  matcher.match?(path, headers, rackreq)
@@ -129,5 +118,13 @@ module Rack
129
118
  raise GenericProxyURI.new(url) if matcher.is_a?(String) && url.is_a?(String) && URI(url).class == URI::Generic
130
119
  @matchers << ReverseProxyMatcher.new(matcher,url,opts)
131
120
  end
121
+
122
+ def format_headers(headers)
123
+ headers.reduce({}) do |acc, (key, val)|
124
+ formated_key = key.split('-').map(&:capitalize).join('-')
125
+ acc[formated_key] = Array(val)
126
+ acc
127
+ end
128
+ end
132
129
  end
133
130
  end
@@ -0,0 +1,7 @@
1
+ module Rack
2
+ class HttpStreamingResponse
3
+ def set_read_timeout(value)
4
+ self.read_timeout = value
5
+ end
6
+ end
7
+ end
@@ -1,6 +1,6 @@
1
1
  module Rack
2
2
  class ReverseProxyMatcher
3
- def initialize(matcher,url=nil,options)
3
+ def initialize(matcher, url=nil, options={})
4
4
  @default_url=url
5
5
  @url=url
6
6
  @options=options
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-reverse-proxy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.1
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jon Swope
@@ -11,22 +11,8 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2015-05-24 00:00:00.000000000 Z
14
+ date: 2015-07-03 00:00:00.000000000 Z
15
15
  dependencies:
16
- - !ruby/object:Gem::Dependency
17
- name: rspec
18
- requirement: !ruby/object:Gem::Requirement
19
- requirements:
20
- - - "~>"
21
- - !ruby/object:Gem::Version
22
- version: '3.1'
23
- type: :development
24
- prerelease: false
25
- version_requirements: !ruby/object:Gem::Requirement
26
- requirements:
27
- - - "~>"
28
- - !ruby/object:Gem::Version
29
- version: '3.1'
30
16
  - !ruby/object:Gem::Dependency
31
17
  name: rake
32
18
  requirement: !ruby/object:Gem::Requirement
@@ -41,34 +27,6 @@ dependencies:
41
27
  - - "~>"
42
28
  - !ruby/object:Gem::Version
43
29
  version: '10.3'
44
- - !ruby/object:Gem::Dependency
45
- name: rack-test
46
- requirement: !ruby/object:Gem::Requirement
47
- requirements:
48
- - - "~>"
49
- - !ruby/object:Gem::Version
50
- version: '0.6'
51
- type: :development
52
- prerelease: false
53
- version_requirements: !ruby/object:Gem::Requirement
54
- requirements:
55
- - - "~>"
56
- - !ruby/object:Gem::Version
57
- version: '0.6'
58
- - !ruby/object:Gem::Dependency
59
- name: webmock
60
- requirement: !ruby/object:Gem::Requirement
61
- requirements:
62
- - - "~>"
63
- - !ruby/object:Gem::Version
64
- version: '1.18'
65
- type: :development
66
- prerelease: false
67
- version_requirements: !ruby/object:Gem::Requirement
68
- requirements:
69
- - - "~>"
70
- - !ruby/object:Gem::Version
71
- version: '1.18'
72
30
  - !ruby/object:Gem::Dependency
73
31
  name: guard-rspec
74
32
  requirement: !ruby/object:Gem::Requirement
@@ -140,6 +98,7 @@ files:
140
98
  - README.md
141
99
  - lib/rack/exception.rb
142
100
  - lib/rack/reverse_proxy.rb
101
+ - lib/rack/reverse_proxy/http_streaming_response.rb
143
102
  - lib/rack/reverse_proxy_matcher.rb
144
103
  homepage: http://github.com/waterlink/rack-reverse-proxy
145
104
  licenses: []
@@ -160,7 +119,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
160
119
  version: '0'
161
120
  requirements: []
162
121
  rubyforge_project:
163
- rubygems_version: 2.2.2
122
+ rubygems_version: 2.4.6
164
123
  signing_key:
165
124
  specification_version: 4
166
125
  summary: A Simple Reverse Proxy for Rack