Ruby proxy
authorJames Bunton <jamesbunton@fastmail.fm>
Fri, 26 Oct 2007 09:51:06 +0000 (19:51 +1000)
committerJames Bunton <jamesbunton@fastmail.fm>
Fri, 26 Oct 2007 09:51:06 +0000 (19:51 +1000)
php/path.php [moved from path.php with 100% similarity]
php/proxy.php [moved from proxy.php with 100% similarity]
ruby/path.cgi [new file with mode: 0755]
ruby/proxy.rb [new file with mode: 0755]

similarity index 100%
rename from path.php
rename to php/path.php
similarity index 100%
rename from proxy.php
rename to php/proxy.php
diff --git a/ruby/path.cgi b/ruby/path.cgi
new file mode 100755 (executable)
index 0000000..108d1f5
--- /dev/null
@@ -0,0 +1,5 @@
+#!/usr/bin/env ruby
+
+require 'proxy.rb'
+proxyTo "http://internalserver/somepath"
+
diff --git a/ruby/proxy.rb b/ruby/proxy.rb
new file mode 100755 (executable)
index 0000000..f03091d
--- /dev/null
@@ -0,0 +1,94 @@
+#!/usr/bin/env ruby
+
+require 'net/http'
+require 'uri'
+
+class NilClass
+       # Make it easier to check for empty string or nil
+       def empty?; true; end
+       def to_s; ""; end
+end
+
+
+def createUrl(url)
+       if !ENV["PATH_INFO"].empty?
+               url += ENV["PATH_INFO"]
+       end
+       if !ENV["QUERY_STRING"].empty?
+               url += "?" + ENV["QUERY_STRING"]
+       end
+       url = URI.parse(url);
+       if url.scheme != "http"
+               raise RuntimeError, "Unsupported scheme: #{url.scheme}"
+       end
+       return url
+end
+
+def createRequest(method, url)
+       path = url.path
+       if !url.query.empty?
+               path += "?" + url.query
+       end
+       if method == "GET"
+               req = Net::HTTP::Get.new(path)
+       elsif method == "POST"
+               req = Net::HTTP::Post.new(path)
+               req.body = $stdin.read()
+       else
+               raise RuntimeError, "No support for method: #{method}"
+       end
+       return req
+end
+
+def insertHeaders(req)
+       req["X-Forwarded-For"] = ENV["REMOTE_ADDR"]
+       req["Host"] = ENV["HTTP_HOST"]
+       req["Cookie"] = ENV["HTTP_COOKIE"]
+       req["Referer"] = ENV["HTTP_REFERER"]
+       req["Content-Length"] = ENV["CONTENT_LENGTH"]
+       req["Content-Type"] = ENV["CONTENT_TYPE"]
+       req["User-Agent"] = ENV["HTTP_USER_AGENT"]
+       req["Cache-Control"] = ENV["HTTP_CACHE_CONTROL"]
+       req["Authorization"] = ENV["HTTP_AUTHORIZATION"]
+       req["Accept"] = ENV["HTTP_ACCEPT"]
+       req["Accept-Charset"] = ENV["HTTP_ACCEPT_CHARSET"]
+       req["Accept-Encoding"] = ENV["HTTP_ACCEPT_ENCODING"]
+       req["Accept-Language"] = ENV["HTTP_ACCEPT_LANGUAGE"]
+end
+
+def doRequest(req, host, port)
+       # Make the request
+       res = Net::HTTP.start(host, port) do |http|
+               http.request(req)
+       end
+
+       # Tweak the headers a little
+       res.delete("transfer-encoding")
+       res.delete("transfer-length")
+       res["connection"] = "close"
+
+       return res
+end
+
+def printResult(res)
+       res.each_capitalized do |key, value|
+               print "#{key}: #{value}\r\n"
+       end
+       print "\r\n"
+       print res.body
+end
+
+def debug(msg)
+       File.open("/tmp/debuglog.txt", "a") { |f|
+               f << Time.new.to_s + " " + msg + "\n"
+       }
+end
+
+def proxyTo(basePath)
+       url = createUrl(basePath)
+       req = createRequest(ENV["REQUEST_METHOD"], url)
+       insertHeaders(req)
+       res = doRequest(req, url.host, url.port)
+       printResult(res)
+end
+