From daa6505e2b79f2413b52a60774d78278a11b70b7 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Wed, 7 Sep 2022 22:33:34 +0200 Subject: [PATCH] UI Server: fix write after end MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- .../ui-server/AbstractUIServer.ts | 13 +++++++++++-- .../ui-server/UIHttpServer.ts | 19 ++++++++++--------- .../ui-server/UIWebSocketServer.ts | 9 +++------ 3 files changed, 24 insertions(+), 17 deletions(-) diff --git a/src/charging-station/ui-server/AbstractUIServer.ts b/src/charging-station/ui-server/AbstractUIServer.ts index 48151dc4..f6e2010f 100644 --- a/src/charging-station/ui-server/AbstractUIServer.ts +++ b/src/charging-station/ui-server/AbstractUIServer.ts @@ -1,4 +1,7 @@ -import type { IncomingMessage, Server } from 'http'; +import { type IncomingMessage, Server } from 'http'; +import type { Socket } from 'net'; + +import type { WebSocket } from 'ws'; import type { ChargingStationData } from '../../types/ChargingStationWorker'; import type { UIServerConfiguration } from '../../types/ConfigurationData'; @@ -17,10 +20,13 @@ import UIServiceFactory from './ui-services/UIServiceFactory'; export abstract class AbstractUIServer { public readonly chargingStations: Map; protected httpServer: Server; + protected sockets: Set; protected readonly uiServices: Map; public constructor(protected readonly uiServerConfiguration: UIServerConfiguration) { this.chargingStations = new Map(); + this.httpServer = new Server(); + this.sockets = new Set(); this.uiServices = new Map(); } @@ -36,6 +42,10 @@ export abstract class AbstractUIServer { return [id, responsePayload]; } + public stop(): void { + this.chargingStations.clear(); + } + protected registerProtocolVersionUIService(version: ProtocolVersion): void { if (this.uiServices.has(version) === false) { this.uiServices.set(version, UIServiceFactory.getUIServiceImplementation(version, this)); @@ -63,7 +73,6 @@ export abstract class AbstractUIServer { } public abstract start(): void; - public abstract stop(): void; public abstract sendRequest(request: ProtocolRequest): void; public abstract sendResponse(response: ProtocolResponse): void; public abstract logPrefix( diff --git a/src/charging-station/ui-server/UIHttpServer.ts b/src/charging-station/ui-server/UIHttpServer.ts index 4db72bf7..d6ce5310 100644 --- a/src/charging-station/ui-server/UIHttpServer.ts +++ b/src/charging-station/ui-server/UIHttpServer.ts @@ -1,4 +1,4 @@ -import { IncomingMessage, RequestListener, Server, ServerResponse } from 'http'; +import type { IncomingMessage, RequestListener, ServerResponse } from 'http'; import { StatusCodes } from 'http-status-codes'; @@ -27,20 +27,22 @@ export default class UIHttpServer extends AbstractUIServer { public constructor(protected readonly uiServerConfiguration: UIServerConfiguration) { super(uiServerConfiguration); - this.httpServer = new Server(this.requestListener.bind(this) as RequestListener); this.responseHandlers = new Map(); } public start(): void { + this.httpServer.on('connection', (socket) => { + this.sockets.add(socket); + socket.on('close', () => { + this.sockets.delete(socket); + }); + }); + this.httpServer.on('request', this.requestListener.bind(this) as RequestListener); if (this.httpServer.listening === false) { this.httpServer.listen(this.uiServerConfiguration.options); } } - public stop(): void { - this.chargingStations.clear(); - } - // eslint-disable-next-line @typescript-eslint/no-unused-vars public sendRequest(request: ProtocolRequest): void { // This is intentionally left blank @@ -53,8 +55,7 @@ export default class UIHttpServer extends AbstractUIServer { res.writeHead(this.responseStatusToStatusCode(payload.status), { 'Content-Type': 'application/json', }); - res.write(JSON.stringify(payload)); - res.end(); + res.end(JSON.stringify(payload)); this.responseHandlers.delete(uuid); } else { logger.error( @@ -90,13 +91,13 @@ export default class UIHttpServer extends AbstractUIServer { if (UIServiceUtils.isProtocolAndVersionSupported(protocol, version) === false) { throw new BaseError(`Unsupported UI protocol version: '/${protocol}/${version}'`); } + this.registerProtocolVersionUIService(version); req.on('error', (error) => { logger.error( `${this.logPrefix(moduleName, 'requestListener.req.onerror')} Error on HTTP request:`, error ); }); - this.registerProtocolVersionUIService(version); if (req.method === 'POST') { const bodyBuffer = []; req diff --git a/src/charging-station/ui-server/UIWebSocketServer.ts b/src/charging-station/ui-server/UIWebSocketServer.ts index f789d396..bb80c60a 100644 --- a/src/charging-station/ui-server/UIWebSocketServer.ts +++ b/src/charging-station/ui-server/UIWebSocketServer.ts @@ -1,4 +1,4 @@ -import { IncomingMessage, createServer } from 'http'; +import type { IncomingMessage } from 'http'; import type internal from 'stream'; import { StatusCodes } from 'http-status-codes'; @@ -20,7 +20,6 @@ export default class UIWebSocketServer extends AbstractUIServer { public constructor(protected readonly uiServerConfiguration: UIServerConfiguration) { super(uiServerConfiguration); - this.httpServer = createServer(); this.webSocketServer = new WebSocketServer({ handleProtocols: UIServiceUtils.handleProtocols, noServer: true, @@ -39,6 +38,7 @@ export default class UIWebSocketServer extends AbstractUIServer { ); ws.close(WebSocketCloseEventStatusCode.CLOSE_PROTOCOL_ERROR); } + this.sockets.add(ws); this.registerProtocolVersionUIService(version); ws.on('message', (rawData) => { const request = this.validateRawDataRequest(rawData); @@ -58,6 +58,7 @@ export default class UIWebSocketServer extends AbstractUIServer { logger.error(`${this.logPrefix(moduleName, 'start.ws.onerror')} WebSocket error:`, error); }); ws.on('close', (code, reason) => { + this.sockets.delete(ws); logger.debug( `${this.logPrefix( moduleName, @@ -88,10 +89,6 @@ export default class UIWebSocketServer extends AbstractUIServer { } } - public stop(): void { - this.chargingStations.clear(); - } - public sendRequest(request: ProtocolRequest): void { this.broadcastToClients(JSON.stringify(request)); } -- 2.34.1