X-Git-Url: http://git.squeep.com/?a=blobdiff_plain;f=lib%2Fdingus.js;h=943aa2f91e7bfc141960b35182bbbc23ef816f16;hb=953c58b691c50950e7c5a87966b5ea56ef6ffa95;hp=51da6fab6b2185680cace1f8d1ad48921283d59d;hpb=3b70d88ed735041cb0ec358dfc478b825c1c12e7;p=squeep-api-dingus diff --git a/lib/dingus.js b/lib/dingus.js index 51da6fa..943aa2f 100644 --- a/lib/dingus.js +++ b/lib/dingus.js @@ -27,7 +27,7 @@ const defaultOptions = { strictAccept: true, selfBaseUrl: '', staticMetadata: true, - staticPath: undefined, // no reasonable default + staticPath: undefined, // No reasonable default trustProxy: true, querystring, }; @@ -349,8 +349,9 @@ class Dingus { * 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 = []; @@ -363,7 +364,10 @@ class Dingus { 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); @@ -521,7 +525,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) { @@ -561,18 +565,20 @@ class Dingus { break; } const suffix = Enum.EncodingTypeSuffix[encoding]; - if (suffix) { - const encodedFilePath = `${filePath}${suffix}`; - const [ encodedStat, encodedData ] = await this._readFileInfo(encodedFilePath); - if (encodedStat) { - ([ stat, data ] = [ encodedStat, encodedData ]); - ctx.selectedEncoding = encoding; - Dingus.addEncodingHeader(res, encoding); - res.setHeader(Enum.Header.Vary, Enum.Header.AcceptEncoding); - this.logger.debug(_scope, 'serving encoded version', { ctx, encodedFilePath }); - } - break; + if (!suffix) { + this.logger.error(_scope, 'supported encoding missing mapped suffix', { ctx, encoding }); + continue; + } + const encodedFilePath = `${filePath}${suffix}`; + const [ encodedStat, encodedData ] = await this._readFileInfo(encodedFilePath); + if (encodedStat) { + ([ stat, data ] = [ encodedStat, encodedData ]); + ctx.selectedEncoding = encoding; + Dingus.addEncodingHeader(res, encoding); + res.setHeader(Enum.Header.Vary, Enum.Header.AcceptEncoding); + this.logger.debug(_scope, 'serving encoded version', { ctx, encodedFilePath }); } + break; } const lastModifiedDate = new Date(stat.mtimeMs); @@ -649,11 +655,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); }