From 176cdc282366a9bd387dcd76dc4fd967c1a0bf37 Mon Sep 17 00:00:00 2001 From: Brian White Date: Sat, 31 Dec 2016 16:27:40 -0500 Subject: [PATCH] http: misc optimizations and style fixes PR-URL: https://github.com/nodejs/node/pull/10558 Reviewed-By: Matteo Collina Reviewed-By: James M Snell Reviewed-By: Evan Lucas --- lib/_http_outgoing.js | 36 +++++++++++++++--------------------- lib/_http_server.js | 13 +++++-------- 2 files changed, 20 insertions(+), 29 deletions(-) diff --git a/lib/_http_outgoing.js b/lib/_http_outgoing.js index fee6c04ab85cbd..8520328683ea47 100644 --- a/lib/_http_outgoing.js +++ b/lib/_http_outgoing.js @@ -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'); } diff --git a/lib/_http_server.js b/lib/_http_server.js index d473fd32814ded..be55784e7ec37d 100644 --- a/lib/_http_server.js +++ b/lib/_http_server.js @@ -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)) {