32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
# File 'lib/sproutcore/rack/proxy.rb', line 32
def handle_proxy(proxy, proxy_url, env)
origin_host = env['SERVER_NAME']
http_method = env['REQUEST_METHOD'].to_s.downcase
url = env['PATH_INFO']
params = env['QUERY_STRING']
= {}
env.each do |key, value|
next unless key =~ /^HTTP_/
key = key.gsub(/^HTTP_/,'').downcase.sub(/^\w/){|l| l.upcase}.gsub(/_(\w)/){|l| "-#{$1.upcase}"}
[key] = value
end
['Content-Type'] = env['CONTENT_TYPE'] if env['CONTENT_TYPE']
['Content-Length'] = env['CONTENT_LENGTH'] if env['CONTENT_LENGTH']
http_host, http_port = proxy[:to].split(':')
http_port = '80' if http_port.nil?
['Host'] = "#{http_host}:#{http_port}"
if proxy[:url]
url = url.sub(/^#{Regexp.escape proxy_url}/, proxy[:url])
end
http_path = [url]
http_path << params if params && params.size>0
http_path = http_path.join('?')
response = nil
no_body_method = %w(delete get copy head move options trace)
::Net::HTTP.start(http_host, http_port) do |http|
if no_body_method.include?(http_method)
response = http.send(http_method, http_path, )
else
http_body = env['rack.input'].gets || ''
response = http.send(http_method, http_path, http_body, )
end
end
status = response.code
SC.logger << "~ PROXY: #{http_method.upcase} #{status} #{url} -> http://#{http_host}:#{http_port}#{http_path}\n"
= {}
= ['transfer-encoding', 'keep-alive', 'connection']
response.each do |key, value|
next if .include?(key.downcase)
value.gsub!(/domain=[^\;]+\;? ?/,'') if key.downcase == 'set-cookie'
value.gsub!(/^http:\/\/#{http_host}(:[0-9]+)?\//, "http://#{http_host}/") if key.downcase == 'location'
SC.logger << " #{key}: #{value}\n"
[key] = value
end
return [status, ::Rack::Utils::.new(), [response.body]]
end
|