2020-04-08 19:44:30 +02:00
|
|
|
module websocket
|
2020-04-08 14:22:31 +02:00
|
|
|
|
2020-05-26 12:50:37 +02:00
|
|
|
fn (mut ws Client) read_handshake(seckey string) {
|
2020-07-22 16:31:22 +02:00
|
|
|
ws.log.debug('reading handshake...')
|
2020-04-08 14:22:31 +02:00
|
|
|
mut bytes_read := 0
|
2020-04-24 07:32:51 +02:00
|
|
|
max_buffer := 1024
|
2020-04-08 14:22:31 +02:00
|
|
|
buffer_size := 1
|
|
|
|
mut buffer := malloc(max_buffer)
|
2020-07-22 17:42:57 +02:00
|
|
|
for bytes_read < max_buffer - 1 {
|
2020-07-03 18:10:10 +02:00
|
|
|
mut res := 0
|
|
|
|
unsafe {
|
|
|
|
res = ws.read_from_server(buffer + bytes_read, buffer_size)
|
|
|
|
}
|
2020-05-17 13:51:18 +02:00
|
|
|
if res == 0 || res == -1 {
|
2020-07-22 16:31:22 +02:00
|
|
|
ws.log.fatal('read_handshake: Failed to read handshake.')
|
2020-04-08 14:22:31 +02:00
|
|
|
}
|
2020-07-22 19:28:53 +02:00
|
|
|
if unsafe {buffer[bytes_read] == `\n` &&
|
2020-07-22 16:31:22 +02:00
|
|
|
buffer[bytes_read - 1] == `\r` && buffer[bytes_read - 2] == `\n` &&
|
2020-07-22 19:28:53 +02:00
|
|
|
buffer[bytes_read - 3] == `\r`} {
|
2020-04-08 14:22:31 +02:00
|
|
|
break
|
|
|
|
}
|
|
|
|
bytes_read += buffer_size
|
|
|
|
}
|
2020-07-22 19:28:53 +02:00
|
|
|
unsafe {
|
|
|
|
buffer[max_buffer - 1] = `\0`
|
|
|
|
}
|
2020-08-22 12:29:15 +02:00
|
|
|
ws.handshake_handler(unsafe{ byteptr(buffer).vstring_with_len(max_buffer-1) }, seckey)
|
2020-04-08 14:22:31 +02:00
|
|
|
}
|
|
|
|
|
2020-05-26 12:50:37 +02:00
|
|
|
fn (mut ws Client) handshake_handler(handshake_response, seckey string) {
|
2020-07-22 16:31:22 +02:00
|
|
|
ws.log.debug('handshake_handler:\r\n$handshake_response')
|
2020-04-08 14:22:31 +02:00
|
|
|
lines := handshake_response.split_into_lines()
|
|
|
|
header := lines[0]
|
2020-05-26 12:50:37 +02:00
|
|
|
if !header.starts_with('HTTP/1.1 101') && !header.starts_with('HTTP/1.0 101') {
|
2020-07-22 16:31:22 +02:00
|
|
|
ws.log.fatal('handshake_handler: invalid HTTP status response code')
|
2020-04-08 14:22:31 +02:00
|
|
|
}
|
2020-05-26 12:50:37 +02:00
|
|
|
for i in 1 .. lines.len {
|
|
|
|
if lines[i].len <= 0 || lines[i] == '\r\n' {
|
2020-04-08 14:22:31 +02:00
|
|
|
continue
|
|
|
|
}
|
2020-05-26 12:50:37 +02:00
|
|
|
keys := lines[i].split(':')
|
2020-04-08 14:22:31 +02:00
|
|
|
match keys[0] {
|
2020-05-26 12:50:37 +02:00
|
|
|
'Upgrade', 'upgrade' {
|
2020-04-24 07:32:51 +02:00
|
|
|
ws.flags << .has_upgrade
|
2020-04-08 14:22:31 +02:00
|
|
|
}
|
2020-05-26 12:50:37 +02:00
|
|
|
'Connection', 'connection' {
|
2020-04-24 07:32:51 +02:00
|
|
|
ws.flags << .has_connection
|
2020-04-08 14:22:31 +02:00
|
|
|
}
|
2020-05-26 12:50:37 +02:00
|
|
|
'Sec-WebSocket-Accept', 'sec-websocket-accept' {
|
2020-07-22 16:31:22 +02:00
|
|
|
ws.log.debug('comparing hashes')
|
|
|
|
ws.log.debug('seckey: $seckey')
|
2020-04-24 07:32:51 +02:00
|
|
|
challenge := create_key_challenge_response(seckey)
|
2020-07-22 16:31:22 +02:00
|
|
|
ws.log.debug('challenge: $challenge')
|
|
|
|
ws.log.debug('response: ${keys[1]}')
|
2020-04-24 07:32:51 +02:00
|
|
|
if keys[1].trim_space() != challenge {
|
2020-07-22 16:31:22 +02:00
|
|
|
ws.log.error('handshake_handler: Sec-WebSocket-Accept header does not match computed sha1/base64 response.')
|
2020-04-08 14:22:31 +02:00
|
|
|
}
|
2020-04-24 07:32:51 +02:00
|
|
|
ws.flags << .has_accept
|
2020-04-08 14:22:31 +02:00
|
|
|
unsafe {
|
2020-04-24 07:32:51 +02:00
|
|
|
challenge.free()
|
2020-04-08 14:22:31 +02:00
|
|
|
}
|
2020-05-26 12:50:37 +02:00
|
|
|
}
|
|
|
|
else {}
|
2020-04-08 14:22:31 +02:00
|
|
|
}
|
|
|
|
unsafe {
|
|
|
|
keys.free()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ws.flags.len < 3 {
|
2020-05-26 12:50:37 +02:00
|
|
|
ws.close(1002, 'invalid websocket HTTP headers')
|
2020-07-22 16:31:22 +02:00
|
|
|
ws.log.error('invalid websocket HTTP headers')
|
2020-04-08 14:22:31 +02:00
|
|
|
}
|
2020-07-22 16:31:22 +02:00
|
|
|
ws.log.info('handshake successful!')
|
2020-04-08 14:22:31 +02:00
|
|
|
unsafe {
|
|
|
|
handshake_response.free()
|
|
|
|
lines.free()
|
|
|
|
header.free()
|
|
|
|
}
|
2020-05-26 12:50:37 +02:00
|
|
|
}
|