Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Tell the user why the HTTP response should not have a body. #2260

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 7 additions & 3 deletions lib/http.js
Expand Up @@ -592,7 +592,7 @@ OutgoingMessage.prototype.write = function(chunk, encoding) {
} }


if (!this._hasBody) { if (!this._hasBody) {
console.error('This type of response MUST NOT have a body. ' + console.error('This type of response (' + this._noBodyReason + ') MUST NOT have a body. ' +
'Ignoring write() calls.'); 'Ignoring write() calls.');
return true; return true;
} }
Expand Down Expand Up @@ -654,7 +654,7 @@ OutgoingMessage.prototype.end = function(data, encoding) {
} }


if (data && !this._hasBody) { if (data && !this._hasBody) {
console.error('This type of response MUST NOT have a body. ' + console.error('This type of response (' + this._noBodyReason + ') MUST NOT have a body. ' +
'Ignoring data passed to end().'); 'Ignoring data passed to end().');
data = false; data = false;
} }
Expand Down Expand Up @@ -773,7 +773,10 @@ OutgoingMessage.prototype._flush = function() {
function ServerResponse(req) { function ServerResponse(req) {
OutgoingMessage.call(this); OutgoingMessage.call(this);


if (req.method === 'HEAD') this._hasBody = false; if (req.method === 'HEAD') {
this._hasBody = false;
this._noBodyReason = "HEAD";
Copy link
Member

Choose a reason for hiding this comment

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

I would strongly prefer it if you don't add another property to the ServerResponse class. It's never used in a well-debugged application so it only adds overhead.

Copy link
Author

Choose a reason for hiding this comment

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

It just seemed like the best way to do it. There are already so many I didn't think one more would be a blocker.

}


if (req.httpVersionMajor < 1 || req.httpVersionMinor < 1) { if (req.httpVersionMajor < 1 || req.httpVersionMinor < 1) {
this.useChunkedEncodingByDefault = false; this.useChunkedEncodingByDefault = false;
Expand Down Expand Up @@ -858,6 +861,7 @@ ServerResponse.prototype.writeHead = function(statusCode) {
// consisting only of the Status-Line and optional headers, and is // consisting only of the Status-Line and optional headers, and is
// terminated by an empty line. // terminated by an empty line.
this._hasBody = false; this._hasBody = false;
this._noBodyReason = statusCode;
} }


// don't keep alive connections where the client expects 100 Continue // don't keep alive connections where the client expects 100 Continue
Expand Down