]> code.delx.au - cgiproxy/blobdiff - ruby/proxy.rb
proxy.rb fixes
[cgiproxy] / ruby / proxy.rb
index 2c77d4803a8d0363dfb1deb1c91972de185005ac..bef860216233b8e1b8297fdc8fe9e040b2f39797 100644 (file)
@@ -11,7 +11,7 @@ class NilClass
 end
 
 
-def getParams(url)
+def get_params(url)
        if !ENV["PATH_INFO"].empty?
                url += ENV["PATH_INFO"]
        end
@@ -32,7 +32,14 @@ def getParams(url)
        return url.host, url.port, use_ssl, filename, path
 end
 
-def createRequest(method, path, ffHeader)
+def add_header(req, env_key, header_key)
+       value = ENV[env_key]
+       if !value.empty?
+               req[header_key] = value
+       end
+end
+
+def create_request(method, path, ff_header)
        if method == "GET"
                req = Net::HTTP::Get.new(path)
        elsif method == "POST"
@@ -42,50 +49,50 @@ def createRequest(method, path, ffHeader)
                raise RuntimeError, "No support for method: #{method}"
        end
 
-       if ffHeader
-               req["X-Forwarded-For"] = ENV["REMOTE_ADDR"]
+       if ff_header
+               add_header(req, "REMOTE_ADDR", "X-Forwarded-For")
        end
-       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"]
+       add_header(req, "HTTP_HOST", "Host")
+       add_header(req, "HTTP_COOKIE", "Cookie")
+       add_header(req, "HTTP_REFERER", "Referer")
+       add_header(req, "CONTENT_LENGTH", "Content-Length")
+       add_header(req, "CONTENT_TYPE", "Content-Type")
+       add_header(req, "HTTP_USER_AGENT", "User-Agent")
+       add_header(req, "HTTP_CACHE_CONTROL", "Cache-Control")
+       add_header(req, "HTTP_AUTHORIZATION", "Authorization")
+       add_header(req, "HTTP_ACCEPT", "Accept")
+       add_header(req, "HTTP_ACCEPT_CHARSET", "Accept-Charset")
+       add_header(req, "HTTP_ACCEPT_ENCODING", "Accept-Encoding")
+       add_header(req, "HTTP_ACCEPT_LANGUAGE", "Accept-Language")
 
        return req
 end
 
-def doProxy(req, host, port, use_ssl, filename, outputDir)
+def do_proxy(req, host, port, use_ssl, filename, output_dir)
        # Make the request
        http = Net::HTTP.new(host, port)
        http.use_ssl = use_ssl
        res = http.request req do |res|
 
-               # Tweak the headers a little
-               res.delete("transfer-encoding")
-               res.delete("transfer-length")
-               res["connection"] = "close"
-
                if res.code != "200"
                        res["Status"] = "#{res.code} #{res.message}"
                end
                res.each_capitalized_name do |key|
                        res.get_fields(key).each do |value|
-                               print "#{key}: #{value}\r\n"
+                               $stdout.write "#{key}: #{value}\r\n"
                        end
                end
-               print "\r\n"
+               $stdout.write "\r\n"
 
-               out = File.open("#{outputDir}/#{filename}", 'w')
+               out = nil
+               if output_dir
+                       out = File.open("#{output_dir}/#{filename}", 'w')
+               end
                res.read_body do |chunk|
-                       print chunk
-                       out.write chunk
+                       $stdout.write chunk
+                       if out
+                               out.write chunk
+                       end
                end
        end
 end
@@ -96,9 +103,9 @@ def debug(msg)
        }
 end
 
-def proxyTo(basePath, ffHeader=True, outputDir=None)
-       host, port, use_ssl, filename, path = getParams(basePath)
-       req = createRequest(ENV["REQUEST_METHOD"], path, ffHeader)
-       doProxy(req, host, port, use_ssl, filename, outputDir)
+def proxy_to(base_path, ff_header=True, output_dir=nil)
+       host, port, use_ssl, filename, path = get_params(base_path)
+       req = create_request(ENV["REQUEST_METHOD"], path, ff_header)
+       do_proxy(req, host, port, use_ssl, filename, output_dir)
 end