From 271373c095882b4c6de759e86075d8c95354bd8c Mon Sep 17 00:00:00 2001 From: Fredrik Ekre Date: Mon, 4 Jul 2022 18:20:52 +0200 Subject: [PATCH] Allow any string for parameter names in router, fixes #871. --- src/Handlers.jl | 6 +++--- test/handlers.jl | 3 +++ 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/Handlers.jl b/src/Handlers.jl index bcb8bc8a8..ca8933973 100644 --- a/src/Handlers.jl +++ b/src/Handlers.jl @@ -145,7 +145,7 @@ mutable struct Variable pattern::Union{Nothing, Regex} end -const VARREGEX = r"^{([[:alnum:]]+):?(.*)}$" +const VARREGEX = r"^{([^:{}]+)(?::(.*))?}$" function Variable(pattern) re = Base.match(VARREGEX, pattern) @@ -153,7 +153,7 @@ function Variable(pattern) error("problem parsing path variable for route: `$pattern`") end pat = re.captures[2] - return Variable(re.captures[1], pat == "" ? nothing : Regex(pat)) + return Variable(re.captures[1], pat === nothing ? nothing : Regex(pat)) end struct Leaf @@ -178,7 +178,7 @@ end Base.show(io::IO, x::Node) = print(io, "Node($(x.segment))") isvariable(x) = startswith(x, "{") && endswith(x, "}") -segment(x) = segment == "*" ? String(segment) : isvariable(x) ? Variable(x) : String(x) +segment(x) = isvariable(x) ? Variable(x) : String(x) Node(x) = Node(x, Node[], Node[], nothing, nothing, Leaf[]) Node() = Node("*") diff --git a/test/handlers.jl b/test/handlers.jl index 1f08e94af..c6c9f37c4 100644 --- a/test/handlers.jl +++ b/test/handlers.jl @@ -32,6 +32,9 @@ using HTTP, Test @test r(HTTP.Request("GET", "/test/sarv/ghotra/seven")) == 10 @test r(HTTP.Request("GET", "/test/foo")) == 8 + HTTP.register!(r, "/api/issue/{issue_id}", req -> req.context[:params]["issue_id"]) + @test r(HTTP.Request("GET", "/api/issue/871")) == "871" + HTTP.register!(r, "/api/widgets/{id}", req -> req.context[:params]["id"]) @test r(HTTP.Request("GET", "/api/widgets/11")) == "11"