Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

fix proxy

  • Loading branch information...
commit da1db33229bce8b5cbafcedb635009949ad77149 1 parent 926ad5f
@ddollar ddollar authored
Showing with 36 additions and 3 deletions.
  1. +36 −3 config.ru
View
39 config.ru
@@ -1,7 +1,40 @@
require "rest-client"
require "sinatra"
-get "/" do
- RestClient.proxy = ENV["PROXIMO_URL"]
- RestClient.get("http://www.whatismyip.com").to_s
+helpers do
+ def proxy
+ RestClient.proxy = ENV["PROXIMO_URL"] if ENV["PROXIMO_URL"]
+ RestClient::Resource.new(ENV["PROXY_URL"] || "https://httpbin.org")
+ end
end
+
+get "/*" do
+
+ # rebuild the full path and querystring
+ path = request.env["REQUEST_PATH"] + "?" + request.env["QUERY_STRING"]
+
+ # rack munges client headers, let's un-munge
+ headers = request.env.select { |k,v| k =~ /^HTTP_/ }.inject({}) do |ax, (key, val)|
+ ax.update(key.sub(/^HTTP_/, "").gsub("_", "-").split(/(\W)/).map(&:capitalize).join => val)
+ end
+
+ # delete host so RestClient can replace it
+ headers.delete "Host"
+
+ # proxy the request
+ proxy[path].get(headers) do |res|
+
+ # RestClient munges response headers, let's un-munge
+ fixed_headers = res.raw_headers.inject({}) do |ax, (key, val)|
+ ax.update(key => val.first)
+ end
+
+ # relay back to the user
+ status res.code
+ headers fixed_headers
+ body res.to_s
+ end
+
+end
+
+run Sinatra::Application
Please sign in to comment.
Something went wrong with that request. Please try again.