Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update readme and examples to make it easier for other folks to make …
…user of rack-proxy.
- Loading branch information
Dan Mayer
committed
Mar 5, 2018
1 parent
fca3a78
commit 2d566a8
Showing
7 changed files
with
279 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
PATH | ||
remote: . | ||
specs: | ||
rack-proxy (0.6.2) | ||
rack-proxy (0.6.4) | ||
rack | ||
|
||
GEM | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
### | ||
# This is an example of how to use Rack-Proxy in a Rails application. | ||
# | ||
# Setup: | ||
# 1. rails new test_app | ||
# 2. cd test_app | ||
# 3. install Rack-Proxy in `Gemfile` | ||
# a. `gem 'rack-proxy', '~> 0.6.3'` | ||
# 4. install gem: `bundle install` | ||
# 5. create `config/initializers/proxy.rb` adding this line `require 'rack_proxy_examples/example_service_proxy'` | ||
# 6. run: `SERVICE_URL=http://guides.rubyonrails.org rails server` | ||
# 7. open in browser: `http://localhost:3000/example_service` | ||
# | ||
### | ||
ENV['SERVICE_URL'] ||= 'http://guides.rubyonrails.org' | ||
|
||
class ExampleServiceProxy < Rack::Proxy | ||
def perform_request(env) | ||
request = Rack::Request.new(env) | ||
|
||
# use rack proxy for anything hitting our host app at /example_service | ||
if request.path =~ %r{^/example_service} | ||
backend = URI(ENV['SERVICE_URL']) | ||
# most backends required host set properly, but rack-proxy doesn't set this for you automatically | ||
# even when a backend host is passed in via the options | ||
env["HTTP_HOST"] = backend.host | ||
|
||
# This is the only path that needs to be set currently on Rails 5 & greater | ||
env['PATH_INFO'] = ENV['SERVICE_PATH'] || '/configuring.html' | ||
|
||
# don't send your sites cookies to target service, unless it is a trusted internal service that can parse all your cookies | ||
env['HTTP_COOKIE'] = '' | ||
super(env) | ||
else | ||
@app.call(env) | ||
end | ||
end | ||
end | ||
|
||
Rails.application.config.middleware.use ExampleServiceProxy, backend: ENV['SERVICE_URL'], streaming: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
class ForwardHost < Rack::Proxy | ||
|
||
def rewrite_env(env) | ||
env["HTTP_HOST"] = "example.com" | ||
env | ||
end | ||
|
||
def rewrite_response(triplet) | ||
status, headers, body = triplet | ||
|
||
# example of inserting an additional header | ||
headers["X-Foo"] = "Bar" | ||
|
||
# if you rewrite env, it appears that content-length isn't calculated correctly | ||
# resulting in only partial responses being sent to users | ||
# you can remove it or recalculate it here | ||
headers["content-length"] = nil | ||
|
||
triplet | ||
end | ||
|
||
end | ||
|
||
Rails.application.config.middleware.use ForwardHost, backend: 'http://example.com', streaming: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
### | ||
# Open http://localhost:3000/test.php to trigger proxy | ||
### | ||
class RackPhpProxy < Rack::Proxy | ||
|
||
def perform_request(env) | ||
request = Rack::Request.new(env) | ||
if request.path =~ %r{\.php} | ||
env["HTTP_HOST"] = ENV["HTTP_HOST"] ? URI(ENV["HTTP_HOST"]).host : "localhost" | ||
ENV["PHP_PATH"] ||= '/manual/en/tutorial.firstpage.php' | ||
|
||
# Rails 3 & 4 | ||
env["REQUEST_PATH"] = ENV["PHP_PATH"] || "/php/#{request.fullpath}" | ||
# Rails 5 and above | ||
env['PATH_INFO'] = ENV["PHP_PATH"] || "/php/#{request.fullpath}" | ||
|
||
env['content-length'] = nil | ||
|
||
super(env) | ||
else | ||
@app.call(env) | ||
end | ||
end | ||
|
||
def rewrite_response(triplet) | ||
status, headers, body = triplet | ||
|
||
# if you proxy depending on the backend, it appears that content-length isn't calculated correctly | ||
# resulting in only partial responses being sent to users | ||
# you can remove it or recalculate it here | ||
headers["content-length"] = nil | ||
|
||
triplet | ||
end | ||
end | ||
|
||
Rails.application.config.middleware.use RackPhpProxy, backend: ENV["HTTP_HOST"]='http://php.net', streaming: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
class TrustingProxy < Rack::Proxy | ||
|
||
def rewrite_env(env) | ||
env["HTTP_HOST"] = "self-signed.badssl.com" | ||
|
||
# We are going to trust the self-signed SSL | ||
env["rack.ssl_verify_none"] = true | ||
env | ||
end | ||
|
||
def rewrite_response(triplet) | ||
status, headers, body = triplet | ||
|
||
# if you rewrite env, it appears that content-length isn't calculated correctly | ||
# resulting in only partial responses being sent to users | ||
# you can remove it or recalculate it here | ||
headers["content-length"] = nil | ||
|
||
triplet | ||
end | ||
|
||
end | ||
|
||
Rails.application.config.middleware.use TrustingProxy, backend: 'https://self-signed.badssl.com', streaming: false |