diff --git a/defnet/http_router.lua b/defnet/http_router.lua index 5ca52f1..2b6d273 100644 --- a/defnet/http_router.lua +++ b/defnet/http_router.lua @@ -34,25 +34,17 @@ function M.create() end function instance.match(method, uri) - local response if uri then for _,route in ipairs(routes) do if not route.method or route.method == method then local matches = { uri:match(route.pattern) } if next(matches) then - response = route.fn(matches, stream_fn, headers, message_body) - break + return true, route.fn, matches end end end end - - -- unhandled response - if not response and unhandled_route_fn then - response = unhandled_route_fn(method, uri, stream_fn, headers, message_body) - end - - return response + return false, unhandled_route_fn end return instance @@ -60,14 +52,9 @@ end --- Route HTTP GET requests matching a specific pattern to a -- provided function. --- The function will receive a list of matches from the pattern as --- it's first arguments. The second argument is a stream function in case --- the response should be streamed. --- The function must either return the full response or a function that --- can be called multiple times to get more data to return. -- @param router -- @param pattern Standard Lua pattern --- @param fn Function to call +-- @param fn Function to call when this route is matched function M.get(router, pattern, fn) assert(router) return router.get(pattern, fn) @@ -75,14 +62,9 @@ end --- Route HTTP POST requests matching a specific pattern to a -- provided function. --- The function will receive a list of matches from the pattern as --- it's first arguments. The second argument is a stream function in case --- the response should be streamed. --- The function must either return the full response or a function that --- can be called multiple times to get more data to return. -- @param router -- @param pattern Standard Lua pattern --- @param fn Function to call +-- @param fn Function to call when this route is matched function M.post(router, pattern, fn) assert(router) return router.post(pattern, fn) @@ -90,14 +72,9 @@ end --- Route all HTTP requests matching a specific pattern to a -- provided function. --- The function will receive a list of matches from the pattern as --- it's first arguments. The second argument is a stream function in case --- the response should be streamed. --- The function must either return the full response or a function that --- can be called multiple times to get more data to return. -- @param router -- @param pattern Standard Lua pattern --- @param fn Function to call +-- @param fn Function to call when this route is matched function M.all(pattern, fn) assert(router) return router.all(pattern, fn) @@ -106,19 +83,20 @@ end --- Add a handler for unhandled routes. This is typically where -- you would return a 404 page -- @param router --- @param fn The function to call when an unhandled route is encountered. The --- function will receive the method and uri of the unhandled route as +-- @param fn The function to call when an unhandled route is encountered -- arguments. function M.unhandled(router, fn) assert(router) return router.unhandled(fn) end ---- Match a method and uri with a route. If no match exists the unhandled route function is used +--- Match a method and uri with a route. -- @param router -- @param method -- @param uri --- @return response +-- @return handled Boolean indicating if the route was handled or not +-- @return fn Route function, or the unhandled route function if no route was found +-- @return matches Any matches captured from the route pattern function M.match(router, method, uri) assert(router) return router.match(method, uri) diff --git a/defnet/http_server.lua b/defnet/http_server.lua index c77ace6..bbb836d 100644 --- a/defnet/http_server.lua +++ b/defnet/http_server.lua @@ -118,6 +118,16 @@ end --- Create a new HTTP server +-- Use start(), stop() and update() on the returned instance +-- Set http route functions to react to incoming requests +-- +-- A note on route functions: +-- The function will receive a list of matches from the pattern as +-- it's first arguments. The second argument is a stream function in case +-- the response should be streamed. +-- The function must either return the full response or a function that +-- can be called multiple times to get more data to return. +-- -- @return Server instance function M.create(port) local instance = { @@ -159,7 +169,13 @@ function M.create(port) end -- handle request and get a response - local response = http_router.match(instance.router, method, uri) + local response + local handled, fn, matches = instance.router.match(method, uri) + if handled then + response = fn(matches, stream_fn, headers, message_body) + elseif not handled and fn then + response = fn(method, uri, stream_fn, headers, message_body) + end -- send response if response then @@ -188,14 +204,17 @@ function M.create(port) function instance.update() ss.update() for k,handler in pairs(request_handlers) do - print("calling request handler") if not handler() then - print("removing request handler") request_handlers[k] = nil end end end + function instance.set_router(router) + assert(router) + instance.router = router + end + --- Return a properly formatted HTML response with the -- appropriate response headers set -- If the document is omitted the response is assumed to be @@ -310,7 +329,7 @@ end function M.set_router(server, router) assert(server) assert(router) - server.router = router + return server.set_router(router) end return M