Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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();
};
parser.execute(httpContentBuffer);
parser.finish();
parser.close();
return xtend(httpData, {
body: completeBody
});
};
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()
}
this.cer = this.cer === opts.pipelining - 1 ? 0 : this.cer++
this._doRequest(this.cer)
}
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()
}
}
this.parser[HTTPParser.kOnBody] = (chunk, offset, length) => {
this._lastBody.push(chunk.slice(offset, offset + length))
}
this.parser[HTTPParser.kOnMessageComplete] = () => {
const body = this._lastBody
this._lastBody = null
body.push(null)
}
this[kReadCb] = () => {
this[kIsWaiting] = false
this[kRead]()
}
}
function parseRequestBody(request) {
var parser = new HTTPParser(HTTPParser.REQUEST);
parser.body = '';
parser.bodyStart = 0;
parser[HTTPParser.kOnBody | 0] = function (b, start) {
if (!parser.bodyStart) {
parser.bodyStart = start;
}
parser.body = b;
};
if (typeof request === 'string') {
request = Buffer.from(request);
}
parser.execute(request, 0, request.length);
return parser.body.slice(parser.bodyStart);
}