From 328a235f94340d49fc314ddead0cf80546fdb8fa Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Mon, 1 Jun 2020 20:31:45 +0300 Subject: [PATCH] websocket: add Client.nonce_size field --- vlib/net/websocket/utils.v | 10 +++++----- vlib/net/websocket/ws.v | 3 ++- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/vlib/net/websocket/utils.v b/vlib/net/websocket/utils.v index c1bfec04de..213ac77fc5 100644 --- a/vlib/net/websocket/utils.v +++ b/vlib/net/websocket/utils.v @@ -42,11 +42,11 @@ fn create_key_challenge_response(seckey string) string { return b64 } -fn get_nonce() string { - mut nonce := []byte{} +fn get_nonce(nonce_size int) string { + mut nonce := []byte{len: nonce_size, cap: nonce_size} alphanum := '0123456789ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvwxyz' - for _ in 0 .. 18 { - nonce << alphanum[rand.next(61)] + for i in 0 .. nonce_size { + nonce[i] = alphanum[rand.next(61)] } - return string(byteptr(nonce.data)) + return tos(nonce.data, nonce.len).clone() } diff --git a/vlib/net/websocket/ws.v b/vlib/net/websocket/ws.v index 489f873647..1398e3c047 100644 --- a/vlib/net/websocket/ws.v +++ b/vlib/net/websocket/ws.v @@ -19,6 +19,7 @@ pub struct Client { // cwebsocket_subprotocol *subprotocol; // cwebsocket_subprotocol *subprotocols[]; mut: + nonce_size int = 18 // you can try 16 too lock &sync.Mutex = sync.new_mutex() write_lock &sync.Mutex = sync.new_mutex() state State @@ -137,7 +138,7 @@ pub fn (mut ws Client) connect() int { ws.state = .connecting ws.lock.unlock() uri := ws.parse_uri() - nonce := get_nonce() + nonce := get_nonce(ws.nonce_size) seckey := base64.encode(nonce) ai_family := C.AF_INET ai_socktype := C.SOCK_STREAM