-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy_interactions.rb
154 lines (135 loc) · 4.66 KB
/
proxy_interactions.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
module ProxyInteractions
def build_params(env)
headers = env['client-headers'] #.merge("Host" => 'dev-api.dtime.com')
should_prefetch = env.params["prefetch"]
params = {:prefetch => should_prefetch, :head => headers, :query => env.params}
# Strip/add body params
case(env[Goliath::Request::REQUEST_METHOD])
when 'GET', 'OPTIONS', 'HEAD', 'DELETE'
params[:head].delete("Content-Length")
params.delete(:body)
when 'POST', 'PUT', 'PATCH'
params.merge!(:body => env[Goliath::Request::RACK_INPUT].read)
else p "UNKNOWN METHOD #{env[Goliath::Request::REQUEST_METHOD]}"
end
puts [ env[Goliath::Request::REQUEST_METHOD], env[Goliath::Request::REQUEST_PATH], headers, env.params ].inspect
params
end
def trigger_request(url, params)
req = EM::HttpRequest.new(url)
resp = case(env[Goliath::Request::REQUEST_METHOD])
when 'GET' then req.get(params)
when 'POST' then req.post(params)
when 'PUT' then req.put(params)
when 'HEAD' then req.head(params)
when 'OPTIONS' then req.options(params)
when 'PATCH' then req.patch(params)
when 'DELETE' then req.delete(params)
else p "UNKNOWN METHOD #{env[Goliath::Request::REQUEST_METHOD]}"
end
resp
end
def build_response(resp, opts = {})
# if opts[:prefetch]
response_headers = {}
content_type = :text
resp.response_header.each_pair do |k, v|
# Skip internal negotiation headers
next if k == "CONNECTION"
next if k == "TRANSFER_ENCODING"
next if k == "CONTENT_LENGTH"
if k == "CONTENT_TYPE"
content_type = case v
when /json/
:json
else
:text
end
end
response_headers[to_http_header(k)] = v
end
# record(process_time, resp, env['client-headers'], response_headers)
#
#
#
if content_type == :json
response_string = resp.response
_response = rewrite_response(response_string)
_response = Yajl::Parser.new.parse(_response)
_response = prefetch_with(_response, opts) if resp.response_header.status == 200
_response = Yajl::Encoder.new.encode(_response)
else
response_string = resp.response
_response = rewrite_response(response_string)
end
puts [resp.response_header.status, response_headers].inspect
[resp.response_header.status, response_headers, _response]
end
def prefetch_with(response, opts)
return response unless opts[:prefetch]
return response unless response.is_a?(Hash)
return response unless response["_links"].is_a?(Hash)
prefetching = []
response["_embedded"].each do |k,v|
if opts[:prefetch].include?(k)
if v.is_a?(Hash)
prefetching << [k, v["_links"]["self"]["href"]]
else
v.each do |v|
prefetching << [k, v["_links"]["self"]["href"]]
end
end
end
end
concurrency = ENV["FIBERS"].to_i || 2
prefetch_results = {}
start_time = Time.now.to_f
EM::Synchrony::FiberIterator.new(prefetching, concurrency).each do |(rel, url)|
puts ["Prefetching #{rel} => #{url}"]
resp = EventMachine::HttpRequest.new(url).get
response_string = resp.response
_response = rewrite_response(response_string)
resp_remapped = Yajl::Parser.new.parse(_response)
if prefetch_results[rel] && prefetch_results[rel].is_a?(Array)
prefetch_results[rel] << resp_remapped
elsif prefetch_results[rel]
prefetch_results[rel] = [prefetch_results[rel]]
prefetch_results[rel] << resp_remapped
else
prefetch_results[rel] = resp_remapped
end
end
prefetch_results.each do |k, v|
response["_embedded"][k] = v
end
process_time = Time.now.to_f - start_time
puts "Built embedded results in #{process_time}"
response
end
# Need to convert from the CONTENT_TYPE we'll get back from the server
# to the normal Content-Type header
def to_http_header(k)
k.downcase.split('_').collect { |e| e.capitalize }.join('-')
end
def rewrite_response(string)
proxy_to = ENV['PROXY_TARGET'] || 'http://localhost:9292'
proxy_from = ENV['PROXY_URL'] || 'http://localhost:9293'
string.gsub(proxy_to, proxy_from)
end
def remap_response(hash)
if hash.is_a?(Array)
hash = hash.map do |h|
remap_response(h)
end
end
if hash.respond_to?(:has_key?) && hash.has_key?("href") && hash.has_key?("uri")
proxy_host = ENV['PROXY_URL'] || 'http://localhost:9293'
hash["href"] = "#{proxy_host}#{hash["uri"]}"
elsif hash.respond_to?(:has_key?)
hash.each do |k,v|
hash[k] = remap_response(v)
end
end
hash
end
end