11module Handlers
22
3- import .. Request
3+ export Handler, Middleware, serve, serve!, Router, register!, getroute, getparams, getparam, getcookies
4+
5+ import .. Request, .. Cookies
46
57"""
68 Handler
@@ -285,8 +287,7 @@ register!(r::Router, path, handler) = register!(r, "*", path, handler)
285287const Params = Dict{String, String}
286288
287289function gethandler (r:: Router , req:: Request )
288- url = req. uri
289- segments = split (url. path, ' /' ; keepempty= false )
290+ segments = split (req. path, ' /' ; keepempty= false )
290291 leaf = match (r. routes, req. method, segments, 1 )
291292 params = Params ()
292293 if leaf isa Leaf
@@ -329,9 +330,9 @@ function (r::Router)(req::Request)
329330 # matched the path, but method not supported
330331 return r. _405 (req)
331332 else
332- req. context[ : route] = route
333+ req. route = route
333334 if ! isempty (params)
334- req. context[ : params] = params
335+ req. params = params
335336 end
336337 return handler (req)
337338 end
@@ -344,7 +345,7 @@ Retrieve the original route registration string for a request after its url has
344345matched against a router. Helpful for metric logging to ignore matched variables in
345346a path and only see the registered routes.
346347"""
347- getroute (req) = Base . get ( req. context, : route, nothing )
348+ getroute (req) = req. route
348349
349350"""
350351 HTTP.getparams(req) -> Dict{String, String}
@@ -354,7 +355,7 @@ If a path was registered with a router via `HTTP.register!` like
354355"/api/widget/{id}", then the path parameters are available in the request context
355356and can be retrieved like `id = HTTP.getparams(req)["id"]`.
356357"""
357- getparams (req) = Base . get ( req. context, : params, nothing )
358+ getparams (req) = req. params
358359
359360"""
360361 HTTP.getparam(req, name, default=nothing) -> String
@@ -378,8 +379,8 @@ request in the request context. Cookies can then be retrieved by calling
378379"""
379380function cookie_middleware (handler)
380381 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)
383384 end
384385 return handler (req)
385386 end
@@ -393,6 +394,6 @@ are expected to be stored in the `req.context[:cookies]` of the
393394request context as implemented in the [`HTTP.Handlers.cookie_middleware`](@ref)
394395middleware.
395396"""
396- getcookies (req) = Base . get (() -> Cookie[], req. context, : cookies)
397+ getcookies (req) = req . cookies === nothing ? Cookies . Cookie[] : req. cookies:: Vector{Cookies.Cookie}
397398
398399end # module Handlers
0 commit comments