Skip to content

Commit 63aee3b

Browse files
Kasheraddaleax
authored andcommitted
http: OutgoingMessage change writable after end
When an OutgoingMessage is closed (for example, using the `end` method), its 'writable' property should be changed to false - since it is not writable anymore. The 'writable' property should have the opposite value of the 'finished' property. PR-URL: #14024 Fixes: #14023 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Gireesh Punathil <[email protected]>
1 parent 6f13d7d commit 63aee3b

3 files changed

+71
-0
lines changed

lib/_http_outgoing.js

+1
Original file line numberDiff line numberDiff line change
@@ -784,6 +784,7 @@ OutgoingMessage.prototype.end = function end(chunk, encoding, callback) {
784784
this.connection.uncork();
785785

786786
this.finished = true;
787+
this.writable = false;
787788

788789
// There is the first message on the outgoing queue, and we've sent
789790
// everything to the socket.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const http = require('http');
5+
6+
// Verify that after calling end() on an `OutgoingMessage` (or a type that
7+
// inherits from `OutgoingMessage`), its `writable` property is set to false.
8+
9+
const server = http.createServer(common.mustCall(function(req, res) {
10+
assert.strictEqual(res.writable, true);
11+
assert.strictEqual(res.finished, false);
12+
res.end();
13+
assert.strictEqual(res.writable, false);
14+
assert.strictEqual(res.finished, true);
15+
16+
server.close();
17+
}));
18+
19+
server.listen(0);
20+
21+
server.on('listening', common.mustCall(function() {
22+
const clientRequest = http.request({
23+
port: server.address().port,
24+
method: 'GET',
25+
path: '/'
26+
});
27+
28+
assert.strictEqual(clientRequest.writable, true);
29+
clientRequest.end();
30+
assert.strictEqual(clientRequest.writable, false);
31+
}));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
'use strict';
2+
const common = require('../common');
3+
const http = require('http');
4+
const util = require('util');
5+
const stream = require('stream');
6+
7+
// Verify that when piping a stream to an `OutgoingMessage` (or a type that
8+
// inherits from `OutgoingMessage`), if data is emitted after the
9+
// `OutgoingMessage` was closed - no `write after end` error is raised (this
10+
// should be the case when piping - when writing data directly to the
11+
// `OutgoingMessage` this error should be raised).
12+
13+
function MyStream() {
14+
stream.call(this);
15+
}
16+
util.inherits(MyStream, stream);
17+
18+
const server = http.createServer(common.mustCall(function(req, res) {
19+
const myStream = new MyStream();
20+
myStream.pipe(res);
21+
22+
process.nextTick(common.mustCall(() => {
23+
res.end();
24+
myStream.emit('data', 'some data');
25+
26+
// If we got here - 'write after end' wasn't raised and the test passed.
27+
process.nextTick(common.mustCall(() => server.close()));
28+
}));
29+
}));
30+
31+
server.listen(0);
32+
33+
server.on('listening', common.mustCall(function() {
34+
http.request({
35+
port: server.address().port,
36+
method: 'GET',
37+
path: '/'
38+
}).end();
39+
}));

0 commit comments

Comments
 (0)