Cleaned up code slightly
authorJames Bunton <jamesbunton@fastmail.fm>
Fri, 26 Oct 2007 10:41:53 +0000 (20:41 +1000)
committerJames Bunton <jamesbunton@fastmail.fm>
Fri, 26 Oct 2007 10:41:53 +0000 (20:41 +1000)
ruby/proxy.rb

index f03091d..4eb744b 100755 (executable)
@@ -10,25 +10,25 @@ class NilClass
 end
 
 
-def createUrl(url)
+def getParams(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
+       if !ENV["QUERY_STRING"].empty?
+               path += "?" + ENV["QUERY_STRING"]
        end
+
+       return url.host, url.port, path
+end
+
+def createRequest(method, path)
        if method == "GET"
                req = Net::HTTP::Get.new(path)
        elsif method == "POST"
@@ -85,10 +85,10 @@ def debug(msg)
 end
 
 def proxyTo(basePath)
-       url = createUrl(basePath)
-       req = createRequest(ENV["REQUEST_METHOD"], url)
+       host, port, path = getParams(basePath)
+       req = createRequest(ENV["REQUEST_METHOD"], path)
        insertHeaders(req)
-       res = doRequest(req, url.host, url.port)
+       res = doRequest(req, host, port)
        printResult(res)
 end