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 69f338e..ac9054f 100755 (executable)
@@ -6,5 +6,5 @@ case host
 when "public.example.com"
        host = "internal.example.com"
 end
-proxyTo "https://#{host}"
+proxy_to "https://#{host}"
 
index 2c77d48..9563f88 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,7 @@ def getParams(url)
        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"
@@ -42,7 +42,7 @@ def createRequest(method, path, ffHeader)
                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"]
@@ -61,7 +61,7 @@ def createRequest(method, path, ffHeader)
        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
@@ -82,10 +82,15 @@ def doProxy(req, host, port, use_ssl, filename, outputDir)
                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
-                       out.write chunk
+                       if out
+                               out.write chunk
+                       end
                end
        end
 end
@@ -96,9 +101,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