From c96300a2f4d32b6673f7e7a0d219c241f53adb30 Mon Sep 17 00:00:00 2001 From: Ryan Date: Fri, 7 Mar 2025 02:46:54 +0100 Subject: [PATCH] WIP: 404 handling --- seafire/resources/handle-get.txx | 24 +++++++++++++++++++----- seafire/resources/traits.hxx | 4 ++++ 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/seafire/resources/handle-get.txx b/seafire/resources/handle-get.txx index a29e75c..6e76142 100644 --- a/seafire/resources/handle-get.txx +++ b/seafire/resources/handle-get.txx @@ -11,14 +11,28 @@ namespace seafire::resources { auto rep = common::invoke(resource, req, &R::get); - if (!rep) { + auto select = [&]() -> decltype(rep) + { + if constexpr (representation::traits::is_optional_representation_v) { + if (!rep) { + return std::nullopt; + } + + return representation::select(*rep, accepted_type); + } + else { + return representation::select(rep, accepted_type); + } + }; + + auto selected_rep = select(); + + if (!selected_rep) { res.send(server::common_error_t::not_found); return; } - auto selected_rep = representation::select(*rep, accepted_type); - - if (!check_preconditions(req, res, selected_rep)) + if (!check_preconditions(req, res, *selected_rep)) return; namespace rfc7231 = protocol::rfc7231; @@ -41,7 +55,7 @@ namespace seafire::resources status = 201; bool const send_content = kind != get_kind_t::head; - representation::send(req, res, status, selected_rep, send_content); + representation::send(req, res, status, *selected_rep, send_content); } } // namespace seafire::resources diff --git a/seafire/resources/traits.hxx b/seafire/resources/traits.hxx index d4f1c4a..b8f2618 100644 --- a/seafire/resources/traits.hxx +++ b/seafire/resources/traits.hxx @@ -43,6 +43,10 @@ namespace seafire::resources::traits typename common::traits::function_traits::return_type > || + representation::traits::is_representation_v< + typename common::traits::function_traits::return_type + > + || representation::traits::is_optional_representation_v< typename common::traits::function_traits::return_type >