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

@ -37,35 +37,35 @@ const (
)
// validate_client validates client frame rules from RFC6455
pub fn (mut ws Client) validate_frame(frame &Frame) ? {
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')?
ws.close(1002, 'rsv cannot be other than 0, not negotiated')!
return error('rsv cannot be other than 0, not negotiated')
}
if (int(frame.opcode) >= 3 && int(frame.opcode) <= 7)
|| (int(frame.opcode) >= 11 && int(frame.opcode) <= 15) {
ws.close(1002, 'use of reserved opcode')?
ws.close(1002, 'use of reserved opcode')!
return error('use of reserved opcode')
}
if frame.has_mask && !ws.is_server {
// server should never send masked frames
// to client, close connection
ws.close(1002, 'client got masked frame')?
ws.close(1002, 'client got masked frame')!
return error('client sent masked frame')
}
if is_control_frame(frame.opcode) {
if !frame.fin {
ws.close(1002, 'control message must not be fragmented')?
ws.close(1002, 'control message must not be fragmented')!
return error('unexpected control frame with no fin')
}
if frame.payload_len > 125 {
ws.close(1002, 'control frames must not exceed 125 bytes')?
ws.close(1002, 'control frames must not exceed 125 bytes')!
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'
ws.close(1002, err_msg)?
ws.close(1002, err_msg)!
return error(err_msg)
}
}
@ -81,7 +81,7 @@ fn is_data_frame(opcode OPCode) bool {
}
// read_payload reads the message payload from the socket
fn (mut ws Client) read_payload(frame &Frame) ?[]u8 {
fn (mut ws Client) read_payload(frame &Frame) ![]u8 {
if frame.payload_len == 0 {
return []u8{}
}
@ -89,7 +89,7 @@ fn (mut ws Client) read_payload(frame &Frame) ?[]u8 {
mut read_buf := [1]u8{}
mut bytes_read := 0
for bytes_read < frame.payload_len {
len := ws.socket_read_ptr(&read_buf[0], 1)?
len := ws.socket_read_ptr(&read_buf[0], 1)!
if len != 1 {
return error('expected read all message, got zero')
}
@ -109,21 +109,21 @@ fn (mut ws Client) read_payload(frame &Frame) ?[]u8 {
// validate_utf_8 validates payload for valid utf8 encoding
// - Future implementation needs to support fail fast utf errors for strict autobahn conformance
fn (mut ws Client) validate_utf_8(opcode OPCode, payload []u8) ? {
fn (mut ws Client) validate_utf_8(opcode OPCode, payload []u8) ! {
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')?
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 {
pub fn (mut ws Client) read_next_message() !Message {
for {
frame := ws.parse_frame_header()?
ws.validate_frame(&frame)?
frame_payload := ws.read_payload(&frame)?
frame := ws.parse_frame_header()!
ws.validate_frame(&frame)!
frame_payload := ws.read_payload(&frame)!
if is_control_frame(frame.opcode) {
// Control frames can interject other frames
// and need to be returned immediately
@ -161,12 +161,12 @@ pub fn (mut ws Client) read_next_message() ?Message {
ws.fragments = []
}
if is_data_frame(frame.opcode) {
ws.close(0, '')?
ws.close(0, '')!
return error('Unexpected frame opcode')
}
payload := ws.payload_from_fragments(frame_payload)?
payload := ws.payload_from_fragments(frame_payload)!
opcode := ws.opcode_from_fragments()
ws.validate_utf_8(opcode, payload)?
ws.validate_utf_8(opcode, payload)!
msg := Message{
opcode: opcode
payload: payload.clone()
@ -177,11 +177,11 @@ pub fn (mut ws Client) read_next_message() ?Message {
}
return msg
}
return none
return error('none')
}
// payload_from_fragments returs the whole paylaod from fragmented message
fn (ws Client) payload_from_fragments(fin_payload []u8) ?[]u8 {
fn (ws Client) payload_from_fragments(fin_payload []u8) ![]u8 {
mut total_size := 0
for f in ws.fragments {
if f.data.len > 0 {
@ -208,14 +208,14 @@ fn (ws Client) opcode_from_fragments() OPCode {
}
// parse_frame_header parses next message by decoding the incoming frames
pub fn (mut ws Client) parse_frame_header() ?Frame {
pub fn (mut ws Client) parse_frame_header() !Frame {
mut buffer := [256]u8{}
mut bytes_read := 0
mut frame := Frame{}
mut rbuff := [1]u8{}
mut mask_end_byte := 0
for ws.state == .open {
read_bytes := ws.socket_read_ptr(&rbuff[0], 1)?
read_bytes := ws.socket_read_ptr(&rbuff[0], 1)!
if read_bytes == 0 {
// this is probably a timeout or close
continue