mirror of
https://github.com/nim-lang/Nim.git
synced 2026-01-03 11:42:33 +00:00
minor optimization for asynchttpserver.nim
This commit is contained in:
@@ -128,20 +128,6 @@ proc parseProtocol(protocol: string): tuple[orig: string, major, minor: int] =
|
||||
proc sendStatus(client: AsyncSocket, status: string): Future[void] =
|
||||
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(
|
||||
server: AsyncHttpServer,
|
||||
req: FutureVar[Request],
|
||||
@@ -187,9 +173,17 @@ proc processRequest(
|
||||
for linePart in lineFut.mget.split(' '):
|
||||
case i
|
||||
of 0:
|
||||
try:
|
||||
request.reqMethod = parseUppercaseMethod(linePart)
|
||||
except ValueError:
|
||||
case linePart
|
||||
of "GET": request.reqMethod = HttpGet
|
||||
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)
|
||||
return true # Retry processing of request
|
||||
of 1:
|
||||
@@ -242,8 +236,7 @@ proc processRequest(
|
||||
# - Check for Content-length header
|
||||
if request.headers.hasKey("Content-Length"):
|
||||
var contentLength = 0
|
||||
if parseSaturatedNatural(request.headers["Content-Length"],
|
||||
contentLength) == 0:
|
||||
if parseSaturatedNatural(request.headers["Content-Length"], contentLength) == 0:
|
||||
await request.respond(Http400, "Bad Request. Invalid Content-Length.")
|
||||
return true
|
||||
else:
|
||||
|
||||
Reference in New Issue
Block a user