|
| 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-045 -- Out-of-line bodies for hook_handle and peer_address. |
| 22 | +// |
| 23 | +// hook_handle's destructor + remove() + move ops need the complete |
| 24 | +// type of detail::webserver_impl (the per-phase vectors live there), |
| 25 | +// so the bodies live here, not in the public header. Same pattern as |
| 26 | +// http_response's move/dtor. |
| 27 | + |
| 28 | +#include "httpserver/hook_handle.hpp" |
| 29 | + |
| 30 | +#include <cstdio> |
| 31 | +#include <mutex> |
| 32 | +#include <shared_mutex> |
| 33 | +#include <string> |
| 34 | +#include <utility> |
| 35 | + |
| 36 | +#include "httpserver/hook_context.hpp" |
| 37 | +#include "httpserver/detail/webserver_impl.hpp" |
| 38 | + |
| 39 | +namespace httpserver { |
| 40 | + |
| 41 | +// ---- hook_handle --------------------------------------------------------- |
| 42 | + |
| 43 | +hook_handle::hook_handle(hook_handle&& other) noexcept |
| 44 | + : impl_(other.impl_), |
| 45 | + slot_id_(other.slot_id_), |
| 46 | + phase_(other.phase_), |
| 47 | + armed_(other.armed_) { |
| 48 | + // Disarm the source so its destructor is a no-op. |
| 49 | + other.armed_ = false; |
| 50 | + other.impl_ = nullptr; |
| 51 | +} |
| 52 | + |
| 53 | +hook_handle& hook_handle::operator=(hook_handle&& other) noexcept { |
| 54 | + if (this != &other) { |
| 55 | + // Remove any registration this object currently owns before |
| 56 | + // taking over the source's. remove() is idempotent and |
| 57 | + // noexcept, so this is safe even if `armed_` is already false. |
| 58 | + remove(); |
| 59 | + impl_ = other.impl_; |
| 60 | + slot_id_ = other.slot_id_; |
| 61 | + phase_ = other.phase_; |
| 62 | + armed_ = other.armed_; |
| 63 | + other.armed_ = false; |
| 64 | + other.impl_ = nullptr; |
| 65 | + } |
| 66 | + return *this; |
| 67 | +} |
| 68 | + |
| 69 | +hook_handle::~hook_handle() { |
| 70 | + if (armed_) { |
| 71 | + remove(); |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +void hook_handle::remove() noexcept { |
| 76 | + if (!armed_ || impl_ == nullptr) { |
| 77 | + return; |
| 78 | + } |
| 79 | + // Snapshot the source state and disarm BEFORE doing the erase, so |
| 80 | + // any exception from the lambda's destructor (very unlikely, but |
| 81 | + // remove() is declared noexcept and std::terminate is the |
| 82 | + // observable outcome anyway) does not leave the handle in a |
| 83 | + // half-removed state. |
| 84 | + auto* impl = impl_; |
| 85 | + const auto phase = phase_; |
| 86 | + const auto id = slot_id_; |
| 87 | + armed_ = false; |
| 88 | + impl_ = nullptr; |
| 89 | + |
| 90 | + std::unique_lock lock(impl->hook_table_mutex_); |
| 91 | + |
| 92 | + // Linear scan for the matching slot. Phase vectors are tiny in |
| 93 | + // practice (single-digit hook counts). A not-found result is the |
| 94 | + // idempotent no-op case: the slot was already erased by an earlier |
| 95 | + // remove() or never inserted (defensive). Either way we still |
| 96 | + // re-evaluate the any_hooks_ gate against the current vector |
| 97 | + // emptiness. |
| 98 | + auto erase_if_found = [id](auto& vec) -> bool { |
| 99 | + for (auto it = vec.begin(); it != vec.end(); ++it) { |
| 100 | + if (it->slot_id == id) { |
| 101 | + vec.erase(it); |
| 102 | + return true; |
| 103 | + } |
| 104 | + } |
| 105 | + return false; |
| 106 | + }; |
| 107 | + auto reset_gate_if_empty = [impl, phase](const auto& vec) { |
| 108 | + if (vec.empty()) { |
| 109 | + impl->any_hooks_[static_cast<std::size_t>(phase)].store( |
| 110 | + false, std::memory_order_release); |
| 111 | + } |
| 112 | + }; |
| 113 | + |
| 114 | + switch (phase) { |
| 115 | + case hook_phase::connection_opened: |
| 116 | + erase_if_found(impl->hooks_connection_opened_); |
| 117 | + reset_gate_if_empty(impl->hooks_connection_opened_); |
| 118 | + break; |
| 119 | + case hook_phase::accept_decision: |
| 120 | + erase_if_found(impl->hooks_accept_decision_); |
| 121 | + reset_gate_if_empty(impl->hooks_accept_decision_); |
| 122 | + break; |
| 123 | + case hook_phase::request_received: |
| 124 | + erase_if_found(impl->hooks_request_received_); |
| 125 | + reset_gate_if_empty(impl->hooks_request_received_); |
| 126 | + break; |
| 127 | + case hook_phase::body_chunk: |
| 128 | + erase_if_found(impl->hooks_body_chunk_); |
| 129 | + reset_gate_if_empty(impl->hooks_body_chunk_); |
| 130 | + break; |
| 131 | + case hook_phase::route_resolved: |
| 132 | + erase_if_found(impl->hooks_route_resolved_); |
| 133 | + reset_gate_if_empty(impl->hooks_route_resolved_); |
| 134 | + break; |
| 135 | + case hook_phase::before_handler: |
| 136 | + erase_if_found(impl->hooks_before_handler_); |
| 137 | + reset_gate_if_empty(impl->hooks_before_handler_); |
| 138 | + break; |
| 139 | + case hook_phase::handler_exception: |
| 140 | + erase_if_found(impl->hooks_handler_exception_); |
| 141 | + reset_gate_if_empty(impl->hooks_handler_exception_); |
| 142 | + break; |
| 143 | + case hook_phase::after_handler: |
| 144 | + erase_if_found(impl->hooks_after_handler_); |
| 145 | + reset_gate_if_empty(impl->hooks_after_handler_); |
| 146 | + break; |
| 147 | + case hook_phase::response_sent: |
| 148 | + erase_if_found(impl->hooks_response_sent_); |
| 149 | + reset_gate_if_empty(impl->hooks_response_sent_); |
| 150 | + break; |
| 151 | + case hook_phase::request_completed: |
| 152 | + erase_if_found(impl->hooks_request_completed_); |
| 153 | + reset_gate_if_empty(impl->hooks_request_completed_); |
| 154 | + break; |
| 155 | + case hook_phase::connection_closed: |
| 156 | + erase_if_found(impl->hooks_connection_closed_); |
| 157 | + reset_gate_if_empty(impl->hooks_connection_closed_); |
| 158 | + break; |
| 159 | + case hook_phase::count_: |
| 160 | + // Unreachable: an armed handle always carries a valid phase. |
| 161 | + break; |
| 162 | + } |
| 163 | +} |
| 164 | + |
| 165 | +hook_handle hook_handle::detach() && noexcept { |
| 166 | + // Construct a disarmed handle from the same data; do NOT call the |
| 167 | + // private armed-ctor because the spec says detach() leaves the |
| 168 | + // underlying registration in place (no impl interaction). |
| 169 | + hook_handle out; |
| 170 | + out.impl_ = impl_; |
| 171 | + out.slot_id_ = slot_id_; |
| 172 | + out.phase_ = phase_; |
| 173 | + out.armed_ = false; // detach() = "destructor will not touch impl" |
| 174 | + // Disarm the source so its destructor is also a no-op. |
| 175 | + armed_ = false; |
| 176 | + impl_ = nullptr; |
| 177 | + return out; |
| 178 | +} |
| 179 | + |
| 180 | +// ---- peer_address::to_string --------------------------------------------- |
| 181 | + |
| 182 | +std::string peer_address::to_string() const { |
| 183 | + // No <netinet/in.h> / inet_ntop here so we keep this TU free of |
| 184 | + // backend-platform headers. The format is canonical-enough for |
| 185 | + // log lines without dragging in the full POSIX socket surface. |
| 186 | + // 46 is POSIX INET6_ADDRSTRLEN; we round up for snprintf's NUL. |
| 187 | + char buf[64]; |
| 188 | + switch (fam) { |
| 189 | + case family::ipv4: |
| 190 | + std::snprintf(buf, sizeof(buf), "%u.%u.%u.%u", |
| 191 | + static_cast<unsigned>(bytes[0]), |
| 192 | + static_cast<unsigned>(bytes[1]), |
| 193 | + static_cast<unsigned>(bytes[2]), |
| 194 | + static_cast<unsigned>(bytes[3])); |
| 195 | + return std::string{buf}; |
| 196 | + case family::ipv6: { |
| 197 | + // Group as eight uint16_t big-endian words, colon-separated. |
| 198 | + // Skip zero-compression for simplicity at TASK-045; TASK-046 |
| 199 | + // can refine when telemetry/log requirements firm up. |
| 200 | + std::snprintf(buf, sizeof(buf), |
| 201 | + "%x:%x:%x:%x:%x:%x:%x:%x", |
| 202 | + (bytes[0] << 8) | bytes[1], |
| 203 | + (bytes[2] << 8) | bytes[3], |
| 204 | + (bytes[4] << 8) | bytes[5], |
| 205 | + (bytes[6] << 8) | bytes[7], |
| 206 | + (bytes[8] << 8) | bytes[9], |
| 207 | + (bytes[10] << 8) | bytes[11], |
| 208 | + (bytes[12] << 8) | bytes[13], |
| 209 | + (bytes[14] << 8) | bytes[15]); |
| 210 | + return std::string{buf}; |
| 211 | + } |
| 212 | + case family::unspec: |
| 213 | + default: |
| 214 | + return std::string{}; |
| 215 | + } |
| 216 | +} |
| 217 | + |
| 218 | +} // namespace httpserver |
0 commit comments