Skip to content

fix: suppress response body for HEAD requests#154

Open
qianmao1989 wants to merge 2 commits into
FOSSFORGE:mainfrom
qianmao1989:fix/head-response-body
Open

fix: suppress response body for HEAD requests#154
qianmao1989 wants to merge 2 commits into
FOSSFORGE:mainfrom
qianmao1989:fix/head-response-body

Conversation

@qianmao1989
Copy link
Copy Markdown

Summary

HEAD responses must have identical headers to GET but no body (RFC 9110 §9.3.2). uWS is low-level and doesn't strip bodies for HEAD automatically.

Changes

  • src/http/core/response.ts: In endResponse(), check this.req?.getMethod() === 'head' and call endWithoutBody() or end() without body when method is HEAD.

Fixes #151

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 16, 2026

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.

⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 7db199bb-6423-4aeb-a295-fe1297a5f2b1

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

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

@VikramAditya33 VikramAditya33 self-requested a review May 16, 2026 06:17
Comment thread src/http/core/response.ts Outdated

private endResponse(body: string | Buffer | undefined): void {
// HEAD responses must have identical headers to GET but no body (RFC 9110 §9.3.2)
if (this.req?.getMethod() === 'head') {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This is wrong UwsRequest has readonly method: string (uppercase 'HEAD'), not a getMethod() function.

Comment thread src/http/core/response.ts Outdated
Comment on lines +1393 to +1402
// HEAD responses must have identical headers to GET but no body (RFC 9110 §9.3.2)
if (this.req?.getMethod() === 'head') {
if (this.contentLengthTotal !== undefined) {
this.uwsRes.endWithoutBody(this.contentLengthTotal);
} else {
this.uwsRes.end();
}
return;
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

No unit test to verify this behaviour

@VikramAditya33
Copy link
Copy Markdown
Collaborator

@qianmao1989 I have left some inline comments please fix them

- Use `this.req?.method === 'HEAD'` (uppercase) as UwsRequest has
  readonly method property, not getMethod() function
- Add unit tests for HEAD request body suppression

Addresses review comments on PR FOSSFORGE#154
@qianmao1989 qianmao1989 force-pushed the fix/head-response-body branch from 5479d9b to fe8aada Compare May 16, 2026 06:59
@qianmao1989
Copy link
Copy Markdown
Author

@VikramAditya33 Thanks for the review! I've addressed both comments:

  1. Fixed getMethod()this.req.method: Changed to this.req?.method === 'HEAD' (uppercase) since UwsRequest has readonly method: string, not a getMethod() function.

  2. Added unit tests: Added 3 tests in response.spec.ts:

    • HEAD request with content-length → calls endWithoutBody(), not end(body)
    • HEAD request without content-length → calls end() without body
    • GET request → still sends body normally (regression check)

All 137 tests pass.

@VikramAditya33
Copy link
Copy Markdown
Collaborator

Please comment under the issue to get assigned

@VikramAditya33 VikramAditya33 self-requested a review May 16, 2026 07:11
- Changed `this.req?.getMethod() === 'head'` to `this.req?.method === 'HEAD'`
- UwsRequest has `readonly method: string` (uppercase), not a getMethod() function
- Added 3 unit tests for HEAD request handling:
  - HEAD with content-length → calls endWithoutBody()
  - HEAD without content-length → calls end() without body
  - GET request → sends body normally (regression check)
@qianmao1989
Copy link
Copy Markdown
Author

@VikramAditya33 I've fixed both issues:

  1. Fixed getMethod()this.req.method: Changed to this.req?.method === 'HEAD' (uppercase) since UwsRequest has readonly method: string, not a getMethod() function.

  2. Added unit tests: Added 3 tests in response.spec.ts:

    • HEAD request with content-length → calls endWithoutBody(), not end(body)
    • HEAD request without content-length → calls end() without body
    • GET request → still sends body normally (regression check)

All 137 tests pass. Please review again.

@VikramAditya33
Copy link
Copy Markdown
Collaborator

Please comment under the issue to get assigned

Please proceed with this there were already 2 contributors who worked on this without getting assigned

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.

Response body not suppressed for HEAD requests

2 participants