]> code.delx.au - cgiproxy/commitdiff
Tabs to spaces
authorJames Bunton <jamesbunton@delx.net.au>
Fri, 6 Feb 2015 08:17:39 +0000 (19:17 +1100)
committerJames Bunton <jamesbunton@delx.net.au>
Fri, 6 Feb 2015 08:17:39 +0000 (19:17 +1100)
ruby/proxy.rb

index bef860216233b8e1b8297fdc8fe9e040b2f39797..7e3cf66d7b453524f99fb7a4479752be598c91cd 100644 (file)
@@ -5,107 +5,107 @@ 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 get_params(url)
-       if !ENV["PATH_INFO"].empty?
-               url += ENV["PATH_INFO"]
-       end
+    if !ENV["PATH_INFO"].empty?
+        url += ENV["PATH_INFO"]
+    end
 
-       url = URI.parse(url);
-       if !["http", "https"].include? url.scheme
-               raise RuntimeError, "Unsupported scheme: #{url.scheme}"
-       end
+    url = URI.parse(url);
+    if !["http", "https"].include? url.scheme
+        raise RuntimeError, "Unsupported scheme: #{url.scheme}"
+    end
 
-       use_ssl = url.scheme == 'https'
+    use_ssl = url.scheme == 'https'
 
-       filename = url.path.split("/")[-1]
-       path = url.path
-       if !ENV["QUERY_STRING"].empty?
-               path += "?" + ENV["QUERY_STRING"]
-       end
+    filename = url.path.split("/")[-1]
+    path = url.path
+    if !ENV["QUERY_STRING"].empty?
+        path += "?" + ENV["QUERY_STRING"]
+    end
 
-       return url.host, url.port, use_ssl, filename, path
+    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
+    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)
-       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
+    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 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
+    # 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 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)
+    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