Skip to content

Commit

Permalink
http: cleanup _http_common.js
Browse files Browse the repository at this point in the history
Remove constant deep property access by storing a reference,
use more const, make some conditionals stricter.

PR-URL: #20126
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
  • Loading branch information
apapirovski authored and jasnell committed Apr 23, 2018
1 parent d7ff4f0 commit 6886dd1
Showing 1 changed file with 24 additions and 27 deletions.
51 changes: 24 additions & 27 deletions lib/_http_common.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ function parserOnHeadersComplete(versionMajor, versionMinor, headers, method,
const parser = this;
const { socket } = parser;

if (!headers) {
if (headers === undefined) {
headers = parser._headers;
parser._headers = [];
}

if (!url) {
if (url === undefined) {
url = parser._url;
parser._url = '';
}
Expand All @@ -80,64 +80,61 @@ function parserOnHeadersComplete(versionMajor, versionMinor, headers, method,
socket.server[kIncomingMessage]) ||
IncomingMessage;

parser.incoming = new ParserIncomingMessage(socket);
parser.incoming.httpVersionMajor = versionMajor;
parser.incoming.httpVersionMinor = versionMinor;
parser.incoming.httpVersion = `${versionMajor}.${versionMinor}`;
parser.incoming.url = url;
parser.incoming.upgrade = upgrade;
const incoming = parser.incoming = new ParserIncomingMessage(socket);
incoming.httpVersionMajor = versionMajor;
incoming.httpVersionMinor = versionMinor;
incoming.httpVersion = `${versionMajor}.${versionMinor}`;
incoming.url = url;
incoming.upgrade = upgrade;

var n = headers.length;

// If parser.maxHeaderPairs <= 0 assume that there's no limit.
if (parser.maxHeaderPairs > 0)
n = Math.min(n, parser.maxHeaderPairs);

parser.incoming._addHeaderLines(headers, n);
incoming._addHeaderLines(headers, n);

if (typeof method === 'number') {
// server only
parser.incoming.method = methods[method];
incoming.method = methods[method];
} else {
// client only
parser.incoming.statusCode = statusCode;
parser.incoming.statusMessage = statusMessage;
incoming.statusCode = statusCode;
incoming.statusMessage = statusMessage;
}

return parser.onIncoming(parser.incoming, shouldKeepAlive);
return parser.onIncoming(incoming, shouldKeepAlive);
}

// XXX This is a mess.
// TODO: http.Parser should be a Writable emits request/response events.
function parserOnBody(b, start, len) {
var parser = this;
var stream = parser.incoming;
const stream = this.incoming;

// if the stream has already been removed, then drop it.
if (!stream)
if (stream === null)
return;

var socket = stream.socket;

// pretend this was the result of a stream._read call.
if (len > 0 && !stream._dumped) {
var slice = b.slice(start, start + len);
var ret = stream.push(slice);
if (!ret)
readStop(socket);
readStop(this.socket);
}
}

function parserOnMessageComplete() {
var parser = this;
var stream = parser.incoming;
const parser = this;
const stream = parser.incoming;

if (stream) {
if (stream !== null) {
stream.complete = true;
// Emit any trailing headers.
var headers = parser._headers;
if (headers) {
parser.incoming._addHeaderLines(headers, headers.length);
const headers = parser._headers;
if (headers.length) {
stream._addHeaderLines(headers, headers.length);
parser._headers = [];
parser._url = '';
}
Expand All @@ -151,8 +148,8 @@ function parserOnMessageComplete() {
}


var parsers = new FreeList('parsers', 1000, function() {
var parser = new HTTPParser(HTTPParser.REQUEST);
const parsers = new FreeList('parsers', 1000, function() {
const parser = new HTTPParser(HTTPParser.REQUEST);

parser._headers = [];
parser._url = '';
Expand Down

0 comments on commit 6886dd1

Please sign in to comment.