websocket: update to work with latest V

pull/4573/head
Leah Lundqvist 2020-04-24 07:32:51 +02:00 committed by GitHub
parent 998fc8fc07
commit f3e3d7e0c5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 29 additions and 24 deletions

View File

@ -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 {}
}

View File

@ -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.")
}
}

View File

@ -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++

View File

@ -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))

View File

@ -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 {