1
1
module Handlers
2
2
3
- import .. Request
3
+ export Handler, Middleware, serve, serve!, Router, register!, getroute, getparams, getparam, getcookies
4
+
5
+ import .. Request, .. Cookies
4
6
5
7
"""
6
8
Handler
@@ -285,8 +287,7 @@ register!(r::Router, path, handler) = register!(r, "*", path, handler)
285
287
const Params = Dict{String, String}
286
288
287
289
function gethandler (r:: Router , req:: Request )
288
- url = req. uri
289
- segments = split (url. path, ' /' ; keepempty= false )
290
+ segments = split (req. path, ' /' ; keepempty= false )
290
291
leaf = match (r. routes, req. method, segments, 1 )
291
292
params = Params ()
292
293
if leaf isa Leaf
@@ -329,9 +330,9 @@ function (r::Router)(req::Request)
329
330
# matched the path, but method not supported
330
331
return r. _405 (req)
331
332
else
332
- req. context[ : route] = route
333
+ req. route = route
333
334
if ! isempty (params)
334
- req. context[ : params] = params
335
+ req. params = params
335
336
end
336
337
return handler (req)
337
338
end
@@ -344,7 +345,7 @@ Retrieve the original route registration string for a request after its url has
344
345
matched against a router. Helpful for metric logging to ignore matched variables in
345
346
a path and only see the registered routes.
346
347
"""
347
- getroute (req) = Base . get ( req. context, : route, nothing )
348
+ getroute (req) = req. route
348
349
349
350
"""
350
351
HTTP.getparams(req) -> Dict{String, String}
@@ -354,7 +355,7 @@ If a path was registered with a router via `HTTP.register!` like
354
355
"/api/widget/{id}", then the path parameters are available in the request context
355
356
and can be retrieved like `id = HTTP.getparams(req)["id"]`.
356
357
"""
357
- getparams (req) = Base . get ( req. context, : params, nothing )
358
+ getparams (req) = req. params
358
359
359
360
"""
360
361
HTTP.getparam(req, name, default=nothing) -> String
@@ -378,8 +379,8 @@ request in the request context. Cookies can then be retrieved by calling
378
379
"""
379
380
function cookie_middleware (handler)
380
381
function (req)
381
- if ! haskey ( req. context, : cookies)
382
- req. context[ : cookies] = Cookies. cookies (req)
382
+ if req. cookies === nothing
383
+ req. cookies = Cookies. cookies (req)
383
384
end
384
385
return handler (req)
385
386
end
@@ -393,6 +394,6 @@ are expected to be stored in the `req.context[:cookies]` of the
393
394
request context as implemented in the [`HTTP.Handlers.cookie_middleware`](@ref)
394
395
middleware.
395
396
"""
396
- getcookies (req) = Base . get (() -> Cookie[], req. context, : cookies)
397
+ getcookies (req) = req . cookies === nothing ? Cookies . Cookie[] : req. cookies:: Vector{Cookies.Cookie}
397
398
398
399
end # module Handlers
0 commit comments