diff --git a/changelog.markdown b/changelog.markdown index 0a77639..6522980 100644 --- a/changelog.markdown +++ b/changelog.markdown @@ -5,6 +5,7 @@ - **Breaking change:** the `@` operator used for retrieving request parameters now automatically decodes special characters using `decodeUrl`. - Fix for [#211](https://github.com/dom96/jester/issues/211) - custom routers now have the same error handling as normal routes. - Fix for [#269](https://github.com/dom96/jester/issues/269) - a bug that prevented redirecting from within error handlers. +- The `resp` (taking a HTTP code, headers and content) now correctly handles headers with duplicate keys. ## 0.5.0 - 17/10/2020 diff --git a/jester.nim b/jester.nim index 9cebf8e..48c49a1 100644 --- a/jester.nim +++ b/jester.nim @@ -543,6 +543,7 @@ proc serve*( template setHeader*(headers: var ResponseHeaders, key, value: string): typed = ## Sets a response header using the given key and value. + ## ## Overwrites if the header key already exists. bind isNone if isNone(headers): @@ -560,6 +561,17 @@ template setHeader*(headers: var ResponseHeaders, key, value: string): typed = # Add key if it doesn't exist. headers = some(h & @({key: value})) +template addHeader*(headers: var ResponseHeaders, key, value: string): typed = + ## Adds a response header using the given key and value. + ## + ## If a header of the same `key` already exists then it is kept, + ## the specified value is added and the original isn't overwritten. + bind isNone + if isNone(headers): + headers = some(@({key: value})) + else: + headers = some(h & @({key: value})) + template resp*(code: HttpCode, headers: openarray[tuple[key, val: string]], content: string): typed = @@ -567,7 +579,7 @@ template resp*(code: HttpCode, bind TCActionSend result = (TCActionSend, code, none[RawHeaders](), content, true) for header in headers: - setHeader(result[2], header[0], header[1]) + addHeader(result[2], header[0], header[1]) break route @@ -1406,7 +1418,7 @@ proc routesEx(name: string, body: NimNode): NimNode = let `matchProcVarIdent`: MatchProcSync = `matchIdent` let `errorProcVarIdent`: ErrorProc = `errorHandlerIdent` let `pairIdent`: MatchPairSync = (`matchProcVarIdent`, `errorProcVarIdent`) - + # TODO: Replace `body`, `headers`, `code` in routes with `result[i]` to # get these shortcuts back without sacrificing usability.