Skip to content

Commit

Permalink
asynchttpserver cleanups [backport:1.0] (nim-lang#15966)
Browse files Browse the repository at this point in the history
* asynchttpserver cleanups [backport:1.0]
  • Loading branch information
Araq authored Nov 15, 2020
1 parent 1978b91 commit 122f22d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 12 deletions.
20 changes: 9 additions & 11 deletions lib/pure/asynchttpserver.nim
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
## application in production you should use a reverse proxy (for example nginx)
## instead of allowing users to connect directly to this server.
##
## Basic usage
## ===========
## Example
## =======
##
## This example will create an HTTP server on port 8080. The server will
## respond to all requests with a ``200 OK`` response code and "Hello World"
Expand All @@ -32,11 +32,10 @@
## "Content-type": "text/plain; charset=utf-8"}
## await req.respond(Http200, "Hello World", headers.newHttpHeaders())
##
## server.listen Port(5555)
## server.listen Port(8080)
## while true:
## if server.shouldAcceptRequest(5):
## var (address, client) = await server.socket.acceptAddr()
## asyncCheck processClient(server, client, address, cb)
## if server.shouldAcceptRequest():
## asyncCheck server.acceptRequest(cb)
## else:
## poll()
##
Expand Down Expand Up @@ -333,7 +332,7 @@ proc shouldAcceptRequest*(server: AsyncHttpServer;
result = assumedDescriptorsPerRequest < 0 or
(activeDescriptors() + assumedDescriptorsPerRequest < server.maxFDs)

proc acceptRequest*(server: AsyncHttpServer, port: Port,
proc acceptRequest*(server: AsyncHttpServer,
callback: proc (request: Request): Future[void] {.closure, gcsafe.}) {.async.} =
## Accepts a single request. Write an explicit loop around this proc so that
## errors can be handled properly.
Expand All @@ -343,7 +342,7 @@ proc acceptRequest*(server: AsyncHttpServer, port: Port,
proc serve*(server: AsyncHttpServer, port: Port,
callback: proc (request: Request): Future[void] {.closure, gcsafe.},
address = "";
assumedDescriptorsPerRequest = 5) {.async.} =
assumedDescriptorsPerRequest = -1) {.async.} =
## Starts the process of listening for incoming HTTP connections on the
## specified address and port.
##
Expand Down Expand Up @@ -382,9 +381,8 @@ when not defined(testing) and isMainModule:

server.listen Port(5555)
while true:
if server.shouldAcceptRequest(5):
var (address, client) = await server.socket.acceptAddr()
asyncCheck processClient(server, client, address, cb)
if server.shouldAcceptRequest():
asyncCheck server.acceptRequest(cb)
else:
poll()

Expand Down
2 changes: 1 addition & 1 deletion tests/errmsgs/tgcsafety.nim
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ type mismatch: got <AsyncHttpServer, Port, proc (req: Request): Future[system.vo
but expected one of:
proc serve(server: AsyncHttpServer; port: Port;
callback: proc (request: Request): Future[void] {.closure, gcsafe.};
address = ""; assumedDescriptorsPerRequest = 5): owned(Future[void])
address = ""; assumedDescriptorsPerRequest = -1): owned(Future[void])
first type mismatch at position: 3
required type for callback: proc (request: Request): Future[system.void]{.closure, gcsafe.}
but expression 'cb' is of type: proc (req: Request): Future[system.void]{.locks: <unknown>.}
Expand Down

0 comments on commit 122f22d

Please sign in to comment.