x.websocket: add header to client (#9517)

pull/9536/head
Anton Zavodchikov 2021-03-30 14:39:07 +05:00 committed by GitHub
parent 3ced970b17
commit 9b9ef5fe1b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 1 deletions

View File

@ -22,7 +22,12 @@ fn (mut ws Client) handshake() ? {
sb.write_string('\r\nUpgrade: websocket\r\nConnection: Upgrade\r\n')
sb.write_string('Sec-WebSocket-Key: ')
sb.write_string(seckey)
sb.write_string('\r\nSec-WebSocket-Version: 13\r\n\r\n')
sb.write_string('\r\nSec-WebSocket-Version: 13')
for key in ws.header.keys() {
val := ws.header.values_str(key).join(',')
sb.write_string('\r\n$key:$val')
}
sb.write_string('\r\n\r\n')
handshake := sb.str()
defer {
unsafe { handshake.free() }

View File

@ -4,6 +4,7 @@
module websocket
import net
import net.http
import x.openssl
import net.urllib
import time
@ -30,6 +31,7 @@ pub:
uri Uri // uri of current connection
id string // unique id of client
pub mut:
header http.Header // headers that will be passed when connecting
conn &net.TcpConn // underlying TCP socket connection
nonce_size int = 16 // size of nounce used for masking
panic_on_callback bool // set to true of callbacks can panic
@ -85,6 +87,7 @@ pub fn new_client(address string) ?&Client {
uri: uri
state: .closed
id: rand.uuid_v4()
header: http.new_header()
}
}
@ -486,5 +489,6 @@ pub fn (c &Client) free() {
c.error_callbacks.free()
c.open_callbacks.free()
c.close_callbacks.free()
c.header.free()
}
}