forked from lifo/cramp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rainbows.rb
42 lines (38 loc) · 1020 Bytes
/
rainbows.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
class Cramp::Websocket::Rainbows < Rainbows::EventMachine::Client
include Cramp::WebsocketExtension
def receive_data(data)
case @state
when :websocket
callback = @env[WEBSOCKET_RECEIVE_CALLBACK]
callback.call(data) if callback
else
super
end
end
def on_read(data)
if @state == :headers
@hp.add_parse(data) or return want_more
@state = :body
if 0 == @hp.content_length && !websocket?
app_call NULL_IO # common case
else # nil or len > 0
prepare_request_body
end
elsif @state == :body && websocket? && @hp.body_eof?
@state = :websocket
@input.rewind
write(protocol_class.new(@env, websocket_url, @buf).handshake)
app_call NULL_IO
else
super
end
rescue => e
handle_error(e)
end
def write_response(status, headers, body, alive)
write_headers(status, headers, alive) unless websocket?
write_body_each(body)
ensure
body.close if body.respond_to?(:close)
end
end