Skip to content

Commit

Permalink
http: throw error when attempting to write to response with no body
Browse files Browse the repository at this point in the history
  • Loading branch information
mertcanaltin committed Apr 25, 2023
1 parent ab3a54f commit f7fef0c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
5 changes: 1 addition & 4 deletions lib/_http_outgoing.js
Original file line number Diff line number Diff line change
Expand Up @@ -917,10 +917,7 @@ function write_(msg, chunk, encoding, callback, fromEnd) {
}

if (!msg._hasBody) {
debug('This type of response MUST NOT have a body. ' +
'Ignoring write() calls.');
process.nextTick(callback);
return true;
throw new ERR_STREAM_NULL_VALUES();
}

if (!fromEnd && msg.socket && !msg.socket.writableCorked) {
Expand Down
22 changes: 22 additions & 0 deletions test/parallel/test-http-outgoing-implicit-header.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';

require('../common');

const assert = require('assert');
const http = require('http');

const server = http.createServer((req, res) => {
res._hasBody = false;
res.end();
});

server.listen(3000, () => {
console.log('Server is listening on port 3000');

http.get('http://localhost:3000', (res) => {
assert.throws(() => {
res.write('test');
}, Error);
server.close();
});
});

0 comments on commit f7fef0c

Please sign in to comment.