proxy.rb fixes
authorJames Bunton <jamesbunton@delx.net.au>
Tue, 5 Nov 2013 11:26:59 +0000 (22:26 +1100)
committerJames Bunton <jamesbunton@delx.net.au>
Tue, 5 Nov 2013 11:26:59 +0000 (22:26 +1100)
ruby/proxy.rb

index 9563f88..bef8602 100644 (file)
@@ -32,6 +32,13 @@ def get_params(url)
        return url.host, url.port, use_ssl, filename, path
 end
 
+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)
@@ -43,20 +50,20 @@ def create_request(method, path, ff_header)
        end
 
        if ff_header
-               req["X-Forwarded-For"] = ENV["REMOTE_ADDR"]
+               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
@@ -67,27 +74,22 @@ def do_proxy(req, host, port, use_ssl, filename, output_dir)
        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 = nil
                if output_dir
                        out = File.open("#{output_dir}/#{filename}", 'w')
                end
                res.read_body do |chunk|
-                       print chunk
+                       $stdout.write chunk
                        if out
                                out.write chunk
                        end