Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes bug in resp where duplicate headers are lost. #299

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
16 changes: 14 additions & 2 deletions jester.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -560,14 +561,25 @@ 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 =
## Sets ``(code, headers, content)`` as the response.
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


Expand Down Expand Up @@ -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.
Expand Down