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

Store the original registered route path when Router matches #866

Merged
merged 3 commits into from
Jun 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
50 changes: 28 additions & 22 deletions src/Handlers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ end
const VARREGEX = r"^{([[:alnum:]]+):?(.*)}$"

function Variable(pattern)
re = match(VARREGEX, pattern)
re = Base.match(VARREGEX, pattern)
if re === nothing
error("problem parsing path variable for route: `$pattern`")
end
Expand Down Expand Up @@ -190,7 +190,7 @@ function find(y, itr; by=identity, eq=(==))
return nothing
end

function Base.insert!(node::Node, leaf, segments, i)
function insert!(node::Node, leaf, segments, i)
if i > length(segments)
# time to insert leaf method match node
j = find(leaf.method, node.methods; by=x->x.method, eq=(x, y) -> x == "*" || x == y)
Expand Down Expand Up @@ -254,7 +254,7 @@ function Base.insert!(node::Node, leaf, segments, i)
end
end

function Base.match(node::Node, params, method, segments, i)
function match(node::Node, method, segments, i)
# @show node.segment, i, segments
if i > length(segments)
if isempty(node.methods)
Expand All @@ -266,15 +266,8 @@ function Base.match(node::Node, params, method, segments, i)
# i.e. we matched the route, but there wasn't a matching method
return missing
else
leaf = node.methods[j]
# @show leaf.variables, segments
if !isempty(leaf.variables)
# we have variables to fill in
for (i, v) in leaf.variables
params[v] = segments[i]
end
end
return leaf.handler
# return matched leaf node
return node.methods[j]
end
end
segment = segments[i]
Expand All @@ -283,7 +276,7 @@ function Base.match(node::Node, params, method, segments, i)
j = find(segment, node.exact; by=x->x.segment)
if j !== nothing
# found an exact match, recurse
m = match(node.exact[j], params, method, segments, i + 1)
m = match(node.exact[j], method, segments, i + 1)
anymissing = m === missing
m = coalesce(m, nothing)
# @show :exact, m
Expand All @@ -294,9 +287,9 @@ function Base.match(node::Node, params, method, segments, i)
# check for conditional matches
for node in node.conditional
# @show node.segment.pattern, segment
if match(node.segment.pattern, segment) !== nothing
if Base.match(node.segment.pattern, segment) !== nothing
# matched a conditional node, recurse
m = match(node, params, method, segments, i + 1)
m = match(node, method, segments, i + 1)
anymissing = m === missing
m = coalesce(m, nothing)
if m !== nothing
Expand All @@ -305,15 +298,15 @@ function Base.match(node::Node, params, method, segments, i)
end
end
if node.wildcard !== nothing
m = match(node.wildcard, params, method, segments, i + 1)
m = match(node.wildcard, method, segments, i + 1)
anymissing = m === missing
m = coalesce(m, nothing)
if m !== nothing
return m
end
end
if node.doublestar !== nothing
m = match(node.doublestar, params, method, segments, length(segments) + 1)
m = match(node.doublestar, method, segments, length(segments) + 1)
anymissing = m === missing
m = coalesce(m, nothing)
if m !== nothing
Expand Down Expand Up @@ -363,7 +356,8 @@ Router(_404=default404, _405=default405) = Router(_404, _405, Node())

Register a handler function that should be called when an incoming request matches `path`
and the optionally provided `method` (if not provided, any method is allowed). Can be used
to dynamically register routes.
to dynamically register routes. When a registered route is matched, the original route string
is stored in the `request.context[:route]` variable.
The following path types are allowed for matching:
* `/api/widgets`: exact match of static strings
* `/api/*/owner`: single `*` to wildcard match anything for a single segment
Expand All @@ -386,21 +380,32 @@ const Params = Dict{String, String}
function gethandler(r::Router, req::Request)
url = URI(req.target)
segments = split(url.path, '/'; keepempty=false)
leaf = match(r.routes, req.method, segments, 1)
params = Params()
handler = match(r.routes, params, req.method, segments, 1)
return handler, params
if leaf isa Leaf
# @show leaf.variables, segments
if !isempty(leaf.variables)
# we have variables to fill in
for (i, v) in leaf.variables
params[v] = segments[i]
end
end
return leaf.handler, leaf.path, params
end
return leaf, "", params
end

function (r::Router)(stream::Stream{<:Request})
req = stream.message
handler, params = gethandler(r, req)
handler, route, params = gethandler(r, req)
if handler === nothing
# didn't match a registered route
return r._404(stream)
elseif handler === missing
# matched the path, but method not supported
return r._405(stream)
else
req.context[:route] = route
if !isempty(params)
req.context[:params] = params
end
Expand All @@ -409,14 +414,15 @@ function (r::Router)(stream::Stream{<:Request})
end

function (r::Router)(req::Request)
handler, params = gethandler(r, req)
handler, route, params = gethandler(r, req)
if handler === nothing
# didn't match a registered route
return r._404(req)
elseif handler === missing
# matched the path, but method not supported
return r._405(req)
else
req.context[:route] = route
if !isempty(params)
req.context[:params] = params
end
Expand Down
4 changes: 2 additions & 2 deletions test/handlers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ using HTTP, Test
HTTP.register!(r, "/api/widgets/{id}", req -> req.context[:params]["id"])
@test r(HTTP.Request("GET", "/api/widgets/11")) == "11"

HTTP.register!(r, "/api/widgets/{name}", req -> req.context[:params]["name"])
@test r(HTTP.Request("GET", "/api/widgets/11")) == "11"
HTTP.register!(r, "/api/widgets/{name}", req -> (req.context[:params]["name"], req.context[:route]))
@test r(HTTP.Request("GET", "/api/widgets/11")) == ("11", "/api/widgets/{name}")

HTTP.register!(r, "/api/widgets/acme/{id:[a-z]+}", req -> req.context[:params]["id"])
@test r(HTTP.Request("GET", "/api/widgets/acme/11")) == 0
Expand Down