websocket utf8: move utf8 functions from websocket to encoding.utf8, add utf8_test.v (4/4) (#5924)
parent
38aa5d6930
commit
b0d76c59f7
|
@ -1,8 +1,4 @@
|
||||||
module websocket
|
module utf8
|
||||||
|
|
||||||
pub fn utf8_validate_str(str string) bool {
|
|
||||||
return utf8_validate(str.str, str.len)
|
|
||||||
}
|
|
||||||
|
|
||||||
struct Utf8State {
|
struct Utf8State {
|
||||||
mut:
|
mut:
|
||||||
|
@ -11,7 +7,11 @@ mut:
|
||||||
failed bool
|
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{}
|
mut state := Utf8State{}
|
||||||
for i := 0; i < len; i++ {
|
for i := 0; i < len; i++ {
|
||||||
s := data[i]
|
s := data[i]
|
||||||
|
@ -22,7 +22,6 @@ pub fn utf8_validate(data byteptr, len int) bool {
|
||||||
if state.failed {
|
if state.failed {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
// i++ //fast forward
|
|
||||||
}
|
}
|
||||||
return !state.failed && state.subindex <= 0
|
return !state.failed && state.subindex <= 0
|
||||||
}
|
}
|
|
@ -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
|
||||||
import net.urllib
|
import net.urllib
|
||||||
import encoding.base64
|
import encoding.base64
|
||||||
|
import encoding.utf8
|
||||||
import eventbus
|
import eventbus
|
||||||
import sync
|
import sync
|
||||||
import log
|
import log
|
||||||
|
@ -478,7 +479,7 @@ pub fn (mut ws Client) read() int {
|
||||||
}
|
}
|
||||||
payload[payload_len] = `\0`
|
payload[payload_len] = `\0`
|
||||||
if frame.opcode == .text_frame && 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.log.error('malformed utf8 payload')
|
||||||
ws.send_error_event('Recieved malformed utf8.')
|
ws.send_error_event('Recieved malformed utf8.')
|
||||||
ws.close(1007, 'malformed utf8 payload')
|
ws.close(1007, 'malformed utf8 payload')
|
||||||
|
@ -563,7 +564,7 @@ pub fn (mut ws Client) read() int {
|
||||||
payload_len -= 2
|
payload_len -= 2
|
||||||
reason = string(&data[header_len])
|
reason = string(&data[header_len])
|
||||||
ws.log.info('Closing with reason: $reason & code: $code')
|
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.log.error('malformed utf8 payload')
|
||||||
ws.send_error_event('Recieved malformed utf8.')
|
ws.send_error_event('Recieved malformed utf8.')
|
||||||
ws.close(1007, 'malformed utf8 payload')
|
ws.close(1007, 'malformed utf8 payload')
|
||||||
|
|
Loading…
Reference in New Issue