const common = require('./common');
const ContentNegotiation = require('./content-negotiation');
const Enum = require('./enum');
-const { DingusError, ResponseError } = require('./errors');
+const { DingusError, ResponseError, RouterNoMethodError, RouterNoPathError } = require('./errors');
const { extensionToMime } = require('./mime-helper');
const Router = require('./router');
const Template = require('./template');
strictAccept: true,
selfBaseUrl: '',
staticMetadata: true,
+ staticPath: undefined, // No reasonable default
trustProxy: true,
querystring,
};
* @param {Boolean} options.trustProxy trust some header data to be provided by proxy
* @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);
];
this.logger = logger;
- common.ensureLoggerLevels(this.logger);
}
*/
_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];
}
*/
_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];
}
/**
* 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 = [];
};
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);
};
}
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);
- }
+ if (e instanceof RouterNoPathError) {
+ handler = this.handlerNotFound.bind(this);
+ } else if (e instanceof RouterNoMethodError) {
+ handler = this.handlerMethodNotAllowed.bind(this);
+ } else if (e instanceof DingusError) {
+ this.logger.error(_scope, 'unknown dingus error', { error: e });
+ handler = this.handlerInternalServerError.bind(this);
} else if (e instanceof URIError) {
handler = this.handlerBadRequest.bind(this);
} else {
/**
- * 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} rawBody
+ */
+ parseBody(contentType, ctx, rawBody) {
const _scope = _fileScope('parseBody');
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) {
+ async bodyData(req, maximumBodySize, toString = true) {
const _scope = _fileScope('bodyData');
return new Promise((resolve, reject) => {
const body = [];
- req.on('data', (chunk) => body.push(chunk));
- req.on('end', () => resolve(Buffer.concat(body).toString()));
+ 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', () => {
+ 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) {
+ 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);
// 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);
}
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);
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, ...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);
}