fixes for asynchttpserver

This commit is contained in:
Andreas Rumpf
2014-04-13 00:39:15 +02:00
parent 455c3c19ca
commit fe387888bd
3 changed files with 15 additions and 8 deletions

View File

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

View File

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

View File

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