1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

all: change optional to result of io (#16075)

This commit is contained in:
yuyi
2022-10-16 14:28:57 +08:00
committed by GitHub
parent 6e46933c55
commit f6844e9766
187 changed files with 1885 additions and 1874 deletions

View File

@ -5,7 +5,7 @@ import encoding.base64
import strings
// handshake manages the websocket handshake process
fn (mut ws Client) handshake() ? {
fn (mut ws Client) handshake() ! {
nonce := get_nonce(ws.nonce_size)
seckey := base64.encode_str(nonce)
mut sb := strings.new_builder(1024)
@ -34,21 +34,21 @@ fn (mut ws Client) handshake() ? {
}
handshake_bytes := handshake.bytes()
ws.debug_log('sending handshake: $handshake')
ws.socket_write(handshake_bytes)?
ws.read_handshake(seckey)?
ws.socket_write(handshake_bytes)!
ws.read_handshake(seckey)!
unsafe { handshake_bytes.free() }
}
// handle_server_handshake manages websocket server handshake process
fn (mut s Server) handle_server_handshake(mut c Client) ?(string, &ServerClient) {
msg := c.read_handshake_str()?
handshake_response, client := s.parse_client_handshake(msg, mut c)?
fn (mut s Server) handle_server_handshake(mut c Client) !(string, &ServerClient) {
msg := c.read_handshake_str()!
handshake_response, client := s.parse_client_handshake(msg, mut c)!
unsafe { msg.free() }
return handshake_response, client
}
// parse_client_handshake parses result from handshake process
fn (mut s Server) parse_client_handshake(client_handshake string, mut c Client) ?(string, &ServerClient) {
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(' ')
@ -81,7 +81,7 @@ fn (mut s Server) parse_client_handshake(client_handshake string, mut c Client)
'Sec-WebSocket-Key', 'sec-websocket-key' {
key = keys[1].trim_space()
s.logger.debug('server-> got key: $key')
seckey = create_key_challenge_response(key)?
seckey = create_key_challenge_response(key)!
s.logger.debug('server-> challenge: $seckey, response: ${keys[1]}')
flags << .has_accept
}
@ -112,12 +112,12 @@ fn (mut s Server) parse_client_handshake(client_handshake string, mut c Client)
}
// read_handshake_str returns the handshake response
fn (mut ws Client) read_handshake_str() ?string {
fn (mut ws Client) read_handshake_str() !string {
mut total_bytes_read := 0
mut msg := [1024]u8{}
mut buffer := [1]u8{}
for total_bytes_read < 1024 {
bytes_read := ws.socket_read_ptr(&buffer[0], 1)?
bytes_read := ws.socket_read_ptr(&buffer[0], 1)!
if bytes_read == 0 {
return error_with_code('unexpected no response from handshake', 5)
}
@ -134,15 +134,15 @@ fn (mut ws Client) read_handshake_str() ?string {
}
// read_handshake reads the handshake result and check if valid
fn (mut ws Client) read_handshake(seckey string) ? {
mut msg := ws.read_handshake_str()?
ws.check_handshake_response(msg, seckey)?
fn (mut ws Client) read_handshake(seckey string) ! {
mut msg := ws.read_handshake_str()!
ws.check_handshake_response(msg, seckey)!
unsafe { msg.free() }
}
// check_handshake_response checks the response from handshake and returns
// the response and secure key provided by the websocket client
fn (mut ws Client) check_handshake_response(handshake_response string, seckey string) ? {
fn (mut ws Client) check_handshake_response(handshake_response string, seckey string) ! {
ws.debug_log('handshake response:\n$handshake_response')
lines := handshake_response.split_into_lines()
header := lines[0]
@ -164,7 +164,7 @@ fn (mut ws Client) check_handshake_response(handshake_response string, seckey st
}
'Sec-WebSocket-Accept', 'sec-websocket-accept' {
ws.debug_log('seckey: $seckey')
challenge := create_key_challenge_response(seckey)?
challenge := create_key_challenge_response(seckey)!
ws.debug_log('challenge: $challenge, response: ${keys[1]}')
if keys[1].trim_space() != challenge {
return error_with_code('handshake_handler: Sec-WebSocket-Accept header does not match computed sha1/base64 response.',
@ -179,7 +179,7 @@ fn (mut ws Client) check_handshake_response(handshake_response string, seckey st
}
unsafe { lines.free() }
if ws.flags.len < 3 {
ws.close(1002, 'invalid websocket HTTP headers')?
ws.close(1002, 'invalid websocket HTTP headers')!
return error_with_code('invalid websocket HTTP headers', 8)
}
}