#!/usr/bin/env ruby
require 'net/http'
+require 'net/https'
require 'uri'
class NilClass
- # Make it easier to check for empty string or nil
- def empty?; true; end
- def to_s; ""; end
+ # Make it easier to check for empty string or nil
+ def empty?; true; end
+ def to_s; ""; end
end
-def getParams(url)
- if !ENV["PATH_INFO"].empty?
- url += ENV["PATH_INFO"]
- end
+def get_params(url)
+ if !ENV["PATH_INFO"].empty?
+ url += ENV["PATH_INFO"]
+ end
- url = URI.parse(url);
- if url.scheme != "http"
- raise RuntimeError, "Unsupported scheme: #{url.scheme}"
- end
+ url = URI.parse(url);
+ if !["http", "https"].include? url.scheme
+ raise RuntimeError, "Unsupported scheme: #{url.scheme}"
+ end
- path = url.path
- if !ENV["QUERY_STRING"].empty?
- path += "?" + ENV["QUERY_STRING"]
- end
+ use_ssl = url.scheme == 'https'
- return url.host, url.port, path
-end
+ filename = url.path.split("/")[-1]
+ path = url.path
+ if !ENV["QUERY_STRING"].empty?
+ path += "?" + ENV["QUERY_STRING"]
+ end
-def createRequest(method, path)
- if method == "GET"
- req = Net::HTTP::Get.new(path)
- elsif method == "POST"
- req = Net::HTTP::Post.new(path)
- req.body = $stdin.read()
- else
- raise RuntimeError, "No support for method: #{method}"
- end
- return req
+ return url.host, url.port, use_ssl, filename, path
end
-def insertHeaders(req)
- req["X-Forwarded-For"] = ENV["REMOTE_ADDR"]
- 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"]
+def add_header(req, env_key, header_key)
+ value = ENV[env_key]
+ if !value.empty?
+ req[header_key] = value
+ end
end
-def doRequest(req, host, port)
- # Make the request
- res = Net::HTTP.start(host, port) do |http|
- http.request(req)
- end
-
- # Tweak the headers a little
- res.delete("transfer-encoding")
- res.delete("transfer-length")
- res["connection"] = "close"
-
- return res
+def create_request(method, path, ff_header)
+ if method == "GET"
+ req = Net::HTTP::Get.new(path)
+ elsif method == "POST"
+ req = Net::HTTP::Post.new(path)
+ req.body = $stdin.read()
+ else
+ raise RuntimeError, "No support for method: #{method}"
+ end
+
+ if ff_header
+ add_header(req, "REMOTE_ADDR", "X-Forwarded-For")
+ end
+ 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
-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"
- end
- end
- print "\r\n"
- print res.body
+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
+ res = http.request req do |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|
+ $stdout.write "#{key}: #{value}\r\n"
+ end
+ end
+ $stdout.write "\r\n"
+
+ out = nil
+ if output_dir
+ out = File.open("#{output_dir}/#{filename}", 'w')
+ end
+ res.read_body do |chunk|
+ $stdout.write chunk
+ if out
+ out.write chunk
+ end
+ end
+ end
end
def debug(msg)
- File.open("/tmp/debuglog.txt", "a") { |f|
- f << Time.new.to_s + " " + msg + "\n"
- }
+ File.open("/tmp/debuglog.txt", "a") { |f|
+ f << Time.new.to_s + " " + msg + "\n"
+ }
end
-def proxyTo(basePath)
- host, port, path = getParams(basePath)
- req = createRequest(ENV["REQUEST_METHOD"], path)
- insertHeaders(req)
- res = doRequest(req, host, port)
- printResult(res)
+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