X-Git-Url: https://git.squeep.com/?a=blobdiff_plain;f=lib%2Fdingus.js;h=d4af0e102d63d576cbe1cc71956ee7165bff9b6f;hb=1b2868b99eba20b50d88a0d858bcd8c51c5b8f07;hp=79cee2c448e6d61e7d641b45072c532bc52b8237;hpb=3254d4bd8914ab48cb12c5039c87e2beeca55f7a;p=squeep-api-dingus diff --git a/lib/dingus.js b/lib/dingus.js index 79cee2c..d4af0e1 100644 --- a/lib/dingus.js +++ b/lib/dingus.js @@ -13,22 +13,25 @@ const querystring = require('querystring'); const common = require('./common'); const ContentNegotiation = require('./content-negotiation'); const Enum = require('./enum'); -const { DingusError, ResponseError } = require('./errors'); +const { ResponseError, RouterNoPathError, RouterNoMethodError } = require('./errors'); const { extensionToMime } = require('./mime-helper'); const Router = require('./router'); const Template = require('./template'); // For logging. -const _fileScope = common.fileScope(__filename); +const { fileScope } = require('@squeep/log-helper'); +const _fileScope = fileScope(__filename); const defaultOptions = { - ignoreTrailingSlash: false, + ignoreTrailingSlash: true, proxyPrefix: '', strictAccept: true, selfBaseUrl: '', staticMetadata: true, - staticPath: undefined, // no reasonable default + staticPath: undefined, // No reasonable default trustProxy: true, + intrinsicHeadMethod: true, + intrinsicHeadPersistBody: false, querystring, }; @@ -36,15 +39,17 @@ class Dingus { /** * @param {Object} logger object which implements logging methods * @param {Object} options - * @param {Boolean} options.ignoreTrailingSlash + * @param {Boolean} options.ignoreTrailingSlash requests for '/foo/' will match a '/foo' route * @param {string} options.proxyPrefix leading part of url path to strip * @param {Boolean} options.strictAccept whether to error on unsupported Accept type * @param {string} options.selfBaseUrl for constructing links * @param {Boolean} options.staticMetadata serve static headers with static files * @param {Boolean} options.trustProxy trust some header data to be provided by proxy + * @param {Boolean} options.intrinsicHeadMethod handle HEAD requests automatically if not specified as a route method + * @param {Boolean} options.intrinsicHeadPersistBody include un-sent body on ctx for automatic HEAD requests * @param {Object} options.querystring alternate qs parser to use */ - constructor(logger = common.nullLogger, options = {}) { + constructor(logger = console, options = {}) { common.setOptions(this, defaultOptions, options); this.router = new Router(options); @@ -60,7 +65,6 @@ class Dingus { ]; this.logger = logger; - common.ensureLoggerLevels(this.logger); } @@ -139,9 +143,9 @@ class Dingus { */ _getAddress(req) { // TODO: RFC7239 Forwarded support - const address = (this.trustProxy && req && req.getHeader(Enum.Header.XForwardedFor)) || - (this.trustProxy && req && req.getHeader(Enum.Header.XRealIP)) || - (req && req.connection && req.connection.remoteAddress) || + const address = (this.trustProxy && req?.getHeader(Enum.Header.XForwardedFor)) || + (this.trustProxy && req?.getHeader(Enum.Header.XRealIP)) || + (req?.connection?.remoteAddress) || ''; return address.split(/\s*,\s*/u)[0]; } @@ -153,8 +157,8 @@ class Dingus { */ _getProtocol(req) { // TODO: RFC7239 Forwarded support - const protocol = (this.trustProxy && req && req.getHeader(Enum.Header.XForwardedProto)) || - ((req && req.connection && req.connection.encrypted) ? 'https' : 'http'); + const protocol = (this.trustProxy && req?.getHeader(Enum.Header.XForwardedProto)) || + ((req?.connection?.encrypted) ? 'https' : 'http'); return protocol.split(/\s*,\s*/u)[0]; } @@ -254,13 +258,14 @@ class Dingus { /** - * Dispatch the handler for a request - * @param {http.ClientRequest} req - * @param {http.ServerResponse} res - * @param {object} ctx + * Resolve the handler to invoke for a request. + * @param {http.ClientRequest} req + * @param {http.ServerResponse} res + * @param {object} ctx + * @returns {object} */ - async dispatch(req, res, ctx = {}) { - const _scope = _fileScope('dispatch'); + _determineHandler(req, res, ctx) { + const _scope = _fileScope('_determineHandler'); const { pathPart, queryParams } = this._splitUrl(req.url); ctx.queryParams = queryParams; @@ -269,26 +274,59 @@ class Dingus { try { ({ handler, handlerArgs } = this.router.lookup(req.method, pathPart, ctx)); } catch (e) { - if (e instanceof DingusError) { - switch (e.message) { - case 'NoPath': - handler = this.handlerNotFound.bind(this); - break; - case 'NoMethod': - handler = this.handlerMethodNotAllowed.bind(this); - break; - default: - this.logger.error(_scope, 'unknown dingus error', { error: e }); - handler = this.handlerInternalServerError.bind(this); - } - } else if (e instanceof URIError) { + if (e instanceof URIError) { handler = this.handlerBadRequest.bind(this); + } else if (e instanceof RouterNoPathError) { + handler = this.handlerNotFound.bind(this); + } else if (e instanceof RouterNoMethodError) { + if (this.intrinsicHeadMethod && req.method === 'HEAD') { + ({ handler, handlerArgs } = this._determineHeadHandler(req, res, ctx, pathPart)); + } else { + handler = this.handlerMethodNotAllowed.bind(this); + } + } else { + this.logger.error(_scope, 'unexpected error', { error: e }); + handler = this.handlerInternalServerError.bind(this); + } + } + return { handler, handlerArgs }; + } + + + /** + * For intrinsic HEAD requests, resolve the handler to invoke. + * @param {http.ClientRequest} req + * @param {http.ServerResponse} res + * @param {object} ctx + * @param {string} pathPart + * @returns {object} + */ + _determineHeadHandler(req, res, ctx, pathPart) { + const _scope = _fileScope('_determineHeadHandler'); + let handler, handlerArgs = []; + try { + ({ handler, handlerArgs } = this.router.lookup('GET', pathPart, ctx)); + Dingus.setHeadHandler(req, res, ctx, this.intrinsicHeadPersistBody); + } catch (e) { + if (e instanceof RouterNoMethodError) { + handler = this.handlerMethodNotAllowed.bind(this); } else { - this.logger.error(_scope, 'lookup failure', { error: e }); + this.logger.error(_scope, 'unexpected error', { error: e }); handler = this.handlerInternalServerError.bind(this); } } + return { handler, handlerArgs }; + } + + /** + * Dispatch the handler for a request + * @param {http.ClientRequest} req + * @param {http.ServerResponse} res + * @param {object} ctx + */ + async dispatch(req, res, ctx = {}) { + const { handler, handlerArgs } = this._determineHandler(req, res, ctx); try { await this.preHandler(req, res, ctx); return await handler(req, res, ctx, ...handlerArgs); @@ -314,16 +352,11 @@ class Dingus { * Parse rawBody as contentType into ctx.parsedBody. * @param {string} contentType * @param {object} ctx - * @param {string|buffer} + * @param {string|buffer} rawBody */ 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(rawBody); @@ -539,7 +572,7 @@ class Dingus { // We will not deal with any subdirs, nor any dot-files. // (Note that we could not deal with subdirs even if we wanted, due to simple router matching scheme.) if (fileName.indexOf(path.sep) >= 0 - || fileName.charAt(0) === '.') { + || fileName.startsWith('.')) { this.logger.debug(_scope, 'rejected filename', { fileName }); return this.handlerNotFound(req, res, ctx); } @@ -652,7 +685,7 @@ class Dingus { res.setHeader(Enum.Header.ContentType, Enum.ContentType.TextPlain); } - if (err && err.statusCode) { + if (err?.statusCode) { res.statusCode = err.statusCode; body = this.renderError(res.getHeader(Enum.Header.ContentType), err); this.logger.debug(_scope, 'handler error', { err, req, res, ctx });