Commit 32dfdf1
webserver: decompose finalize_answer under the CCN 10 bar (46 -> 6)
finalize_answer was a 255-line monolith doing WebSocket upgrade
detection, v1 resource lookup with regex+LRU cache, centralized
auth, handler dispatch (with try/catch + method-not-allowed), and
response materialisation/queue all inline. CCN 46.
Decomposed into per-responsibility helpers on detail::webserver_impl
(which already has the parent back-pointer and includes the MHD
headers behind the PIMPL barrier):
try_handle_websocket_upgrade probe + delegate. optional<MHD_Result>.
validate_websocket_handshake RFC 6455 header validation.
complete_websocket_upgrade handler lookup, accept header, queue.
resolve_resource_for_request single-resource / exact / regex+cache.
lookup_route_cache LRU front-promote + match copy.
scan_regex_routes longest-match-wins linear scan.
store_route_cache LRU front-insert + eviction.
apply_extracted_params url_pars/chunks -> request args.
apply_auth_short_circuit centralized auth handler short-circuit.
dispatch_resource_handler try/catch around handler invocation
serialize_allow_methods method_set -> "GET, HEAD, ..." (TASK-021).
materialize_and_queue_response final stage with fallback path.
finalize_answer is now a 6-call orchestrator at CCN 6. Every new
helper sits at CCN <= 8.
Locking discipline preserved:
* resolve_resource_for_request takes the shared registered_resources
lock for the entire lookup phase and releases at scope exit, exactly
as the original {…} block did.
* lookup_route_cache / store_route_cache each take route_cache_mutex
internally (nested under registered_resources, matching the original
lock-order documented at the head of route_cache_v2).
* TASK-035: complete_websocket_upgrade takes the shared_ptr copy
under the shared lock before unlocking, preserving the
handler-alive-across-upgrade invariant.
Subtle reformulation in scan_regex_routes: the original guarded the
inner match() with `!found || pieces_len > len || (pieces_len == len
&& tot_len > tot_len)`. I inverted it to a `continue` for clarity
(skip when found AND not strictly longer in either dimension). The
boolean is identical.
Verified locally: full `make check` (all 48 unit tests + invariants).
scripts/check-complexity.sh CCN_MAX ratcheted 47 -> 35 (the new worst
offender is ip_representation::ip_representation at CCN 34).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>1 parent 60b4c64 commit 32dfdf1
3 files changed
Lines changed: 377 additions & 228 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
27 | 27 | | |
28 | 28 | | |
29 | 29 | | |
30 | | - | |
| 30 | + | |
31 | 31 | | |
32 | 32 | | |
33 | 33 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
50 | 50 | | |
51 | 51 | | |
52 | 52 | | |
| 53 | + | |
53 | 54 | | |
54 | 55 | | |
55 | 56 | | |
| |||
383 | 384 | | |
384 | 385 | | |
385 | 386 | | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
| 480 | + | |
386 | 481 | | |
387 | 482 | | |
388 | 483 | | |
| |||
0 commit comments