Merge branch 'v1.3-dev' as v1.3.11
[websub-hub] / src / service.js
index df894011dc84f7cad424467b9e62720d4890f9d4..e76959978eddab7a554dbe80eb4d31ffa9c10d8a 100644 (file)
@@ -69,24 +69,6 @@ class Service extends Dingus {
 
   }
 
-  /**
-   * Wrap the Dingus head handler, to remove the response body from the context,
-   * lest it be logged.
-   * @param {http.ClientRequest} req
-   * @param {http.ServerResponse} res
-   * @param {object} ctx
-   */
-  static setHeadHandler(req, res, ctx) {
-    if (req.method === 'HEAD') {
-      Dingus.setHeadHandler(req, res, ctx);
-      const origEnd = res.end.bind(res);
-      res.end = function (data, encoding, ...rest) {
-        const origResult = origEnd(data, encoding, ...rest);
-        delete ctx.responseBody;
-        return origResult;
-      };
-    }
-  }
 
   /**
    * @param {http.ClientRequest} req
@@ -95,7 +77,7 @@ class Service extends Dingus {
    */
   async handlerPostRoot(req, res, ctx) {
     const _scope = _fileScope('handlerPostRoot');
-    this.logger.debug(_scope, 'called', { req: common.requestLogData(req), ctx });
+    this.logger.debug(_scope, 'called', { req, ctx });
 
     this.setResponseType(this.responseTypes, req, res, ctx);
     await this.ingestBody(req, res, ctx);
@@ -114,9 +96,9 @@ class Service extends Dingus {
     const responseTypes = [
       Enum.ContentType.TextHTML,
     ];
-    this.logger.debug(_scope, 'called', { req: common.requestLogData(req), ctx });
+    this.logger.debug(_scope, 'called', { req, ctx });
 
-    Service.setHeadHandler(req, res, ctx);
+    Dingus.setHeadHandler(req, res, ctx);
 
     this.setResponseType(responseTypes, req, res, ctx);
 
@@ -133,9 +115,9 @@ class Service extends Dingus {
    */
   async handlerGetHealthcheck(req, res, ctx) {
     const _scope = _fileScope('handlerGetHealthcheck');
-    this.logger.debug(_scope, 'called', { req: common.requestLogData(req), ctx });
+    this.logger.debug(_scope, 'called', { req, ctx });
   
-    Service.setHeadHandler(req, res, ctx);
+    Dingus.setHeadHandler(req, res, ctx);
 
     this.setResponseType(this.responseTypes, req, res, ctx);
 
@@ -150,11 +132,11 @@ class Service extends Dingus {
    */
   async handlerGetInfo(req, res, ctx) {
     const _scope = _fileScope('handlerGetInfo');
-    this.logger.debug(_scope, 'called', { req: common.requestLogData(req), ctx });
+    this.logger.debug(_scope, 'called', { req, ctx });
 
     const responseTypes = [...this.responseTypes, Enum.ContentType.ImageSVG];
 
-    Service.setHeadHandler(req, res, ctx);
+    Dingus.setHeadHandler(req, res, ctx);
 
     this.setResponseType(responseTypes, req, res, ctx);
 
@@ -164,11 +146,11 @@ class Service extends Dingus {
 
   async handlerGetHistorySVG(req, res, ctx) {
     const _scope = _fileScope('handlerGetHist');
-    this.logger.debug(_scope, 'called', { req: common.requestLogData(req), ctx });
+    this.logger.debug(_scope, 'called', { req, ctx });
 
     const responseTypes = [Enum.ContentType.ImageSVG];
 
-    Service.setHeadHandler(req, res, ctx);
+    Dingus.setHeadHandler(req, res, ctx);
 
     this.setResponseType(responseTypes, req, res, ctx);
 
@@ -183,15 +165,15 @@ class Service extends Dingus {
    */
   async handlerGetAdminOverview(req, res, ctx) {
     const _scope = _fileScope('handlerGetAdminOverview');
-    this.logger.debug(_scope, 'called', { req: common.requestLogData(req), ctx });
+    this.logger.debug(_scope, 'called', { req, ctx });
 
-    Service.setHeadHandler(req, res, ctx);
+    Dingus.setHeadHandler(req, res, ctx);
 
     this.setResponseType(this.responseTypes, req, res, ctx);
 
-    await this.authenticator.sessionRequired(req, res, ctx, this.loginPath);
-
-    await this.manager.getAdminOverview(res, ctx);
+    if (await this.authenticator.sessionRequired(req, res, ctx, this.loginPath)) {
+      await this.manager.getAdminOverview(res, ctx);
+    }
   }
 
 
@@ -202,33 +184,28 @@ class Service extends Dingus {
    */
   async handlerGetAdminTopicDetails(req, res, ctx) {
     const _scope = _fileScope('handlerGetAdminTopicDetails');
-    this.logger.debug(_scope, 'called', { req: common.requestLogData(req), ctx });
+    this.logger.debug(_scope, 'called', { req, ctx });
 
-    Service.setHeadHandler(req, res, ctx);
+    Dingus.setHeadHandler(req, res, ctx);
 
     this.setResponseType(this.responseTypes, req, res, ctx);
 
-    await this.authenticator.sessionRequired(req, res, ctx, this.loginPath);
-
-    await this.manager.getTopicDetails(res, ctx);
+    if (await this.authenticator.sessionRequired(req, res, ctx, this.loginPath)) {
+      await this.manager.getTopicDetails(res, ctx);
+    }
   }
 
 
   /**
-   * Similar to super.ingestBody, but if no body was sent, do not parse (and
-   * thus avoid possible unsupported media type error).
-   * Also removes raw body from context, to simplify scrubbing sensitive data from logs.
+   * If no body was sent, do not parse (and thus avoid possible unsupported media type error).
    * @param {http.ClientRequest} req
    * @param {http.ServerResponse} res
    * @param {Object} ctx
    */
   async maybeIngestBody(req, res, ctx) {
-    ctx.rawBody = await this.bodyData(req);
-    const contentType = Dingus.getRequestContentType(req);
-    if (ctx.rawBody) {
-      this.parseBody(contentType, ctx);
-      delete ctx.rawBody;
-    }
+    return super.ingestBody(req, res, ctx, {
+      parseEmptyBody: false,
+    });
   }
 
 
@@ -239,7 +216,7 @@ class Service extends Dingus {
    */
   async handlerUpdateTopic(req, res, ctx) {
     const _scope = _fileScope('handlerUpdateTopic');
-    this.logger.debug(_scope, 'called', { req: common.requestLogData(req), ctx });
+    this.logger.debug(_scope, 'called', { req, ctx });
 
     this.setResponseType(this.responseTypes, req, res, ctx);
 
@@ -258,7 +235,7 @@ class Service extends Dingus {
    */
   async handlerUpdateSubscription(req, res, ctx) {
     const _scope = _fileScope('handlerUpdateSubscription');
-    this.logger.debug(_scope, 'called', { req: common.requestLogData(req), ctx });
+    this.logger.debug(_scope, 'called', { req, ctx });
 
     this.setResponseType(this.responseTypes, req, res, ctx);
 
@@ -277,7 +254,7 @@ class Service extends Dingus {
    */
   async handlerPostAdminProcess(req, res, ctx) {
     const _scope = _fileScope('handlerPostAdminProcess');
-    this.logger.debug(_scope, 'called', { req: common.requestLogData(req), ctx });
+    this.logger.debug(_scope, 'called', { req, ctx });
 
     this.setResponseType(this.responseTypes, req, res, ctx);
 
@@ -295,9 +272,9 @@ class Service extends Dingus {
    */
   async handlerGetAdminLogin(req, res, ctx) {
     const _scope = _fileScope('handlerGetAdminLogin');
-    this.logger.debug(_scope, 'called', { req: common.requestLogData(req), ctx });
+    this.logger.debug(_scope, 'called', { req, ctx });
 
-    Service.setHeadHandler(req, res, ctx);
+    Dingus.setHeadHandler(req, res, ctx);
 
     this.setResponseType(this.responseTypes, req, res, ctx);
 
@@ -313,7 +290,7 @@ class Service extends Dingus {
    */
   async handlerPostAdminLogin(req, res, ctx) {
     const _scope = _fileScope('handlerPostAdminLogin');
-    this.logger.debug(_scope, 'called', { req: common.requestLogData(req), ctx });
+    this.logger.debug(_scope, 'called', { req, ctx });
 
     this.setResponseType(this.responseTypes, req, res, ctx);
 
@@ -333,7 +310,7 @@ class Service extends Dingus {
    */
   async handlerGetAdminLogout(req, res, ctx) {
     const _scope = _fileScope('handlerGetAdminLogout');
-    this.logger.debug(_scope, 'called', { req: common.requestLogData(req), ctx });
+    this.logger.debug(_scope, 'called', { req, ctx });
 
     this.setResponseType(this.responseTypes, req, res, ctx);
 
@@ -351,7 +328,7 @@ class Service extends Dingus {
    */
   async handlerGetAdminIA(req, res, ctx) {
     const _scope = _fileScope('handlerGetAdminIA');
-    this.logger.debug(_scope, 'called', { req: common.requestLogData(req), ctx });
+    this.logger.debug(_scope, 'called', { req, ctx });
 
     this.setResponseType(this.responseTypes, req, res, ctx);