]> code.delx.au - cgiproxy/commitdiff
Renamed methods, make output_dir optional
authorJames Bunton <jamesbunton@delx.net.au>
Tue, 5 Nov 2013 10:16:38 +0000 (21:16 +1100)
committerJames Bunton <jamesbunton@delx.net.au>
Tue, 5 Nov 2013 10:16:38 +0000 (21:16 +1100)
ruby/path.cgi
ruby/proxy.rb

index 69f338ea69f75ee5c990840ac44fc3ec6e657a34..ac9054f6aa2858105f576bbeab1e80f1772b2d89 100755 (executable)
@@ -6,5 +6,5 @@ case host
 when "public.example.com"
        host = "internal.example.com"
 end
 when "public.example.com"
        host = "internal.example.com"
 end
-proxyTo "https://#{host}"
+proxy_to "https://#{host}"
 
 
index 2c77d4803a8d0363dfb1deb1c91972de185005ac..9563f8870814fdb95c09735822747980637addfb 100644 (file)
@@ -11,7 +11,7 @@ class NilClass
 end
 
 
 end
 
 
-def getParams(url)
+def get_params(url)
        if !ENV["PATH_INFO"].empty?
                url += ENV["PATH_INFO"]
        end
        if !ENV["PATH_INFO"].empty?
                url += ENV["PATH_INFO"]
        end
@@ -32,7 +32,7 @@ def getParams(url)
        return url.host, url.port, use_ssl, filename, path
 end
 
        return url.host, url.port, use_ssl, filename, path
 end
 
-def createRequest(method, path, ffHeader)
+def create_request(method, path, ff_header)
        if method == "GET"
                req = Net::HTTP::Get.new(path)
        elsif method == "POST"
        if method == "GET"
                req = Net::HTTP::Get.new(path)
        elsif method == "POST"
@@ -42,7 +42,7 @@ def createRequest(method, path, ffHeader)
                raise RuntimeError, "No support for method: #{method}"
        end
 
                raise RuntimeError, "No support for method: #{method}"
        end
 
-       if ffHeader
+       if ff_header
                req["X-Forwarded-For"] = ENV["REMOTE_ADDR"]
        end
        req["Host"] = ENV["HTTP_HOST"]
                req["X-Forwarded-For"] = ENV["REMOTE_ADDR"]
        end
        req["Host"] = ENV["HTTP_HOST"]
@@ -61,7 +61,7 @@ def createRequest(method, path, ffHeader)
        return req
 end
 
        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
        # Make the request
        http = Net::HTTP.new(host, port)
        http.use_ssl = use_ssl
@@ -82,10 +82,15 @@ def doProxy(req, host, port, use_ssl, filename, outputDir)
                end
                print "\r\n"
 
                end
                print "\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
                res.read_body do |chunk|
                        print chunk
-                       out.write chunk
+                       if out
+                               out.write chunk
+                       end
                end
        end
 end
                end
        end
 end
@@ -96,9 +101,9 @@ def debug(msg)
        }
 end
 
        }
 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
 
 end