Support for https
authorJames Bunton <jamesbunton@delx.net.au>
Sun, 19 Feb 2012 06:12:14 +0000 (17:12 +1100)
committerJames Bunton <jamesbunton@delx.net.au>
Sun, 19 Feb 2012 06:12:14 +0000 (17:12 +1100)
ruby/proxy.rb

index 11b14d2..10002f1 100755 (executable)
@@ -1,6 +1,7 @@
 #!/usr/bin/env ruby
 
 require 'net/http'
+require 'net/https'
 require 'uri'
 
 class NilClass
@@ -16,16 +17,18 @@ def getParams(url)
        end
 
        url = URI.parse(url);
-       if url.scheme != "http"
+       if !["http", "https"].include? url.scheme
                raise RuntimeError, "Unsupported scheme: #{url.scheme}"
        end
 
+       use_ssl = url.scheme == 'https'
+
        path = url.path
        if !ENV["QUERY_STRING"].empty?
                path += "?" + ENV["QUERY_STRING"]
        end
 
-       return url.host, url.port, path
+       return url.host, url.port, use_ssl, path
 end
 
 def createRequest(method, path)
@@ -58,11 +61,11 @@ def insertHeaders(req, ffHeader)
        req["Accept-Language"] = ENV["HTTP_ACCEPT_LANGUAGE"]
 end
 
-def doRequest(req, host, port)
+def doRequest(req, host, port, use_ssl)
        # Make the request
-       res = Net::HTTP.start(host, port) do |http|
-               http.request(req)
-       end
+       http = Net::HTTP.new(host, port)
+       http.use_ssl = use_ssl
+       res = http.request(req)
 
        # Tweak the headers a little
        res.delete("transfer-encoding")
@@ -92,10 +95,10 @@ def debug(msg)
 end
 
 def proxyTo(basePath, ffHeader=True)
-       host, port, path = getParams(basePath)
+       host, port, use_ssl, path = getParams(basePath)
        req = createRequest(ENV["REQUEST_METHOD"], path)
        insertHeaders(req, ffHeader)
-       res = doRequest(req, host, port)
+       res = doRequest(req, host, port, use_ssl)
        printResult(res)
 end