Class: Protocol::WebSocket::Frame

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/protocol/websocket/frame.rb

Constant Summary collapse

OPCODE =
0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(finished = true, payload = nil, opcode: self.class::OPCODE, mask: false) ⇒ Frame

Returns a new instance of Frame.

Parameters:

  • length (Integer)

    the length of the payload, or nil if the header has not been read yet.



31
32
33
34
35
36
37
# File 'lib/protocol/websocket/frame.rb', line 31

def initialize(finished = true, payload = nil, opcode: self.class::OPCODE, mask: false)
	@finished = finished
	@opcode = opcode
	@mask = mask
	@length = payload&.bytesize
	@payload = payload
end

Instance Attribute Details

#finishedObject

The generic frame header uses the following binary representation:

0                   1                   2                   3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1

-------------------------——————————-+ |F|R|R|R| opcode|M| Payload len | Extended payload length | |I|S|S|S| (4) |A| (7) | (16/64) | |N|V|V|V| |S| | (if payload len==126/127) | | |1|2|3| |K| | | ------------------------- - - - - - - - - - - - - - - - + | Extended payload length continued, if payload len == 127 | + - - - - - - - - - - - - - - - ------------------------------- | |Masking-key, if MASK set to 1 | -------------------------------——————————-+ | Masking-key (continued) | Payload Data | ——————————– - - - - - - - - - - - - - - - : Payload Data continued … : + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + | Payload Data continued … | ---------------------------------------------------------------



84
85
86
# File 'lib/protocol/websocket/frame.rb', line 84

def finished
  @finished
end

#lengthObject

Returns the value of attribute length.



87
88
89
# File 'lib/protocol/websocket/frame.rb', line 87

def length
  @length
end

#maskObject

Returns the value of attribute mask.



86
87
88
# File 'lib/protocol/websocket/frame.rb', line 86

def mask
  @mask
end

#opcodeObject

Returns the value of attribute opcode.



85
86
87
# File 'lib/protocol/websocket/frame.rb', line 85

def opcode
  @opcode
end

#payloadObject

Returns the value of attribute payload.



88
89
90
# File 'lib/protocol/websocket/frame.rb', line 88

def payload
  @payload
end

Class Method Details

.parse_header(buffer) ⇒ Object



129
130
131
132
133
134
135
136
137
# File 'lib/protocol/websocket/frame.rb', line 129

def self.parse_header(buffer)
	byte = buffer.unpack("C").first
	
	finished = (byte & 0b1000_0000 != 0)
	# rsv = byte & 0b0111_0000
	opcode = byte & 0b0000_1111
	
	return finished, opcode
end

.read(finished, opcode, stream, maximum_frame_size) ⇒ Object



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
# File 'lib/protocol/websocket/frame.rb', line 139

def self.read(finished, opcode, stream, maximum_frame_size)
	buffer = stream.read(1) or raise EOFError, "Could not read header!"
	byte = buffer.unpack("C").first
	
	mask = (byte & 0b1000_0000 != 0)
	length = byte & 0b0111_1111
	
	if length == 126
		buffer = stream.read(2) or raise EOFError, "Could not read length!"
		length = buffer.unpack('n').first
	elsif length == 127
		buffer = stream.read(4) or raise EOFError, "Could not read length!"
		length = buffer.unpack('Q>').first
	end
	
	if length > maximum_frame_size
		raise ProtocolError, "Invalid payload length: #{@length} > #{maximum_frame_size}!"
	end
	
	if mask
		mask = stream.read(4) or raise EOFError, "Could not read mask!"
	end
	
	payload = stream.read(length) or raise EOFError, "Could not read payload!"
	
	if payload.bytesize != length
		raise EOFError, "Incorrect payload length: #{@length} != #{@payload.bytesize}!"
	end
	
	return self.new(finished, payload, opcode: opcode, mask: mask)
end

Instance Method Details

#<=>(other) ⇒ Object



39
40
41
# File 'lib/protocol/websocket/frame.rb', line 39

def <=> other
	to_ary <=> other.to_ary
end

#apply(connection) ⇒ Object



125
126
127
# File 'lib/protocol/websocket/frame.rb', line 125

def apply(connection)
	connection.receive_frame(self)
end

#continued?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/protocol/websocket/frame.rb', line 59

def continued?
	@finished == false
end

#control?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/protocol/websocket/frame.rb', line 47

def control?
	@opcode & 0x8
end

#data?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/protocol/websocket/frame.rb', line 51

def data?
	false
end

#finished?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/protocol/websocket/frame.rb', line 55

def finished?
	@finished == true
end

#pack(data) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/protocol/websocket/frame.rb', line 90

def pack(data)
	length = data.bytesize
	
	if length.bit_length > 63
		raise ProtocolError, "Frame length #{@length} bigger than allowed maximum!"
	end

	if @mask
		@payload = String.new.b
		
		for i in 0...data.bytesize do
			@payload << (data.getbyte(i) ^ mask.getbyte(i % 4))
		end
		
		@length = length
	else
		@payload = data
		@length = length
	end
end

#to_aryObject



43
44
45
# File 'lib/protocol/websocket/frame.rb', line 43

def to_ary
	[@finished, @opcode, @mask, @length, @payload]
end

#unpackObject



111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/protocol/websocket/frame.rb', line 111

def unpack
	if @mask
		data = String.new.b
		
		for i in 0...@payload.bytesize do
			data << (@payload.getbyte(i) ^ @mask.getbyte(i % 4))
		end
		
		return data
	else
		return @payload
	end
end

#write(stream) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/protocol/websocket/frame.rb', line 171

def write(stream)
	buffer = String.new.b
	
	if @payload&.bytesize != @length
		raise ProtocolError, "Invalid payload length: #{@length} != #{@payload.bytesize} for #{self}!"
	end
	
	if @mask and @mask.bytesize != 4
		raise ProtocolError, "Invalid mask length!"
	end
	
	if length <= 125
		short_length = length
	elsif length.bit_length <= 16
		short_length = 126
	else
		short_length = 127
	end
	
	buffer << [
		(@finished ? 0b1000_0000 : 0) | @opcode,
		(@mask ? 0b1000_0000 : 0) | short_length,
	].pack('CC')
	
	if short_length == 126
		buffer << [@length].pack('n')
	elsif short_length == 127
		buffer << [@length].pack('Q>')
	end
	
	buffer << @mask if @mask
	
	stream.write(buffer)
	stream.write(@payload)
end