From b2e2c274c2f60fb80857640f0399f76cf65e169f Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Fri, 9 Sep 2022 07:31:44 +0200 Subject: [PATCH] UI Server: fix 'write after end' issue for real MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- .../ui-server/UIHttpServer.ts | 21 +++++++++++-------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/charging-station/ui-server/UIHttpServer.ts b/src/charging-station/ui-server/UIHttpServer.ts index 7dfff0ad..822244f9 100644 --- a/src/charging-station/ui-server/UIHttpServer.ts +++ b/src/charging-station/ui-server/UIHttpServer.ts @@ -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 -- 2.34.1