From 68ce808db131081a565e9fb2dfca3d66197d8175 Mon Sep 17 00:00:00 2001 From: Andreas Rumpf Date: Thu, 3 Oct 2019 16:45:45 +0200 Subject: minor optimization for asynchttpserver.nim --- lib/pure/asynchttpserver.nim | 31 ++++++++++++------------------- 1 file changed, 12 insertions(+), 19 deletions(-) (limited to 'lib') diff --git a/lib/pure/asynchttpserver.nim b/lib/pure/asynchttpserver.nim index 2f0420cd7..186f0da41 100644 --- a/lib/pure/asynchttpserver.nim +++ b/lib/pure/asynchttpserver.nim @@ -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: -- cgit 1.4.1-2-gfad0 h: root/lib/posix/posix_other.nim
blob: ba1dd89ede6de46fe7079bd80523fe4aac9ab231 (plain) (tree)
1
2
3
4
5
6
7
8
9