planety / prologue

Powerful and flexible web framework written in Nim
https://planety.github.io/prologue
Apache License 2.0
1.25k stars 47 forks source link

[Feature Request] Automatically URL-decode path parameters #164

Closed PhilippMDoerner closed 2 years ago

PhilippMDoerner commented 2 years ago

Heyho, as part of the webapp I build with prologue I also have endpoints that perform searches on my db via path parameters in the URL.

As such, I want the path parameter in the URL to be URL-decoded because I'm not going to have anything with %20 stored in my database. However, when using prologue normally it appears to not decode the URL path parameters, as observed with this minimal setup:

import prologue
import std/[strformat]

const SEARCH_TEXT_PARAM* = "searchText"
const SEARCH_TEXT_PATTERN* = fmt r"(?P<{SEARCH_TEXT_PARAM}>[^/]+)"

proc controller(ctx: Context) {.async.} =
    let searchText: string = ctx.getPathParamsOption(SEARCH_TEXT_PARAM).get()
    echo searchText

    await ctx.respond(Http200, searchText)

proc main() =
    var app: Prologue = newApp()
    app.addRoute(
        re fmt"/test/{SEARCH_TEXT_PATTERN}",
        handler = controller,
        httpMethod = HttpGet,
    )
    app.run()

main()

When calling this endpoint with the URL {{host}}/test/Walumba bla it will put Walumba%20bla in searchText, which is not what I would want. Luckily this is a pretty easy fix, as std/uri provides a decodeUrl proc that can be used like this to fix it:

ctx.getPathParamsOption(SEARCH_TEXT_PARAM).get().decodeUrl()

However, I think it makes sense to possibly have that be the default behaviour of prologue. Would this be alright? If so, I'd throw in a small PR in which I would add a call to decodeUrl in front of getPathParamsOption, getPathParams, getQueryParamsOption and getQueryParams.

ringabout commented 2 years ago

Looks good to me. Thank you!

see also https://github.com/dom96/jester/commit/a03b5be0b72f1a686b6ca594c145cee665e6db7b

ringabout commented 2 years ago

fixed by https://github.com/planety/prologue/pull/166