/**
- * Parse rawBody from ctx as contentType into parsedBody.
- * @param {string} contentType
- * @param {object} ctx
- */
- parseBody(contentType, ctx) {
+ * Parse rawBody as contentType into ctx.parsedBody.
+ * @param {string} contentType
+ * @param {object} ctx
+ * @param {string|buffer}
+ */
+ parseBody(contentType, ctx, rawBody) {
const _scope = _fileScope('parseBody');
+ if (!rawBody) {
+ // 1.2.4 and earlier expected rawBody on context
+ rawBody = ctx.rawBody;
+ }
+
switch (contentType) {
case Enum.ContentType.ApplicationForm:
- ctx.parsedBody = this.querystring.parse(ctx.rawBody);
+ ctx.parsedBody = this.querystring.parse(rawBody);
break;
case Enum.ContentType.ApplicationJson:
try {
- ctx.parsedBody = JSON.parse(ctx.rawBody);
+ ctx.parsedBody = JSON.parse(rawBody);
} catch (e) {
this.logger.debug(_scope, 'JSON parse failed', { requestId: ctx.requestId, error: e });
throw new ResponseError(Enum.ErrorResponse.BadRequest, e.message);
* Return all body data from a request.
* @param {http.ClientRequest} req
* @param {Number=} maximumBodySize
+ * @param {Boolean=} toString
*/
- async bodyData(req, maximumBodySize) {
+ async bodyData(req, maximumBodySize, toString = true) {
const _scope = _fileScope('bodyData');
return new Promise((resolve, reject) => {
const body = [];
reject(new ResponseError(Enum.ErrorResponse.RequestEntityTooLarge));
}
});
- req.on('end', () => resolve(Buffer.concat(body).toString()));
+ req.on('end', () => {
+ const bodyBuffer = Buffer.concat(body);
+ resolve(toString ? bodyBuffer.toString() : bodyBuffer);
+ });
req.on('error', (e) => {
this.logger.error(_scope, 'failed', { error: e });
reject(e);
* @param {http.ClientRequest} req
* @param {http.ServerResponse} res
* @param {object} ctx
- */
- async ingestBody(req, res, ctx) {
- ctx.rawBody = await this.bodyData(req);
- const contentType = Dingus.getRequestContentType(req);
- this.parseBody(contentType, ctx);
+ * @param {object}
+ * @param {Boolean} .parseEmptyBody
+ * @param {Boolean} .persistRawBody
+ */
+ async ingestBody(req, res, ctx, { parseEmptyBody = true, persistRawBody = false, maximumBodySize } = {}) {
+ const rawBody = await this.bodyData(req, maximumBodySize);
+ if (persistRawBody) {
+ ctx.rawBody = rawBody;
+ }
+ if (rawBody || parseEmptyBody) {
+ const contentType = Dingus.getRequestContentType(req);
+ this.parseBody(contentType, ctx, rawBody);
+ }
}
*/
async serveFile(req, res, ctx, directory, fileName) {
const _scope = _fileScope('serveFile');
- this.logger.debug(_scope, 'called', { req: common.requestLogData(req), ctx });
+ this.logger.debug(_scope, 'called', { req, ctx });
// Require a directory field.
if (!directory) {
if (err && err.statusCode) {
res.statusCode = err.statusCode;
body = this.renderError(res.getHeader(Enum.Header.ContentType), err);
- this.logger.debug(_scope, 'handler error', { err, ...common.handlerLogData(req, res, ctx) });
+ this.logger.debug(_scope, 'handler error', { err, req, res, ctx });
} else {
res.statusCode = 500;
body = this.renderError(res.getHeader(Enum.Header.ContentType), Enum.ErrorResponse.InternalServerError);
- this.logger.error(_scope, 'handler exception', { err, ...common.handlerLogData(req, res, ctx) });
+ this.logger.error(_scope, 'handler exception', { err, req, res, ctx });
}
res.end(body);
}