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
|
# File 'lib/em-websocket/framing07.rb', line 13
def process_data
error = false
while !error && @data.size >= 2
pointer = 0
fin = (@data.getbyte(pointer) & 0b10000000) == 0b10000000
opcode = @data.getbyte(pointer) & 0b00001111
pointer += 1
mask = (@data.getbyte(pointer) & 0b10000000) == 0b10000000
length = @data.getbyte(pointer) & 0b01111111
pointer += 1
payload_length = case length
when 127 if @data.getbyte(pointer+8-1) == nil
debug [:buffer_incomplete, @data]
error = true
next
end
l = @data.getbytes(pointer+4, 4).unpack('N').first
pointer += 8
l
when 126 if @data.getbyte(pointer+2-1) == nil
debug [:buffer_incomplete, @data]
error = true
next
end
l = @data.getbytes(pointer, 2).unpack('n').first
pointer += 2
l
else
length
end
frame_length = pointer + payload_length
frame_length += 4 if mask
if frame_length > @connection.max_frame_size
raise WSMessageTooBigError, "Frame length too long (#{frame_length} bytes)"
end
if @data.getbyte(frame_length - 1) == nil
debug [:buffer_incomplete, @data]
error = true
next
end
@data.slice!(0...pointer)
pointer = 0
@data.read_mask if mask
pointer += 4 if mask
application_data = @data.getbytes(pointer, payload_length)
pointer += payload_length
@data.unset_mask if mask
@data.slice!(0...pointer)
frame_type = opcode_to_type(opcode)
if frame_type == :continuation
if !@frame_type
raise WSProtocolError, 'Continuation frame not expected'
end
else if @frame_type && data_frame?(frame_type)
raise WSProtocolError, "Continuation frame expected"
end
end
if !fin && !data_frame?(frame_type)
raise WSProtocolError, 'Control frames must not be fragmented'
end
if !fin
debug [:moreframe, frame_type, application_data]
@application_data_buffer << application_data
@frame_type ||= frame_type
else
if frame_type == :continuation
@application_data_buffer << application_data
message(@frame_type, '', @application_data_buffer)
@application_data_buffer = ''
@frame_type = nil
else
message(frame_type, '', application_data)
end
end
end end
|