From b0d76c59f70924dfd757d653cace891f0dfaf8a1 Mon Sep 17 00:00:00 2001 From: Carlos Esquerdo Bernat Date: Wed, 22 Jul 2020 17:36:24 +0200 Subject: [PATCH] websocket utf8: move utf8 functions from websocket to encoding.utf8, add utf8_test.v (4/4) (#5924) --- vlib/{net/websocket => encoding/utf8}/utf8.v | 13 ++++++------- vlib/encoding/utf8/utf8_test.v | 9 +++++++++ vlib/net/websocket/ws.v | 5 +++-- 3 files changed, 18 insertions(+), 9 deletions(-) rename vlib/{net/websocket => encoding/utf8}/utf8.v (90%) create mode 100644 vlib/encoding/utf8/utf8_test.v diff --git a/vlib/net/websocket/utf8.v b/vlib/encoding/utf8/utf8.v similarity index 90% rename from vlib/net/websocket/utf8.v rename to vlib/encoding/utf8/utf8.v index 18e2f1eab8..0311a1f6cd 100644 --- a/vlib/net/websocket/utf8.v +++ b/vlib/encoding/utf8/utf8.v @@ -1,8 +1,4 @@ -module websocket - -pub fn utf8_validate_str(str string) bool { - return utf8_validate(str.str, str.len) -} +module utf8 struct Utf8State { mut: @@ -11,7 +7,11 @@ mut: failed bool } -pub fn utf8_validate(data byteptr, len int) bool { +pub fn validate_str(str string) bool { + return validate(str.str, str.len) +} + +pub fn validate(data byteptr, len int) bool { mut state := Utf8State{} for i := 0; i < len; i++ { s := data[i] @@ -22,7 +22,6 @@ pub fn utf8_validate(data byteptr, len int) bool { if state.failed { return false } - // i++ //fast forward } return !state.failed && state.subindex <= 0 } diff --git a/vlib/encoding/utf8/utf8_test.v b/vlib/encoding/utf8/utf8_test.v new file mode 100644 index 0000000000..d94be6717e --- /dev/null +++ b/vlib/encoding/utf8/utf8_test.v @@ -0,0 +1,9 @@ +import encoding.utf8 { validate_str } + +fn test_validate_str() { + assert validate_str('añçá') == true + assert validate_str('\x61\xC3\xB1\xC3\xA7\xC3\xA1') == true + assert validate_str('\xC0\xC1') == false + assert validate_str('\xF5\xFF') == false + assert validate_str('\xE0\xEF') == false +} diff --git a/vlib/net/websocket/ws.v b/vlib/net/websocket/ws.v index 96c7e5c291..1693bbd30b 100644 --- a/vlib/net/websocket/ws.v +++ b/vlib/net/websocket/ws.v @@ -3,6 +3,7 @@ module websocket import net import net.urllib import encoding.base64 +import encoding.utf8 import eventbus import sync import log @@ -478,7 +479,7 @@ pub fn (mut ws Client) read() int { } payload[payload_len] = `\0` if frame.opcode == .text_frame && payload_len > 0 { - if !utf8_validate(payload, int(payload_len)) { + if !utf8.validate(payload, int(payload_len)) { ws.log.error('malformed utf8 payload') ws.send_error_event('Recieved malformed utf8.') ws.close(1007, 'malformed utf8 payload') @@ -563,7 +564,7 @@ pub fn (mut ws Client) read() int { payload_len -= 2 reason = string(&data[header_len]) ws.log.info('Closing with reason: $reason & code: $code') - if reason.len > 1 && !utf8_validate(reason.str, reason.len) { + if reason.len > 1 && !utf8.validate(reason.str, reason.len) { ws.log.error('malformed utf8 payload') ws.send_error_event('Recieved malformed utf8.') ws.close(1007, 'malformed utf8 payload')