2021-01-11 11:03:10 +03:00
|
|
|
[manualfree]
|
2020-08-22 01:50:38 +03:00
|
|
|
module websocket
|
|
|
|
|
|
|
|
import encoding.base64
|
|
|
|
import strings
|
|
|
|
|
2020-12-04 03:52:26 +03:00
|
|
|
// handshake manages the websocket handshake process
|
2020-08-22 01:50:38 +03:00
|
|
|
fn (mut ws Client) handshake() ? {
|
|
|
|
nonce := get_nonce(ws.nonce_size)
|
|
|
|
seckey := base64.encode(nonce)
|
|
|
|
mut sb := strings.new_builder(1024)
|
|
|
|
defer {
|
|
|
|
sb.free()
|
|
|
|
}
|
|
|
|
sb.write('GET ')
|
|
|
|
sb.write(ws.uri.resource)
|
|
|
|
sb.write(ws.uri.querystring)
|
|
|
|
sb.write(' HTTP/1.1\r\nHost: ')
|
|
|
|
sb.write(ws.uri.hostname)
|
|
|
|
sb.write(':')
|
|
|
|
sb.write(ws.uri.port)
|
|
|
|
sb.write('\r\nUpgrade: websocket\r\nConnection: Upgrade\r\n')
|
|
|
|
sb.write('Sec-WebSocket-Key: ')
|
|
|
|
sb.write(seckey)
|
|
|
|
sb.write('\r\nSec-WebSocket-Version: 13\r\n\r\n')
|
|
|
|
handshake := sb.str()
|
|
|
|
defer {
|
|
|
|
handshake.free()
|
|
|
|
}
|
|
|
|
handshake_bytes := handshake.bytes()
|
|
|
|
ws.debug_log('sending handshake: $handshake')
|
2020-10-17 16:26:56 +03:00
|
|
|
ws.socket_write(handshake_bytes) ?
|
|
|
|
ws.read_handshake(seckey) ?
|
2020-12-28 08:25:46 +03:00
|
|
|
unsafe { handshake_bytes.free() }
|
2020-08-22 01:50:38 +03:00
|
|
|
}
|
|
|
|
|
2020-12-04 03:52:26 +03:00
|
|
|
// handle_server_handshake manages websocket server handshake process
|
2020-08-22 01:50:38 +03:00
|
|
|
fn (mut s Server) handle_server_handshake(mut c Client) ?(string, &ServerClient) {
|
2020-10-17 16:26:56 +03:00
|
|
|
msg := c.read_handshake_str() ?
|
|
|
|
handshake_response, client := s.parse_client_handshake(msg, mut c) ?
|
2020-12-28 08:25:46 +03:00
|
|
|
unsafe { msg.free() }
|
2020-08-22 01:50:38 +03:00
|
|
|
return handshake_response, client
|
|
|
|
}
|
|
|
|
|
2020-12-04 03:52:26 +03:00
|
|
|
// parse_client_handshake parses result from handshake process
|
2020-08-22 01:50:38 +03:00
|
|
|
fn (mut s Server) parse_client_handshake(client_handshake string, mut c Client) ?(string, &ServerClient) {
|
|
|
|
s.logger.debug('server-> client handshake:\n$client_handshake')
|
|
|
|
lines := client_handshake.split_into_lines()
|
|
|
|
get_tokens := lines[0].split(' ')
|
|
|
|
if get_tokens.len < 3 {
|
2020-11-06 17:10:05 +03:00
|
|
|
return error_with_code('unexpected get operation, $get_tokens', 1)
|
2020-08-22 01:50:38 +03:00
|
|
|
}
|
|
|
|
if get_tokens[0].trim_space() != 'GET' {
|
2020-12-02 06:02:53 +03:00
|
|
|
return error_with_code("unexpected request '${get_tokens[0]}', expected 'GET'",
|
|
|
|
2)
|
2020-08-22 01:50:38 +03:00
|
|
|
}
|
|
|
|
if get_tokens[2].trim_space() != 'HTTP/1.1' {
|
2020-12-02 06:02:53 +03:00
|
|
|
return error_with_code("unexpected request $get_tokens, expected 'HTTP/1.1'",
|
|
|
|
3)
|
2020-08-22 01:50:38 +03:00
|
|
|
}
|
|
|
|
mut seckey := ''
|
|
|
|
mut flags := []Flag{}
|
|
|
|
mut key := ''
|
|
|
|
for i in 1 .. lines.len {
|
|
|
|
if lines[i].len <= 0 || lines[i] == '\r\n' {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
keys := lines[i].split(':')
|
|
|
|
match keys[0] {
|
|
|
|
'Upgrade', 'upgrade' {
|
|
|
|
flags << .has_upgrade
|
|
|
|
}
|
|
|
|
'Connection', 'connection' {
|
|
|
|
flags << .has_connection
|
|
|
|
}
|
|
|
|
'Sec-WebSocket-Key', 'sec-websocket-key' {
|
|
|
|
key = keys[1].trim_space()
|
|
|
|
s.logger.debug('server-> got key: $key')
|
2020-10-17 16:26:56 +03:00
|
|
|
seckey = create_key_challenge_response(key) ?
|
2020-08-22 01:50:38 +03:00
|
|
|
s.logger.debug('server-> challenge: $seckey, response: ${keys[1]}')
|
|
|
|
flags << .has_accept
|
|
|
|
}
|
|
|
|
else {
|
2020-12-04 03:52:26 +03:00
|
|
|
// we ignore other headers like protocol for now
|
2020-08-22 01:50:38 +03:00
|
|
|
}
|
|
|
|
}
|
2020-12-28 08:25:46 +03:00
|
|
|
unsafe { keys.free() }
|
2020-08-22 01:50:38 +03:00
|
|
|
}
|
|
|
|
if flags.len < 3 {
|
2020-11-06 17:10:05 +03:00
|
|
|
return error_with_code('invalid client handshake, $client_handshake', 4)
|
2020-08-22 01:50:38 +03:00
|
|
|
}
|
|
|
|
server_handshake := 'HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: $seckey\r\n\r\n'
|
|
|
|
server_client := &ServerClient{
|
|
|
|
resource_name: get_tokens[1]
|
|
|
|
client_key: key
|
|
|
|
client: c
|
|
|
|
server: s
|
|
|
|
}
|
|
|
|
unsafe {
|
|
|
|
lines.free()
|
|
|
|
flags.free()
|
|
|
|
get_tokens.free()
|
|
|
|
seckey.free()
|
|
|
|
key.free()
|
|
|
|
}
|
|
|
|
return server_handshake, server_client
|
|
|
|
}
|
|
|
|
|
2020-12-04 03:52:26 +03:00
|
|
|
// read_handshake_str returns the handshake response
|
2020-08-22 01:50:38 +03:00
|
|
|
fn (mut ws Client) read_handshake_str() ?string {
|
|
|
|
mut total_bytes_read := 0
|
|
|
|
mut msg := [1024]byte{}
|
|
|
|
mut buffer := [1]byte{}
|
|
|
|
for total_bytes_read < 1024 {
|
2020-11-15 23:54:47 +03:00
|
|
|
bytes_read := ws.socket_read_ptr(byteptr(&buffer), 1) ?
|
2020-08-22 01:50:38 +03:00
|
|
|
if bytes_read == 0 {
|
2020-11-06 17:10:05 +03:00
|
|
|
return error_with_code('unexpected no response from handshake', 5)
|
2020-08-22 01:50:38 +03:00
|
|
|
}
|
|
|
|
msg[total_bytes_read] = buffer[0]
|
|
|
|
total_bytes_read++
|
|
|
|
if total_bytes_read > 5 && msg[total_bytes_read - 1] == `\n` && msg[total_bytes_read -
|
2021-01-16 04:12:17 +03:00
|
|
|
2] == `\r` && msg[total_bytes_read - 3] == `\n` && msg[total_bytes_read - 4] == `\r`
|
|
|
|
{
|
2020-08-22 01:50:38 +03:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2020-08-22 13:29:15 +03:00
|
|
|
res := msg[..total_bytes_read].bytestr()
|
2020-08-22 01:50:38 +03:00
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2020-12-04 03:52:26 +03:00
|
|
|
// read_handshake reads the handshake result and check if valid
|
2020-08-22 01:50:38 +03:00
|
|
|
fn (mut ws Client) read_handshake(seckey string) ? {
|
2020-10-17 16:26:56 +03:00
|
|
|
mut msg := ws.read_handshake_str() ?
|
|
|
|
ws.check_handshake_response(msg, seckey) ?
|
2020-12-28 08:25:46 +03:00
|
|
|
unsafe { msg.free() }
|
2020-08-22 01:50:38 +03:00
|
|
|
}
|
|
|
|
|
2020-12-02 06:02:53 +03:00
|
|
|
// check_handshake_response checks the response from handshake and returns
|
2020-12-04 03:52:26 +03:00
|
|
|
// the response and secure key provided by the websocket client
|
2020-10-17 16:26:56 +03:00
|
|
|
fn (mut ws Client) check_handshake_response(handshake_response string, seckey string) ? {
|
2020-08-22 01:50:38 +03:00
|
|
|
ws.debug_log('handshake response:\n$handshake_response')
|
|
|
|
lines := handshake_response.split_into_lines()
|
|
|
|
header := lines[0]
|
|
|
|
if !header.starts_with('HTTP/1.1 101') && !header.starts_with('HTTP/1.0 101') {
|
2020-12-02 06:02:53 +03:00
|
|
|
return error_with_code('handshake_handler: invalid HTTP status response code, $header',
|
|
|
|
6)
|
2020-08-22 01:50:38 +03:00
|
|
|
}
|
|
|
|
for i in 1 .. lines.len {
|
|
|
|
if lines[i].len <= 0 || lines[i] == '\r\n' {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
keys := lines[i].split(':')
|
|
|
|
match keys[0] {
|
|
|
|
'Upgrade', 'upgrade' {
|
|
|
|
ws.flags << .has_upgrade
|
|
|
|
}
|
|
|
|
'Connection', 'connection' {
|
|
|
|
ws.flags << .has_connection
|
|
|
|
}
|
|
|
|
'Sec-WebSocket-Accept', 'sec-websocket-accept' {
|
|
|
|
ws.debug_log('seckey: $seckey')
|
2020-10-17 16:26:56 +03:00
|
|
|
challenge := create_key_challenge_response(seckey) ?
|
2020-08-22 01:50:38 +03:00
|
|
|
ws.debug_log('challenge: $challenge, response: ${keys[1]}')
|
|
|
|
if keys[1].trim_space() != challenge {
|
2020-12-02 06:02:53 +03:00
|
|
|
return error_with_code('handshake_handler: Sec-WebSocket-Accept header does not match computed sha1/base64 response.',
|
|
|
|
7)
|
2020-08-22 01:50:38 +03:00
|
|
|
}
|
|
|
|
ws.flags << .has_accept
|
2020-12-28 08:25:46 +03:00
|
|
|
unsafe { challenge.free() }
|
2020-08-22 01:50:38 +03:00
|
|
|
}
|
|
|
|
else {}
|
|
|
|
}
|
2020-12-28 08:25:46 +03:00
|
|
|
unsafe { keys.free() }
|
2020-08-22 01:50:38 +03:00
|
|
|
}
|
2020-12-28 08:25:46 +03:00
|
|
|
unsafe { lines.free() }
|
2020-08-22 01:50:38 +03:00
|
|
|
if ws.flags.len < 3 {
|
2020-10-17 16:26:56 +03:00
|
|
|
ws.close(1002, 'invalid websocket HTTP headers') ?
|
2020-11-06 17:10:05 +03:00
|
|
|
return error_with_code('invalid websocket HTTP headers', 8)
|
2020-08-22 01:50:38 +03:00
|
|
|
}
|
|
|
|
}
|