Created
March 18, 2012 23:13
-
-
Save alexschwartz/2084493 to your computer and use it in GitHub Desktop.
Sinatra proxy
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
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