]> code.delx.au - cgiproxy/commitdiff
Option to write proxied files to a directory
authorJames Bunton <jamesbunton@delx.net.au>
Fri, 5 Oct 2012 09:04:32 +0000 (19:04 +1000)
committerJames Bunton <jamesbunton@delx.net.au>
Fri, 5 Oct 2012 09:04:32 +0000 (19:04 +1000)
ruby/proxy.rb [changed mode: 0755->0644]

old mode 100755 (executable)
new mode 100644 (file)
index 10002f1..27e5538
@@ -23,12 +23,13 @@ def getParams(url)
 
        use_ssl = url.scheme == 'https'
 
+       filename = url.path
        path = url.path
        if !ENV["QUERY_STRING"].empty?
                path += "?" + ENV["QUERY_STRING"]
        end
 
-       return url.host, url.port, use_ssl, path
+       return url.host, url.port, use_ssl, filename, path
 end
 
 def createRequest(method, path)
@@ -88,17 +89,23 @@ def printResult(res)
        print res.body
 end
 
+def writeFile(res, filename, outputDir)
+       filename = filename.split("/")[-1]
+       File.open("#{outputDir}/#{filename}", 'w') {|f| f.write(res.body) }
+end
+
 def debug(msg)
        File.open("/tmp/debuglog.txt", "a") { |f|
                f << Time.new.to_s + " " + msg + "\n"
        }
 end
 
-def proxyTo(basePath, ffHeader=True)
-       host, port, use_ssl, path = getParams(basePath)
+def proxyTo(basePath, ffHeader=True, outputDir=None)
+       host, port, use_ssl, filename, path = getParams(basePath)
        req = createRequest(ENV["REQUEST_METHOD"], path)
        insertHeaders(req, ffHeader)
        res = doRequest(req, host, port, use_ssl)
+       writeFile(res, filename, outputDir)
        printResult(res)
 end