Skip to content

Commit

Permalink
Merge pull request oesmith#118 from springleaf/proxy-support
Browse files Browse the repository at this point in the history
Add support for internal proxies
  • Loading branch information
ronwsmith committed Aug 25, 2015
2 parents 7663b9a + 3c33fae commit 1e03e13
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 62 deletions.
131 changes: 74 additions & 57 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,40 @@ PATH
addressable
em-http-request (~> 1.1.0)
em-synchrony
eventmachine
eventmachine (= 1.0.4)
eventmachine_httpserver
http_parser.rb (~> 0.6.0)
multi_json

GEM
remote: https://rubygems.org/
specs:
addressable (2.3.6)
addressable (2.3.8)
builder (3.2.2)
capybara (2.1.0)
capybara (2.4.4)
mime-types (>= 1.16)
nokogiri (>= 1.3.3)
rack (>= 1.0.0)
rack-test (>= 0.5.4)
xpath (~> 2.0)
capybara-webkit (1.1.1)
capybara (>= 2.0.2, < 2.2.0)
capybara-webkit (1.6.0)
capybara (>= 2.3.0, < 2.5.0)
json
celluloid (0.15.2)
timers (~> 1.1.0)
childprocess (0.4.0)
childprocess (0.5.6)
ffi (~> 1.0, >= 1.0.11)
cliver (0.3.2)
coderay (1.1.0)
cookiejar (0.3.2)
cucumber (1.3.10)
cucumber (2.0.2)
builder (>= 2.1.2)
cucumber-core (~> 1.2.0)
diff-lcs (>= 1.1.3)
gherkin (~> 2.12)
multi_json (>= 1.7.5, < 2.0)
multi_test (>= 0.0.2)
daemons (1.1.9)
multi_test (>= 0.1.2)
cucumber-core (1.2.0)
gherkin (~> 2.12.0)
daemons (1.2.3)
diff-lcs (1.2.5)
em-http-request (1.1.2)
addressable (>= 2.3.4)
Expand All @@ -47,75 +48,88 @@ GEM
http_parser.rb (>= 0.6.0)
em-socksify (0.3.0)
eventmachine (>= 1.0.0.beta.4)
em-synchrony (1.0.3)
em-synchrony (1.0.4)
eventmachine (>= 1.0.0.beta.1)
eventmachine (1.0.3)
eventmachine (1.0.4)
eventmachine_httpserver (0.2.1)
faraday (0.9.0)
faraday (0.9.1)
multipart-post (>= 1.2, < 3)
ffi (1.9.3)
formatador (0.2.4)
ffi (1.9.10)
formatador (0.2.5)
gherkin (2.12.2)
multi_json (~> 1.3)
guard (2.4.0)
guard (2.13.0)
formatador (>= 0.2.4)
listen (~> 2.1)
listen (>= 2.7, <= 4.0)
lumberjack (~> 1.0)
nenv (~> 0.1)
notiffany (~> 0.0)
pry (>= 0.9.12)
shellany (~> 0.0)
thor (>= 0.18.1)
http_parser.rb (0.6.0)
json (1.8.1)
listen (2.4.1)
celluloid (>= 0.15.2)
json (1.8.3)
listen (3.0.3)
rb-fsevent (>= 0.9.3)
rb-inotify (>= 0.9)
lumberjack (1.0.4)
lumberjack (1.0.9)
method_source (0.8.2)
mime-types (2.1)
mini_portile (0.5.2)
multi_json (1.8.4)
multi_test (0.0.3)
mime-types (2.6.1)
mini_portile (0.6.2)
multi_json (1.11.2)
multi_test (0.1.2)
multipart-post (2.0.0)
nokogiri (1.6.1)
mini_portile (~> 0.5.0)
poltergeist (1.5.0)
nenv (0.2.0)
nokogiri (1.6.6.2)
mini_portile (~> 0.6.0)
notiffany (0.0.7)
nenv (~> 0.1)
shellany (~> 0.0)
poltergeist (1.6.0)
capybara (~> 2.1)
cliver (~> 0.3.1)
multi_json (~> 1.0)
websocket-driver (>= 0.2.0)
pry (0.9.12.6)
coderay (~> 1.0)
method_source (~> 0.8)
pry (0.10.1)
coderay (~> 1.1.0)
method_source (~> 0.8.1)
slop (~> 3.4)
rack (1.5.2)
rack-test (0.6.2)
rack (1.6.4)
rack-test (0.6.3)
rack (>= 1.0)
rb-fsevent (0.9.4)
rb-inotify (0.9.3)
rb-fsevent (0.9.5)
rb-inotify (0.9.5)
ffi (>= 0.5.0)
rspec (2.14.1)
rspec-core (~> 2.14.0)
rspec-expectations (~> 2.14.0)
rspec-mocks (~> 2.14.0)
rspec-core (2.14.7)
rspec-expectations (2.14.5)
diff-lcs (>= 1.1.3, < 2.0)
rspec-mocks (2.14.5)
rubyzip (1.1.0)
selenium-webdriver (2.39.0)
childprocess (>= 0.2.5)
rspec (3.3.0)
rspec-core (~> 3.3.0)
rspec-expectations (~> 3.3.0)
rspec-mocks (~> 3.3.0)
rspec-core (3.3.2)
rspec-support (~> 3.3.0)
rspec-expectations (3.3.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.3.0)
rspec-mocks (3.3.2)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.3.0)
rspec-support (3.3.0)
rubyzip (1.1.7)
selenium-webdriver (2.47.1)
childprocess (~> 0.5)
multi_json (~> 1.0)
rubyzip (~> 1.0)
websocket (~> 1.0.4)
slop (3.4.7)
thin (1.6.1)
daemons (>= 1.0.9)
eventmachine (>= 1.0.0)
rack (>= 1.0.0)
thor (0.18.1)
timers (1.1.0)
websocket (1.0.7)
websocket-driver (0.3.2)
websocket (~> 1.0)
shellany (0.0.1)
slop (3.6.0)
thin (1.6.3)
daemons (~> 1.0, >= 1.0.9)
eventmachine (~> 1.0)
rack (~> 1.0)
thor (0.19.1)
websocket (1.2.2)
websocket-driver (0.6.2)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.2)
xpath (2.0.0)
nokogiri (~> 1.3)

Expand All @@ -136,3 +150,6 @@ DEPENDENCIES
rspec
selenium-webdriver
thin

BUNDLED WITH
1.10.6
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,8 @@ Billy.configure do |c|
c.cache_path = 'spec/req_cache/'
c.proxy_host = 'example.com' # defaults to localhost
c.proxy_port = 12345 # defaults to random
c.proxied_request_host = nil
c.proxied_request_port = 80
end
```

Expand Down Expand Up @@ -258,6 +260,11 @@ allowed, all others will throw an error with the URL attempted to be accessed.
This is useful for debugging issues in isolated environments (ie.
continuous integration).

`c.proxy_host` and `c.proxy_port` are used for the Billy proxy itself which runs locally.

`c.proxied_request_host` and `c.proxied_request_port` are used if an internal proxy
server is required to access the internet. Most common in larger companies.

### Cache Scopes

If you need to cache different responses to the same HTTP request, you can use
Expand Down
4 changes: 3 additions & 1 deletion lib/billy/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Config
:persist_cache, :ignore_cache_port, :non_successful_cache_disabled, :non_successful_error_level,
:non_whitelisted_requests_disabled, :cache_path, :proxy_host, :proxy_port, :proxied_request_inactivity_timeout,
:proxied_request_connect_timeout, :dynamic_jsonp, :dynamic_jsonp_keys, :merge_cached_responses_whitelist,
:strip_query_params
:strip_query_params, :proxied_request_host, :proxied_request_port

def initialize
@logger = defined?(Rails) ? Rails.logger : Logger.new(STDOUT)
Expand All @@ -37,6 +37,8 @@ def reset
@proxied_request_inactivity_timeout = 10 # defaults from https://github.com/igrigorik/em-http-request/wiki/Redirects-and-Timeouts
@proxied_request_connect_timeout = 5
@strip_query_params = true
@proxied_request_host = nil
@proxied_request_port = 80
end
end

Expand Down
15 changes: 12 additions & 3 deletions lib/billy/handlers/proxy_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,15 @@ def handles_request?(_method, url, _headers, _body)

def handle_request(method, url, headers, body)
if handles_request?(method, url, headers, body)
req = EventMachine::HttpRequest.new(url,
inactivity_timeout: Billy.config.proxied_request_inactivity_timeout,
connect_timeout: Billy.config.proxied_request_connect_timeout)
opts = { inactivity_timeout: Billy.config.proxied_request_inactivity_timeout,
connect_timeout: Billy.config.proxied_request_connect_timeout }

if Billy.config.proxied_request_host && !bypass_internal_proxy?(url)
opts.merge!({ proxy: { host: Billy.config.proxied_request_host,
port: Billy.config.proxied_request_port }} )
end

req = EventMachine::HttpRequest.new(url, opts)
req = req.send(method.downcase, build_request_options(headers, body))

if req.error
Expand Down Expand Up @@ -109,5 +114,9 @@ def successful_status?(status)
def cacheable_status?(status)
Billy.config.non_successful_cache_disabled ? successful_status?(status) : true
end

def bypass_internal_proxy?(url)
url.include?('localhost') || url.include?('127.') || url.include?('.dev') || url.include?('.fin')
end
end
end
2 changes: 1 addition & 1 deletion puffing-billy.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Gem::Specification.new do |gem|
gem.add_development_dependency 'pry'
gem.add_development_dependency 'cucumber'
gem.add_runtime_dependency 'addressable'
gem.add_runtime_dependency 'eventmachine'
gem.add_runtime_dependency 'eventmachine', '= 1.0.4'
gem.add_runtime_dependency 'em-synchrony'
gem.add_runtime_dependency 'em-http-request', '~> 1.1.0'
gem.add_runtime_dependency 'eventmachine_httpserver'
Expand Down
16 changes: 16 additions & 0 deletions spec/lib/billy/handlers/proxy_handler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,22 @@
request[:headers],
request[:body])
end

it 'uses the internal proxy settings defined in configuration' do
allow(Billy.config).to receive(:proxied_request_host).and_return('10.10.10.10')
allow(Billy.config).to receive(:proxied_request_port).and_return('2080')

expect(EventMachine::HttpRequest).to receive(:new).with(request[:url],
inactivity_timeout: 10,
connect_timeout: 5,
proxy: { host: '10.10.10.10', port: '2080' }
)

subject.handle_request(request[:method],
request[:url],
request[:headers],
request[:body])
end
end
end
end

0 comments on commit 1e03e13

Please sign in to comment.