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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
# File 'lib/gripmiddleware.rb', line 18
def call(env)
env['grip_proxied'] = false
env['grip_wscontext'] = nil
grip_signed = false
grip_proxies = RailsSettings.get_grip_proxies
if env.key?('HTTP_GRIP_SIG') and !grip_proxies.nil?
grip_proxies.each do |entry|
if GripControl.validate_sig(env['HTTP_GRIP_SIG'], entry['key'])
grip_signed = true
break
end
end
end
content_type = nil
if env.key?('CONTENT_TYPE')
content_type = env['CONTENT_TYPE']
at = content_type.index(';')
if !at.nil?
content_type = content_type[0..at-1]
end
end
accept_types = nil
if env.key?('HTTP_ACCEPT')
accept_types = env['HTTP_ACCEPT']
tmp = accept_types.split(',')
accept_types = []
tmp.each do |s|
accept_types.push(s.strip)
end
end
wscontext = nil
if env['REQUEST_METHOD'] == 'POST' and ((content_type ==
'application/websocket-events') or (!accept_types.nil? and
accept_types.include?('application/websocket-events')))
cid = nil
if env.key?('HTTP_CONNECTION_ID')
cid = env['HTTP_CONNECTION_ID']
end
meta = {}
env.each do |k, v|
if k.start_with?('HTTP_META_')
meta[(k[10..-1])] = v
end
end
events = nil
begin
events = GripControl.decode_websocket_events(env["rack.input"].read)
rescue
return [ 400, {}, ["Error parsing WebSocket events.\n"]]
end
wscontext = WebSocketContext.new(cid, meta, events)
end
env['grip_proxied'] = grip_signed
env['grip_wscontext'] = wscontext
begin
status, , response = @app.call(env)
rescue NonWebSocketRequestError => e
return [400, {}, [e.message + "\n"]]
end
if !env['grip_wscontext'].nil? and status == 200
wscontext = env['grip_wscontext']
meta_remove = Set.new
wscontext.orig_meta.each do |k, v|
found = false
wscontext.meta.each do |nk, nv|
if nk.downcase == k
found = true
break
end
end
if !found
meta_remove.add(k)
end
end
meta_set = {}
wscontext.meta.each do |k, v|
lname = k.downcase
need_set = true
wscontext.orig_meta.each do |ok, ov|
if lname == ok and v == ov
need_set = false
break
end
end
if need_set
meta_set[lname] = v
end
end
events = []
if wscontext.accepted
events.push(WebSocketEvent.new('OPEN'))
end
events.push(*wscontext.out_events)
if wscontext.closed
events.push(WebSocketEvent.new('CLOSE',
[wscontext.out_close_code].pack('S>')))
end
if response.respond_to?(:content_type)
response.body = GripControl.encode_websocket_events(events)
response.content_type = 'application/websocket-events'
else
response = [GripControl.encode_websocket_events(events)]
end
['Content-Type'] = 'application/websocket-events'
if wscontext.accepted
['Sec-WebSocket-Extensions'] = 'grip'
end
meta_remove.each do |k, v|
['Set-Meta-' + k] = ''
end
meta_set.each do |k, v|
['Set-Meta-' + k] = v
end
elsif !env['grip_hold'].nil?
if !env['grip_proxied'] and RailsSettings.get_grip_proxy_required
return [ 501, {}, ["Not implemented.\n"]]
end
channels = env['grip_channels']
prefix = RailsSettings.get_prefix
if prefix != ''
channels.each do |channel|
channel.name = prefix + channel.name
end
end
if status == 304
= .clone
if !.key?('Location') and response.respond_to?(:location) and
!response.location.nil?
['Location'] = response.location
end
if response.respond_to?(:body)
orig_body = response.body
else
orig_body = response.to_s
end
iresponse = Response.new(status, nil, , orig_body)
timeout = nil
if !env['grip_timeout'].nil?
timeout = env['grip_timeout']
end
if response.respond_to?(:content_type)
response.body = GripControl.create_hold(env['grip_hold'],
channels, iresponse, timeout)
response.content_type = 'application/grip-instruct'
else
response = [GripControl.create_hold(env['grip_hold'],
channels, iresponse, timeout)]
end
= {'Content-Type' => 'application/grip-instruct'}
else
['Grip-Hold'] = env['grip_hold']
['Grip-Channel'] = GripControl.(
channels)
if !env['grip_timeout'].nil?
['Grip-Timeout'] = env['grip_timeout'].to_s
end
end
end
return [status, , response]
end
|