Skip to content

Commit 176cdc2

Browse files
committed
http: misc optimizations and style fixes
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>
1 parent 73d9445 commit 176cdc2

File tree

2 files changed

+20
-29
lines changed

2 files changed

+20
-29
lines changed

lib/_http_outgoing.js

+15-21
Original file line numberDiff line numberDiff line change
@@ -136,36 +136,32 @@ OutgoingMessage.prototype._send = function _send(data, encoding, callback) {
136136

137137
OutgoingMessage.prototype._writeRaw = _writeRaw;
138138
function _writeRaw(data, encoding, callback) {
139+
const conn = this.connection;
140+
if (conn && conn.destroyed) {
141+
// The socket was destroyed. If we're still trying to write to it,
142+
// then we haven't gotten the 'close' event yet.
143+
return false;
144+
}
145+
139146
if (typeof encoding === 'function') {
140147
callback = encoding;
141148
encoding = null;
142149
}
143150

144-
var connection = this.connection;
145-
if (connection &&
146-
connection._httpMessage === this &&
147-
connection.writable &&
148-
!connection.destroyed) {
151+
if (conn && conn._httpMessage === this && conn.writable && !conn.destroyed) {
149152
// There might be pending data in the this.output buffer.
150-
var outputLength = this.output.length;
151-
if (outputLength > 0) {
152-
this._flushOutput(connection);
153-
} else if (data.length === 0) {
153+
if (this.output.length) {
154+
this._flushOutput(conn);
155+
} else if (!data.length) {
154156
if (typeof callback === 'function')
155157
process.nextTick(callback);
156158
return true;
157159
}
158-
159160
// Directly write to socket.
160-
return connection.write(data, encoding, callback);
161-
} else if (connection && connection.destroyed) {
162-
// The socket was destroyed. If we're still trying to write to it,
163-
// then we haven't gotten the 'close' event yet.
164-
return false;
165-
} else {
166-
// buffer, as long as we're not destroyed.
167-
return this._buffer(data, encoding, callback);
161+
return conn.write(data, encoding, callback);
168162
}
163+
// Buffer, as long as we're not destroyed.
164+
return this._buffer(data, encoding, callback);
169165
}
170166

171167

@@ -477,6 +473,7 @@ Object.defineProperty(OutgoingMessage.prototype, 'headersSent', {
477473
});
478474

479475

476+
const crlf_buf = Buffer.from('\r\n');
480477
OutgoingMessage.prototype.write = function write(chunk, encoding, callback) {
481478
if (this.finished) {
482479
var err = new Error('write after end');
@@ -583,9 +580,6 @@ OutgoingMessage.prototype.addTrailers = function addTrailers(headers) {
583580
}
584581
};
585582

586-
587-
const crlf_buf = Buffer.from('\r\n');
588-
589583
function onFinish(outmsg) {
590584
outmsg.emit('finish');
591585
}

lib/_http_server.js

+5-8
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,8 @@ function writeHead(statusCode, reason, obj) {
170170
this.statusMessage = reason;
171171
} else {
172172
// writeHead(statusCode[, headers])
173-
this.statusMessage =
174-
this.statusMessage || STATUS_CODES[statusCode] || 'unknown';
173+
if (!this.statusMessage)
174+
this.statusMessage = STATUS_CODES[statusCode] || 'unknown';
175175
obj = reason;
176176
}
177177
this.statusCode = statusCode;
@@ -514,9 +514,8 @@ function parserOnIncoming(server, socket, state, req, keepAlive) {
514514
// so that we don't become overwhelmed by a flood of
515515
// pipelined requests that may never be resolved.
516516
if (!socket._paused) {
517-
var needPause = socket._writableState.needDrain ||
518-
state.outgoingData >= socket._writableState.highWaterMark;
519-
if (needPause) {
517+
var ws = socket._writableState;
518+
if (ws.needDrain || state.outgoingData >= ws.highWaterMark) {
520519
socket._paused = true;
521520
// We also need to pause the parser, but don't do that until after
522521
// the call to execute, because we may still be processing the last
@@ -542,9 +541,7 @@ function parserOnIncoming(server, socket, state, req, keepAlive) {
542541

543542
// When we're finished writing the response, check if this is the last
544543
// response, if so destroy the socket.
545-
var finish =
546-
resOnFinish.bind(undefined, req, res, socket, state);
547-
res.on('finish', finish);
544+
res.on('finish', resOnFinish.bind(undefined, req, res, socket, state));
548545

549546
if (req.headers.expect !== undefined &&
550547
(req.httpVersionMajor === 1 && req.httpVersionMinor === 1)) {

0 commit comments

Comments
 (0)