Skip to content

fix: Don't overwrite Content-Length when set as header - #291

Merged
nielsenko merged 2 commits into
serverpod:mainfrom
nielsenko:fix-content-length-overwrite
Nov 27, 2025
Merged

nielsenko merged 2 commits into
serverpod:mainfrom
nielsenko:fix-content-length-overwrite

Conversation

@nielsenko

@nielsenko nielsenko commented Nov 27, 2025 •

Copy link
Copy Markdown
Collaborator

Description

Preserve Content-Length header when already set on the HTTP response. Previously, applyHeaders would overwrite a pre-set content length with the body's content length.

Related Issues

Pre-Launch Checklist

  • This update focuses on a single feature or bug fix.
  • I have read and followed the Dart Style Guide and formatted the code using dart format.
  • I have referenced at least one issue this PR fixes or is related to.
  • I have updated/added relevant documentation (doc comments with ///).
  • I have added new tests to verify the changes.
  • All existing and new tests pass successfully.
  • I have documented any breaking changes below.

Breaking Changes

  • No breaking changes.

Additional Notes

None.

Summary by CodeRabbit

  • Bug Fixes
    • Improved HTTP response header handling to preserve pre-set content-length values during header application, preventing unintended overwrites.

✏️ Tip: You can customize this high-level summary in your review settings.

@nielsenko nielsenko self-assigned this Nov 27, 2025
@coderabbitai

coderabbitai Bot commented Nov 27, 2025 •

Copy link
Copy Markdown
Contributor

Important

Review skipped

Auto reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

📝 Walkthrough

Walkthrough

The changes modify Content-Length header handling to preserve pre-existing values in response headers instead of overwriting them, and add test coverage to validate this behavior.

Changes

Cohort / File(s) Summary
HTTP Response Extension Logic
lib/src/adapter/io/http_response_extension.dart
Adds an early return after setting Content-Type header when responseHeaders.contentLength > 0, preventing content-length inference and chunked-encoding setup when content length is already pre-set.
Test Coverage
test/message/apply_headers_test.dart
Adds a test case asserting that applying headers with a Body containing a contentLength value does not overwrite a pre-existing response.headers.contentLength.
Configuration
pubspec.yaml
Minor version or dependency updates.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~5 minutes

  • Straightforward conditional logic addition with minimal scope and test coverage addition following an existing pattern.

Possibly related PRs

  • PR #161: Moves Content-Length responsibility from headers to Body object; complements this PR's focus on preserving existing header content-length values during header application.

Pre-merge checks and finishing touches

❌ Failed checks (1 inconclusive)
Check name Status Explanation Resolution
Linked Issues check ❓ Inconclusive The PR preserves pre-set Content-Length headers, which partially addresses issue #164's goal of preventing conflicts. However, the implementation doesn't fully meet the proposed solution of validating conflicting values and returning 500 errors. Clarify whether the early return approach adequately resolves issue #164's requirements or if explicit validation with error responses is still needed.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: preserving the Content-Length header when already set, which matches the code modifications and PR objectives.
Description check ✅ Passed The PR description covers all required template sections: detailed description of changes, linked issue reference, complete pre-launch checklist, breaking changes documentation, and follows repository guidelines.
Out of Scope Changes check ✅ Passed All changes focus on fixing Content-Length header handling: the extension logic prevents overwriting existing headers, and the test validates this behavior. Changes are directly aligned with issue #164.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@nielsenko

Copy link
Copy Markdown
Collaborator Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Nov 27, 2025

Copy link
Copy Markdown
Contributor
✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@codecov

codecov Bot commented Nov 27, 2025 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.99%. Comparing base (f081d9e) to head (0717f2f).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #291   +/-   ##
=======================================
  Coverage   91.99%   91.99%           
=======================================
  Files          97       97           
  Lines        3661     3662    +1     
  Branches     1880     1881    +1     
=======================================
+ Hits         3368     3369    +1     
  Misses        293      293           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@nielsenko
nielsenko force-pushed the fix-content-length-overwrite branch from 1777a97 to 08f1c53 Compare November 27, 2025 09:54
@nielsenko

Copy link
Copy Markdown
Collaborator Author

@coderabbitai full review

@coderabbitai

coderabbitai Bot commented Nov 27, 2025

Copy link
Copy Markdown
Contributor
✅ Actions performed

Full review triggered.

@coderabbitai coderabbitai Bot 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.

Actionable comments posted: 2

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f081d9e and 08f1c53.

📒 Files selected for processing (2)
  • lib/src/adapter/io/http_response_extension.dart (1 hunks)
  • test/message/apply_headers_test.dart (1 hunks)
🧰 Additional context used
📓 Path-based instructions (3)
**/*.dart

📄 CodeRabbit inference engine (.github/copilot-instructions.md)

**/*.dart: All Dart code must pass static analysis using dart analyze --fatal-infos with no issues
All Dart files must be formatted with dart format (CI enforces dart format --set-exit-if-changed .)

Files:

  • test/message/apply_headers_test.dart
  • lib/src/adapter/io/http_response_extension.dart
test/**/*.dart

📄 CodeRabbit inference engine (.github/copilot-instructions.md)

test/**/*.dart: Tests should follow the Given-When-Then pattern in descriptions (flexible structuring allowed)
Use Arrange-Act-Assert pattern within test bodies
Provide clear, descriptive test titles; prefer single responsibility per test unless related assertions improve clarity
Place tests in the test/ directory mirroring the lib/ structure

Files:

  • test/message/apply_headers_test.dart
lib/**/*.dart

📄 CodeRabbit inference engine (.github/copilot-instructions.md)

lib/**/*.dart: Use Uint8List for request/response bodies for performance; avoid List for body payloads
Use type-safe HTTP header parsing and validation when accessing headers
Use router with trie-based matching and symbol-based path parameters (e.g., #name, #age) for routing
Ensure WebSocket handling includes proper lifecycle management (e.g., ping/pong for connection health)

Files:

  • lib/src/adapter/io/http_response_extension.dart
🧠 Learnings (3)
📓 Common learnings
Learnt from: nielsenko
Repo: serverpod/relic PR: 38
File: lib/src/headers/exception/header_exception.dart:50-72
Timestamp: 2025-04-02T18:04:20.933Z
Learning: Test coverage for the `MissingHeaderException` class's `httpResponseBody` getter and `toString` method has been deferred to a later time.
Learnt from: nielsenko
Repo: serverpod/relic PR: 48
File: lib/src/handler/handler.dart:59-67
Timestamp: 2025-04-25T07:39:38.915Z
Learning: Nielsenko prefers using switch statements with pattern matching over if statements when working with sealed classes in Dart, as they provide exhaustiveness checking at compile time and can be more concise.
📚 Learning: 2025-10-09T16:21:09.310Z
Learnt from: CR
Repo: serverpod/relic PR: 0
File: .github/copilot-instructions.md:0-0
Timestamp: 2025-10-09T16:21:09.310Z
Learning: Applies to lib/**/*.dart : Use type-safe HTTP header parsing and validation when accessing headers

Applied to files:

  • test/message/apply_headers_test.dart
  • lib/src/adapter/io/http_response_extension.dart
📚 Learning: 2025-10-09T16:21:09.310Z
Learnt from: CR
Repo: serverpod/relic PR: 0
File: .github/copilot-instructions.md:0-0
Timestamp: 2025-10-09T16:21:09.310Z
Learning: Applies to test/**/*.dart : Provide clear, descriptive test titles; prefer single responsibility per test unless related assertions improve clarity

Applied to files:

  • test/message/apply_headers_test.dart
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (7)
  • GitHub Check: Run Unit Tests (macos-latest, beta, upgrade)
  • GitHub Check: Run Unit Tests (macos-latest, stable, downgrade)
  • GitHub Check: Run Unit Tests (macos-latest, beta, downgrade)
  • GitHub Check: Run Unit Tests (macos-latest, 3.7.0, downgrade)
  • GitHub Check: Run Unit Tests (macos-latest, stable, upgrade)
  • GitHub Check: Run Unit Tests (macos-latest, 3.7.0, upgrade)
  • GitHub Check: Run Unit Tests (windows-latest, beta, downgrade)

Comment thread test/message/apply_headers_test.dart Resolved
@nielsenko
nielsenko force-pushed the fix-content-length-overwrite branch from 08f1c53 to f956c54 Compare November 27, 2025 10:23
@nielsenko
nielsenko force-pushed the fix-content-length-overwrite branch from f956c54 to 0af1c99 Compare November 27, 2025 10:25
@nielsenko
nielsenko force-pushed the fix-content-length-overwrite branch from 0af1c99 to 0717f2f Compare November 27, 2025 10:28
@nielsenko
nielsenko marked this pull request as ready for review November 27, 2025 10:31
@nielsenko
nielsenko requested a review from SandPod November 27, 2025 10:31

@SandPod SandPod 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.

🚀

@nielsenko
nielsenko merged commit ac65f70 into serverpod:main Nov 27, 2025
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.

Add validation for conflicting Content-Length values between Body and Headers

2 participants