diff options
-rw-r--r-- | lib/pure/asyncdispatch.nim | 11 | ||||
-rw-r--r-- | lib/pure/asynchttpserver.nim | 11 | ||||
-rw-r--r-- | lib/pure/asyncnet.nim | 1 |
3 files changed, 15 insertions, 8 deletions
diff --git a/lib/pure/asyncdispatch.nim b/lib/pure/asyncdispatch.nim index 880458ee5..a8802dec3 100644 --- a/lib/pure/asyncdispatch.nim +++ b/lib/pure/asyncdispatch.nim @@ -353,11 +353,11 @@ when defined(windows) or defined(nimdoc): var retFuture = newFuture[string]() var dataBuf: TWSABuf - dataBuf.buf = newString(size) + dataBuf.buf = cast[cstring](alloc0(size)) dataBuf.len = size var bytesReceived: DWord - var flagsio = flags.dword + var flagsio = flags.DWord var ol = cast[PCustomOverlapped](alloc0(sizeof(TCustomOverlapped))) ol.data = TCompletionData(sock: socket, cb: proc (sock: TAsyncFD, bytesCount: DWord, errcode: TOSErrorCode) = @@ -367,10 +367,12 @@ when defined(windows) or defined(nimdoc): retFuture.complete("") else: var data = newString(bytesCount) + assert bytesCount <= size copyMem(addr data[0], addr dataBuf.buf[0], bytesCount) retFuture.complete($data) else: retFuture.fail(newException(EOS, osErrorMsg(errcode))) + dealloc dataBuf.buf ) let ret = WSARecv(socket.TSocketHandle, addr dataBuf, 1, addr bytesReceived, @@ -378,6 +380,7 @@ when defined(windows) or defined(nimdoc): if ret == -1: let err = osLastError() if err.int32 != ERROR_IO_PENDING: + dealloc dataBuf.buf retFuture.fail(newException(EOS, osErrorMsg(err))) dealloc(ol) elif ret == 0 and bytesReceived == 0 and dataBuf.buf[0] == '\0': @@ -401,7 +404,9 @@ when defined(windows) or defined(nimdoc): else: bytesReceived var data = newString(realSize) + assert realSize <= size copyMem(addr data[0], addr dataBuf.buf[0], realSize) + #dealloc dataBuf.buf retFuture.complete($data) # We don't deallocate ``ol`` here because even though this completed # immediately poll will still be notified about its completion and it will @@ -415,7 +420,7 @@ when defined(windows) or defined(nimdoc): var retFuture = newFuture[void]() var dataBuf: TWSABuf - dataBuf.buf = data + dataBuf.buf = data # since this is not used in a callback, this is fine dataBuf.len = data.len var bytesReceived, flags: DWord diff --git a/lib/pure/asynchttpserver.nim b/lib/pure/asynchttpserver.nim index 74b044e05..f1aa1a09d 100644 --- a/lib/pure/asynchttpserver.nim +++ b/lib/pure/asynchttpserver.nim @@ -92,8 +92,11 @@ proc processClient(client: PAsyncSocket, address: string, # GET /path HTTP/1.1 # Header: val # \n - var request = newRequest() + request.hostname = address + assert client != nil + request.client = client + # First line - GET /path HTTP/1.1 let line = await client.recvLine() # TODO: Timeouts. if line == "": @@ -102,6 +105,8 @@ proc processClient(client: PAsyncSocket, address: string, let lineParts = line.split(' ') if lineParts.len != 3: request.respond(Http400, "Invalid request. Got: " & line) + client.close() + return let reqMethod = lineParts[0] let path = lineParts[1] @@ -127,15 +132,11 @@ proc processClient(client: PAsyncSocket, address: string, except EInvalidValue: request.respond(Http400, "Invalid request protocol. Got: " & protocol) return - request.hostname = address - request.client = client case reqMethod.normalize of "get": await callback(request) else: - echo(reqMethod.repr) - echo(line.repr) request.respond(Http400, "Invalid request method. Got: " & reqMethod) # Persistent connections diff --git a/lib/pure/asyncnet.nim b/lib/pure/asyncnet.nim index daa6c8839..7cf2ad134 100644 --- a/lib/pure/asyncnet.nim +++ b/lib/pure/asyncnet.nim @@ -92,6 +92,7 @@ proc recv*(socket: PAsyncSocket, size: int, proc send*(socket: PAsyncSocket, data: string): PFuture[void] = ## Sends ``data`` to ``socket``. The returned future will complete once all ## data has been sent. + assert socket != nil result = send(socket.fd.TAsyncFD, data) proc acceptAddr*(socket: PAsyncSocket): |