diff --git a/src/routing.jl b/src/routing.jl index 55f8d07d..e4ee696f 100644 --- a/src/routing.jl +++ b/src/routing.jl @@ -1,6 +1,6 @@ using HttpCommon -export method, GET, route, page, probabilty +export method, GET, route, page, probabilty, query, print # Request type @@ -19,6 +19,7 @@ function matchpath(target, path) params = d() for i = 1:length(target) if startswith(target[i], ":") + params = d() params[Symbol(target[i][2:end])] = path[i] else target[i] == path[i] || return @@ -44,6 +45,30 @@ page(p::AbstractString, app...) = page(splitpath(p), app...) page(app...) = page([], app...) page(app::Function, p) = page(p, app) +# Query routing + +function pt(req) + println("query ", req[:query]) + return true +end + +print(m::AbstractString, app...) = branch(req -> pt(req), app...) + +function matchquery(q, req) + qdict = parsequerystring(req[:query]) + length(q) != length(qdict) && return false + for pair in q + if haskey(qdict, pair[1]) && (pair[2] == "" || pair[2] == qdict[pair[1]]) + continue + else + return false + end + end + return true +end + +query(q::Dict{String, String}, app...) = branch(req -> matchquery(q, req), app...) + # Misc probabilty(x, app...) = branch(_->rand() "1", "two"=> "2") +d2 = Dict("one"=> "1", "two"=> "") # Test basic server @app test = ( Mux.defaults, page(respond("

Hello World!

")), page("/about", respond("

Boo!

")), page("/user/:user", req -> "

Hello, $(req[:params][:user])!

"), + query(d1, respond("

query1

")), + query(d2, respond("

query2

")), Mux.notfound()) serve(test) @test Requests.text(Requests.get("http://localhost:8000")) == @@ -19,3 +22,11 @@ serve(test) "

Boo!

" @test Requests.text(Requests.get("http://localhost:8000/user/julia")) == "

Hello, julia!

" +@test Requests.text(Requests.get("http://localhost:8000/dum?one=1&two=2")) == "

query1

" +@test Requests.text(Requests.get("http://localhost:8000/dum?one=1")) != "

query1

" +@test Requests.text(Requests.get("http://localhost:8000/dum?one=1&two=2&sarv=boo")) != "

query1

" + +@test Requests.text(Requests.get("http://localhost:8000/dum?one=1")) != "

query2

" +@test Requests.text(Requests.get("http://localhost:8000/dum?one=1&two=56")) == "

query2

" +@test Requests.text(Requests.get("http://localhost:8000/dum?one=1&two=hfjd")) == "

query2

" +@test Requests.text(Requests.get("http://localhost:8000/dum?one=1&two=2&sarv=boo")) != "

query2

"