2020-12-04 03:52:26 +03:00
|
|
|
// websocket module implements websocket client and a websocket server
|
2020-12-28 03:06:57 +03:00
|
|
|
// attribution: @thecoderr the author of original websocket client
|
2021-01-11 11:03:10 +03:00
|
|
|
[manualfree]
|
2020-08-22 01:50:38 +03:00
|
|
|
module websocket
|
|
|
|
|
2020-11-15 23:54:47 +03:00
|
|
|
import net
|
2020-08-22 01:50:38 +03:00
|
|
|
import x.openssl
|
|
|
|
import net.urllib
|
|
|
|
import time
|
|
|
|
import log
|
|
|
|
import rand
|
|
|
|
|
|
|
|
const (
|
2020-12-04 03:52:26 +03:00
|
|
|
empty_bytearr = []byte{} // used as empty response to avoid allocation
|
2020-08-22 01:50:38 +03:00
|
|
|
)
|
|
|
|
|
2020-12-04 03:52:26 +03:00
|
|
|
// Client represents websocket client
|
2020-08-22 01:50:38 +03:00
|
|
|
pub struct Client {
|
2021-01-12 06:38:43 +03:00
|
|
|
is_server bool
|
2020-08-22 01:50:38 +03:00
|
|
|
mut:
|
2020-12-04 03:52:26 +03:00
|
|
|
ssl_conn &openssl.SSLConn // secure connection used when wss is used
|
2021-01-03 23:10:25 +03:00
|
|
|
flags []Flag // flags used in handshake
|
2020-12-04 03:52:26 +03:00
|
|
|
fragments []Fragment // current fragments
|
|
|
|
message_callbacks []MessageEventHandler // all callbacks on_message
|
2021-01-03 23:10:25 +03:00
|
|
|
error_callbacks []ErrorEventHandler // all callbacks on_error
|
|
|
|
open_callbacks []OpenEventHandler // all callbacks on_open
|
|
|
|
close_callbacks []CloseEventHandler // all callbacks on_close
|
2020-08-22 01:50:38 +03:00
|
|
|
pub:
|
2021-01-12 06:38:43 +03:00
|
|
|
is_ssl bool // true if secure socket is used
|
|
|
|
uri Uri // uri of current connection
|
|
|
|
id string // unique id of client
|
2020-08-22 01:50:38 +03:00
|
|
|
pub mut:
|
2021-01-20 13:11:01 +03:00
|
|
|
conn &net.TcpConn // underlying TCP socket connection
|
2020-12-28 16:38:21 +03:00
|
|
|
nonce_size int = 16 // size of nounce used for masking
|
2021-01-03 23:10:25 +03:00
|
|
|
panic_on_callback bool // set to true of callbacks can panic
|
|
|
|
state State // current state of connection
|
2020-12-04 03:52:26 +03:00
|
|
|
logger &log.Log // logger used to log messages
|
2021-01-03 23:10:25 +03:00
|
|
|
resource_name string // name of current resource
|
|
|
|
last_pong_ut u64 // last time in unix time we got a pong message
|
2020-08-22 01:50:38 +03:00
|
|
|
}
|
|
|
|
|
2020-12-04 03:52:26 +03:00
|
|
|
// Flag represents different types of headers in websocket handshake
|
2020-08-22 01:50:38 +03:00
|
|
|
enum Flag {
|
2020-12-28 08:13:25 +03:00
|
|
|
has_accept // Webs
|
2020-08-22 01:50:38 +03:00
|
|
|
has_connection
|
|
|
|
has_upgrade
|
|
|
|
}
|
|
|
|
|
2020-12-04 03:52:26 +03:00
|
|
|
// State represents the state of the websocket connection.
|
2020-08-22 01:50:38 +03:00
|
|
|
enum State {
|
|
|
|
connecting = 0
|
|
|
|
open
|
|
|
|
closing
|
|
|
|
closed
|
|
|
|
}
|
|
|
|
|
2020-12-04 03:52:26 +03:00
|
|
|
// Message represents a whole message combined from 1 to n frames
|
2020-08-22 01:50:38 +03:00
|
|
|
pub struct Message {
|
|
|
|
pub:
|
2020-12-28 08:13:25 +03:00
|
|
|
opcode OPCode // websocket frame type of this message
|
|
|
|
payload []byte // payload of the message
|
2020-08-22 01:50:38 +03:00
|
|
|
}
|
|
|
|
|
2020-12-04 03:52:26 +03:00
|
|
|
// OPCode represents the supported websocket frame types
|
2020-08-22 01:50:38 +03:00
|
|
|
pub enum OPCode {
|
|
|
|
continuation = 0x00
|
|
|
|
text_frame = 0x01
|
|
|
|
binary_frame = 0x02
|
|
|
|
close = 0x08
|
|
|
|
ping = 0x09
|
|
|
|
pong = 0x0A
|
|
|
|
}
|
|
|
|
|
2020-12-04 03:52:26 +03:00
|
|
|
// new_client instance a new websocket client
|
2020-08-22 01:50:38 +03:00
|
|
|
pub fn new_client(address string) ?&Client {
|
2020-12-02 06:02:53 +03:00
|
|
|
uri := parse_uri(address) ?
|
2020-08-22 01:50:38 +03:00
|
|
|
return &Client{
|
2021-01-20 13:11:01 +03:00
|
|
|
conn: 0
|
2020-08-22 01:50:38 +03:00
|
|
|
is_server: false
|
|
|
|
ssl_conn: openssl.new_ssl_conn()
|
|
|
|
is_ssl: address.starts_with('wss')
|
|
|
|
logger: &log.Log{
|
|
|
|
level: .info
|
|
|
|
}
|
|
|
|
uri: uri
|
|
|
|
state: .closed
|
|
|
|
id: rand.uuid_v4()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-04 03:52:26 +03:00
|
|
|
// connect connects to remote websocket server
|
2020-08-22 01:50:38 +03:00
|
|
|
pub fn (mut ws Client) connect() ? {
|
2020-12-02 06:02:53 +03:00
|
|
|
ws.assert_not_connected() ?
|
2020-08-22 01:50:38 +03:00
|
|
|
ws.set_state(.connecting)
|
|
|
|
ws.logger.info('connecting to host $ws.uri')
|
2020-12-02 06:02:53 +03:00
|
|
|
ws.conn = ws.dial_socket() ?
|
2020-11-21 16:45:45 +03:00
|
|
|
// Todo: make setting configurable
|
|
|
|
ws.conn.set_read_timeout(time.second * 30)
|
|
|
|
ws.conn.set_write_timeout(time.second * 30)
|
2020-12-02 06:02:53 +03:00
|
|
|
ws.handshake() ?
|
2020-08-22 01:50:38 +03:00
|
|
|
ws.set_state(.open)
|
|
|
|
ws.logger.info('successfully connected to host $ws.uri')
|
|
|
|
ws.send_open_event()
|
|
|
|
}
|
|
|
|
|
2020-12-04 03:52:26 +03:00
|
|
|
// listen listens and processes incoming messages
|
2020-08-22 01:50:38 +03:00
|
|
|
pub fn (mut ws Client) listen() ? {
|
2021-01-11 11:03:10 +03:00
|
|
|
mut log := 'Starting client listener, server($ws.is_server)...'
|
|
|
|
ws.logger.info(log)
|
2021-02-14 21:31:42 +03:00
|
|
|
unsafe { log.free() }
|
2020-08-22 01:50:38 +03:00
|
|
|
defer {
|
|
|
|
ws.logger.info('Quit client listener, server($ws.is_server)...')
|
2020-11-21 16:45:45 +03:00
|
|
|
if ws.state == .open {
|
2021-03-06 22:04:51 +03:00
|
|
|
ws.close(1000, 'closed by client') or {}
|
2020-11-21 16:45:45 +03:00
|
|
|
}
|
2020-08-22 01:50:38 +03:00
|
|
|
}
|
|
|
|
for ws.state == .open {
|
|
|
|
msg := ws.read_next_message() or {
|
|
|
|
if ws.state in [.closed, .closing] {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ws.debug_log('failed to read next message: $err')
|
|
|
|
ws.send_error_event('failed to read next message: $err')
|
2021-02-28 23:20:21 +03:00
|
|
|
return err
|
2020-08-22 01:50:38 +03:00
|
|
|
}
|
2020-11-21 16:45:45 +03:00
|
|
|
if ws.state in [.closed, .closing] {
|
2020-12-02 06:02:53 +03:00
|
|
|
return
|
2020-11-21 16:45:45 +03:00
|
|
|
}
|
2020-12-28 03:06:57 +03:00
|
|
|
ws.debug_log('got message: $msg.opcode')
|
2020-08-22 01:50:38 +03:00
|
|
|
match msg.opcode {
|
|
|
|
.text_frame {
|
2021-01-11 11:03:10 +03:00
|
|
|
log = 'read: text'
|
|
|
|
ws.debug_log(log)
|
2021-02-14 21:31:42 +03:00
|
|
|
unsafe { log.free() }
|
2020-08-22 01:50:38 +03:00
|
|
|
ws.send_message_event(msg)
|
2020-12-28 08:13:25 +03:00
|
|
|
unsafe { msg.free() }
|
2020-08-22 01:50:38 +03:00
|
|
|
}
|
|
|
|
.binary_frame {
|
|
|
|
ws.debug_log('read: binary')
|
|
|
|
ws.send_message_event(msg)
|
2020-12-28 08:13:25 +03:00
|
|
|
unsafe { msg.free() }
|
2020-08-22 01:50:38 +03:00
|
|
|
}
|
|
|
|
.ping {
|
|
|
|
ws.debug_log('read: ping, sending pong')
|
|
|
|
ws.send_control_frame(.pong, 'PONG', msg.payload) or {
|
|
|
|
ws.logger.error('error in message callback sending PONG: $err')
|
|
|
|
ws.send_error_event('error in message callback sending PONG: $err')
|
|
|
|
if ws.panic_on_callback {
|
2021-03-01 02:18:14 +03:00
|
|
|
panic(err)
|
2020-08-22 01:50:38 +03:00
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if msg.payload.len > 0 {
|
2020-12-28 08:13:25 +03:00
|
|
|
unsafe { msg.free() }
|
2020-08-22 01:50:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
.pong {
|
|
|
|
ws.debug_log('read: pong')
|
|
|
|
ws.last_pong_ut = time.now().unix
|
|
|
|
ws.send_message_event(msg)
|
|
|
|
if msg.payload.len > 0 {
|
2020-12-28 08:13:25 +03:00
|
|
|
unsafe { msg.free() }
|
2020-08-22 01:50:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
.close {
|
2021-01-11 11:03:10 +03:00
|
|
|
log = 'read: close'
|
|
|
|
ws.debug_log(log)
|
2021-02-14 21:31:42 +03:00
|
|
|
unsafe { log.free() }
|
2020-08-22 01:50:38 +03:00
|
|
|
defer {
|
|
|
|
ws.manage_clean_close()
|
|
|
|
}
|
|
|
|
if msg.payload.len > 0 {
|
|
|
|
if msg.payload.len == 1 {
|
2020-12-02 06:02:53 +03:00
|
|
|
ws.close(1002, 'close payload cannot be 1 byte') ?
|
2020-08-22 01:50:38 +03:00
|
|
|
return error('close payload cannot be 1 byte')
|
|
|
|
}
|
|
|
|
code := (int(msg.payload[0]) << 8) + int(msg.payload[1])
|
|
|
|
if code in invalid_close_codes {
|
2020-12-02 06:02:53 +03:00
|
|
|
ws.close(1002, 'invalid close code: $code') ?
|
2020-08-22 01:50:38 +03:00
|
|
|
return error('invalid close code: $code')
|
|
|
|
}
|
|
|
|
reason := if msg.payload.len > 2 { msg.payload[2..] } else { []byte{} }
|
|
|
|
if reason.len > 0 {
|
2020-12-02 06:02:53 +03:00
|
|
|
ws.validate_utf_8(.close, reason) ?
|
2020-08-22 01:50:38 +03:00
|
|
|
}
|
|
|
|
if ws.state !in [.closing, .closed] {
|
|
|
|
// sending close back according to spec
|
|
|
|
ws.debug_log('close with reason, code: $code, reason: $reason')
|
2020-08-22 13:29:15 +03:00
|
|
|
r := reason.bytestr()
|
2020-12-02 06:02:53 +03:00
|
|
|
ws.close(code, r) ?
|
2020-08-22 01:50:38 +03:00
|
|
|
}
|
2020-12-28 08:13:25 +03:00
|
|
|
unsafe { msg.free() }
|
2020-08-22 01:50:38 +03:00
|
|
|
} else {
|
|
|
|
if ws.state !in [.closing, .closed] {
|
|
|
|
ws.debug_log('close with reason, no code')
|
|
|
|
// sending close back according to spec
|
2020-12-02 06:02:53 +03:00
|
|
|
ws.close(1000, 'normal') ?
|
2020-08-22 01:50:38 +03:00
|
|
|
}
|
2020-12-28 08:13:25 +03:00
|
|
|
unsafe { msg.free() }
|
2020-08-22 01:50:38 +03:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
.continuation {
|
|
|
|
ws.logger.error('unexpected opcode continuation, nothing to continue')
|
|
|
|
ws.send_error_event('unexpected opcode continuation, nothing to continue')
|
2020-12-02 06:02:53 +03:00
|
|
|
ws.close(1002, 'nothing to continue') ?
|
2020-08-22 01:50:38 +03:00
|
|
|
return error('unexpected opcode continuation, nothing to continue')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-04 03:52:26 +03:00
|
|
|
// manage_clean_close closes connection in a clean websocket way
|
2020-08-22 01:50:38 +03:00
|
|
|
fn (mut ws Client) manage_clean_close() {
|
|
|
|
ws.send_close_event(1000, 'closed by client')
|
|
|
|
}
|
|
|
|
|
2020-12-04 03:52:26 +03:00
|
|
|
// ping sends ping message to server
|
2020-08-22 01:50:38 +03:00
|
|
|
pub fn (mut ws Client) ping() ? {
|
2020-12-02 06:02:53 +03:00
|
|
|
ws.send_control_frame(.ping, 'PING', []) ?
|
2020-08-22 01:50:38 +03:00
|
|
|
}
|
|
|
|
|
2020-12-04 03:52:26 +03:00
|
|
|
// pong sends pong message to server,
|
2020-08-22 01:50:38 +03:00
|
|
|
pub fn (mut ws Client) pong() ? {
|
2020-12-02 06:02:53 +03:00
|
|
|
ws.send_control_frame(.pong, 'PONG', []) ?
|
2020-08-22 01:50:38 +03:00
|
|
|
}
|
|
|
|
|
2020-12-04 03:52:26 +03:00
|
|
|
// write_ptr writes len bytes provided a byteptr with a websocket messagetype
|
2021-02-27 11:29:18 +03:00
|
|
|
pub fn (mut ws Client) write_ptr(bytes byteptr, payload_len int, code OPCode) ?int {
|
2021-01-11 11:03:10 +03:00
|
|
|
// ws.debug_log('write_ptr code: $code')
|
2020-08-22 01:50:38 +03:00
|
|
|
if ws.state != .open || ws.conn.sock.handle < 1 {
|
2020-12-04 03:52:26 +03:00
|
|
|
// todo: send error here later
|
2020-08-22 01:50:38 +03:00
|
|
|
return error('trying to write on a closed socket!')
|
|
|
|
}
|
2021-01-28 13:23:48 +03:00
|
|
|
mut header_len := 2 + if payload_len > 125 { 2 } else { 0 } +
|
|
|
|
if payload_len > 0xffff { 6 } else { 0 }
|
2020-08-22 01:50:38 +03:00
|
|
|
if !ws.is_server {
|
|
|
|
header_len += 4
|
|
|
|
}
|
|
|
|
mut header := []byte{len: header_len, init: `0`} // [`0`].repeat(header_len)
|
|
|
|
header[0] = byte(int(code)) | 0x80
|
|
|
|
masking_key := create_masking_key()
|
|
|
|
if ws.is_server {
|
|
|
|
if payload_len <= 125 {
|
|
|
|
header[1] = byte(payload_len)
|
|
|
|
} else if payload_len > 125 && payload_len <= 0xffff {
|
|
|
|
len16 := C.htons(payload_len)
|
|
|
|
header[1] = 126
|
2020-12-28 08:13:25 +03:00
|
|
|
unsafe { C.memcpy(&header[2], &len16, 2) }
|
2020-08-22 01:50:38 +03:00
|
|
|
} else if payload_len > 0xffff && payload_len <= 0xffffffffffffffff {
|
|
|
|
len_bytes := htonl64(u64(payload_len))
|
2020-12-28 03:06:57 +03:00
|
|
|
header[1] = 127
|
2020-12-28 08:13:25 +03:00
|
|
|
unsafe { C.memcpy(&header[2], len_bytes.data, 8) }
|
2020-08-22 01:50:38 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if payload_len <= 125 {
|
|
|
|
header[1] = byte(payload_len | 0x80)
|
|
|
|
header[2] = masking_key[0]
|
|
|
|
header[3] = masking_key[1]
|
|
|
|
header[4] = masking_key[2]
|
|
|
|
header[5] = masking_key[3]
|
|
|
|
} else if payload_len > 125 && payload_len <= 0xffff {
|
|
|
|
len16 := C.htons(payload_len)
|
|
|
|
header[1] = (126 | 0x80)
|
2020-12-28 08:13:25 +03:00
|
|
|
unsafe { C.memcpy(&header[2], &len16, 2) }
|
2020-08-22 01:50:38 +03:00
|
|
|
header[4] = masking_key[0]
|
|
|
|
header[5] = masking_key[1]
|
|
|
|
header[6] = masking_key[2]
|
|
|
|
header[7] = masking_key[3]
|
2020-12-28 03:06:57 +03:00
|
|
|
} else if payload_len > 0xffff && payload_len <= 0xffffffffffffffff {
|
2020-08-22 01:50:38 +03:00
|
|
|
len64 := htonl64(u64(payload_len))
|
|
|
|
header[1] = (127 | 0x80)
|
2020-12-28 08:13:25 +03:00
|
|
|
unsafe { C.memcpy(&header[2], len64.data, 8) }
|
2020-08-22 01:50:38 +03:00
|
|
|
header[10] = masking_key[0]
|
|
|
|
header[11] = masking_key[1]
|
|
|
|
header[12] = masking_key[2]
|
|
|
|
header[13] = masking_key[3]
|
|
|
|
} else {
|
2020-12-02 06:02:53 +03:00
|
|
|
ws.close(1009, 'frame too large') ?
|
2020-08-22 01:50:38 +03:00
|
|
|
return error('frame too large')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
len := header.len + payload_len
|
|
|
|
mut frame_buf := []byte{len: len}
|
|
|
|
unsafe {
|
|
|
|
C.memcpy(&frame_buf[0], byteptr(header.data), header.len)
|
|
|
|
if payload_len > 0 {
|
|
|
|
C.memcpy(&frame_buf[header.len], bytes, payload_len)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !ws.is_server {
|
|
|
|
for i in 0 .. payload_len {
|
|
|
|
frame_buf[header_len + i] ^= masking_key[i % 4] & 0xff
|
|
|
|
}
|
|
|
|
}
|
2021-02-27 11:29:18 +03:00
|
|
|
written_len := ws.socket_write(frame_buf) ?
|
2020-08-22 01:50:38 +03:00
|
|
|
unsafe {
|
|
|
|
frame_buf.free()
|
|
|
|
masking_key.free()
|
|
|
|
header.free()
|
|
|
|
}
|
2021-02-27 11:29:18 +03:00
|
|
|
return written_len
|
2020-08-22 01:50:38 +03:00
|
|
|
}
|
|
|
|
|
2020-12-04 03:52:26 +03:00
|
|
|
// write writes a byte array with a websocket messagetype to socket
|
2021-02-27 11:29:18 +03:00
|
|
|
pub fn (mut ws Client) write(bytes []byte, code OPCode) ?int {
|
|
|
|
return ws.write_ptr(byteptr(bytes.data), bytes.len, code)
|
2020-08-22 01:50:38 +03:00
|
|
|
}
|
|
|
|
|
2021-03-20 19:25:51 +03:00
|
|
|
// write_string, writes a string with a websocket texttype to socket
|
|
|
|
[deprecated: 'use Client.write_string() instead']
|
2021-02-27 11:29:18 +03:00
|
|
|
pub fn (mut ws Client) write_str(str string) ?int {
|
2021-03-20 19:25:51 +03:00
|
|
|
return ws.write_string(str)
|
|
|
|
}
|
|
|
|
|
|
|
|
// write_str, writes a string with a websocket texttype to socket
|
|
|
|
pub fn (mut ws Client) write_string(str string) ?int {
|
2021-02-27 11:29:18 +03:00
|
|
|
return ws.write_ptr(str.str, str.len, .text_frame)
|
2020-08-22 01:50:38 +03:00
|
|
|
}
|
|
|
|
|
2020-12-04 03:52:26 +03:00
|
|
|
// close closes the websocket connection
|
2020-08-22 01:50:38 +03:00
|
|
|
pub fn (mut ws Client) close(code int, message string) ? {
|
|
|
|
ws.debug_log('sending close, $code, $message')
|
|
|
|
if ws.state in [.closed, .closing] || ws.conn.sock.handle <= 1 {
|
|
|
|
ws.debug_log('close: Websocket allready closed ($ws.state), $message, $code handle($ws.conn.sock.handle)')
|
|
|
|
err_msg := 'Socket allready closed: $code'
|
2021-02-28 22:24:29 +03:00
|
|
|
return error(err_msg)
|
2020-08-22 01:50:38 +03:00
|
|
|
}
|
|
|
|
defer {
|
2021-03-06 22:04:51 +03:00
|
|
|
ws.shutdown_socket() or {}
|
2020-08-22 01:50:38 +03:00
|
|
|
ws.reset_state()
|
|
|
|
}
|
|
|
|
ws.set_state(.closing)
|
|
|
|
mut code32 := 0
|
|
|
|
if code > 0 {
|
|
|
|
code_ := C.htons(code)
|
|
|
|
message_len := message.len + 2
|
2020-12-02 06:02:53 +03:00
|
|
|
mut close_frame := []byte{len: message_len}
|
2020-08-22 01:50:38 +03:00
|
|
|
close_frame[0] = byte(code_ & 0xFF)
|
|
|
|
close_frame[1] = byte(code_ >> 8)
|
|
|
|
code32 = (close_frame[0] << 8) + close_frame[1]
|
|
|
|
for i in 0 .. message.len {
|
|
|
|
close_frame[i + 2] = message[i]
|
|
|
|
}
|
2020-12-02 06:02:53 +03:00
|
|
|
ws.send_control_frame(.close, 'CLOSE', close_frame) ?
|
2020-12-28 08:13:25 +03:00
|
|
|
unsafe { close_frame.free() }
|
2020-08-22 01:50:38 +03:00
|
|
|
} else {
|
2020-12-02 06:02:53 +03:00
|
|
|
ws.send_control_frame(.close, 'CLOSE', []) ?
|
2020-08-22 01:50:38 +03:00
|
|
|
}
|
|
|
|
ws.fragments = []
|
|
|
|
}
|
|
|
|
|
2020-12-04 03:52:26 +03:00
|
|
|
// send_control_frame sends a control frame to the server
|
2020-08-22 01:50:38 +03:00
|
|
|
fn (mut ws Client) send_control_frame(code OPCode, frame_typ string, payload []byte) ? {
|
2020-12-28 03:06:57 +03:00
|
|
|
ws.debug_log('send control frame $code, frame_type: $frame_typ')
|
2020-08-22 01:50:38 +03:00
|
|
|
if ws.state !in [.open, .closing] && ws.conn.sock.handle > 1 {
|
|
|
|
return error('socket is not connected')
|
|
|
|
}
|
|
|
|
header_len := if ws.is_server { 2 } else { 6 }
|
|
|
|
frame_len := header_len + payload.len
|
2020-12-28 03:06:57 +03:00
|
|
|
mut control_frame := []byte{len: frame_len}
|
2021-01-25 12:26:20 +03:00
|
|
|
mut masking_key := if !ws.is_server { create_masking_key() } else { websocket.empty_bytearr }
|
2020-08-22 01:50:38 +03:00
|
|
|
defer {
|
|
|
|
unsafe {
|
|
|
|
control_frame.free()
|
|
|
|
if masking_key.len > 0 {
|
|
|
|
masking_key.free()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
control_frame[0] = byte(int(code) | 0x80)
|
|
|
|
if !ws.is_server {
|
|
|
|
control_frame[1] = byte(payload.len | 0x80)
|
|
|
|
control_frame[2] = masking_key[0]
|
|
|
|
control_frame[3] = masking_key[1]
|
|
|
|
control_frame[4] = masking_key[2]
|
|
|
|
control_frame[5] = masking_key[3]
|
|
|
|
} else {
|
|
|
|
control_frame[1] = byte(payload.len)
|
|
|
|
}
|
|
|
|
if code == .close {
|
|
|
|
if payload.len >= 2 {
|
|
|
|
if !ws.is_server {
|
|
|
|
mut parsed_payload := []byte{len: payload.len + 1}
|
2020-12-28 08:13:25 +03:00
|
|
|
unsafe { C.memcpy(parsed_payload.data, &payload[0], payload.len) }
|
2020-08-22 01:50:38 +03:00
|
|
|
parsed_payload[payload.len] = `\0`
|
|
|
|
for i in 0 .. payload.len {
|
|
|
|
control_frame[6 + i] = (parsed_payload[i] ^ masking_key[i % 4]) & 0xff
|
|
|
|
}
|
2020-12-28 08:13:25 +03:00
|
|
|
unsafe { parsed_payload.free() }
|
2020-08-22 01:50:38 +03:00
|
|
|
} else {
|
2020-12-28 08:13:25 +03:00
|
|
|
unsafe { C.memcpy(&control_frame[2], &payload[0], payload.len) }
|
2020-08-22 01:50:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if !ws.is_server {
|
|
|
|
if payload.len > 0 {
|
|
|
|
for i in 0 .. payload.len {
|
|
|
|
control_frame[header_len + i] = (payload[i] ^ masking_key[i % 4]) & 0xff
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if payload.len > 0 {
|
2020-12-28 08:13:25 +03:00
|
|
|
unsafe { C.memcpy(&control_frame[2], &payload[0], payload.len) }
|
2020-08-22 01:50:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ws.socket_write(control_frame) or {
|
|
|
|
return error('send_control_frame: error sending $frame_typ control frame.')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-04 03:52:26 +03:00
|
|
|
// parse_uri parses the url to a Uri
|
2020-08-22 01:50:38 +03:00
|
|
|
fn parse_uri(url string) ?&Uri {
|
2020-12-02 06:02:53 +03:00
|
|
|
u := urllib.parse(url) ?
|
2021-01-11 11:03:10 +03:00
|
|
|
request_uri := u.request_uri()
|
|
|
|
v := request_uri.split('?')
|
2020-11-07 19:14:33 +03:00
|
|
|
mut port := u.port()
|
2021-01-11 11:03:10 +03:00
|
|
|
uri := u.str()
|
2020-11-07 19:14:33 +03:00
|
|
|
if port == '' {
|
2021-01-11 11:03:10 +03:00
|
|
|
port = if uri.starts_with('ws://') {
|
2020-12-02 06:02:53 +03:00
|
|
|
'80'
|
2021-01-11 11:03:10 +03:00
|
|
|
} else if uri.starts_with('wss://') {
|
2020-12-02 06:02:53 +03:00
|
|
|
'443'
|
|
|
|
} else {
|
|
|
|
u.port()
|
|
|
|
}
|
2020-11-05 08:36:50 +03:00
|
|
|
}
|
2020-08-22 01:50:38 +03:00
|
|
|
querystring := if v.len > 1 { '?' + v[1] } else { '' }
|
|
|
|
return &Uri{
|
|
|
|
url: url
|
|
|
|
hostname: u.hostname()
|
2020-11-05 08:36:50 +03:00
|
|
|
port: port
|
2020-08-22 01:50:38 +03:00
|
|
|
resource: v[0]
|
|
|
|
querystring: querystring
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-04 03:52:26 +03:00
|
|
|
// set_state sets current state of the websocket connection
|
2020-08-22 01:50:38 +03:00
|
|
|
fn (mut ws Client) set_state(state State) {
|
|
|
|
lock {
|
|
|
|
ws.state = state
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-02 06:02:53 +03:00
|
|
|
// assert_not_connected returns error if the connection is not connected
|
2020-08-22 01:50:38 +03:00
|
|
|
fn (ws Client) assert_not_connected() ? {
|
|
|
|
match ws.state {
|
|
|
|
.connecting { return error('connect: websocket is connecting') }
|
|
|
|
.open { return error('connect: websocket already open') }
|
2020-11-21 16:45:45 +03:00
|
|
|
.closing { return error('connect: reconnect on closing websocket not supported, please use new client') }
|
2020-08-22 01:50:38 +03:00
|
|
|
else {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-04 03:52:26 +03:00
|
|
|
// reset_state resets the websocket and initialize default settings
|
2020-08-22 01:50:38 +03:00
|
|
|
fn (mut ws Client) reset_state() {
|
|
|
|
lock {
|
|
|
|
ws.state = .closed
|
|
|
|
ws.ssl_conn = openssl.new_ssl_conn()
|
|
|
|
ws.flags = []
|
|
|
|
ws.fragments = []
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-04 03:52:26 +03:00
|
|
|
// debug_log handles debug logging output for client and server
|
2020-08-22 01:50:38 +03:00
|
|
|
fn (mut ws Client) debug_log(text string) {
|
|
|
|
if ws.is_server {
|
|
|
|
ws.logger.debug('server-> $text')
|
|
|
|
} else {
|
|
|
|
ws.logger.debug('client-> $text')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-04 03:52:26 +03:00
|
|
|
// free handles manual free memory of Message struct
|
2020-08-22 01:50:38 +03:00
|
|
|
pub fn (m &Message) free() {
|
2020-12-28 08:13:25 +03:00
|
|
|
unsafe { m.payload.free() }
|
2020-08-22 01:50:38 +03:00
|
|
|
}
|
|
|
|
|
2020-12-04 03:52:26 +03:00
|
|
|
// free handles manual free memory of Client struct
|
2020-08-22 01:50:38 +03:00
|
|
|
pub fn (c &Client) free() {
|
|
|
|
unsafe {
|
|
|
|
c.flags.free()
|
|
|
|
c.fragments.free()
|
|
|
|
c.message_callbacks.free()
|
|
|
|
c.error_callbacks.free()
|
|
|
|
c.open_callbacks.free()
|
|
|
|
c.close_callbacks.free()
|
|
|
|
}
|
|
|
|
}
|