X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2Fui-server%2FUIHttpServer.ts;h=7b777b86040c3cba972d886da3df9a11dbab999a;hb=7cb5b17fac198fe3ecd009ac4c692c0d88dd051e;hp=d6ce53103e31c38bed3910fef957acb56bd282af;hpb=daa6505e2b79f2413b52a60774d78278a11b70b7;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/ui-server/UIHttpServer.ts b/src/charging-station/ui-server/UIHttpServer.ts index d6ce5310..7b777b86 100644 --- a/src/charging-station/ui-server/UIHttpServer.ts +++ b/src/charging-station/ui-server/UIHttpServer.ts @@ -20,27 +20,14 @@ import { UIServiceUtils } from './ui-services/UIServiceUtils'; const moduleName = 'UIHttpServer'; -type responseHandler = { procedureName: ProcedureName; res: ServerResponse }; - export default class UIHttpServer extends AbstractUIServer { - private readonly responseHandlers: Map; - public constructor(protected readonly uiServerConfiguration: UIServerConfiguration) { super(uiServerConfiguration); - 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); - } + this.startHttpServer(); } // eslint-disable-next-line @typescript-eslint/no-unused-vars @@ -50,17 +37,26 @@ export default class UIHttpServer extends AbstractUIServer { public sendResponse(response: ProtocolResponse): void { const [uuid, payload] = response; - if (this.responseHandlers.has(uuid) === true) { - const { res } = this.responseHandlers.get(uuid); - res.writeHead(this.responseStatusToStatusCode(payload.status), { - 'Content-Type': 'application/json', - }); - res.end(JSON.stringify(payload)); - this.responseHandlers.delete(uuid); - } else { + 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', + }) + .end(JSON.stringify(payload)); + } else { + logger.error( + `${this.logPrefix(moduleName, 'sendResponse')} Response for unknown request id: ${uuid}` + ); + } + } catch (error) { logger.error( - `${this.logPrefix(moduleName, 'sendResponse')} Response for unknown request id: ${uuid}` + `${this.logPrefix(moduleName, 'sendResponse')} Error at sending response id '${uuid}':`, + error ); + } finally { + this.responseHandlers.delete(uuid); } } @@ -72,13 +68,18 @@ export default class UIHttpServer extends AbstractUIServer { } private requestListener(req: IncomingMessage, res: ServerResponse): void { - if (this.authenticate(req) === false) { - res.setHeader('Content-Type', 'text/plain'); - res.setHeader('WWW-Authenticate', 'Basic realm=users'); - res.writeHead(StatusCodes.UNAUTHORIZED); - res.end(`${StatusCodes.UNAUTHORIZED} Unauthorized`); - return; - } + this.authenticate(req, (err) => { + if (err) { + res + .writeHead(StatusCodes.UNAUTHORIZED, { + 'Content-Type': 'text/plain', + 'WWW-Authenticate': 'Basic realm=users', + }) + .end(`${StatusCodes.UNAUTHORIZED} Unauthorized`) + .destroy(); + req.destroy(); + } + }); // Expected request URL pathname: /ui/:version/:procedureName const [protocol, version, procedureName] = req.url?.split('/').slice(1) as [ Protocol, @@ -86,10 +87,11 @@ export default class UIHttpServer extends AbstractUIServer { ProcedureName ]; const uuid = Utils.generateUUID(); - this.responseHandlers.set(uuid, { procedureName, res }); + this.responseHandlers.set(uuid, res); try { - if (UIServiceUtils.isProtocolAndVersionSupported(protocol, version) === false) { - throw new BaseError(`Unsupported UI protocol version: '/${protocol}/${version}'`); + const fullProtocol = `${protocol}${version}`; + if (UIServiceUtils.isProtocolAndVersionSupported(fullProtocol) === false) { + throw new BaseError(`Unsupported UI protocol version: '${fullProtocol}'`); } this.registerProtocolVersionUIService(version); req.on('error', (error) => { @@ -110,9 +112,7 @@ export default class UIHttpServer extends AbstractUIServer { .get(version) .requestHandler(this.buildProtocolRequest(uuid, procedureName, body ?? {})) .catch(() => { - this.sendResponse( - this.buildProtocolResponse(uuid, { status: ResponseStatus.FAILURE }) - ); + /* Error caught by AbstractUIService */ }); }); } else { @@ -127,16 +127,6 @@ export default class UIHttpServer extends AbstractUIServer { } } - private authenticate(req: IncomingMessage): boolean { - if (this.isBasicAuthEnabled() === true) { - if (this.isValidBasicAuth(req) === true) { - return true; - } - return false; - } - return true; - } - private responseStatusToStatusCode(status: ResponseStatus): StatusCodes { switch (status) { case ResponseStatus.SUCCESS: