Stream proxied content to clients
authorJames Bunton <jamesbunton@delx.net.au>
Fri, 5 Oct 2012 12:55:17 +0000 (22:55 +1000)
committerJames Bunton <jamesbunton@delx.net.au>
Fri, 5 Oct 2012 12:55:17 +0000 (22:55 +1000)
ruby/proxy.rb

index 27e5538..2c77d48 100644 (file)
@@ -23,7 +23,7 @@ def getParams(url)
 
        use_ssl = url.scheme == 'https'
 
-       filename = url.path
+       filename = url.path.split("/")[-1]
        path = url.path
        if !ENV["QUERY_STRING"].empty?
                path += "?" + ENV["QUERY_STRING"]
@@ -32,7 +32,7 @@ def getParams(url)
        return url.host, url.port, use_ssl, filename, path
 end
 
-def createRequest(method, path)
+def createRequest(method, path, ffHeader)
        if method == "GET"
                req = Net::HTTP::Get.new(path)
        elsif method == "POST"
@@ -41,10 +41,7 @@ def createRequest(method, path)
        else
                raise RuntimeError, "No support for method: #{method}"
        end
-       return req
-end
 
-def insertHeaders(req, ffHeader)
        if ffHeader
                req["X-Forwarded-For"] = ENV["REMOTE_ADDR"]
        end
@@ -60,38 +57,37 @@ def insertHeaders(req, ffHeader)
        req["Accept-Charset"] = ENV["HTTP_ACCEPT_CHARSET"]
        req["Accept-Encoding"] = ENV["HTTP_ACCEPT_ENCODING"]
        req["Accept-Language"] = ENV["HTTP_ACCEPT_LANGUAGE"]
+
+       return req
 end
 
-def doRequest(req, host, port, use_ssl)
+def doProxy(req, host, port, use_ssl, filename, outputDir)
        # Make the request
        http = Net::HTTP.new(host, port)
        http.use_ssl = use_ssl
-       res = http.request(req)
+       res = http.request req do |res|
 
-       # Tweak the headers a little
-       res.delete("transfer-encoding")
-       res.delete("transfer-length")
-       res["connection"] = "close"
+               # Tweak the headers a little
+               res.delete("transfer-encoding")
+               res.delete("transfer-length")
+               res["connection"] = "close"
 
-       return res
-end
+               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"
+                       end
+               end
+               print "\r\n"
 
-def printResult(res)
-       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"
+               out = File.open("#{outputDir}/#{filename}", 'w')
+               res.read_body do |chunk|
+                       print chunk
+                       out.write chunk
                end
        end
-       print "\r\n"
-       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)
@@ -102,10 +98,7 @@ end
 
 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)
+       req = createRequest(ENV["REQUEST_METHOD"], path, ffHeader)
+       doProxy(req, host, port, use_ssl, filename, outputDir)
 end