Class: Faye::WebSocket::HybiParser
- Inherits:
-
Object
- Object
- Faye::WebSocket::HybiParser
show all
- Defined in:
- lib/faye/websocket/hybi_parser.rb,
lib/faye/websocket/hybi_parser/handshake.rb,
lib/faye/websocket/hybi_parser/stream_reader.rb
Defined Under Namespace
Classes: Handshake, StreamReader
Constant Summary
collapse
- BYTE =
0b11111111
- FIN =
MASK = 0b10000000
- RSV1 =
0b01000000
- RSV2 =
0b00100000
- RSV3 =
0b00010000
- OPCODE =
0b00001111
- LENGTH =
0b01111111
- OPCODES =
{
:continuation => 0,
:text => 1,
:binary => 2,
:close => 8,
:ping => 9,
:pong => 10
}
- FRAGMENTED_OPCODES =
OPCODES.values_at(:continuation, :text, :binary)
- OPENING_OPCODES =
OPCODES.values_at(:text, :binary)
- ERRORS =
{
:normal_closure => 1000,
:going_away => 1001,
:protocol_error => 1002,
:unacceptable => 1003,
:encoding_error => 1007,
:policy_violation => 1008,
:too_large => 1009,
:extension_error => 1010,
:unexpected_condition => 1011
}
- ERROR_CODES =
ERRORS.values
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(web_socket, options = {}) ⇒ HybiParser
Returns a new instance of HybiParser.
45
46
47
48
49
50
51
52
53
54
55
|
# File 'lib/faye/websocket/hybi_parser.rb', line 45
def initialize(web_socket, options = {})
reset
@socket = web_socket
@reader = StreamReader.new
@stage = 0
@masking = options[:masking]
@protocols = options[:protocols]
@protocols = @protocols.split(/\s*,\s*/) if String === @protocols
@ping_callbacks = {}
end
|
Instance Attribute Details
#protocol ⇒ Object
Returns the value of attribute protocol.
43
44
45
|
# File 'lib/faye/websocket/hybi_parser.rb', line 43
def protocol
@protocol
end
|
Instance Method Details
#close(code = nil, reason = nil, &callback) ⇒ Object
187
188
189
190
191
192
|
# File 'lib/faye/websocket/hybi_parser.rb', line 187
def close(code = nil, reason = nil, &callback)
return if @closed
@closing_callback ||= callback
@socket.send(reason || '', :close, code || ERRORS[:normal_closure])
@closed = true
end
|
#create_handshake ⇒ Object
89
90
91
|
# File 'lib/faye/websocket/hybi_parser.rb', line 89
def create_handshake
Handshake.new(@socket.uri, @protocols)
end
|
#frame(data, type = nil, code = nil) ⇒ Object
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
178
179
180
|
# File 'lib/faye/websocket/hybi_parser.rb', line 134
def frame(data, type = nil, code = nil)
return nil if @closed
is_text = (String === data)
opcode = OPCODES[type || (is_text ? :text : :binary)]
buffer = data.respond_to?(:bytes) ? data.bytes.to_a : data
insert = code ? 2 : 0
length = buffer.size + insert
= (length <= 125) ? 2 : (length <= 65535 ? 4 : 10)
offset = + (@masking ? 4 : 0)
masked = @masking ? MASK : 0
frame = Array.new(offset)
frame[0] = FIN | opcode
if length <= 125
frame[1] = masked | length
elsif length <= 65535
frame[1] = masked | 126
frame[2] = (length >> 8) & BYTE
frame[3] = length & BYTE
else
frame[1] = masked | 127
frame[2] = (length >> 56) & BYTE
frame[3] = (length >> 48) & BYTE
frame[4] = (length >> 40) & BYTE
frame[5] = (length >> 32) & BYTE
frame[6] = (length >> 24) & BYTE
frame[7] = (length >> 16) & BYTE
frame[8] = (length >> 8) & BYTE
frame[9] = length & BYTE
end
if code
buffer = [(code >> 8) & BYTE, code & BYTE] + buffer
end
if @masking
mask = [rand(256), rand(256), rand(256), rand(256)]
frame[...offset] = mask
buffer = WebSocketMask.mask(buffer, mask)
end
frame.concat(buffer)
WebSocket.encode(frame)
end
|
#handshake_response ⇒ Object
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
|
# File 'lib/faye/websocket/hybi_parser.rb', line 61
def handshake_response
sec_key = @socket.env['HTTP_SEC_WEBSOCKET_KEY']
return '' unless String === sec_key
accept = Base64.encode64(Digest::SHA1.digest(sec_key + Handshake::GUID)).strip
protos = @socket.env['HTTP_SEC_WEBSOCKET_PROTOCOL']
supported = @protocols
proto = nil
= [
"HTTP/1.1 101 Switching Protocols",
"Upgrade: websocket",
"Connection: Upgrade",
"Sec-WebSocket-Accept: #{accept}"
]
if protos and supported
protos = protos.split(/\s*,\s*/) if String === protos
proto = protos.find { |p| supported.include?(p) }
if proto
@protocol = proto
<< "Sec-WebSocket-Protocol: #{proto}"
end
end
( + ['','']).join("\r\n")
end
|
#open? ⇒ Boolean
93
94
95
|
# File 'lib/faye/websocket/hybi_parser.rb', line 93
def open?
true
end
|
#parse(data) ⇒ Object
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
|
# File 'lib/faye/websocket/hybi_parser.rb', line 97
def parse(data)
@reader.put(data.bytes.to_a)
buffer = true
while buffer
case @stage
when 0 then
buffer = @reader.read(1)
parse_opcode(buffer[0]) if buffer
when 1 then
buffer = @reader.read(1)
parse_length(buffer[0]) if buffer
when 2 then
buffer = @reader.read(@length_size)
parse_extended_length(buffer) if buffer
when 3 then
buffer = @reader.read(4)
if buffer
@mask = buffer
@stage = 4
end
when 4 then
buffer = @reader.read(@length)
if buffer
@payload = buffer
emit_frame
@stage = 0
end
end
end
nil
end
|
#ping(message = '', &callback) ⇒ Object
182
183
184
185
|
# File 'lib/faye/websocket/hybi_parser.rb', line 182
def ping(message = '', &callback)
@ping_callbacks[message] = callback if callback
@socket.send(message, :ping)
end
|
#version ⇒ Object
57
58
59
|
# File 'lib/faye/websocket/hybi_parser.rb', line 57
def version
"hybi-#{@socket.env['HTTP_SEC_WEBSOCKET_VERSION']}"
end
|