From f3e3d7e0c5f2987587f1a1197e84073f9f38734d Mon Sep 17 00:00:00 2001 From: Leah Lundqvist Date: Fri, 24 Apr 2020 07:32:51 +0200 Subject: [PATCH] websocket: update to work with latest V --- vlib/net/websocket/handshake.v | 23 +++++++++++++---------- vlib/net/websocket/ssl.v | 14 +++++++------- vlib/net/websocket/utf8.v | 2 +- vlib/net/websocket/utils.v | 2 +- vlib/net/websocket/ws.v | 12 +++++++----- 5 files changed, 29 insertions(+), 24 deletions(-) diff --git a/vlib/net/websocket/handshake.v b/vlib/net/websocket/handshake.v index 9450261b4c..8fe3beb0b3 100644 --- a/vlib/net/websocket/handshake.v +++ b/vlib/net/websocket/handshake.v @@ -3,7 +3,7 @@ module websocket fn (ws mut Client) read_handshake(seckey string){ l.d("reading handshake...") mut bytes_read := 0 - max_buffer := 256 + max_buffer := 1024 buffer_size := 1 mut buffer := malloc(max_buffer) @@ -37,21 +37,24 @@ fn (ws mut Client) handshake_handler(handshake_response, seckey string){ keys := lines[i].split(":") match keys[0] { - "Upgrade" { - ws.flags << Flag.has_upgrade + "Upgrade", "upgrade" { + ws.flags << .has_upgrade } - "Connection" { - ws.flags << Flag.has_connection + "Connection", "connection" { + ws.flags << .has_connection } - "Sec-WebSocket-Accept" { + "Sec-WebSocket-Accept", "sec-websocket-accept" { l.d("comparing hashes") - response := create_key_challenge_response(seckey) - if keys[1].trim_space() != response { + l.d("seckey: ${seckey}") + challenge := create_key_challenge_response(seckey) + l.d("challenge: ${challenge}") + l.d("response: ${keys[1]}") + if keys[1].trim_space() != challenge { l.e("handshake_handler: Sec-WebSocket-Accept header does not match computed sha1/base64 response.") } - ws.flags << Flag.has_accept + ws.flags << .has_accept unsafe { - response.free() + challenge.free() } } else {} } diff --git a/vlib/net/websocket/ssl.v b/vlib/net/websocket/ssl.v index e9323c6eb0..09036efa41 100644 --- a/vlib/net/websocket/ssl.v +++ b/vlib/net/websocket/ssl.v @@ -10,9 +10,9 @@ struct C.SSL struct C.SSL_METHOD fn C.SSL_load_error_strings() fn C.SSL_library_init() -fn C.SSLv23_client_method() &SSL_METHOD -fn C.SSL_CTX_new() &SSL_CTX -fn C.SSL_new() &SSL +fn C.SSLv23_client_method() &C.SSL_METHOD +fn C.SSL_CTX_new() &C.SSL_CTX +fn C.SSL_new() &C.SSL fn C.SSL_set_fd() int fn C.SSL_connect() int fn C.SSL_shutdown() @@ -26,21 +26,21 @@ fn (ws mut Client) connect_ssl(){ C.SSL_load_error_strings() C.SSL_library_init() - ws.sslctx = SSL_CTX_new(SSLv23_client_method()) + ws.sslctx = C.SSL_CTX_new(C.SSLv23_client_method()) if ws.sslctx == C.NULL { l.f("Couldn't get ssl context") } - ws.ssl = SSL_new(ws.sslctx) + ws.ssl = C.SSL_new(ws.sslctx) if ws.ssl == C.NULL { l.f("Couldn't create OpenSSL instance.") } - if SSL_set_fd(ws.ssl, ws.socket.sockfd) != 1 { + if C.SSL_set_fd(ws.ssl, ws.socket.sockfd) != 1 { l.f("Couldn't assign ssl to socket.") } - if SSL_connect(ws.ssl) != 1 { + if C.SSL_connect(ws.ssl) != 1 { l.f("Couldn't connect using SSL.") } } \ No newline at end of file diff --git a/vlib/net/websocket/utf8.v b/vlib/net/websocket/utf8.v index 85395df6d8..0aee273285 100644 --- a/vlib/net/websocket/utf8.v +++ b/vlib/net/websocket/utf8.v @@ -49,7 +49,7 @@ fn (s mut Utf8State) seq(r0 bool, r1 bool, is_tail bool) bool { fn (s mut Utf8State) next_state (c byte) { //sequence 1 if s.index == 0 { - if ((c >= 0x00 + 1 && c <= 0x7F) || c == 0x00) { + if (c >= 0x00 + 1 && c <= 0x7F) || c == 0x00 { return } s.index++ diff --git a/vlib/net/websocket/utils.v b/vlib/net/websocket/utils.v index 3863dec0d4..76df52d8f6 100644 --- a/vlib/net/websocket/utils.v +++ b/vlib/net/websocket/utils.v @@ -48,7 +48,7 @@ fn create_key_challenge_response(seckey string) string { fn get_nonce() string { mut nonce := []byte alphanum := "0123456789ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvwxyz" - for i in 0..16 { + for i in 0..18 { nonce << alphanum[rand.next(61)] } return string(byteptr(nonce.data)) diff --git a/vlib/net/websocket/ws.v b/vlib/net/websocket/ws.v index 3e320d54ee..9b4e8b1737 100644 --- a/vlib/net/websocket/ws.v +++ b/vlib/net/websocket/ws.v @@ -6,7 +6,7 @@ import ( encoding.base64 eventbus sync - logger + net.websocket.logger ) const ( @@ -144,8 +144,10 @@ pub fn (ws mut Client) connect() int { ai_family := C.AF_INET ai_socktype := C.SOCK_STREAM + l.d("handshake header:") handshake := "GET ${uri.resource}${uri.querystring} HTTP/1.1\r\nHost: ${uri.hostname}:${uri.port}\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Key: ${seckey}\r\nSec-WebSocket-Version: 13\r\n\r\n" - + l.d(handshake) + socket := net.new_socket(ai_family, ai_socktype, 0) or { l.f(err) return -1 @@ -215,10 +217,10 @@ pub fn (ws mut Client) close(code int, message string){ } if ws.ssl != C.NULL { - SSL_shutdown(ws.ssl) - SSL_free(ws.ssl) + C.SSL_shutdown(ws.ssl) + C.SSL_free(ws.ssl) if ws.sslctx != C.NULL { - SSL_CTX_free(ws.sslctx) + C.SSL_CTX_free(ws.sslctx) } } else { if C.shutdown(ws.socket.sockfd, C.SHUT_WR) == -1 {