]>
code.delx.au - cgiproxy/blob - ruby/proxy.rb
8 # Make it easier to check for empty string or nil
15 if !ENV["PATH_INFO"].empty
?
16 url
+= ENV["PATH_INFO"]
20 if !["http", "https"].include? url
.scheme
21 raise RuntimeError
, "Unsupported scheme: #{url.scheme}"
24 use_ssl
= url
.scheme
== 'https'
26 filename
= url
.path
.split("/")[-1]
28 if !ENV["QUERY_STRING"].empty
?
29 path
+= "?" + ENV["QUERY_STRING"]
32 return url
.host
, url
.port
, use_ssl
, filename
, path
35 def createRequest(method
, path
, ffHeader
)
37 req
= Net
::HTTP::Get.new(path
)
38 elsif method
== "POST"
39 req
= Net
::HTTP::Post.new(path
)
40 req
.body
= $stdin.read()
42 raise RuntimeError
, "No support for method: #{method}"
46 req
["X-Forwarded-For"] = ENV["REMOTE_ADDR"]
48 req
["Host"] = ENV["HTTP_HOST"]
49 req
["Cookie"] = ENV["HTTP_COOKIE"]
50 req
["Referer"] = ENV["HTTP_REFERER"]
51 req
["Content-Length"] = ENV["CONTENT_LENGTH"]
52 req
["Content-Type"] = ENV["CONTENT_TYPE"]
53 req
["User-Agent"] = ENV["HTTP_USER_AGENT"]
54 req
["Cache-Control"] = ENV["HTTP_CACHE_CONTROL"]
55 req
["Authorization"] = ENV["HTTP_AUTHORIZATION"]
56 req
["Accept"] = ENV["HTTP_ACCEPT"]
57 req
["Accept-Charset"] = ENV["HTTP_ACCEPT_CHARSET"]
58 req
["Accept-Encoding"] = ENV["HTTP_ACCEPT_ENCODING"]
59 req
["Accept-Language"] = ENV["HTTP_ACCEPT_LANGUAGE"]
64 def doProxy(req
, host
, port
, use_ssl
, filename
, outputDir
)
66 http
= Net
::HTTP.new(host
, port
)
67 http
.use_ssl
= use_ssl
68 res
= http
.request req
do |res
|
70 # Tweak the headers a little
71 res
.delete("transfer-encoding")
72 res
.delete("transfer-length")
73 res
["connection"] = "close"
76 res
["Status"] = "#{res.code} #{res.message}"
78 res
.each_capitalized_name
do |key
|
79 res
.get_fields(key
).each
do |value
|
80 print
"#{key}: #{value}\r\n"
85 out
= File
.open("#{outputDir}/#{filename}", 'w')
86 res
.read_body
do |chunk
|
94 File
.open("/tmp/debuglog.txt", "a") { |f
|
95 f
<< Time
.new
.to_s
+ " " + msg
+ "\n"
99 def proxyTo(basePath
, ffHeader
=True
, outputDir
=None
)
100 host
, port
, use_ssl
, filename
, path
= getParams(basePath
)
101 req
= createRequest(ENV["REQUEST_METHOD"], path
, ffHeader
)
102 doProxy(req
, host
, port
, use_ssl
, filename
, outputDir
)