Skip to content

Instantly share code, notes, and snippets.

@jkamenik
Created August 17, 2011 13:01
Show Gist options
  • Save jkamenik/1151485 to your computer and use it in GitHub Desktop.
Save jkamenik/1151485 to your computer and use it in GitHub Desktop.
Sinatra Proxy
res = Net::HTTP.start(<server>,<port>) do |http|
http.get <url>
end
res.body
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