Fix fragmented message sending (#116)

We previously crashed when sending things over 1mb
This commit is contained in:
Tanguy 2022-06-30 17:14:07 +02:00 committed by GitHub
parent 283a9bb1fc
commit 92d350fe88
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 6 deletions

View File

@ -783,11 +783,11 @@ suite "Test Payload":
asyncTest "Send text message with multiple frames":
const FrameSize = 3000
let testData = rndStr(FrameSize * 3)
let testData = rndStr(FrameSize * 3 + 100)
proc handle(request: HttpRequest) {.async.} =
check request.uri.path == WSPath
let server = WSServer.new(protos = ["proto"])
let server = WSServer.new(protos = ["proto"], frameSize = FrameSize)
let ws = await server.handleRequest(request)
let res = await ws.recvMsg()

View File

@ -37,21 +37,21 @@ proc writeMessage*(ws: WSSession,
let maxSize = ws.frameSize
var i = 0
while ws.readyState notin {ReadyState.Closing}:
let len = min(data.len, maxSize)
let canSend = min(data.len - i, maxSize)
let frame = Frame(
fin: if (len + i >= data.len): true else: false,
fin: if (canSend + i >= data.len): true else: false,
rsv1: false,
rsv2: false,
rsv3: false,
opcode: if i > 0: Opcode.Cont else: opcode, # fragments have to be `Continuation` frames
mask: ws.masked,
data: data[i ..< len + i],
data: data[i ..< canSend + i],
maskKey: maskKey)
let encoded = await frame.encode(extensions)
await ws.stream.writer.write(encoded)
i += len
i += canSend
if i >= data.len:
break