Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy_interactions.rb
More file actions
Latest commit
154 lines (135 loc) · 4.66 KB
/
Copy pathproxy_interactions.rb
File metadata and controls
154 lines (135 loc) · 4.66 KB
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
moduleProxyInteractions
defbuild_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)
elsep"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
deftrigger_request(url,params)
req=EM::HttpRequest.new(url)
resp=case(env[Goliath::Request::REQUEST_METHOD])
when'GET'thenreq.get(params)
when'POST'thenreq.post(params)
when'PUT'thenreq.put(params)
when'HEAD'thenreq.head(params)
when'OPTIONS'thenreq.options(params)
when'PATCH'thenreq.patch(params)
when'DELETE'thenreq.delete(params)
elsep"UNKNOWN METHOD #{env[Goliath::Request::REQUEST_METHOD]}"
end
resp
end
defbuild_response(resp,opts={})
# if opts[:prefetch]
response_headers={}
content_type=:text
resp.response_header.each_pairdo |k,v|
# Skip internal negotiation headers
nextifk == "CONNECTION"
nextifk == "TRANSFER_ENCODING"
nextifk == "CONTENT_LENGTH"
ifk == "CONTENT_TYPE"
content_type=casev
when/json/
:json
else
:text
end
end
response_headers[to_http_header(k)]=v
end
# record(process_time, resp, env['client-headers'], response_headers)
#
#
#
ifcontent_type == :json
response_string=resp.response
_response=rewrite_response(response_string)
_response=Yajl::Parser.new.parse(_response)
_response=prefetch_with(_response,opts)ifresp.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
defprefetch_with(response,opts)
returnresponseunlessopts[:prefetch]
returnresponseunlessresponse.is_a?(Hash)
returnresponseunlessresponse["_links"].is_a?(Hash)
prefetching=[]
response["_embedded"].eachdo |k,v|
ifopts[:prefetch].include?(k)
ifv.is_a?(Hash)
prefetching << [k,v["_links"]["self"]["href"]]
else
v.eachdo |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).eachdo |(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)
ifprefetch_results[rel] && prefetch_results[rel].is_a?(Array)
prefetch_results[rel] << resp_remapped
elsifprefetch_results[rel]
prefetch_results[rel]=[prefetch_results[rel]]
prefetch_results[rel] << resp_remapped
else
prefetch_results[rel]=resp_remapped
end
end
prefetch_results.eachdo |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
defto_http_header(k)
k.downcase.split('_').collect{ |e| e.capitalize}.join('-')
end
defrewrite_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
defremap_response(hash)
ifhash.is_a?(Array)
hash=hash.mapdo |h|
remap_response(h)
end
end
ifhash.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"]}"
elsifhash.respond_to?(:has_key?)
hash.eachdo |k,v|
hash[k]=remap_response(v)
end
end
hash
end
end