From 40070216e54515585ee0a6b6ab6675cee49083d2 Mon Sep 17 00:00:00 2001 From: James Bunton Date: Tue, 5 Nov 2013 21:16:38 +1100 Subject: [PATCH] Renamed methods, make output_dir optional --- ruby/path.cgi | 2 +- ruby/proxy.rb | 25 +++++++++++++++---------- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/ruby/path.cgi b/ruby/path.cgi index 69f338e..ac9054f 100755 --- a/ruby/path.cgi +++ b/ruby/path.cgi @@ -6,5 +6,5 @@ case host when "public.example.com" host = "internal.example.com" end -proxyTo "https://#{host}" +proxy_to "https://#{host}" diff --git a/ruby/proxy.rb b/ruby/proxy.rb index 2c77d48..9563f88 100644 --- a/ruby/proxy.rb +++ b/ruby/proxy.rb @@ -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 -- 2.39.2