X-Git-Url: http://git.squeep.com/?a=blobdiff_plain;f=lib%2Fdingus.js;h=11bdf998373789b9a9a1584ffb71f5b30661be76;hb=21c492b52444c5c95db2913d7429e281384a469f;hp=0143585c1d83732a9e064d2f8687596af88f42be;hpb=ca35f167826c0732571da5f35e3c25881d138b79;p=squeep-api-dingus diff --git a/lib/dingus.js b/lib/dingus.js index 0143585..11bdf99 100644 --- a/lib/dingus.js +++ b/lib/dingus.js @@ -27,6 +27,7 @@ const defaultOptions = { strictAccept: true, selfBaseUrl: '', staticMetadata: true, + staticPath: undefined, // no reasonable default trustProxy: true, querystring, }; @@ -105,8 +106,8 @@ class Dingus { * @param {string} urlPath * @param {fn} handler */ - on(method, urlPath, handler) { - this.router.on(method, urlPath, handler); + on(method, urlPath, handler, ...handlerArgs) { + this.router.on(method, urlPath, handler, handlerArgs); } @@ -259,9 +260,9 @@ class Dingus { const { pathPart, queryParams } = this._splitUrl(req.url); ctx.queryParams = queryParams; - let handler; + let handler, handlerArgs = []; try { - handler = this.router.lookup(req.method, pathPart, ctx); + ({ handler, handlerArgs } = this.router.lookup(req.method, pathPart, ctx)); } catch (e) { if (e instanceof DingusError) { switch (e.message) { @@ -285,7 +286,7 @@ class Dingus { try { await this.preHandler(req, res, ctx); - return await handler(req, res, ctx); + return await handler(req, res, ctx, ...handlerArgs); } catch (e) { ctx.error = e; this.sendErrorResponse(e, req, res, ctx); @@ -336,12 +337,21 @@ class Dingus { /** * Return all body data from a request. * @param {http.ClientRequest} req + * @param {Number=} maximumBodySize */ - async bodyData(req) { + async bodyData(req, maximumBodySize) { const _scope = _fileScope('bodyData'); return new Promise((resolve, reject) => { const body = []; - req.on('data', (chunk) => body.push(chunk)); + let length = 0; + req.on('data', (chunk) => { + body.push(chunk); + length += Buffer.byteLength(chunk); + if (maximumBodySize && length > maximumBodySize) { + this.logger.debug(_scope, 'body data exceeded limit', { length, maximumBodySize }); + reject(new ResponseError(Enum.ErrorResponse.RequestEntityTooLarge)); + } + }); req.on('end', () => resolve(Buffer.concat(body).toString())); req.on('error', (e) => { this.logger.error(_scope, 'failed', { error: e }); @@ -494,6 +504,12 @@ class Dingus { const _scope = _fileScope('serveFile'); this.logger.debug(_scope, 'called', { req: common.requestLogData(req), ctx }); + // Require a directory field. + if (!directory) { + this.logger.debug(_scope, 'rejected unset directory', { fileName }); + return this.handlerNotFound(req, res, ctx); + } + // Normalize the supplied path, as encoded path-navigation may have been (maliciously) present. fileName = path.normalize(fileName); @@ -624,6 +640,37 @@ class Dingus { } + /** + * @param {http.ClientRequest} req + * @param {http.ServerResponse} res + * @param {object} ctx + * @param {String} file - override ctx.params.file + */ + async handlerGetStaticFile(req, res, ctx, file) { + Dingus.setHeadHandler(req, res, ctx); + + // Set a default response type to handle any errors; will be re-set to serve actual static content type. + this.setResponseType(this.responseTypes, req, res, ctx); + + await this.serveFile(req, res, ctx, this.staticPath, file || ctx.params.file); + } + + + /** + * @param {http.ClientRequest} req + * @param {http.ServerResponse} res + * @param {Object} ctx + * @param {String} newPath + * @param {Number} statusCode + */ + async handlerRedirect(req, res, ctx, newPath, statusCode = 307) { + this.setResponseType(this.responseTypes, req, res, ctx); + res.setHeader(Enum.Header.Location, newPath); + res.statusCode = statusCode; + res.end(); + } + + /** * @param {http.ClientRequest} req * @param {http.ServerResponse} res