Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
})
}
return
}
endRequest()
})
this.pipelining = opts.pipelining || 1
this[kQueue].drain = () => {
this.emit('drain')
}
this.parser[HTTPParser.kOnHeaders] = () => {}
this.parser[HTTPParser.kOnHeadersComplete] = ({ statusCode, headers }) => {
// TODO move this[kCallbacks] from being an array. The array allocation
// is showing up in the flamegraph.
const cb = this[kCallbacks].shift()
this._needHeaders--
this._lastBody = new Readable({ read: this[kRead].bind(this) })
this._lastBody.push = this[kRequests].shift().wrapSimple(this._lastBody, this._lastBody.push)
cb(null, {
statusCode,
headers: parseHeaders(headers),
body: this._lastBody
})
if (this.closed && this[kQueue].length() === 0) {
this.destroy()
}
module.exports = function getHttpResponseData (httpContentBuffer) {
const parser = new HTTPParser(HTTPParser.RESPONSE);
let httpData = {};
parser[HTTPParser.kOnMessageComplete] = noop;
parser[HTTPParser.kOnHeaders] = noop;
// Get headers and parse them to an object format for easier use
parser[HTTPParser.kOnHeadersComplete] = function (meta) {
const headerObject = {};
for (let i = 0; i < meta.headers.length; i += 2) {
headerObject[meta.headers[i]] = meta.headers[i + 1];
}
httpData = xtend(httpData, meta, {
headers: headerObject
});
};
// The below function can fire multiple times for "transfer-encoding: chunked"
// We need to build up the body in a buffer to ensure we capture it entirely
let completeBody = '';
parser[HTTPParser.kOnBody] = function (body, contentOffset, len) {
completeBody += body.slice(contentOffset, contentOffset + len).toString();
this.timeoutTicker = retimer(handleTimeout, this.timeout)
this._connect()
}
if (this.rate) {
this.rateInterval = setInterval(() => {
this.reqsMadeThisSecond = 0
if (this.paused) this._doRequest(this.cer)
this.paused = false
}, 1000)
}
this.timeoutTicker = retimer(handleTimeout, this.timeout)
this.parser[HTTPParser.kOnHeaders] = () => {}
this.parser[HTTPParser.kOnHeadersComplete] = (opts) => {
this.emit('headers', opts)
this.resData[this.cer].headers = opts
}
this.parser[HTTPParser.kOnBody] = (body) => {
this.emit('body', body)
}
this.parser[HTTPParser.kOnMessageComplete] = () => {
const end = process.hrtime(this.resData[this.cer].startTime)
const responseTime = end[0] * 1e3 + end[1] / 1e6
this.emit('response', this.resData[this.cer].headers.statusCode, this.resData[this.cer].bytes, responseTime)
this.resData[this.cer].bytes = 0
if (!this.destroyed && this.reconnectRate && this.reqsMade % this.reconnectRate === 0) {
return this._resetConnection()
self = this;
this._parser.onHeaderField = function(b, start, length) {
current = b.toString('utf8', start, start + length).toLowerCase();
};
this._parser.onHeaderValue = function(b, start, length) {
var value = b.toString('utf8', start, start + length);
if (self.headers.hasOwnProperty(current))
self.headers[current] += ', ' + value;
else
self.headers[current] = value;
};
this._parser.onHeadersComplete = this._parser[NodeHTTPParser.kOnHeadersComplete] =
function(majorVersion, minorVersion, headers, method, pathname, statusCode) {
var info = arguments[0];
if (typeof info === 'object') {
method = info.method;
pathname = info.url;
statusCode = info.statusCode;
headers = info.headers;
}
self.method = (typeof method === 'number') ? HttpParser.METHODS[method] : method;
self.statusCode = statusCode;
self.url = pathname;
if (!headers) return;
self = this;
this._parser.onHeaderField = function(b, start, length) {
current = b.toString('utf8', start, start + length).toLowerCase();
};
this._parser.onHeaderValue = function(b, start, length) {
var value = b.toString('utf8', start, start + length);
if (self.headers.hasOwnProperty(current))
self.headers[current] += ', ' + value;
else
self.headers[current] = value;
};
this._parser.onHeadersComplete = this._parser[NodeHTTPParser.kOnHeadersComplete] =
function(majorVersion, minorVersion, headers, method, pathname, statusCode) {
var info = arguments[0];
if (typeof info === 'object') {
method = info.method;
pathname = info.url;
statusCode = info.statusCode;
headers = info.headers;
}
self.method = (typeof method === 'number') ? HttpParser.METHODS[method] : method;
self.statusCode = statusCode;
self.url = pathname;
if (!headers) return;
self = this;
this._parser.onHeaderField = function(b, start, length) {
current = b.toString('utf8', start, start + length).toLowerCase();
};
this._parser.onHeaderValue = function(b, start, length) {
var value = b.toString('utf8', start, start + length);
if (self.headers.hasOwnProperty(current))
self.headers[current] += ', ' + value;
else
self.headers[current] = value;
};
this._parser.onHeadersComplete = this._parser[NodeHTTPParser.kOnHeadersComplete] =
function(majorVersion, minorVersion, headers, method, pathname, statusCode) {
var info = arguments[0];
if (typeof info === 'object') {
method = info.method;
pathname = info.url;
statusCode = info.statusCode;
headers = info.headers;
}
self.method = (typeof method === 'number') ? HttpParser.METHODS[method] : method;
self.statusCode = statusCode;
self.url = pathname;
if (!headers) return;