Skip to content

Commit

Permalink
Merge pull request #117 from cmcaine/recurse-a-bit-less
Browse files Browse the repository at this point in the history
Fix stack overflows with page(func, func)
  • Loading branch information
jpsamaroo authored Dec 6, 2020
2 parents 6fdaec3 + e6bfbb8 commit 43d9bf4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/routing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,17 @@ function matchpath!(target, req)
return true
end

route(p, app...) = branch(req -> matchpath!(p, req), app...)
route(p::Vector, app...) = branch(req -> matchpath!(p, req), app...)
route(p::AbstractString, app...) = route(splitpath(p), app...)
route(app...) = route([], app...)
route(app::Function, p) = route(p, app)
route(app1::Function, app2::Function) = route([], app1, app2)

page(p::Vector, app...) = branch(req -> length(p) == length(req[:path]) && matchpath!(p, req), app...)
page(p::AbstractString, app...) = page(splitpath(p), app...)
page(app...) = page([], app...)
page(app::Function, p) = page(p, app)
page(app1::Function, app2::Function) = page([], app1, app2)

# Query routing

Expand Down
11 changes: 11 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,14 @@ serve(test, 8001)
@test String(HTTP.get("http://localhost:8001/badurl";
status_exception=false).body) ==
"Internal server error"

# Test page and route are callable without a string argument
# (previously the first two raised StackOverflowError)
@test page(identity, identity) isa Function
@test route(identity, identity) isa Function
@test page(identity) isa Function
@test route(identity) isa Function

# Test you can pass the string last if you really want.
@test page(identity, "") isa Function
@test route(identity, "") isa Function

0 comments on commit 43d9bf4

Please sign in to comment.