http: misc optimizations and style fixes
PR-URL: https://github.com/nodejs/node/pull/10558 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Evan Lucas <evanlucas@me.com>
This commit is contained in:
parent
73d9445782
commit
176cdc2823
@ -136,36 +136,32 @@ OutgoingMessage.prototype._send = function _send(data, encoding, callback) {
|
||||
|
||||
OutgoingMessage.prototype._writeRaw = _writeRaw;
|
||||
function _writeRaw(data, encoding, callback) {
|
||||
const conn = this.connection;
|
||||
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;
|
||||
}
|
||||
|
||||
var connection = this.connection;
|
||||
if (connection &&
|
||||
connection._httpMessage === this &&
|
||||
connection.writable &&
|
||||
!connection.destroyed) {
|
||||
if (conn && conn._httpMessage === this && conn.writable && !conn.destroyed) {
|
||||
// There might be pending data in the this.output buffer.
|
||||
var outputLength = this.output.length;
|
||||
if (outputLength > 0) {
|
||||
this._flushOutput(connection);
|
||||
} else if (data.length === 0) {
|
||||
if (this.output.length) {
|
||||
this._flushOutput(conn);
|
||||
} else if (!data.length) {
|
||||
if (typeof callback === 'function')
|
||||
process.nextTick(callback);
|
||||
return true;
|
||||
}
|
||||
|
||||
// Directly write to socket.
|
||||
return connection.write(data, encoding, callback);
|
||||
} else if (connection && connection.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;
|
||||
} else {
|
||||
// buffer, as long as we're not destroyed.
|
||||
return this._buffer(data, encoding, callback);
|
||||
return conn.write(data, encoding, callback);
|
||||
}
|
||||
// Buffer, as long as we're not destroyed.
|
||||
return this._buffer(data, encoding, callback);
|
||||
}
|
||||
|
||||
|
||||
@ -477,6 +473,7 @@ Object.defineProperty(OutgoingMessage.prototype, 'headersSent', {
|
||||
});
|
||||
|
||||
|
||||
const crlf_buf = Buffer.from('\r\n');
|
||||
OutgoingMessage.prototype.write = function write(chunk, encoding, callback) {
|
||||
if (this.finished) {
|
||||
var err = new Error('write after end');
|
||||
@ -583,9 +580,6 @@ OutgoingMessage.prototype.addTrailers = function addTrailers(headers) {
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
const crlf_buf = Buffer.from('\r\n');
|
||||
|
||||
function onFinish(outmsg) {
|
||||
outmsg.emit('finish');
|
||||
}
|
||||
|
@ -170,8 +170,8 @@ function writeHead(statusCode, reason, obj) {
|
||||
this.statusMessage = reason;
|
||||
} else {
|
||||
// writeHead(statusCode[, headers])
|
||||
this.statusMessage =
|
||||
this.statusMessage || STATUS_CODES[statusCode] || 'unknown';
|
||||
if (!this.statusMessage)
|
||||
this.statusMessage = STATUS_CODES[statusCode] || 'unknown';
|
||||
obj = reason;
|
||||
}
|
||||
this.statusCode = statusCode;
|
||||
@ -514,9 +514,8 @@ function parserOnIncoming(server, socket, state, req, keepAlive) {
|
||||
// so that we don't become overwhelmed by a flood of
|
||||
// pipelined requests that may never be resolved.
|
||||
if (!socket._paused) {
|
||||
var needPause = socket._writableState.needDrain ||
|
||||
state.outgoingData >= socket._writableState.highWaterMark;
|
||||
if (needPause) {
|
||||
var ws = socket._writableState;
|
||||
if (ws.needDrain || state.outgoingData >= ws.highWaterMark) {
|
||||
socket._paused = true;
|
||||
// We also need to pause the parser, but don't do that until after
|
||||
// the call to execute, because we may still be processing the last
|
||||
@ -542,9 +541,7 @@ function parserOnIncoming(server, socket, state, req, keepAlive) {
|
||||
|
||||
// When we're finished writing the response, check if this is the last
|
||||
// response, if so destroy the socket.
|
||||
var finish =
|
||||
resOnFinish.bind(undefined, req, res, socket, state);
|
||||
res.on('finish', finish);
|
||||
res.on('finish', resOnFinish.bind(undefined, req, res, socket, state));
|
||||
|
||||
if (req.headers.expect !== undefined &&
|
||||
(req.httpVersionMajor === 1 && req.httpVersionMinor === 1)) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user