Skip to content

Commit

Permalink
http: misc optimizations and style fixes
Browse files Browse the repository at this point in the history
PR-URL: #10558
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Evan Lucas <evanlucas@me.com>
  • Loading branch information
mscdex committed Jan 11, 2017
1 parent 73d9445 commit 176cdc2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 29 deletions.
36 changes: 15 additions & 21 deletions lib/_http_outgoing.js
Expand Up @@ -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);
}


Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -583,9 +580,6 @@ OutgoingMessage.prototype.addTrailers = function addTrailers(headers) {
}
};


const crlf_buf = Buffer.from('\r\n');

function onFinish(outmsg) {
outmsg.emit('finish');
}
Expand Down
13 changes: 5 additions & 8 deletions lib/_http_server.js
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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)) {
Expand Down

0 comments on commit 176cdc2

Please sign in to comment.