Make respond a template again
This commit is contained in:
parent
134eb6e582
commit
2410e667bc
1 changed files with 11 additions and 11 deletions
|
|
@ -99,7 +99,7 @@ proc newAsyncHttpServer*(reuseAddr = true): AsyncHttpServer =
|
||||||
new result
|
new result
|
||||||
result.reuseAddr = reuseAddr
|
result.reuseAddr = reuseAddr
|
||||||
|
|
||||||
proc addHeaders*(msg: var string, headers: StringTableRef) =
|
proc addHeaders(msg: var string, headers: StringTableRef) =
|
||||||
for k, v in headers:
|
for k, v in headers:
|
||||||
msg.add(k & ": " & v & "\c\L")
|
msg.add(k & ": " & v & "\c\L")
|
||||||
|
|
||||||
|
|
@ -109,8 +109,8 @@ proc sendHeaders*(req: Request, headers: StringTableRef): Future[void] =
|
||||||
addHeaders(msg, headers)
|
addHeaders(msg, headers)
|
||||||
return req.client.send(msg)
|
return req.client.send(msg)
|
||||||
|
|
||||||
template respond*(req: Request, code: HttpCode,
|
proc respond*(req: Request, code: HttpCode, content: string,
|
||||||
content: string, headers: StringTableRef = nil) =
|
headers: StringTableRef = nil): Future[void] =
|
||||||
## Responds to the request with the specified ``HttpCode``, headers and
|
## Responds to the request with the specified ``HttpCode``, headers and
|
||||||
## content. This template returns a Future[void].
|
## content. This template returns a Future[void].
|
||||||
##
|
##
|
||||||
|
|
@ -177,11 +177,11 @@ proc processClient(client: AsyncSocket, address: string,
|
||||||
try:
|
try:
|
||||||
request.protocol = parseProtocol(linePart)
|
request.protocol = parseProtocol(linePart)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
request.respond(Http400, "Invalid request protocol. Got: " &
|
asyncCheck request.respond(Http400,
|
||||||
linePart)
|
"Invalid request protocol. Got: " & linePart)
|
||||||
continue
|
continue
|
||||||
else:
|
else:
|
||||||
request.respond(Http400, "Invalid request. Got: " & line)
|
await request.respond(Http400, "Invalid request. Got: " & line)
|
||||||
continue
|
continue
|
||||||
inc i
|
inc i
|
||||||
|
|
||||||
|
|
@ -210,19 +210,19 @@ proc processClient(client: AsyncSocket, address: string,
|
||||||
if request.headers.hasKey("Content-Length"):
|
if request.headers.hasKey("Content-Length"):
|
||||||
var contentLength = 0
|
var contentLength = 0
|
||||||
if parseInt(request.headers["Content-Length"], contentLength) == 0:
|
if parseInt(request.headers["Content-Length"], contentLength) == 0:
|
||||||
request.respond(Http400, "Bad Request. Invalid Content-Length.")
|
await request.respond(Http400, "Bad Request. Invalid Content-Length.")
|
||||||
else:
|
else:
|
||||||
request.body = await client.recv(contentLength)
|
request.body = await client.recv(contentLength)
|
||||||
assert request.body.len == contentLength
|
assert request.body.len == contentLength
|
||||||
else:
|
else:
|
||||||
request.respond(Http400, "Bad Request. No Content-Length.")
|
await request.respond(Http400, "Bad Request. No Content-Length.")
|
||||||
continue
|
continue
|
||||||
|
|
||||||
case request.reqMethod
|
case request.reqMethod
|
||||||
of "get", "post", "head", "put", "delete", "trace", "options", "connect", "patch":
|
of "get", "post", "head", "put", "delete", "trace", "options", "connect", "patch":
|
||||||
await callback(request)
|
await callback(request)
|
||||||
else:
|
else:
|
||||||
request.respond(Http400, "Invalid request method. Got: " & request.reqMethod)
|
await request.respond(Http400, "Invalid request method. Got: " & request.reqMethod)
|
||||||
|
|
||||||
# Persistent connections
|
# Persistent connections
|
||||||
if (request.protocol == HttpVer11 and
|
if (request.protocol == HttpVer11 and
|
||||||
|
|
@ -271,7 +271,7 @@ when isMainModule:
|
||||||
#echo(req.headers)
|
#echo(req.headers)
|
||||||
let headers = {"Date": "Tue, 29 Apr 2014 23:40:08 GMT",
|
let headers = {"Date": "Tue, 29 Apr 2014 23:40:08 GMT",
|
||||||
"Content-type": "text/plain; charset=utf-8"}
|
"Content-type": "text/plain; charset=utf-8"}
|
||||||
req.respond(Http200, "Hello World", headers.newStringTable())
|
await req.respond(Http200, "Hello World", headers.newStringTable())
|
||||||
|
|
||||||
asyncCheck server.serve(Port(5555), cb)
|
asyncCheck server.serve(Port(5555), cb)
|
||||||
runForever()
|
runForever()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue