2020-08-22 00:50:38 +02:00
|
|
|
module websocket
|
|
|
|
|
|
|
|
import encoding.utf8
|
|
|
|
|
|
|
|
const (
|
2020-12-04 01:52:26 +01:00
|
|
|
header_len_offset = 2 // offset for lengthpart of websocket header
|
|
|
|
buffer_size = 256 // default buffer size
|
|
|
|
extended_payload16_end_byte = 4 // header length with 16-bit extended payload
|
|
|
|
extended_payload64_end_byte = 10 // header length with 64-bit extended payload
|
2020-08-22 00:50:38 +02:00
|
|
|
)
|
|
|
|
|
2020-12-04 01:52:26 +01:00
|
|
|
// Fragment represents a websocket data fragment
|
2020-08-22 00:50:38 +02:00
|
|
|
struct Fragment {
|
2020-12-04 01:52:26 +01:00
|
|
|
data []byte // included data payload data in a fragment
|
|
|
|
opcode OPCode // interpretation of the payload data
|
2020-08-22 00:50:38 +02:00
|
|
|
}
|
|
|
|
|
2020-12-04 01:52:26 +01:00
|
|
|
// Frame represents a data frame header
|
2020-08-22 00:50:38 +02:00
|
|
|
struct Frame {
|
|
|
|
mut:
|
2020-12-28 06:25:46 +01:00
|
|
|
// length of the websocket header part
|
2021-01-12 04:38:43 +01:00
|
|
|
header_len int = 2
|
2020-12-28 06:25:46 +01:00
|
|
|
// size of total frame
|
|
|
|
frame_size int = 2
|
2021-01-03 21:10:25 +01:00
|
|
|
fin bool // true if final fragment of message
|
|
|
|
rsv1 bool // reserved for future use in websocket RFC
|
|
|
|
rsv2 bool // reserved for future use in websocket RFC
|
|
|
|
rsv3 bool // reserved for future use in websocket RFC
|
|
|
|
opcode OPCode // interpretation of the payload data
|
|
|
|
has_mask bool // true if the payload data is masked
|
|
|
|
payload_len int // payload length
|
2020-12-04 01:52:26 +01:00
|
|
|
masking_key [4]byte // all frames from client to server is masked with this key
|
2020-08-22 00:50:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
2020-12-28 06:25:46 +01:00
|
|
|
invalid_close_codes = [999, 1004, 1005, 1006, 1014, 1015, 1016, 1100, 2000, 2999, 5000, 65536]
|
2020-08-22 00:50:38 +02:00
|
|
|
)
|
|
|
|
|
2020-12-04 01:52:26 +01:00
|
|
|
// validate_client validates client frame rules from RFC6455
|
2020-08-22 00:50:38 +02:00
|
|
|
pub fn (mut ws Client) validate_frame(frame &Frame) ? {
|
|
|
|
if frame.rsv1 || frame.rsv2 || frame.rsv3 {
|
|
|
|
ws.close(1002, 'rsv cannot be other than 0, not negotiated')
|
|
|
|
return error('rsv cannot be other than 0, not negotiated')
|
|
|
|
}
|
2021-01-23 09:33:22 +01:00
|
|
|
if (int(frame.opcode) >= 3 && int(frame.opcode) <= 7)
|
|
|
|
|| (int(frame.opcode) >= 11 && int(frame.opcode) <= 15) {
|
2020-12-02 04:02:53 +01:00
|
|
|
ws.close(1002, 'use of reserved opcode') ?
|
2020-08-22 00:50:38 +02:00
|
|
|
return error('use of reserved opcode')
|
|
|
|
}
|
|
|
|
if frame.has_mask && !ws.is_server {
|
2020-12-04 01:52:26 +01:00
|
|
|
// server should never send masked frames
|
2020-08-22 00:50:38 +02:00
|
|
|
// to client, close connection
|
2020-12-02 04:02:53 +01:00
|
|
|
ws.close(1002, 'client got masked frame') ?
|
2020-08-22 00:50:38 +02:00
|
|
|
return error('client sent masked frame')
|
|
|
|
}
|
|
|
|
if is_control_frame(frame.opcode) {
|
|
|
|
if !frame.fin {
|
2020-12-02 04:02:53 +01:00
|
|
|
ws.close(1002, 'control message must not be fragmented') ?
|
2020-08-22 00:50:38 +02:00
|
|
|
return error('unexpected control frame with no fin')
|
|
|
|
}
|
|
|
|
if frame.payload_len > 125 {
|
2020-12-02 04:02:53 +01:00
|
|
|
ws.close(1002, 'control frames must not exceed 125 bytes') ?
|
2020-08-22 00:50:38 +02:00
|
|
|
return error('unexpected control frame payload length')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if frame.fin == false && ws.fragments.len == 0 && frame.opcode == .continuation {
|
|
|
|
err_msg := 'unexecpected continuation, there are no frames to continue, $frame'
|
2020-12-02 04:02:53 +01:00
|
|
|
ws.close(1002, err_msg) ?
|
2020-08-22 00:50:38 +02:00
|
|
|
return error(err_msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-04 01:52:26 +01:00
|
|
|
// is_control_frame returns true if the frame is a control frame
|
2020-08-22 00:50:38 +02:00
|
|
|
fn is_control_frame(opcode OPCode) bool {
|
|
|
|
return opcode !in [.text_frame, .binary_frame, .continuation]
|
|
|
|
}
|
|
|
|
|
2020-12-04 01:52:26 +01:00
|
|
|
// is_data_frame returns true if the frame is a control frame
|
2020-08-22 00:50:38 +02:00
|
|
|
fn is_data_frame(opcode OPCode) bool {
|
|
|
|
return opcode in [.text_frame, .binary_frame]
|
|
|
|
}
|
|
|
|
|
2020-12-04 01:52:26 +01:00
|
|
|
// read_payload reads the message payload from the socket
|
2020-08-22 00:50:38 +02:00
|
|
|
fn (mut ws Client) read_payload(frame &Frame) ?[]byte {
|
|
|
|
if frame.payload_len == 0 {
|
|
|
|
return []byte{}
|
|
|
|
}
|
|
|
|
mut buffer := []byte{cap: frame.payload_len}
|
|
|
|
mut read_buf := [1]byte{}
|
|
|
|
mut bytes_read := 0
|
|
|
|
for bytes_read < frame.payload_len {
|
2020-12-02 04:02:53 +01:00
|
|
|
len := ws.socket_read_ptr(byteptr(&read_buf), 1) ?
|
2020-08-22 00:50:38 +02:00
|
|
|
if len != 1 {
|
|
|
|
return error('expected read all message, got zero')
|
|
|
|
}
|
|
|
|
bytes_read += len
|
|
|
|
buffer << read_buf[0]
|
|
|
|
}
|
|
|
|
if bytes_read != frame.payload_len {
|
|
|
|
return error('failed to read payload')
|
|
|
|
}
|
|
|
|
if frame.has_mask {
|
|
|
|
for i in 0 .. frame.payload_len {
|
|
|
|
buffer[i] ^= frame.masking_key[i % 4] & 0xff
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return buffer
|
|
|
|
}
|
|
|
|
|
2020-12-04 01:52:26 +01:00
|
|
|
// validate_utf_8 validates payload for valid utf8 encoding
|
2020-12-02 04:02:53 +01:00
|
|
|
// - Future implementation needs to support fail fast utf errors for strict autobahn conformance
|
2020-08-22 00:50:38 +02:00
|
|
|
fn (mut ws Client) validate_utf_8(opcode OPCode, payload []byte) ? {
|
|
|
|
if opcode in [.text_frame, .close] && !utf8.validate(payload.data, payload.len) {
|
|
|
|
ws.logger.error('malformed utf8 payload, payload len: ($payload.len)')
|
|
|
|
ws.send_error_event('Recieved malformed utf8.')
|
|
|
|
ws.close(1007, 'malformed utf8 payload')
|
|
|
|
return error('malformed utf8 payload')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// read_next_message reads 1 to n frames to compose a message
|
|
|
|
pub fn (mut ws Client) read_next_message() ?Message {
|
|
|
|
for {
|
2020-12-02 04:02:53 +01:00
|
|
|
frame := ws.parse_frame_header() ?
|
|
|
|
ws.validate_frame(&frame) ?
|
|
|
|
frame_payload := ws.read_payload(&frame) ?
|
2020-08-22 00:50:38 +02:00
|
|
|
if is_control_frame(frame.opcode) {
|
|
|
|
// Control frames can interject other frames
|
|
|
|
// and need to be returned immediately
|
|
|
|
msg := Message{
|
|
|
|
opcode: OPCode(frame.opcode)
|
|
|
|
payload: frame_payload.clone()
|
|
|
|
}
|
2020-12-28 06:25:46 +01:00
|
|
|
unsafe { frame_payload.free() }
|
2020-08-22 00:50:38 +02:00
|
|
|
return msg
|
|
|
|
}
|
2020-12-04 01:52:26 +01:00
|
|
|
// if the message is fragmented we just put it on fragments
|
2020-08-22 00:50:38 +02:00
|
|
|
// a fragment is allowed to have zero size payload
|
|
|
|
if !frame.fin {
|
|
|
|
ws.fragments << &Fragment{
|
|
|
|
data: frame_payload.clone()
|
|
|
|
opcode: frame.opcode
|
|
|
|
}
|
2020-12-28 06:25:46 +01:00
|
|
|
unsafe { frame_payload.free() }
|
2020-08-22 00:50:38 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if ws.fragments.len == 0 {
|
|
|
|
ws.validate_utf_8(frame.opcode, frame_payload) or {
|
|
|
|
ws.logger.error('UTF8 validation error: $err, len of payload($frame_payload.len)')
|
|
|
|
ws.send_error_event('UTF8 validation error: $err, len of payload($frame_payload.len)')
|
|
|
|
return error(err)
|
|
|
|
}
|
|
|
|
msg := Message{
|
|
|
|
opcode: OPCode(frame.opcode)
|
|
|
|
payload: frame_payload.clone()
|
|
|
|
}
|
2020-12-28 06:25:46 +01:00
|
|
|
unsafe { frame_payload.free() }
|
2020-08-22 00:50:38 +02:00
|
|
|
return msg
|
|
|
|
}
|
|
|
|
defer {
|
|
|
|
ws.fragments = []
|
|
|
|
}
|
|
|
|
if is_data_frame(frame.opcode) {
|
2020-12-02 04:02:53 +01:00
|
|
|
ws.close(0, '') ?
|
2020-08-22 00:50:38 +02:00
|
|
|
return error('Unexpected frame opcode')
|
|
|
|
}
|
2020-12-02 04:02:53 +01:00
|
|
|
payload := ws.payload_from_fragments(frame_payload) ?
|
2020-08-22 00:50:38 +02:00
|
|
|
opcode := ws.opcode_from_fragments()
|
2020-12-02 04:02:53 +01:00
|
|
|
ws.validate_utf_8(opcode, payload) ?
|
2020-08-22 00:50:38 +02:00
|
|
|
msg := Message{
|
|
|
|
opcode: opcode
|
|
|
|
payload: payload.clone()
|
|
|
|
}
|
|
|
|
unsafe {
|
|
|
|
frame_payload.free()
|
|
|
|
payload.free()
|
|
|
|
}
|
|
|
|
return msg
|
|
|
|
}
|
2021-01-12 11:43:55 +01:00
|
|
|
return none
|
2020-08-22 00:50:38 +02:00
|
|
|
}
|
|
|
|
|
2020-12-04 01:52:26 +01:00
|
|
|
// payload_from_fragments returs the whole paylaod from fragmented message
|
2020-08-22 00:50:38 +02:00
|
|
|
fn (ws Client) payload_from_fragments(fin_payload []byte) ?[]byte {
|
|
|
|
mut total_size := 0
|
|
|
|
for f in ws.fragments {
|
|
|
|
if f.data.len > 0 {
|
|
|
|
total_size += f.data.len
|
|
|
|
}
|
|
|
|
}
|
|
|
|
total_size += fin_payload.len
|
|
|
|
if total_size == 0 {
|
|
|
|
return []byte{}
|
|
|
|
}
|
|
|
|
mut total_buffer := []byte{cap: total_size}
|
|
|
|
for f in ws.fragments {
|
|
|
|
if f.data.len > 0 {
|
|
|
|
total_buffer << f.data
|
|
|
|
}
|
|
|
|
}
|
|
|
|
total_buffer << fin_payload
|
|
|
|
return total_buffer
|
|
|
|
}
|
|
|
|
|
2020-12-02 04:02:53 +01:00
|
|
|
// opcode_from_fragments returns the opcode for message from the first fragment sent
|
2020-08-22 00:50:38 +02:00
|
|
|
fn (ws Client) opcode_from_fragments() OPCode {
|
|
|
|
return OPCode(ws.fragments[0].opcode)
|
|
|
|
}
|
|
|
|
|
|
|
|
// parse_frame_header parses next message by decoding the incoming frames
|
|
|
|
pub fn (mut ws Client) parse_frame_header() ?Frame {
|
|
|
|
mut buffer := [256]byte{}
|
|
|
|
mut bytes_read := 0
|
|
|
|
mut frame := Frame{}
|
|
|
|
mut rbuff := [1]byte{}
|
|
|
|
mut mask_end_byte := 0
|
|
|
|
for ws.state == .open {
|
2020-12-02 04:02:53 +01:00
|
|
|
read_bytes := ws.socket_read_ptr(byteptr(rbuff), 1) ?
|
2020-08-22 00:50:38 +02:00
|
|
|
if read_bytes == 0 {
|
2020-12-04 01:52:26 +01:00
|
|
|
// this is probably a timeout or close
|
2020-08-22 00:50:38 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
buffer[bytes_read] = rbuff[0]
|
|
|
|
bytes_read++
|
|
|
|
// parses the first two header bytes to get basic frame information
|
2021-01-25 10:26:20 +01:00
|
|
|
if bytes_read == u64(websocket.header_len_offset) {
|
2020-08-22 00:50:38 +02:00
|
|
|
frame.fin = (buffer[0] & 0x80) == 0x80
|
|
|
|
frame.rsv1 = (buffer[0] & 0x40) == 0x40
|
|
|
|
frame.rsv2 = (buffer[0] & 0x20) == 0x20
|
|
|
|
frame.rsv3 = (buffer[0] & 0x10) == 0x10
|
|
|
|
frame.opcode = OPCode(int(buffer[0] & 0x7F))
|
|
|
|
frame.has_mask = (buffer[1] & 0x80) == 0x80
|
|
|
|
frame.payload_len = buffer[1] & 0x7F
|
|
|
|
// if has mask set the byte postition where mask ends
|
|
|
|
if frame.has_mask {
|
|
|
|
mask_end_byte = if frame.payload_len < 126 {
|
2021-01-25 10:26:20 +01:00
|
|
|
websocket.header_len_offset + 4
|
2020-08-22 00:50:38 +02:00
|
|
|
} else if frame.payload_len == 126 {
|
2021-01-25 10:26:20 +01:00
|
|
|
websocket.header_len_offset + 6
|
2020-08-22 00:50:38 +02:00
|
|
|
} else if frame.payload_len == 127 {
|
2021-01-25 10:26:20 +01:00
|
|
|
websocket.header_len_offset + 12
|
2020-08-22 00:50:38 +02:00
|
|
|
} else {
|
|
|
|
0
|
2020-12-04 01:52:26 +01:00
|
|
|
} // impossible
|
2020-08-22 00:50:38 +02:00
|
|
|
}
|
|
|
|
frame.payload_len = frame.payload_len
|
|
|
|
frame.frame_size = frame.header_len + frame.payload_len
|
|
|
|
if !frame.has_mask && frame.payload_len < 126 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2021-01-25 10:26:20 +01:00
|
|
|
if frame.payload_len == 126 && bytes_read == u64(websocket.extended_payload16_end_byte) {
|
2020-08-22 00:50:38 +02:00
|
|
|
frame.header_len += 2
|
|
|
|
frame.payload_len = 0
|
2021-01-19 10:42:59 +01:00
|
|
|
frame.payload_len |= buffer[2] << 8
|
2021-01-19 05:49:37 +01:00
|
|
|
frame.payload_len |= buffer[3]
|
2020-08-22 00:50:38 +02:00
|
|
|
frame.frame_size = frame.header_len + frame.payload_len
|
|
|
|
if !frame.has_mask {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2021-01-25 10:26:20 +01:00
|
|
|
if frame.payload_len == 127 && bytes_read == u64(websocket.extended_payload64_end_byte) {
|
2020-12-02 04:02:53 +01:00
|
|
|
frame.header_len += 8
|
2021-01-19 10:42:59 +01:00
|
|
|
// these shift operators needs 64 bit on clang with -prod flag
|
|
|
|
mut payload_len := u64(0)
|
|
|
|
payload_len |= buffer[2] << 56
|
|
|
|
payload_len |= buffer[3] << 48
|
|
|
|
payload_len |= buffer[4] << 40
|
|
|
|
payload_len |= buffer[5] << 32
|
|
|
|
payload_len |= buffer[6] << 24
|
|
|
|
payload_len |= buffer[7] << 16
|
|
|
|
payload_len |= buffer[8] << 8
|
|
|
|
payload_len |= buffer[9]
|
|
|
|
frame.payload_len = int(payload_len)
|
2020-08-22 00:50:38 +02:00
|
|
|
if !frame.has_mask {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if frame.has_mask && bytes_read == mask_end_byte {
|
|
|
|
frame.masking_key[0] = buffer[mask_end_byte - 4]
|
|
|
|
frame.masking_key[1] = buffer[mask_end_byte - 3]
|
|
|
|
frame.masking_key[2] = buffer[mask_end_byte - 2]
|
|
|
|
frame.masking_key[3] = buffer[mask_end_byte - 1]
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return frame
|
|
|
|
}
|
|
|
|
|
|
|
|
// unmask_sequence unmask any given sequence
|
|
|
|
fn (f Frame) unmask_sequence(mut buffer []byte) {
|
|
|
|
for i in 0 .. buffer.len {
|
|
|
|
buffer[i] ^= f.masking_key[i % 4] & 0xff
|
|
|
|
}
|
|
|
|
}
|