]>
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 add_header(req
, env_key
, header_key
)
38 req
[header_key
] = value
42 def create_request(method
, path
, ff_header
)
44 req
= Net
::HTTP::Get.new(path
)
45 elsif method
== "POST"
46 req
= Net
::HTTP::Post.new(path
)
47 req
.body
= $stdin.read()
49 raise RuntimeError
, "No support for method: #{method}"
53 add_header(req
, "REMOTE_ADDR", "X-Forwarded-For")
55 add_header(req
, "HTTP_HOST", "Host")
56 add_header(req
, "HTTP_COOKIE", "Cookie")
57 add_header(req
, "HTTP_REFERER", "Referer")
58 add_header(req
, "CONTENT_LENGTH", "Content-Length")
59 add_header(req
, "CONTENT_TYPE", "Content-Type")
60 add_header(req
, "HTTP_USER_AGENT", "User-Agent")
61 add_header(req
, "HTTP_CACHE_CONTROL", "Cache-Control")
62 add_header(req
, "HTTP_AUTHORIZATION", "Authorization")
63 add_header(req
, "HTTP_ACCEPT", "Accept")
64 add_header(req
, "HTTP_ACCEPT_CHARSET", "Accept-Charset")
65 add_header(req
, "HTTP_ACCEPT_ENCODING", "Accept-Encoding")
66 add_header(req
, "HTTP_ACCEPT_LANGUAGE", "Accept-Language")
71 def do_proxy(req
, host
, port
, use_ssl
, filename
, output_dir
)
73 http
= Net
::HTTP.new(host
, port
)
74 http
.use_ssl
= use_ssl
75 res
= http
.request req
do |res
|
78 res
["Status"] = "#{res.code} #{res.message}"
80 res
.each_capitalized_name
do |key
|
81 res
.get_fields(key
).each
do |value
|
82 $stdout.write
"#{key}: #{value}\r\n"
89 out
= File
.open("#{output_dir}/#{filename}", 'w')
91 res
.read_body
do |chunk
|
101 File
.open("/tmp/debuglog.txt", "a") { |f
|
102 f
<< Time
.new
.to_s
+ " " + msg
+ "\n"
106 def proxy_to(base_path
, ff_header
=True
, output_dir
=nil)
107 host
, port
, use_ssl
, filename
, path
= get_params(base_path
)
108 req
= create_request(ENV["REQUEST_METHOD"], path
, ff_header
)
109 do_proxy(req
, host
, port
, use_ssl
, filename
, output_dir
)