Skip to content

fix(responseInterceptor): handle bodyless responses (HEAD/1xx/204/304) with content-encoding#1251

Merged
chimurai merged 1 commit into
masterfrom
fix-responseInterceptor-bodyless-decompression-error
May 25, 2026
Merged

fix(responseInterceptor): handle bodyless responses (HEAD/1xx/204/304) with content-encoding#1251
chimurai merged 1 commit into
masterfrom
fix-responseInterceptor-bodyless-decompression-error

Conversation

@chimurai

@chimurai chimurai commented May 25, 2026

Copy link
Copy Markdown
Owner

fixes: #381
fixes: #658

reproduction:

const targetServer = http.createServer((req, res) => {
  res.statusCode = 204;
  res.setHeader('content-type', 'application/json; charset=utf-8');
  res.setHeader('content-encoding', 'gzip');
  res.end();
});


const app = express();
app.use(
  '/abc',
  createProxyMiddleware({
    target: targetUrl,
    changeOrigin: true,
    selfHandleResponse: true,
    on: {
      proxyRes: responseInterceptor(async (buffer) => buffer),
    },
  }),
);

response:

{
  "status": 200,
  "text": "Error fetching proxied request: unexpected end of file",
  "body": {},
  "headers": {
    "x-powered-by": "Express",
    "date": "Mon, 25 May 2026 10:45:02 GMT",
    "connection": "close",
    "content-length": "54"
  }
}

also mentioned in:

Summary by CodeRabbit

  • Bug Fixes

    • Fixed handling of bodyless HTTP responses (HEAD, 1xx, 204, 304) when upstream includes content-encoding headers. The response interceptor now correctly removes content-encoding from proxied response headers for these cases.
  • Tests

    • Added test coverage for bodyless responses with content-encoding headers.

Review Change Stack

@coderabbitai

coderabbitai Bot commented May 25, 2026

Copy link
Copy Markdown
📝 Walkthrough

Walkthrough

The PR fixes a bug in responseInterceptor where bodyless HTTP responses (HEAD, 1xx, 204, 304) with content-encoding headers caused decompression errors. The fix adds detection logic, skips content-encoding processing for bodyless responses, and exits early without writing a body. Tests validate the behavior across unit and e2e scopes.

Changes

Bodyless Response Handling

Layer / File(s) Summary
Bodyless response detection and interception fix
src/handlers/response-interceptor.ts, test/unit/response-interceptor.spec.ts
Added isBodylessResponse() helper to identify HEAD requests and 1xx/204/304 responses. Integrated helper to skip content-encoding decompression and to end the response early (after headers, without invoking the interceptor). Unit tests verify HEAD and 103 responses bypass interception and body writing.
End-to-end validation for bodyless responses with content-encoding
test/e2e/response-interceptor.spec.ts
E2E test suite validates 204 and 304 responses with upstream content-encoding (gzip/deflate) are returned bodyless, with content-encoding removed from proxy headers, and with other headers (cache-control, etag) preserved.
Documentation and configuration updates
CHANGELOG.md, cspell.json
Changelog documents the fix for bodyless responses with content-encoding. Spellcheck dictionary includes "bodyless".

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • chimurai/http-proxy-middleware#1249: Both PRs modify src/handlers/response-interceptor.ts to adjust header filtering around content-encoding in similar code paths (main PR handles bodyless responses, related PR addresses trailer/transfer-encoding conflicts).
  • chimurai/http-proxy-middleware#1250: Both PRs modify decompression and content-encoding handling in response-interceptor.ts (main PR skips decompression for bodyless statuses, related PR extends decompression support to include zstd).

Poem

🐰 A HEAD without a body, a 204 so bare,
No gzip, no deflate—just empty air!
The interceptor now knows when to stop and rest,
Bodyless responses handled best! ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The PR title clearly and specifically summarizes the main change: fixing responseInterceptor to handle bodyless responses with content-encoding headers.
Linked Issues check ✅ Passed The PR addresses all coding requirements from issue #658: handling bodyless responses (HEAD/1xx/204/304) with content-encoding, avoiding decompression errors, preserving original status codes, and preventing error responses.
Out of Scope Changes check ✅ Passed All changes are directly related to fixing responseInterceptor for bodyless responses: implementation, tests (unit and e2e), changelog, and spell-check additions are all in-scope.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix-responseInterceptor-bodyless-decompression-error

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.

@chimurai chimurai added bug and removed bug labels May 25, 2026
@chimurai chimurai added this to the v4.1 milestone May 25, 2026
@pkg-pr-new

pkg-pr-new Bot commented May 25, 2026

Copy link
Copy Markdown
npm i https://pkg.pr.new/http-proxy-middleware@1251

commit: 35b90cf

@coveralls

Copy link
Copy Markdown

Coverage Status

coverage: 94.815% (+0.07%) from 94.75% — fix-responseInterceptor-bodyless-decompression-error into master

@chimurai chimurai merged commit c65741c into master May 25, 2026
26 checks passed
@chimurai chimurai deleted the fix-responseInterceptor-bodyless-decompression-error branch May 25, 2026 12:01
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.

[bug] Error fetching proxied request: unexpected end of file Webworker proxy with If-Modified-Since header

2 participants