Skip to content

Extract file-serving Range parser below the cognitive-complexity cap - #8561

Merged
Amaury Chamayou (achamayou) merged 4 commits into
mainfrom
achamayou-range-parser-extraction
Oct 16, 2026
Merged

Amaury Chamayou (achamayou) merged 4 commits into
mainfrom
achamayou-range-parser-extraction

Conversation

@achamayou

@achamayou Amaury Chamayou (achamayou) commented Oct 12, 2026 •

Copy link
Copy Markdown
Member

Motivation

The file-serving range helper scores 69 on readability-function-cognitive-complexity, against a threshold of 50, and carries a baseline suppression. Most of the complexity is Range-header parsing. This PR extracts the parser so the suppression can be removed without changing behaviour.

Related to #7358. The remaining suppressions are handled in #8560 and #8562.

Implementation summary

Move the parsing statements into detail::parse_range_header, which returns either a half-open FileRange or an owned error message. The caller keeps Range-header discovery, the single InvalidHeaderValue error response, and empty-range rejection. Every error message, clamping rule, and debug log is preserved; the nine set_error sites collapse into one at the call site.

Add node_frontend_test cases for parser error messages, malformed and overflowing inputs, accepted numeric prefixes and trailing text, open, suffix, and clamped ranges, SIZE_MAX boundaries, GET/HEAD metadata and body handling, and digest and conditional-request ordering.

Since #8214 landed on main, the caller is the templated fill_range_response; the extraction applies there unchanged. The new tests no longer assert on stream open/close state, since #8214 moved stream lifetime to the caller.

Measured with clang-tidy 21.1.8 and IgnoreMacros=true; the repository threshold stays at 50:

Function Before After
fill_range_response 69 28
detail::parse_range_header - 30

Safety and compatibility

Internal refactor with no API or data-format impact. A token comparison against the base shows the only differences are the eight removed set_error prefixes, range_header.value() becoming the const std::string& parameter, and the variant plumbing. Inclusive-end clamping before increment, suffix clamping before subtraction, and the non-zero-size precondition are unchanged. No view escapes the parser; error messages are owned strings.

Validation:

  • Built node_frontend_test on the merged tree and ran it; all cases pass, including the new ones.
  • scripts/ci-checks.sh passed without auto-fix.
  • CI passed on the pre-merge head, including the Debug CLANG_TIDY=ON build with the suppression removed. Required CI must pass again on the current head.

Preserve Range parsing and response behavior while reducing cognitive complexity below the existing cap. Add parser and caller regression coverage to node_frontend_test.

Refs #7358

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Resolve conflicts with #8214: apply the parser extraction to the templated fill_range_response and drop the test assertions on stream open/close state, since stream lifetime now belongs to the caller.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@achamayou
Amaury Chamayou (achamayou) marked this pull request as ready for review October 16, 2026 07:30
@achamayou
Amaury Chamayou (achamayou) requested a review from a team as a code owner October 16, 2026 07:30
Copilot AI balanced review requested due to automatic review settings October 16, 2026 07:30
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The focused extraction preserves existing behavior with no identified blocking issues; required CI must pass on the current head.

0 open findings

What changed in this PR

Extracts Range-header parsing from CCF’s file-serving response helper to meet the cognitive-complexity limit while preserving existing behavior.

Changes:

  • Introduces a parser returning half-open bounds or an owned error message.
  • Centralizes parser-error responses and removes the complexity suppression.
  • Adds regression coverage for parsing boundaries, GET/HEAD responses, digests, and conditional requests.

Custom instructions used

Applied the supplied CCF repository and security/safety guidance; instruction files were unavailable locally.

File Description
src/​node/​rpc/​test/​node_frontend_test.cpp Adds parser and response regression tests.
src/​node/​rpc/​file_serving_handlers.h Extracts parsing and removes the complexity suppression.

🧠 Review effort: Balanced


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@achamayou
Amaury Chamayou (achamayou) merged commit 5684e2b into main Oct 16, 2026
12 checks passed
@achamayou
Amaury Chamayou (achamayou) deleted the achamayou-range-parser-extraction branch October 16, 2026 11:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants