mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
websocket utf8: move utf8 functions from websocket to encoding.utf8, add utf8_test.v (4/4) (#5924)
This commit is contained in:
parent
38aa5d6930
commit
b0d76c59f7
@ -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
|
||||
}
|
9
vlib/encoding/utf8/utf8_test.v
Normal file
9
vlib/encoding/utf8/utf8_test.v
Normal file
@ -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
|
||||
}
|
@ -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')
|
||||
|
Loading…
Reference in New Issue
Block a user