diff options
author | def <dennis@felsin9.de> | 2015-04-26 17:01:04 +0200 |
---|---|---|
committer | def <dennis@felsin9.de> | 2015-04-26 17:01:04 +0200 |
commit | 84315c6a9ce383620a49620cda98ad1158f92010 (patch) | |
tree | 7c1d185e1cd0c7c894c337ccb5843a8e84993bc6 /lib | |
parent | 1cb14f888a4534d8e71f1fb9a64ba172558e138e (diff) | |
download | Nim-84315c6a9ce383620a49620cda98ad1158f92010.tar.gz |
Revert "Introduce FutureVar[T] to make recvLineInto safer."
This reverts commit 72b4912c84b16644657f94e54105739cba4b2457.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/pure/asyncdispatch.nim | 31 | ||||
-rw-r--r-- | lib/pure/asynchttpserver.nim | 25 | ||||
-rw-r--r-- | lib/pure/asyncnet.nim | 38 |
3 files changed, 20 insertions, 74 deletions
diff --git a/lib/pure/asyncdispatch.nim b/lib/pure/asyncdispatch.nim index bec2632d5..a4d7a1632 100644 --- a/lib/pure/asyncdispatch.nim +++ b/lib/pure/asyncdispatch.nim @@ -145,8 +145,6 @@ type Future*[T] = ref object of FutureBase ## Typed future. value: T ## Stored value - FutureVar*[T] = distinct Future[T] - {.deprecated: [PFutureBase: FutureBase, PFuture: Future].} @@ -164,19 +162,6 @@ proc newFuture*[T](fromProc: string = "unspecified"): Future[T] = result.fromProc = fromProc currentID.inc() -proc newFutureVar*[T](fromProc = "unspecified"): FutureVar[T] = - ## Create a new ``FutureVar``. This Future type is ideally suited for - ## situations where you want to avoid unnecessary allocations of Futures. - ## - ## Specifying ``fromProc``, which is a string specifying the name of the proc - ## that this future belongs to, is a good habit as it helps with debugging. - result = FutureVar[T](newFuture[T](fromProc)) - -proc clean*[T](future: FutureVar[T]) = - ## Resets the ``finished`` status of ``future``. - Future[T](future).finished = false - Future[T](future).error = nil - proc checkFinished[T](future: Future[T]) = when not defined(release): if future.finished: @@ -209,15 +194,6 @@ proc complete*(future: Future[void]) = if future.cb != nil: future.cb() -proc complete*[T](future: FutureVar[T]) = - ## Completes a ``FutureVar``. - template fut: expr = Future[T](future) - checkFinished(fut) - assert(fut.error == nil) - fut.finished = true - if fut.cb != nil: - fut.cb() - proc fail*[T](future: Future[T], error: ref Exception) = ## Completes ``future`` with ``error``. #assert(not future.finished, "Future already finished, cannot finish twice.") @@ -288,13 +264,6 @@ proc readError*[T](future: Future[T]): ref Exception = else: raise newException(ValueError, "No error in future.") -proc mget*[T](future: FutureVar[T]): var T = - ## Returns a mutable value stored in ``future``. - ## - ## Unlike ``read``, this function will not raise an exception if the - ## Future has not been finished. - result = Future[T](future).value - proc finished*[T](future: Future[T]): bool = ## Determines whether ``future`` has completed. ## diff --git a/lib/pure/asynchttpserver.nim b/lib/pure/asynchttpserver.nim index 74e9e9f36..279cedb5d 100644 --- a/lib/pure/asynchttpserver.nim +++ b/lib/pure/asynchttpserver.nim @@ -148,8 +148,7 @@ proc processClient(client: AsyncSocket, address: string, var request: Request request.url = initUri() request.headers = newStringTable(modeCaseInsensitive) - var lineFut = newFutureVar[string]("asynchttpserver.processClient") - lineFut.mget() = newStringOfCap(80) + var line = newStringOfCap(80) var key, value = "" while not client.isClosed: @@ -162,15 +161,14 @@ proc processClient(client: AsyncSocket, address: string, request.client = client # First line - GET /path HTTP/1.1 - lineFut.mget().setLen(0) - lineFut.clean() - await client.recvLineInto(lineFut) # TODO: Timeouts. - if lineFut.mget == "": + line.setLen(0) + await client.recvLineInto(addr line) # TODO: Timeouts. + if line == "": client.close() return var i = 0 - for linePart in lineFut.mget.split(' '): + for linePart in line.split(' '): case i of 0: request.reqMethod.shallowCopy(linePart.normalize) of 1: parseUri(linePart, request.url) @@ -182,21 +180,20 @@ proc processClient(client: AsyncSocket, address: string, "Invalid request protocol. Got: " & linePart) continue else: - await request.respond(Http400, "Invalid request. Got: " & lineFut.mget) + await request.respond(Http400, "Invalid request. Got: " & line) continue inc i # Headers while true: i = 0 - lineFut.mget.setLen(0) - lineFut.clean() - await client.recvLineInto(lineFut) + line.setLen(0) + await client.recvLineInto(addr line) - if lineFut.mget == "": + if line == "": client.close(); return - if lineFut.mget == "\c\L": break - let (key, value) = parseHeader(lineFut.mget) + if line == "\c\L": break + let (key, value) = parseHeader(line) request.headers[key] = value if request.reqMethod == "post": diff --git a/lib/pure/asyncnet.nim b/lib/pure/asyncnet.nim index 62e85042f..a79f30ab3 100644 --- a/lib/pure/asyncnet.nim +++ b/lib/pure/asyncnet.nim @@ -307,13 +307,10 @@ proc accept*(socket: AsyncSocket, retFut.complete(future.read.client) return retFut -proc recvLineInto*(socket: AsyncSocket, resString: FutureVar[string], +proc recvLineInto*(socket: AsyncSocket, resString: ptr string, flags = {SocketFlag.SafeDisconn}) {.async.} = ## Reads a line of data from ``socket`` into ``resString``. ## - ## The ``resString`` future and the string value contained within must both - ## be initialised. - ## ## If a full line is read ``\r\L`` is not ## added to ``line``, however if solely ``\r\L`` is read then ``line`` ## will be set to it. @@ -329,23 +326,16 @@ proc recvLineInto*(socket: AsyncSocket, resString: FutureVar[string], ## **Warning**: ``recvLineInto`` on unbuffered sockets assumes that the ## protocol uses ``\r\L`` to delimit a new line. assert SocketFlag.Peek notin flags ## TODO: - assert(not resString.mget.isNil(), - "String inside resString future needs to be initialised") result = newFuture[void]("asyncnet.recvLineInto") - # TODO: Make the async transformation check for FutureVar params and complete - # them when the result future is completed. - # Can we replace the result future with the FutureVar? - template addNLIfEmpty(): stmt = - if resString.mget.len == 0: - resString.mget.add("\c\L") + if resString[].len == 0: + resString[].add("\c\L") if socket.isBuffered: if socket.bufLen == 0: let res = socket.readIntoBuf(flags) if res == 0: - resString.complete() return var lastR = false @@ -353,8 +343,7 @@ proc recvLineInto*(socket: AsyncSocket, resString: FutureVar[string], if socket.currPos >= socket.bufLen: let res = socket.readIntoBuf(flags) if res == 0: - resString.mget().setLen(0) - resString.complete() + resString[].setLen(0) return case socket.buffer[socket.currPos] @@ -364,15 +353,13 @@ proc recvLineInto*(socket: AsyncSocket, resString: FutureVar[string], of '\L': addNLIfEmpty() socket.currPos.inc() - resString.complete() return else: if lastR: socket.currPos.inc() - resString.complete() return else: - resString.mget.add socket.buffer[socket.currPos] + resString[].add socket.buffer[socket.currPos] socket.currPos.inc() else: var c = "" @@ -380,23 +367,18 @@ proc recvLineInto*(socket: AsyncSocket, resString: FutureVar[string], let recvFut = recv(socket, 1, flags) c = recvFut.read() if c.len == 0: - resString.mget.setLen(0) - resString.complete() + resString[].setLen(0) return if c == "\r": let recvFut = recv(socket, 1, flags) # Skip \L c = recvFut.read() assert c == "\L" addNLIfEmpty() - resString.complete() return elif c == "\L": addNLIfEmpty() - resString.complete() return - resString.mget.add c - - resString.complete() + resString[].add c proc recvLine*(socket: AsyncSocket, flags = {SocketFlag.SafeDisconn}): Future[string] {.async.} = @@ -422,10 +404,8 @@ proc recvLine*(socket: AsyncSocket, result.add("\c\L") assert SocketFlag.Peek notin flags ## TODO: - # TODO: Optimise this. - var resString = newFutureVar[string]("asyncnet.recvLine") - await socket.recvLineInto(resString, flags) - result = resString.mget() + result = "" + await socket.recvLineInto(addr result, flags) proc listen*(socket: AsyncSocket, backlog = SOMAXCONN) {.tags: [ReadIOEffect].} = ## Marks ``socket`` as accepting connections. |