summary refs log tree commit diff stats
path: root/lib
diff options
context:
space:
mode:
authorDominik Picheta <dominikpicheta@googlemail.com>2014-07-13 10:19:48 +0100
committerDominik Picheta <dominikpicheta@googlemail.com>2014-07-13 10:19:48 +0100
commit374706b1c3e6ceec6d514c1a1ac01ec6facb6dc7 (patch)
tree60db50c36d6a22e382a5f314be6a99763c8c855c /lib
parent634a416c6769516acab72448e54a1ebc6d27690e (diff)
downloadNim-374706b1c3e6ceec6d514c1a1ac01ec6facb6dc7.tar.gz
Void futures are no longer discardable.
Diffstat (limited to 'lib')
-rw-r--r--lib/pure/asyncdispatch.nim11
-rw-r--r--lib/pure/asynchttpserver.nim17
-rw-r--r--lib/pure/httpclient.nim2
3 files changed, 18 insertions, 12 deletions
diff --git a/lib/pure/asyncdispatch.nim b/lib/pure/asyncdispatch.nim
index 483e4d5af..d93afce6c 100644
--- a/lib/pure/asyncdispatch.nim
+++ b/lib/pure/asyncdispatch.nim
@@ -126,6 +126,15 @@ proc failed*(future: PFutureBase): bool =
   ## Determines whether ``future`` completed with an error.
   future.error != nil
 
+proc asyncCheck*[T](future: PFuture[T]) =
+  ## Sets a callback on ``future`` which raises an exception if the future
+  ## finished with an error.
+  ##
+  ## This should be used instead of ``discard`` to discard void futures.
+  future.callback =
+    proc () =
+      if future.failed: raise future.error
+
 when defined(windows) or defined(nimdoc):
   import winlean, sets, hashes
   type
@@ -1021,8 +1030,6 @@ macro async*(prc: stmt): stmt {.immediate.} =
       result[4].del(i)
   if subtypeIsVoid:
     # Add discardable pragma.
-    if prc.kind == nnkProcDef: # TODO: This is a workaround for #1287
-      result[4].add(newIdentNode("discardable"))
     if returnType.kind == nnkEmpty:
       # Add PFuture[void]
       result[3][0] = parseExpr("PFuture[void]")
diff --git a/lib/pure/asynchttpserver.nim b/lib/pure/asynchttpserver.nim
index 6273d479c..7061940d6 100644
--- a/lib/pure/asynchttpserver.nim
+++ b/lib/pure/asynchttpserver.nim
@@ -106,7 +106,6 @@ proc processClient(client: PAsyncSocket, address: string,
     request.hostname = address
     assert client != nil
     request.client = client
-    var runCallback = true
 
     # First line - GET /path HTTP/1.1
     let line = await client.recvLine() # TODO: Timeouts.
@@ -115,8 +114,8 @@ proc processClient(client: PAsyncSocket, address: string,
       return
     let lineParts = line.split(' ')
     if lineParts.len != 3:
-      request.respond(Http400, "Invalid request. Got: " & line)
-      runCallback = false
+      await request.respond(Http400, "Invalid request. Got: " & line)
+      continue
 
     let reqMethod = lineParts[0]
     let path = lineParts[1]
@@ -140,8 +139,9 @@ proc processClient(client: PAsyncSocket, address: string,
     try:
       request.protocol = protocol.parseProtocol()
     except EInvalidValue:
-      request.respond(Http400, "Invalid request protocol. Got: " & protocol)
-      runCallback = false
+      asyncCheck request.respond(Http400, "Invalid request protocol. Got: " &
+          protocol)
+      continue
 
     if reqMethod.normalize == "post":
       # Check for Expect header
@@ -162,12 +162,11 @@ proc processClient(client: PAsyncSocket, address: string,
           assert request.body.len == contentLength
       else:
         await request.respond(Http400, "Bad Request. No Content-Length.")
-        runCallback = false
+        continue
 
     case reqMethod.normalize
     of "get", "post", "head", "put", "delete", "trace", "options", "connect", "patch":
-      if runCallback:
-        await callback(request)
+      await callback(request)
     else:
       await request.respond(Http400, "Invalid request method. Got: " & reqMethod)
 
@@ -199,7 +198,7 @@ proc serve*(server: PAsyncHttpServer, port: TPort,
     # TODO: Causes compiler crash.
     #var (address, client) = await server.socket.acceptAddr()
     var fut = await server.socket.acceptAddr()
-    processClient(fut.client, fut.address, callback)
+    asyncCheck processClient(fut.client, fut.address, callback)
 
 proc close*(server: PAsyncHttpServer) =
   ## Terminates the async http server instance.
diff --git a/lib/pure/httpclient.nim b/lib/pure/httpclient.nim
index be06a7b8e..9bacc80d6 100644
--- a/lib/pure/httpclient.nim
+++ b/lib/pure/httpclient.nim
@@ -654,7 +654,7 @@ when isMainModule:
       resp = await client.request("http://nimrod-lang.org/download.html")
       echo("Got response: ", resp.status)
 
-    main()
+    asyncCheck main()
     runForever()
 
   else: