UI Server: fix 'write after end' issue for real
authorJérôme Benoit <jerome.benoit@sap.com>
Fri, 9 Sep 2022 05:31:44 +0000 (07:31 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Fri, 9 Sep 2022 05:31:44 +0000 (07:31 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/ui-server/UIHttpServer.ts

index 7dfff0adf5c587284d2c32663fcd4d24ddafc423..822244f96081305ef939ae7f107d67f9c840c1a4 100644 (file)
@@ -42,10 +42,11 @@ export default class UIHttpServer extends AbstractUIServer {
     try {
       if (this.responseHandlers.has(uuid) === true) {
         const res = this.responseHandlers.get(uuid) as ServerResponse;
-        res.writeHead(this.responseStatusToStatusCode(payload.status), {
-          'Content-Type': 'application/json',
-        });
-        res.end(JSON.stringify(payload));
+        res
+          .writeHead(this.responseStatusToStatusCode(payload.status), {
+            'Content-Type': 'application/json',
+          })
+          .end(JSON.stringify(payload));
         this.responseHandlers.delete(uuid);
       } else {
         logger.error(
@@ -70,12 +71,14 @@ export default class UIHttpServer extends AbstractUIServer {
   private requestListener(req: IncomingMessage, res: ServerResponse): void {
     this.authenticate(req, (err) => {
       if (err) {
-        res.setHeader('Content-Type', 'text/plain');
-        res.setHeader('WWW-Authenticate', 'Basic realm=users');
-        res.writeHead(StatusCodes.UNAUTHORIZED);
-        res.end(`${StatusCodes.UNAUTHORIZED} Unauthorized`);
+        res
+          .writeHead(StatusCodes.UNAUTHORIZED, {
+            'Content-Type': 'text/plain',
+            'WWW-Authenticate': 'Basic realm=users',
+          })
+          .end(`${StatusCodes.UNAUTHORIZED} Unauthorized`)
+          .destroy();
         req.destroy();
-        res.destroy();
       }
     });
     // Expected request URL pathname: /ui/:version/:procedureName