Skip to content

Conversation

mcollina
Copy link
Member

@mcollina mcollina commented Oct 5, 2025

This relates to...

Fixes #4594
Related to #4612

Rationale

This PR addresses the discrepancy in behavior between HTTP/1 and HTTP/2 when handling empty POST requests.

According to RFC 9110:

A user agent SHOULD send Content-Length in a request when the method defines a meaning for enclosed content and it is not sending Transfer-Encoding. For example, a user agent normally sends Content-Length in a POST request even when the value is 0 (indicating empty content).

Previously:

  • HTTP/1: Empty POST requests correctly included Content-Length: 0
  • HTTP/2: Empty POST requests omitted the Content-Length header entirely

This inconsistency causes compatibility issues with servers like Internet Information Server (IIS), which reject empty POST requests without a Content-Length header, returning 411 Length Required.

Changes

Implementation (lib/dispatcher/client-h2.js)

  • Modified the logic to only omit Content-Length when both contentLength === 0 AND the method doesn't expect a payload (like GET, HEAD)
  • For methods that expect payloads (POST, PUT, PATCH, etc.), Content-Length: 0 is now sent even when the body is empty
  • Updated the assertion to allow contentLength === 0 without a body

Tests (test/fetch/http2.js)

  • Updated the test to verify that empty POST requests with HTTP/2 now include Content-Length: 0
  • Updated test name and comments to reflect the corrected behavior
  • Both HTTP/1 and HTTP/2 tests now expect consistent behavior

Test Results

All HTTP/2 tests pass (51 tests total):

  • [Fetch] Empty POST without h2 has Content-Length
  • [Fetch] Empty POST with h2 has Content-Length

Breaking Changes and Deprecations

None. This is a bug fix that aligns behavior with RFC 9110 recommendations and HTTP/1 implementation.

Status

🤖 Generated with Claude Code

fredericDelaporte and others added 2 commits October 5, 2025 17:42
This change aligns HTTP/2 behavior with HTTP/1 by ensuring that empty
POST requests (and other methods that expect payloads) include a
Content-Length: 0 header.

According to RFC 9110, a user agent SHOULD send Content-Length in a
request when the method defines a meaning for enclosed content, even
when the value is 0 (indicating empty content).

Previously, HTTP/2 requests would omit the Content-Length header for
empty POST requests, while HTTP/1 requests correctly included it. This
inconsistency could cause issues with servers like IIS that reject
empty POST requests without a Content-Length header (returning 411
Length Required).

Changes:
- Modified lib/dispatcher/client-h2.js to only omit Content-Length when
  both contentLength is 0 AND the method doesn't expect a payload
- Updated the assertion to allow contentLength === 0 without a body
- Updated test to verify Content-Length: 0 is sent for empty POST with H2
- Updated test comments to reflect the corrected behavior

Fixes #4594
Ref #4612

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
Signed-off-by: Matteo Collina <hello@matteocollina.com>
@codecov-commenter
Copy link

codecov-commenter commented Oct 5, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.93%. Comparing base (96fb72c) to head (e83e4e3).
⚠️ Report is 5 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4613      +/-   ##
==========================================
- Coverage   92.93%   92.93%   -0.01%     
==========================================
  Files         106      106              
  Lines       32973    33091     +118     
==========================================
+ Hits        30645    30753     +108     
- Misses       2328     2338      +10     

☔ 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.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@metcoder95
Copy link
Member

CI failures are unrelated

Co-authored-by: Frédéric Delaporte <12201973+fredericDelaporte@users.noreply.github.com>
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.

4 participants