|
| 1 | +/* |
| 2 | + This file is part of libhttpserver |
| 3 | + Copyright (C) 2011-2026 Sebastiano Merlino |
| 4 | +
|
| 5 | + This library is free software; you can redistribute it and/or |
| 6 | + modify it under the terms of the GNU Lesser General Public |
| 7 | + License as published by the Free Software Foundation; either |
| 8 | + version 2.1 of the License, or (at your option) any later version. |
| 9 | +
|
| 10 | + This library is distributed in the hope that it will be useful, |
| 11 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | + Lesser General Public License for more details. |
| 14 | +
|
| 15 | + You should have received a copy of the GNU Lesser General Public |
| 16 | + License along with this library; if not, write to the Free Software |
| 17 | + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
| 18 | + USA |
| 19 | +*/ |
| 20 | + |
| 21 | +// TASK-025: dispatch shim used by webserver::on_method_ to slot lambda |
| 22 | +// handlers into the existing v1-shaped route table. |
| 23 | +// |
| 24 | +// The shim is a sub-class of http_resource that holds one slot per |
| 25 | +// http_method enumerator. Its render_* virtuals look up the slot for |
| 26 | +// the dispatched method and invoke it. The shim starts with EVERY |
| 27 | +// method disallowed (`disallow_all()`); each on_* call enables exactly |
| 28 | +// the matching bit via `set_allowing(method, true)`. The existing |
| 29 | +// finalize_answer dispatch glue therefore returns 405 for unregistered |
| 30 | +// methods automatically — no edit to webserver.cpp's dispatch path is |
| 31 | +// needed. |
| 32 | +// |
| 33 | +// `final` is intentional: the conflict check in webserver::on_method_ |
| 34 | +// uses dynamic_pointer_cast<lambda_resource>(...) to distinguish |
| 35 | +// lambda-owned routes from class-owned routes. A subclass would hide |
| 36 | +// in that test and break the invariant. |
| 37 | +// |
| 38 | +// Internal header — only reachable when compiling libhttpserver. NOT |
| 39 | +// included from the public umbrella <httpserver.hpp>. |
| 40 | +#if !defined(HTTPSERVER_COMPILATION) |
| 41 | +#error "lambda_resource.hpp is internal; only reachable when compiling libhttpserver." |
| 42 | +#endif |
| 43 | + |
| 44 | +#ifndef SRC_HTTPSERVER_DETAIL_LAMBDA_RESOURCE_HPP_ |
| 45 | +#define SRC_HTTPSERVER_DETAIL_LAMBDA_RESOURCE_HPP_ |
| 46 | + |
| 47 | +#include <array> |
| 48 | +#include <cassert> |
| 49 | +#include <cstddef> |
| 50 | +#include <cstdint> |
| 51 | +#include <memory> |
| 52 | +#include <utility> |
| 53 | + |
| 54 | +#include "httpserver/http_method.hpp" |
| 55 | +#include "httpserver/http_resource.hpp" |
| 56 | +#include "httpserver/detail/route_entry.hpp" |
| 57 | + |
| 58 | +namespace httpserver { |
| 59 | +class http_request; |
| 60 | +class http_response; |
| 61 | +} // namespace httpserver |
| 62 | + |
| 63 | +namespace httpserver { |
| 64 | +namespace detail { |
| 65 | + |
| 66 | +// Tiny adapter that wraps a single lambda_handler as an http_resource |
| 67 | +// virtual override. We keep one slot per method enum and dispatch in |
| 68 | +// each render_* override. |
| 69 | +class lambda_resource final : public ::httpserver::http_resource { |
| 70 | + public: |
| 71 | + lambda_resource() { |
| 72 | + // Lambda routes are opt-in per method (the default |
| 73 | + // http_resource constructor enables every method via |
| 74 | + // set_all()). on_* sets the matching bit when populating a slot. |
| 75 | + disallow_all(); |
| 76 | + } |
| 77 | + |
| 78 | + // Install (or replace) the slot for `method`. Caller must have |
| 79 | + // already verified that no slot is currently set for `method` |
| 80 | + // (webserver::on_method_ enforces this and throws on conflict). |
| 81 | + void set_slot(http_method method, lambda_handler h) { |
| 82 | + slots_[static_cast<std::size_t>(method)] = std::move(h); |
| 83 | + set_allowing(method, true); |
| 84 | + } |
| 85 | + |
| 86 | + bool has_slot(http_method method) const noexcept { |
| 87 | + return is_allowed(method); |
| 88 | + } |
| 89 | + |
| 90 | + // These seven overrides are mechanical delegations to invoke_(). |
| 91 | + // Each differs only by the http_method enum constant forwarded. |
| 92 | + // They are required by the http_resource base-class interface and |
| 93 | + // cannot be collapsed further without changing that interface. |
| 94 | + std::shared_ptr<::httpserver::http_response> |
| 95 | + render_get(const ::httpserver::http_request& r) override { |
| 96 | + return invoke_(http_method::get, r); |
| 97 | + } |
| 98 | + std::shared_ptr<::httpserver::http_response> |
| 99 | + render_post(const ::httpserver::http_request& r) override { |
| 100 | + return invoke_(http_method::post, r); |
| 101 | + } |
| 102 | + std::shared_ptr<::httpserver::http_response> |
| 103 | + render_put(const ::httpserver::http_request& r) override { |
| 104 | + return invoke_(http_method::put, r); |
| 105 | + } |
| 106 | + std::shared_ptr<::httpserver::http_response> |
| 107 | + render_delete(const ::httpserver::http_request& r) override { |
| 108 | + return invoke_(http_method::del, r); |
| 109 | + } |
| 110 | + std::shared_ptr<::httpserver::http_response> |
| 111 | + render_patch(const ::httpserver::http_request& r) override { |
| 112 | + return invoke_(http_method::patch, r); |
| 113 | + } |
| 114 | + std::shared_ptr<::httpserver::http_response> |
| 115 | + render_options(const ::httpserver::http_request& r) override { |
| 116 | + return invoke_(http_method::options, r); |
| 117 | + } |
| 118 | + std::shared_ptr<::httpserver::http_response> |
| 119 | + render_head(const ::httpserver::http_request& r) override { |
| 120 | + return invoke_(http_method::head, r); |
| 121 | + } |
| 122 | + |
| 123 | + private: |
| 124 | + std::shared_ptr<::httpserver::http_response> |
| 125 | + invoke_(http_method m, const ::httpserver::http_request& r) { |
| 126 | + auto& slot = slots_[static_cast<std::size_t>(m)]; |
| 127 | + // Invariant: set_slot stores the handler AND calls |
| 128 | + // set_allowing(method, true); the finalize_answer 405 path fires |
| 129 | + // before reaching invoke_ unless has_slot is true. A populated |
| 130 | + // slot is therefore guaranteed here. |
| 131 | + assert(slot); |
| 132 | + // DR-004 contract: lambda_handler returns http_response by value. |
| 133 | + // The make_shared here is the known cost of that ergonomic choice; |
| 134 | + // slot(r) is already a prvalue so this uses move-construction with |
| 135 | + // no extra copy. TASK-036 will address the dispatch cutover. |
| 136 | + return std::make_shared<::httpserver::http_response>(slot(r)); |
| 137 | + } |
| 138 | + |
| 139 | + std::array<lambda_handler, |
| 140 | + static_cast<std::size_t>(http_method::count_)> slots_{}; |
| 141 | +}; |
| 142 | + |
| 143 | +} // namespace detail |
| 144 | +} // namespace httpserver |
| 145 | + |
| 146 | +#endif // SRC_HTTPSERVER_DETAIL_LAMBDA_RESOURCE_HPP_ |
0 commit comments