gen oid instead of hardcoded Sec-WebSocket-Key (#2)

* gen oid instead of hardcoded Sec-WebSocket-Key

* encode key, make proper lenght
This commit is contained in:
Volodymyr Melnychuk 2019-06-13 22:20:20 +03:00 committed by GitHub
parent 45d1ddff1e
commit ca34a89433
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,4 +1,4 @@
import strutils, streams, random, securehash, base64, uri, strformat, nativesockets
import strutils, streams, random, securehash, base64, uri, strformat, nativesockets, oids, base64
when not declaredInScope(newsUseChronos):
# Currently chronos is second class citizen. To use this library in chronos-based
@ -129,12 +129,13 @@ proc newWebSocket*(url: string): Future[WebSocket] {.async.} =
ws.transp = newAsyncSocket()
await ws.transp.connect(uri.hostname, port)
let secKey = encode($genOid())[16..^1]
await ws.transp.send &"""GET {url} HTTP/1.1
Host: {uri.hostname}:{$port}
Connection: Upgrade
Upgrade: websocket
Sec-WebSocket-Version: 13
Sec-WebSocket-Key: JCSoP2Cyk0cHZkKAit5DjA==
Sec-WebSocket-Key: {secKey}
Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits
"""