X-Git-Url: http://git.squeep.com/?a=blobdiff_plain;f=lib%2Fdingus.js;h=4eded66a727e1d831e7249b462abb3d5da50fa17;hb=9ff4b5a259e3583d15aeca1dce4c9ddcbb752023;hp=11bdf998373789b9a9a1584ffb71f5b30661be76;hpb=21c492b52444c5c95db2913d7429e281384a469f;p=squeep-api-dingus diff --git a/lib/dingus.js b/lib/dingus.js index 11bdf99..4eded66 100644 --- a/lib/dingus.js +++ b/lib/dingus.js @@ -226,11 +226,13 @@ class Dingus { /** * Intercept writes for head requests, do not send to client, * but send length, and make body available in context. + * N.B. If persisted, ctx.responseBody will be a raw buffer, be aware when logging. * @param {http.ClientRequest} req * @param {http.ServerResponse} res * @param {object} ctx + * @param {Boolean} persistResponseBody */ - static setHeadHandler(req, res, ctx) { + static setHeadHandler(req, res, ctx, persistResponseBody = false) { if (req.method === 'HEAD') { const origEnd = res.end.bind(res); const chunks = []; @@ -240,8 +242,11 @@ class Dingus { }; res.end = function (data, encoding, ...rest) { Dingus.pushBufChunk(chunks, data, encoding); - ctx.responseBody = Buffer.concat(chunks); - res.setHeader(Enum.Header.ContentLength, Buffer.byteLength(ctx.responseBody)); + const responseBody = Buffer.concat(chunks); + res.setHeader(Enum.Header.ContentLength, Buffer.byteLength(responseBody)); + if (persistResponseBody) { + ctx.responseBody = responseBody; + } return origEnd(undefined, encoding, ...rest); }; } @@ -306,21 +311,27 @@ class Dingus { /** - * 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); @@ -366,11 +377,19 @@ class Dingus { * @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); + } } @@ -502,7 +521,7 @@ class Dingus { */ 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) { @@ -630,11 +649,11 @@ class Dingus { 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); }