Created
August 17, 2011 13:01
-
-
Save jkamenik/1151485 to your computer and use it in GitHub Desktop.
Sinatra Proxy
This file contains hidden or 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
res = Net::HTTP.start(<server>,<port>) do |http| | |
http.get <url> | |
end | |
res.body |
This file contains hidden or 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 'rubygems' | |
require 'sinatra' | |
require 'net/http' | |
require 'cgi' | |
require 'uri' | |
# blindly serve index.html | |
get "/" do | |
File.read('index.html') | |
end | |
# proxy to the remote | |
get "/remote/*" do | |
content_type 'application/json' | |
url = params[:splat].first.split('/') | |
server = url.shift.split(":") | |
url = url.join('/') | |
params.delete('splat') | |
params.delete('_dc') | |
res = Net::HTTP.start(server.first,server.last || 80) do |http| | |
http.get "/#{url}?#{urlencode params}" | |
end | |
res.body | |
end | |
def urlencode(param) | |
items = [] | |
params.each do |key,value| | |
if value.class == Array | |
value.each do |x| | |
items.push "#{CGI.escape key.to_s}[]=#{CGI.escape x.to_s}" | |
end | |
else | |
items.push "#{CGI.escape key.to_s}=#{CGI.escape value.to_s}" | |
end | |
end | |
items.join '&' | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment