Skip to content

Commit df0e18c

Browse files
committed
Merge pull request jaswope#13 from waterlink/refactoring
Refactoring
2 parents 3497b74 + c31253c commit df0e18c

21 files changed

+831
-478
lines changed

.gitignore

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,16 @@
1-
## MAC OS
2-
.DS_Store
3-
4-
## TEXTMATE
5-
*.tmproj
6-
tmtags
7-
8-
## EMACS
9-
*~
10-
\#*
11-
.\#*
12-
13-
## VIM
14-
*.swp
15-
16-
## PROJECT::GENERAL
17-
coverage
18-
rdoc
19-
pkg
1+
/.bundle/
2+
/.yardoc
203
/Gemfile.lock
21-
22-
## PROJECT::SPECIFIC
4+
/_yardoc/
5+
/coverage/
6+
/doc/
7+
/pkg/
8+
/spec/reports/
9+
/spec/examples.txt
10+
/tmp/
11+
*.bundle
12+
*.so
13+
*.o
14+
*.a
15+
mkmf.log
16+
tests.lock

.rubocop.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
Style/StringLiterals:
2+
EnforcedStyle: double_quotes
3+
4+
Metrics/LineLength:
5+
Max: 100
6+
7+
#begin ruby 1.8 support
8+
Style/HashSyntax:
9+
EnforcedStyle: hash_rockets
10+
11+
Style/Lambda:
12+
Enabled: false
13+
14+
Style/TrailingComma:
15+
EnforcedStyleForMultiline: no_comma
16+
17+
Style/EachWithObject:
18+
Enabled: false

.travis.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,11 @@ rvm:
99
- jruby-19mode
1010
- rbx
1111

12-
script: bundle exec rspec
12+
before_install:
13+
- gem install bundler
14+
1315
bundler_args: --without development
16+
17+
script:
18+
- bundle exec rspec
19+
- script/rubocop

Gemfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,9 @@ group :test do
66
gem "rspec"
77
gem "rack-test"
88
gem "webmock"
9+
gem "rubocop", :platform => [:ruby_20, :ruby_21, :ruby_22]
910
end
1011

12+
group :development, :test do
13+
gem "simplecov"
14+
end

Guardfile

Lines changed: 0 additions & 8 deletions
This file was deleted.

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ gem "rack-reverse-proxy", require: "rack/reverse_proxy"
1717
```
1818

1919
## Usage
20-
Matchers can be a regex or a string. If a regex is used, you can use the subcaptures in your forwarding url by denoting them with a `$`.
2120

22-
Right now if more than one matcher matches any given route, it throws an exception for an ambiguous match. This will probably change later. If no match is found, the call is forwarded to your application.
21+
Rules can be a regex or a string. If a regex is used, you can use the subcaptures in your forwarding url by denoting them with a `$`.
22+
23+
Right now if more than one rule matches any given route, it throws an exception for an ambiguous match. This will probably change later. If no match is found, the call is forwarded to your application.
2324

2425
Below is an example for configuring the middleware:
2526

@@ -43,7 +44,8 @@ end
4344
run app
4445
```
4546

46-
reverse_proxy_options sets global options for all reverse proxies. Available options are:
47+
`reverse_proxy_options` sets global options for all reverse proxies. Available options are:
48+
4749
* `:preserve_host` Set to false to omit Host headers
4850
* `:username` username for basic auth
4951
* `:password` password for basic auth

Rakefile

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
require 'rubygems'
2-
require 'rake'
3-
require 'bundler/gem_tasks'
4-
require 'rspec/core/rake_task'
1+
require "rubygems"
2+
require "rake"
3+
require "bundler/gem_tasks"
4+
require "rspec/core/rake_task"
55

66
RSpec::Core::RakeTask.new(:spec) do |spec|
7-
spec.pattern = 'spec/**/*_spec.rb'
7+
spec.pattern = "spec/**/*_spec.rb"
88
end
99

1010
task :default => :spec

VERSION

Lines changed: 0 additions & 1 deletion
This file was deleted.

lib/rack/exception.rb

Lines changed: 0 additions & 31 deletions
This file was deleted.

lib/rack/reverse_proxy.rb

Lines changed: 3 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -1,130 +1,6 @@
1-
require 'net/http'
2-
require 'net/https'
3-
require "rack-proxy"
4-
require "rack/reverse_proxy_matcher"
5-
require "rack/exception"
1+
require "rack_reverse_proxy"
62

3+
# Re-opening Rack module only to define ReverseProxy constant
74
module Rack
8-
class ReverseProxy
9-
include NewRelic::Agent::Instrumentation::ControllerInstrumentation if defined? NewRelic
10-
11-
def initialize(app = nil, &b)
12-
@app = app || lambda {|env| [404, [], []] }
13-
@matchers = []
14-
@global_options = {:preserve_host => true, :x_forwarded_host => true, :matching => :all, :replace_response_host => false}
15-
instance_eval(&b) if block_given?
16-
end
17-
18-
def call(env)
19-
rackreq = Rack::Request.new(env)
20-
matcher = get_matcher(rackreq.fullpath, Proxy.extract_http_request_headers(rackreq.env), rackreq)
21-
return @app.call(env) if matcher.nil?
22-
23-
if @global_options[:newrelic_instrumentation]
24-
action_name = "#{rackreq.path.gsub(/\/\d+/,'/:id').gsub(/^\//,'')}/#{rackreq.request_method}" # Rack::ReverseProxy/foo/bar#GET
25-
perform_action_with_newrelic_trace(:name => action_name, :request => rackreq) do
26-
proxy(env, rackreq, matcher)
27-
end
28-
else
29-
proxy(env, rackreq, matcher)
30-
end
31-
end
32-
33-
private
34-
35-
def proxy(env, source_request, matcher)
36-
uri = matcher.get_uri(source_request.fullpath,env)
37-
if uri.nil?
38-
return @app.call(env)
39-
end
40-
options = @global_options.dup.merge(matcher.options)
41-
42-
# Initialize request
43-
target_request = Net::HTTP.const_get(source_request.request_method.capitalize).new(uri.request_uri)
44-
45-
# Setup headers
46-
target_request_headers = Proxy.extract_http_request_headers(source_request.env)
47-
48-
if options[:preserve_host]
49-
if uri.port == uri.default_port
50-
target_request_headers['HOST'] = uri.host
51-
else
52-
target_request_headers['HOST'] = "#{uri.host}:#{uri.port}"
53-
end
54-
end
55-
56-
if options[:x_forwarded_host]
57-
target_request_headers['X-Forwarded-Host'] = source_request.host
58-
target_request_headers['X-Forwarded-Port'] = "#{source_request.port}"
59-
end
60-
61-
target_request.initialize_http_header(target_request_headers)
62-
63-
# Basic auth
64-
target_request.basic_auth options[:username], options[:password] if options[:username] and options[:password]
65-
66-
# Setup body
67-
if target_request.request_body_permitted? && source_request.body
68-
source_request.body.rewind
69-
target_request.body_stream = source_request.body
70-
end
71-
72-
target_request.content_length = source_request.content_length || 0
73-
target_request.content_type = source_request.content_type if source_request.content_type
74-
75-
# Create a streaming response (the actual network communication is deferred, a.k.a. streamed)
76-
target_response = HttpStreamingResponse.new(target_request, uri.host, uri.port)
77-
78-
# pass the timeout configuration through
79-
target_response.read_timeout = options[:timeout] if options[:timeout].to_i > 0
80-
81-
target_response.use_ssl = "https" == uri.scheme
82-
83-
# Let rack set the transfer-encoding header
84-
response_headers = Rack::Utils::HeaderHash.new Proxy.normalize_headers(format_headers(target_response.headers))
85-
response_headers.delete('Transfer-Encoding')
86-
response_headers.delete('Status')
87-
88-
# Replace the location header with the proxy domain
89-
if response_headers['Location'] && options[:replace_response_host]
90-
response_location = URI(response_headers['location'])
91-
response_location.host = source_request.host
92-
response_location.port = source_request.port
93-
response_headers['Location'] = response_location.to_s
94-
end
95-
96-
[target_response.status, response_headers, target_response.body]
97-
end
98-
99-
def get_matcher(path, headers, rackreq)
100-
matches = @matchers.select do |matcher|
101-
matcher.match?(path, headers, rackreq)
102-
end
103-
104-
if matches.length < 1
105-
nil
106-
elsif matches.length > 1 && @global_options[:matching] != :first
107-
raise AmbiguousProxyMatch.new(path, matches)
108-
else
109-
matches.first
110-
end
111-
end
112-
113-
def reverse_proxy_options(options)
114-
@global_options=options
115-
end
116-
117-
def reverse_proxy(matcher, url=nil, opts={})
118-
raise GenericProxyURI.new(url) if matcher.is_a?(String) && url.is_a?(String) && URI(url).class == URI::Generic
119-
@matchers << ReverseProxyMatcher.new(matcher,url,opts)
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
129-
end
5+
ReverseProxy = RackReverseProxy::Middleware
1306
end

0 commit comments

Comments
 (0)