minor optimization for asynchttpserver.nim
This commit is contained in:
parent
64acc9dbfb
commit
68ce808db1
1 changed files with 12 additions and 19 deletions
|
|
@ -128,20 +128,6 @@ proc parseProtocol(protocol: string): tuple[orig: string, major, minor: int] =
|
||||||
proc sendStatus(client: AsyncSocket, status: string): Future[void] =
|
proc sendStatus(client: AsyncSocket, status: string): Future[void] =
|
||||||
client.send("HTTP/1.1 " & status & "\c\L\c\L")
|
client.send("HTTP/1.1 " & status & "\c\L\c\L")
|
||||||
|
|
||||||
proc parseUppercaseMethod(name: string): HttpMethod =
|
|
||||||
result =
|
|
||||||
case name
|
|
||||||
of "GET": HttpGet
|
|
||||||
of "POST": HttpPost
|
|
||||||
of "HEAD": HttpHead
|
|
||||||
of "PUT": HttpPut
|
|
||||||
of "DELETE": HttpDelete
|
|
||||||
of "PATCH": HttpPatch
|
|
||||||
of "OPTIONS": HttpOptions
|
|
||||||
of "CONNECT": HttpConnect
|
|
||||||
of "TRACE": HttpTrace
|
|
||||||
else: raise newException(ValueError, "Invalid HTTP method " & name)
|
|
||||||
|
|
||||||
proc processRequest(
|
proc processRequest(
|
||||||
server: AsyncHttpServer,
|
server: AsyncHttpServer,
|
||||||
req: FutureVar[Request],
|
req: FutureVar[Request],
|
||||||
|
|
@ -187,9 +173,17 @@ proc processRequest(
|
||||||
for linePart in lineFut.mget.split(' '):
|
for linePart in lineFut.mget.split(' '):
|
||||||
case i
|
case i
|
||||||
of 0:
|
of 0:
|
||||||
try:
|
case linePart
|
||||||
request.reqMethod = parseUppercaseMethod(linePart)
|
of "GET": request.reqMethod = HttpGet
|
||||||
except ValueError:
|
of "POST": request.reqMethod = HttpPost
|
||||||
|
of "HEAD": request.reqMethod = HttpHead
|
||||||
|
of "PUT": request.reqMethod = HttpPut
|
||||||
|
of "DELETE": request.reqMethod = HttpDelete
|
||||||
|
of "PATCH": request.reqMethod = HttpPatch
|
||||||
|
of "OPTIONS": request.reqMethod = HttpOptions
|
||||||
|
of "CONNECT": request.reqMethod = HttpConnect
|
||||||
|
of "TRACE": request.reqMethod = HttpTrace
|
||||||
|
else:
|
||||||
asyncCheck request.respondError(Http400)
|
asyncCheck request.respondError(Http400)
|
||||||
return true # Retry processing of request
|
return true # Retry processing of request
|
||||||
of 1:
|
of 1:
|
||||||
|
|
@ -242,8 +236,7 @@ proc processRequest(
|
||||||
# - Check for Content-length header
|
# - Check for Content-length header
|
||||||
if request.headers.hasKey("Content-Length"):
|
if request.headers.hasKey("Content-Length"):
|
||||||
var contentLength = 0
|
var contentLength = 0
|
||||||
if parseSaturatedNatural(request.headers["Content-Length"],
|
if parseSaturatedNatural(request.headers["Content-Length"], contentLength) == 0:
|
||||||
contentLength) == 0:
|
|
||||||
await request.respond(Http400, "Bad Request. Invalid Content-Length.")
|
await request.respond(Http400, "Bad Request. Invalid Content-Length.")
|
||||||
return true
|
return true
|
||||||
else:
|
else:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue