]>
code.delx.au - cgiproxy/blob - ruby/proxy.rb
6f98be7af10d85084d0541698a46100483f4081b
7 # Make it easier to check for empty string or nil
14 if !ENV["PATH_INFO"].empty
?
15 url
+= ENV["PATH_INFO"]
19 if url
.scheme
!= "http"
20 raise RuntimeError
, "Unsupported scheme: #{url.scheme}"
24 if !ENV["QUERY_STRING"].empty
?
25 path
+= "?" + ENV["QUERY_STRING"]
28 return url
.host
, url
.port
, path
31 def createRequest(method
, path
)
33 req
= Net
::HTTP::Get.new(path
)
34 elsif method
== "POST"
35 req
= Net
::HTTP::Post.new(path
)
36 req
.body
= $stdin.read()
38 raise RuntimeError
, "No support for method: #{method}"
43 def insertHeaders(req
)
44 req
["X-Forwarded-For"] = ENV["REMOTE_ADDR"]
45 req
["Host"] = ENV["HTTP_HOST"]
46 req
["Cookie"] = ENV["HTTP_COOKIE"]
47 req
["Referer"] = ENV["HTTP_REFERER"]
48 req
["Content-Length"] = ENV["CONTENT_LENGTH"]
49 req
["Content-Type"] = ENV["CONTENT_TYPE"]
50 req
["User-Agent"] = ENV["HTTP_USER_AGENT"]
51 req
["Cache-Control"] = ENV["HTTP_CACHE_CONTROL"]
52 req
["Authorization"] = ENV["HTTP_AUTHORIZATION"]
53 req
["Accept"] = ENV["HTTP_ACCEPT"]
54 req
["Accept-Charset"] = ENV["HTTP_ACCEPT_CHARSET"]
55 req
["Accept-Encoding"] = ENV["HTTP_ACCEPT_ENCODING"]
56 req
["Accept-Language"] = ENV["HTTP_ACCEPT_LANGUAGE"]
59 def doRequest(req
, host
, port
)
61 res
= Net
::HTTP.start(host
, port
) do |http
|
65 # Tweak the headers a little
66 res
.delete("transfer-encoding")
67 res
.delete("transfer-length")
68 res
["connection"] = "close"
75 res
["Status"] = "#{res.code} #{res.message}"
77 res
.each_capitalized
do |key
, value
|
78 if key
== "Set-Cookie"
79 values
= value
.split(",")
83 values
.each
do |value
|
84 print
"#{key}: #{value}\r\n"
92 File
.open("/tmp/debuglog.txt", "a") { |f
|
93 f
<< Time
.new
.to_s
+ " " + msg
+ "\n"
98 host
, port
, path
= getParams(basePath
)
99 req
= createRequest(ENV["REQUEST_METHOD"], path
)
101 res
= doRequest(req
, host
, port
)