Skip to content

Commit adba72d

Browse files
etrclaude
andcommitted
webserver: decompose on_methods_ under the CCN 10 bar (28 -> 7)
webserver::on_methods_ was a 171-line registration entry point doing input validation, v1-map find-or-create-shim, atomicity pre-check, slot installation, v1 insertion across three maps, and v2 3-tier table mirror (radix vs exact vs regex_, with fresh-insert vs methods- merge sub-paths). CCN 28. Decomposed into private member helpers on detail::webserver_impl: prepare_or_create_lambda_shim v1 find-or-create + atomicity pre-check commit_handlers_to_shim install the handler into every slot insert_fresh_v1_entries v1 maps insertion (str + regex tiers) upsert_v2_table_entry v2 3-tier orchestrator upsert_v2_param_route radix tier (read-merge-reinsert) insert_fresh_v2_entry exact / regex_ first-insert update_existing_v2_entry merge methods into existing entry on_methods_ on webserver now reads as: validate args, build endpoint, acquire registered_resources_mutex once for the v1 work, release it, do the v2 work under route_table_mutex_ internally, invalidate cache. CCN 7. Every new helper sits at CCN <= 7. Also dedupes the pre-check loop (line 539) vs commit loop (line 558) duplication flagged by `make lint-duplication` at low minimum-tokens sensitivity: extracts a file-scope `for_each_requested_method` template helper that iterates http_method::count_ in enum order (TASK-021's Allow-header serialization order) and invokes the callback only for methods set in the requested mask. Both former loops collapse to two- line callback invocations. Locking discipline preserved: registered_resources_mutex (unique_lock) held across the v1 helpers, dropped at end of inner scope; route_table_mutex_ held inside upsert_v2_table_entry only. Lock order is registered_resources -> route_table -> route_cache, identical to the original. Verified locally: full `make check`. scripts/check-complexity.sh CCN_MAX ratcheted 29 -> 23 (the new worst offender is http_endpoint::http_endpoint at CCN 22). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent b24eb7c commit adba72d

3 files changed

Lines changed: 207 additions & 143 deletions

File tree

scripts/check-complexity.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
set -euo pipefail
2828

2929
REPO_ROOT="$(cd "$(dirname "$0")/.." && pwd)"
30-
CCN_MAX="${CCN_MAX:-29}"
30+
CCN_MAX="${CCN_MAX:-23}"
3131

3232
# Prefer the standalone `lizard` entrypoint if it's on PATH; fall back to
3333
# `python3 -m lizard` which is what `pip install --user lizard` produces

src/httpserver/detail/webserver_impl.hpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
#include <atomic>
4646
#include <cstddef>
4747
#include <cstring>
48+
#include <functional>
4849
#include <list>
4950
#include <map>
5051
#include <memory>
@@ -86,6 +87,7 @@ class websocket_handler;
8687
namespace detail {
8788

8889
struct modded_request;
90+
class lambda_resource;
8991

9092
// connection_state: per-MHD_Connection arena anchor.
9193
//
@@ -478,6 +480,39 @@ class webserver_impl {
478480
MHD_Result materialize_and_queue_response(MHD_Connection* connection,
479481
modded_request* mr);
480482

483+
// Helpers carved out of webserver::on_methods_ to stay under the
484+
// cyclomatic-complexity bar. The orchestrator on_methods_ retains
485+
// input validation and the public ordering; everything that
486+
// mutates the v1 maps or the v2 3-tier table lives here.
487+
//
488+
// Caller must hold registered_resources_mutex (unique_lock) for
489+
// prepare_or_create_lambda_shim, commit_handlers_to_shim, and
490+
// insert_fresh_v1_entries. upsert_v2_table_entry takes
491+
// route_table_mutex_ internally.
492+
std::shared_ptr<detail::lambda_resource>
493+
prepare_or_create_lambda_shim(const detail::http_endpoint& idx,
494+
method_set methods,
495+
bool& fresh_out);
496+
void commit_handlers_to_shim(detail::lambda_resource& shim,
497+
method_set methods,
498+
const std::function<::httpserver::http_response(
499+
const ::httpserver::http_request&)>& handler);
500+
void insert_fresh_v1_entries(const detail::http_endpoint& idx,
501+
std::shared_ptr<::httpserver::http_resource> shim);
502+
void upsert_v2_table_entry(const detail::http_endpoint& idx,
503+
method_set methods,
504+
std::shared_ptr<::httpserver::http_resource> shim,
505+
bool fresh);
506+
void upsert_v2_param_route(const std::string& key,
507+
method_set methods,
508+
std::shared_ptr<::httpserver::http_resource> shim);
509+
void insert_fresh_v2_entry(const detail::http_endpoint& idx,
510+
method_set methods,
511+
std::shared_ptr<::httpserver::http_resource> shim);
512+
void update_existing_v2_entry(const std::string& key,
513+
method_set methods,
514+
std::shared_ptr<::httpserver::http_resource> shim);
515+
481516
MHD_Result complete_request(MHD_Connection* connection, modded_request* mr,
482517
const char* version, const char* method);
483518
struct MHD_Response* get_raw_response_with_fallback(modded_request* mr);

0 commit comments

Comments
 (0)