Browse Source

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>
v6
Brian White 8 years ago
parent
commit
176cdc2823
No known key found for this signature in database GPG Key ID: 606D7358F94DA209
  1. 36
      lib/_http_outgoing.js
  2. 13
      lib/_http_server.js

36
lib/_http_outgoing.js

@ -136,36 +136,32 @@ OutgoingMessage.prototype._send = function _send(data, encoding, callback) {
OutgoingMessage.prototype._writeRaw = _writeRaw; OutgoingMessage.prototype._writeRaw = _writeRaw;
function _writeRaw(data, encoding, callback) { 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') { if (typeof encoding === 'function') {
callback = encoding; callback = encoding;
encoding = null; encoding = null;
} }
var connection = this.connection; if (conn && conn._httpMessage === this && conn.writable && !conn.destroyed) {
if (connection &&
connection._httpMessage === this &&
connection.writable &&
!connection.destroyed) {
// There might be pending data in the this.output buffer. // There might be pending data in the this.output buffer.
var outputLength = this.output.length; if (this.output.length) {
if (outputLength > 0) { this._flushOutput(conn);
this._flushOutput(connection); } else if (!data.length) {
} else if (data.length === 0) {
if (typeof callback === 'function') if (typeof callback === 'function')
process.nextTick(callback); process.nextTick(callback);
return true; return true;
} }
// Directly write to socket. // Directly write to socket.
return connection.write(data, encoding, callback); return conn.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);
} }
// 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) { OutgoingMessage.prototype.write = function write(chunk, encoding, callback) {
if (this.finished) { if (this.finished) {
var err = new Error('write after end'); 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) { function onFinish(outmsg) {
outmsg.emit('finish'); outmsg.emit('finish');
} }

13
lib/_http_server.js

@ -170,8 +170,8 @@ function writeHead(statusCode, reason, obj) {
this.statusMessage = reason; this.statusMessage = reason;
} else { } else {
// writeHead(statusCode[, headers]) // writeHead(statusCode[, headers])
this.statusMessage = if (!this.statusMessage)
this.statusMessage || STATUS_CODES[statusCode] || 'unknown'; this.statusMessage = STATUS_CODES[statusCode] || 'unknown';
obj = reason; obj = reason;
} }
this.statusCode = statusCode; 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 // so that we don't become overwhelmed by a flood of
// pipelined requests that may never be resolved. // pipelined requests that may never be resolved.
if (!socket._paused) { if (!socket._paused) {
var needPause = socket._writableState.needDrain || var ws = socket._writableState;
state.outgoingData >= socket._writableState.highWaterMark; if (ws.needDrain || state.outgoingData >= ws.highWaterMark) {
if (needPause) {
socket._paused = true; socket._paused = true;
// We also need to pause the parser, but don't do that until after // 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 // 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 // When we're finished writing the response, check if this is the last
// response, if so destroy the socket. // response, if so destroy the socket.
var finish = res.on('finish', resOnFinish.bind(undefined, req, res, socket, state));
resOnFinish.bind(undefined, req, res, socket, state);
res.on('finish', finish);
if (req.headers.expect !== undefined && if (req.headers.expect !== undefined &&
(req.httpVersionMajor === 1 && req.httpVersionMinor === 1)) { (req.httpVersionMajor === 1 && req.httpVersionMinor === 1)) {

Loading…
Cancel
Save