From ff9622ba7b060067f7d8caddfd964806b56e9735 Mon Sep 17 00:00:00 2001 From: bung87 Date: Sat, 13 Apr 2024 02:05:10 +0800 Subject: [PATCH] minor improvements --- src/scorper/http/httpcore.nim | 2 +- src/scorper/http/streamserver.nim | 13 ++++++++----- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/scorper/http/httpcore.nim b/src/scorper/http/httpcore.nim index c36e7d7..d552891 100644 --- a/src/scorper/http/httpcore.nim +++ b/src/scorper/http/httpcore.nim @@ -174,7 +174,7 @@ func getOrDefault*(headers: HttpHeaders, key: string, ## Returns the values associated with the given ``key``. If there are no ## values associated with the key, then ``default`` is returned. if headers.hasKey(key): - return headers.getOrDefault(headers.toCaseInsensitive(key)) + return headers.table.getOrDefault(headers.toCaseInsensitive(key)).HttpHeaderValues else: return default diff --git a/src/scorper/http/streamserver.nim b/src/scorper/http/streamserver.nim index 773c082..9a8f86b 100644 --- a/src/scorper/http/streamserver.nim +++ b/src/scorper/http/streamserver.nim @@ -152,7 +152,10 @@ template devLog(req: ImpRequest, content: untyped) = template handleCompress(needCompress: bool; content: string; ctn: var string; length: var int) = if needCompress: headers.ContentEncoding "gzip" - ctn = compress(content, BestSpeed, dfGzip) + try: + ctn = compress(content, BestSpeed, dfGzip) + except ZippyError: + length = ctn.len else: when defined(gcArc) or defined(gcOrc): @@ -192,7 +195,7 @@ proc resp*(req: ImpRequest, content: sink string, await resp(req, content, headers.newHttpHeaders()) proc respError*(req: ImpRequest, code: HttpCode, content: sink string, headers = newHttpHeaders()): Future[ - void] {.async.} = + void] {.async:(raises:[]).} = ## Responds to the req with the specified ``HttpCode``. if req.responded == true: return @@ -212,10 +215,10 @@ proc respError*(req: ImpRequest, code: HttpCode, content: sink string, headers = req.responded = true proc respError*(req: ImpRequest, code: HttpCode, content: sink string, - headers: seq[(string, string)]): Future[void] {.inline, async.} = + headers: seq[(string, string)]): Future[void] {.inline, async: (raises:[]).} = await respError(req, code, content, headers.newHttpHeaders()) -proc respError*(req: ImpRequest, code: HttpCode, headers = newHttpHeaders()): Future[void] {.async.} = +proc respError*(req: ImpRequest, code: HttpCode, headers = newHttpHeaders()): Future[void] {.async:(raises: []).} = ## Responds to the req with the specified ``HttpCode``. if req.responded == true: return @@ -624,7 +627,7 @@ proc postCheck(req: ImpRequest): Future[int]{.async, inline.} = if req.meth in MethodNeedsBody and req.parsed == false: result = await req.reader.consume(req.contentLength.int) -proc defaultErrorHandle(req: ImpRequest, err: ref Exception | HttpError; headers = newHttpHeaders()){.async, gcsafe.} = +proc defaultErrorHandle(req: ImpRequest, err: ref Exception | HttpError; headers = newHttpHeaders()){.async: (raises: []), gcsafe.} = if req.responded: return let code = when err is HttpError: err.code.HttpCode else: Http500 #