Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 30 additions & 5 deletions lib/_http_outgoing.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const {
ERR_METHOD_NOT_IMPLEMENTED,
ERR_STREAM_CANNOT_PIPE,
ERR_STREAM_ALREADY_FINISHED,
ERR_STREAM_DESTROYED,
ERR_STREAM_WRITE_AFTER_END
},
hideStackFrames
Expand All @@ -57,6 +58,7 @@ const HIGH_WATER_MARK = getDefaultHighWaterMark();
const { CRLF, debug } = common;

const kIsCorked = Symbol('isCorked');
const kErrorEmitted = Symbol('errorEmitted');

const RE_CONN_CLOSE = /(?:^|\W)close(?:$|\W)/i;
const RE_TE_CHUNKED = common.chunkExpression;
Expand Down Expand Up @@ -284,17 +286,40 @@ OutgoingMessage.prototype._send = function _send(data, encoding, callback) {
OutgoingMessage.prototype._writeRaw = _writeRaw;
function _writeRaw(data, encoding, callback) {
const conn = this.socket;
if (conn && conn.destroyed) {
// The socket was destroyed. If we're still trying to write to it,
// then we haven't gotten the 'close' event yet.
return false;
}

if (typeof encoding === 'function') {
callback = encoding;
encoding = null;
}

if (conn && conn.destroyed) {
const err = new ERR_STREAM_DESTROYED('write');
if (callback) {
callback(err);
}

// We check if there is a listener for 'error' as a backward-compatible fix.
// TODO(mcollina): remove in a follow-up, semver-major PR.
if (this.listenerCount('error') > 0 &&
this[kErrorEmitted] === undefined &&
!conn._writableState.errorEmitted &&
!conn._hadError) { // _hadError could be set by ClientRequest.

// TODO(mcollina): clean up kErrorEmitted and _hadError
// they can likely be the same state variable.
this[kErrorEmitted] = true;

// If the message is a ClientRequest, we avoid emitting 'error' more
// than once.
conn._hadError = true;

process.nextTick(writeAfterEndNT, this, err);
}
// The socket was destroyed. If we're still trying to write to it,
// then we haven't gotten the 'close' event yet.
return false;
}

if (conn && conn._httpMessage === this && conn.writable) {
// There might be pending data in the this.output buffer.
if (this.outputData.length) {
Expand Down
39 changes: 37 additions & 2 deletions lib/_http_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,16 @@ const {
} = require('internal/http');
const {
defaultTriggerAsyncIdScope,
getOrSetAsyncId
getOrSetAsyncId,
symbols: { async_id_symbol }
} = require('internal/async_hooks');
const { IncomingMessage } = require('_http_incoming');
const {
ERR_HTTP_HEADERS_SENT,
ERR_HTTP_INVALID_STATUS_CODE,
ERR_INVALID_ARG_TYPE,
ERR_INVALID_CHAR
ERR_INVALID_CHAR,
ERR_STREAM_WRITE_AFTER_END
} = require('internal/errors').codes;
const Buffer = require('buffer').Buffer;
const {
Expand Down Expand Up @@ -216,11 +218,44 @@ ServerResponse.prototype.detachSocket = function detachSocket(socket) {
};

ServerResponse.prototype.writeContinue = function writeContinue(cb) {
if (this.finished) {
const err = new ERR_STREAM_WRITE_AFTER_END();
const triggerAsyncId = this.socket ?
this.socket[async_id_symbol] : undefined;
defaultTriggerAsyncIdScope(triggerAsyncId,
process.nextTick,
writeAfterEndNT,
this,
err,
cb);

return true;
}

this._writeRaw(`HTTP/1.1 100 Continue${CRLF}${CRLF}`, 'ascii', cb);
this._sent100 = true;
};

function writeAfterEndNT(msg, err, callback) {
msg.emit('error', err);
if (callback) callback(err);
}

ServerResponse.prototype.writeProcessing = function writeProcessing(cb) {
if (this.finished) {
const err = new ERR_STREAM_WRITE_AFTER_END();
const triggerAsyncId = this.socket ?
this.socket[async_id_symbol] : undefined;
defaultTriggerAsyncIdScope(triggerAsyncId,
process.nextTick,
writeAfterEndNT,
this,
err,
cb);

return true;
}

this._writeRaw(`HTTP/1.1 102 Processing${CRLF}${CRLF}`, 'ascii', cb);
};

Expand Down
9 changes: 8 additions & 1 deletion test/parallel/test-http-destroyed-socket-write2.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ server.listen(0, function() {
});

function write() {
req.write('hello', function() {
req.write('hello', function(err) {
if (err) {
return;
}
setImmediate(write);
});
}
Expand All @@ -52,6 +55,10 @@ server.listen(0, function() {
case 'ECONNRESET':
break;

// This is also ok, depends on the operating system
case 'ERR_STREAM_DESTROYED':
break;

// On Windows, this sometimes manifests as ECONNABORTED
case 'ECONNABORTED':
break;
Expand Down
90 changes: 90 additions & 0 deletions test/parallel/test-http-outgoing-socket-destroyed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
'use strict';

const common = require('../common');
const { createServer, request } = require('http');

{
const server = createServer((req, res) => {
server.close();

req.socket.destroy();

res.write('hello', common.expectsError({
code: 'ERR_STREAM_DESTROYED'
}));
});

server.listen(0, common.mustCall(() => {
const req = request({
host: 'localhost',
port: server.address().port
});

req.on('response', common.mustNotCall());
req.on('error', common.expectsError({
code: 'ECONNRESET'
}));

req.end();
}));
}

{
const server = createServer((req, res) => {
server.close();

req.socket.destroy();

const onError = common.expectsError({
code: 'ERR_STREAM_DESTROYED'
});

res.on('error', (err) => onError(err));
res.write('hello');
});

server.listen(0, common.mustCall(() => {
const req = request({
host: 'localhost',
port: server.address().port
});

req.on('response', common.mustNotCall());
req.on('error', common.mustCall());
Copy link
Member

Choose a reason for hiding this comment

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

If I am not mistaken, this results in the ERR_STREAM_DESTROYED as well?

Suggested change
req.on('error', common.mustCall());
req.on('error', common.expectsError({
code: 'ERR_STREAM_DESTROYED'
}));

Copy link
Member

Choose a reason for hiding this comment

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

ping @mcollina Does that suggestion above seem good to you? (Seems good to me....)

Copy link
Member Author

Choose a reason for hiding this comment

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

That's going to be 'ECONNRESET' on my machine.


req.end();
}));
}

{
const server = createServer((req, res) => {
res.write('hello');
req.resume();

const onError = common.expectsError({
code: 'ERR_STREAM_DESTROYED'
});

res.on('close', () => {
res.write('world');
});

res.on('error', (err) => {
onError(err);
server.close();
});
});

server.listen(0, common.mustCall(() => {
const req = request({
host: 'localhost',
port: server.address().port
});

req.on('response', common.mustCall(() => {
req.socket.destroy();
}));

req.end();
}));
}
28 changes: 28 additions & 0 deletions test/parallel/test-http-write-continue-write-after-end.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict';

const common = require('../common');
const { createServer, request } = require('http');

{
const server = createServer((req, res) => {
server.close();

res.end();
res.writeContinue();

res.on('error', common.expectsError({
code: 'ERR_STREAM_WRITE_AFTER_END'
}));
});

server.listen(0, common.mustCall(() => {
const req = request({
host: 'localhost',
port: server.address().port
});

req.on('response', common.mustCall((res) => res.resume()));

req.end();
}));
}
28 changes: 28 additions & 0 deletions test/parallel/test-http-write-processing-write-after-end.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict';

const common = require('../common');
const { createServer, request } = require('http');

{
const server = createServer((req, res) => {
server.close();

res.end();
res.writeProcessing();

res.on('error', common.expectsError({
code: 'ERR_STREAM_WRITE_AFTER_END'
}));
});

server.listen(0, common.mustCall(() => {
const req = request({
host: 'localhost',
port: server.address().port
});

req.on('response', common.mustCall((res) => res.resume()));

req.end();
}));
}