From cc17f145c5a11fac32bd7c262ea9297b823729a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomas=20Hellstr=C3=B6m?= Date: Mon, 11 Jan 2021 09:03:10 +0100 Subject: [PATCH] websocket: make compile with autofree (#8023) --- vlib/crypto/sha1/sha1.v | 1 + vlib/net/urllib/urllib.v | 1 + vlib/x/websocket/handshake.v | 1 + vlib/x/websocket/websocket_client.v | 23 ++++++++++++++++------- 4 files changed, 19 insertions(+), 7 deletions(-) diff --git a/vlib/crypto/sha1/sha1.v b/vlib/crypto/sha1/sha1.v index 4c70cbc39a..3532a6e95e 100644 --- a/vlib/crypto/sha1/sha1.v +++ b/vlib/crypto/sha1/sha1.v @@ -54,6 +54,7 @@ pub fn new() &Digest { return d } +[manualfree] pub fn (mut d Digest) write(p_ []byte) int { nn := p_.len unsafe { diff --git a/vlib/net/urllib/urllib.v b/vlib/net/urllib/urllib.v index 3a3cd5edcb..d5a74e76b1 100644 --- a/vlib/net/urllib/urllib.v +++ b/vlib/net/urllib/urllib.v @@ -451,6 +451,7 @@ fn parse_request_uri(rawurl string) ?URL { // via_request is true, the URL is assumed to have arrived via an HTTP request, // in which case only absolute URLs or path-absolute relative URLs are allowed. // If via_request is false, all forms of relative URLs are allowed. +[manualfree] fn parse_url(rawurl string, via_request bool) ?URL { if string_contains_ctl_byte(rawurl) { return error(error_msg('parse_url: invalid control character in URL', rawurl)) diff --git a/vlib/x/websocket/handshake.v b/vlib/x/websocket/handshake.v index 010f6f3009..0ec28d9392 100644 --- a/vlib/x/websocket/handshake.v +++ b/vlib/x/websocket/handshake.v @@ -1,3 +1,4 @@ +[manualfree] module websocket import encoding.base64 diff --git a/vlib/x/websocket/websocket_client.v b/vlib/x/websocket/websocket_client.v index 927255899f..df3f7b083b 100644 --- a/vlib/x/websocket/websocket_client.v +++ b/vlib/x/websocket/websocket_client.v @@ -1,5 +1,6 @@ // websocket module implements websocket client and a websocket server // attribution: @thecoderr the author of original websocket client +[manualfree] module websocket import net @@ -103,7 +104,9 @@ pub fn (mut ws Client) connect() ? { // listen listens and processes incoming messages pub fn (mut ws Client) listen() ? { - ws.logger.info('Starting client listener, server($ws.is_server)...') + mut log := 'Starting client listener, server($ws.is_server)...' + ws.logger.info(log) + log.free() defer { ws.logger.info('Quit client listener, server($ws.is_server)...') if ws.state == .open { @@ -125,7 +128,9 @@ pub fn (mut ws Client) listen() ? { ws.debug_log('got message: $msg.opcode') match msg.opcode { .text_frame { - ws.debug_log('read: text') + log = 'read: text' + ws.debug_log(log) + log.free() ws.send_message_event(msg) unsafe { msg.free() } } @@ -157,7 +162,9 @@ pub fn (mut ws Client) listen() ? { } } .close { - ws.debug_log('read: close') + log = 'read: close' + ws.debug_log(log) + log.free() defer { ws.manage_clean_close() } @@ -219,7 +226,7 @@ pub fn (mut ws Client) pong() ? { // write_ptr writes len bytes provided a byteptr with a websocket messagetype pub fn (mut ws Client) write_ptr(bytes byteptr, payload_len int, code OPCode) ? { - ws.debug_log('write_ptr code: $code') + // ws.debug_log('write_ptr code: $code') if ws.state != .open || ws.conn.sock.handle < 1 { // todo: send error here later return error('trying to write on a closed socket!') @@ -402,12 +409,14 @@ fn (mut ws Client) send_control_frame(code OPCode, frame_typ string, payload []b // parse_uri parses the url to a Uri fn parse_uri(url string) ?&Uri { u := urllib.parse(url) ? - v := u.request_uri().split('?') + request_uri := u.request_uri() + v := request_uri.split('?') mut port := u.port() + uri := u.str() if port == '' { - port = if u.str().starts_with('ws://') { + port = if uri.starts_with('ws://') { '80' - } else if u.str().starts_with('wss://') { + } else if uri.starts_with('wss://') { '443' } else { u.port()