Class: Rakie::WebsocketMessage
Constant Summary
Rakie::WebsocketBasicMessage::FLAG_FIN, Rakie::WebsocketBasicMessage::FLAG_MASK, Rakie::WebsocketBasicMessage::OP_BIN, Rakie::WebsocketBasicMessage::OP_CLOSE, Rakie::WebsocketBasicMessage::OP_CONTINUE, Rakie::WebsocketBasicMessage::OP_PING, Rakie::WebsocketBasicMessage::OP_PONG, Rakie::WebsocketBasicMessage::OP_TEXT, Rakie::WebsocketBasicMessage::PARSE_EXT_LEN, Rakie::WebsocketBasicMessage::PARSE_LEN, Rakie::WebsocketBasicMessage::PARSE_MASKING, Rakie::WebsocketBasicMessage::PARSE_OPERATION, Rakie::WebsocketBasicMessage::PARSE_PAYLOAD
Constants included
from Proto
Proto::PARSE_BEGIN
Instance Attribute Summary
#fin, #length, #mask, #op_code, #payload
Instance Method Summary
collapse
#deserialize, #fin?, #initialize, #serialize
Methods included from Proto
#parse, #parse_offset, #parse_offset=, #parse_state, #parse_state=, #parse_status, #to_s
Instance Method Details
#pack_source_len ⇒ Object
250
251
252
253
254
255
256
257
258
259
260
261
|
# File 'lib/rakie/websocket_proto.rb', line 250
def pack_source_len
mask_bit = @mask ? 1 : 0
if @payload.length < 126
return [(mask_bit << 7) + @payload.length].pack('C')
elsif @payload.length < 65536
return [(mask_bit << 7) + 126, @payload.length].pack('CS>')
end
return [(mask_bit << 7) + 127, @payload.length].pack('CQ>')
end
|
#pack_source_masked_payload ⇒ Object
267
268
269
270
271
272
273
274
275
276
277
278
|
# File 'lib/rakie/websocket_proto.rb', line 267
def pack_source_masked_payload
masking = @masking
if masking.empty?
return ''
end
bytes_list = @payload.unpack('C*')
bytes_list_masked = bytes_list.map.with_index { |b, i| b ^ masking[i % 4] }
return bytes_list_masked.pack('C*')
end
|
#pack_source_masking ⇒ Object
263
264
265
|
# File 'lib/rakie/websocket_proto.rb', line 263
def pack_source_masking
return @masking.pack('C*')
end
|
#pack_source_operation ⇒ Object
245
246
247
248
|
# File 'lib/rakie/websocket_proto.rb', line 245
def pack_source_operation
fin_bit = @fin ? 1 : 0
return [(fin_bit << 7) + @op_code].pack('C')
end
|
#parse_source_ext_len(source) ⇒ Object
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
|
# File 'lib/rakie/websocket_proto.rb', line 162
def parse_source_ext_len(source)
offset = parse_offset
ext_len_size = 2
byte_format = 'S'
if @long_ext
ext_len_size = 8
byte_format = 'Q'
end
if source.length >= ext_len_size + offset
bytes = source[offset .. (offset + ext_len_size - 1)]
@length = bytes.unpack(byte_format + '>')[0]
if @mask
self.parse_state = PARSE_MASKING
self.parse_offset = offset + ext_len_size
Log.debug("Rakie::WebsocketMessage parse source ext len ok")
return ParseStatus::CONTINUE
end
self.parse_state = PARSE_PAYLOAD
self.parse_offset = offset + ext_len_size
Log.debug("Rakie::WebsocketMessage parse source ext len ok")
return ParseStatus::CONTINUE
end
return ParseStatus::PENDING
end
|
#parse_source_len(source) ⇒ Object
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
|
# File 'lib/rakie/websocket_proto.rb', line 114
def parse_source_len(source)
offset = parse_offset
if source.length >= 1 + offset
byte = source[offset]
data = byte.unpack('C')[0]
if data & FLAG_MASK > 0
@mask = true
end
len = data & ~FLAG_MASK
Log.debug("Rakie::WebsocketMessage len: #{len}")
if len <= 125
@length = len
if @mask
self.parse_state = PARSE_MASKING
self.parse_offset = offset + 1
Log.debug("Rakie::WebsocketMessage parse source len ok")
return ParseStatus::CONTINUE
end
self.parse_state = PARSE_PAYLOAD
self.parse_offset = offset + 1
Log.debug("Rakie::WebsocketMessage parse source len ok")
return ParseStatus::CONTINUE
end
if len == 127
@long_ext = true
end
self.parse_state = PARSE_EXT_LEN
self.parse_offset = offset + 1
Log.debug("Rakie::WebsocketMessage parse source len ok")
return ParseStatus::CONTINUE
end
return ParseStatus::PENDING
end
|
#parse_source_masking(source) ⇒ Object
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
|
# File 'lib/rakie/websocket_proto.rb', line 195
def parse_source_masking(source)
offset = parse_offset
if source.length >= 4 + offset
bytes = source[offset .. (offset + 4 - 1)]
@masking = bytes.unpack('C*')
self.parse_state = PARSE_PAYLOAD
self.parse_offset = offset + 4
Log.debug("Rakie::WebsocketMessage parse source masking ok")
return ParseStatus::CONTINUE
end
return ParseStatus::PENDING
end
|
#parse_source_operation(source) ⇒ Object
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
|
# File 'lib/rakie/websocket_proto.rb', line 82
def parse_source_operation(source)
offset = parse_offset
if source.length >= 1 + offset
byte = source[offset]
data = byte.unpack('C')[0]
if data & FLAG_FIN > 0
@fin = true
end
i = 3
code = 0x0
while i >= 0
code |= data & (1 << i)
i -= 1
end
@op_code = code
self.parse_state = PARSE_LEN
self.parse_offset = offset + 1
Log.debug("Rakie::WebsocketMessage parse source operation ok")
return ParseStatus::CONTINUE
end
return ParseStatus::PENDING
end
|
#parse_source_payload(source) ⇒ Object
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
|
# File 'lib/rakie/websocket_proto.rb', line 213
def parse_source_payload(source)
offset = parse_offset
if source.length >= @length + offset
bytes = source[offset .. (offset + @length - 1)]
if @mask
bytes_list = bytes.unpack('C*')
masking = @masking
bytes_list_unmasked = bytes_list.map.with_index { |b, i| b ^ masking[i % 4] }
@payload = bytes_list_unmasked.pack('C*')
self.parse_state = PARSE_OPERATION
self.parse_offset = offset + @length
Log.debug("Rakie::WebsocketMessage parse source masked payload ok")
return ParseStatus::COMPLETE
end
@payload = bytes
self.parse_state = PARSE_OPERATION
self.parse_offset = offset + @length
Log.debug("Rakie::WebsocketMessage parse source payload ok")
return ParseStatus::COMPLETE
end
return ParseStatus::PENDING
end
|
#refresh_masking ⇒ Object
280
281
282
283
284
|
# File 'lib/rakie/websocket_proto.rb', line 280
def refresh_masking
masking = []
4.times { masking << rand(1 .. 255) }
@masking = masking
end
|