asynchttpserver cleanups [backport:1.0] (#15966)

* asynchttpserver cleanups [backport:1.0]

(cherry picked from commit 122f22d163)
This commit is contained in:
Andreas Rumpf
2020-11-15 07:27:13 +01:00
committed by narimiran
parent 939702a001
commit db5ce7ed48
2 changed files with 10 additions and 12 deletions

View File

@@ -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"
@@ -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()
##
@@ -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.
@@ -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.
##
@@ -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()

View File

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