Skip to content

Instantly share code, notes, and snippets.

@alexschwartz
Created March 18, 2012 23:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexschwartz/2084493 to your computer and use it in GitHub Desktop.
Save alexschwartz/2084493 to your computer and use it in GitHub Desktop.
Sinatra proxy
require 'sinatra'
require 'open-uri'
get '/?' do
erb :index
end
get '/proxy/:domain/:port/?*' do |domain,port,path|
puts "TBD: proxy domain #{domain}, path #{path}"
open("http://#{domain}:#{port}/#{path}").read
end
# an alternative route, no diret on browser side
get '/hosts/:host/proxy/:port/?*' do |host,port,path|
call env.merge("PATH_INFO" => "/proxy/#{host}/#{port}/#{path}")
end
__END__
@@ layout
<html>
<body>
<%= yield %>
</body>
</html>
@@ index
<h1>Sample: Implement a proxy with ruby/sinatra</h3>
Please try
<ul>
<li><a href='/proxy/www.sinatrarb.com/80/documentation'>/proxy/www.sinatrarb.com/80/documentation</a>
which yields www.sinatrarb.com/documentation
</li>
<li><a href='/hosts/www.sinatrarb.com/proxy/80/documentation'>/hosts/www.sinatrarb.com/proxy/80/documentation</a>
which yields www.sinatrarb.com/documentation
</li>
</ul>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment