X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2Fui-server%2FUIHttpServer.ts;h=37c579dc56a552dc26dbd57ae515378820e9e2d9;hb=07561812b72072b6d9f20997be86a42ee88e15a2;hp=d1615dbbcb893a3bae3454ad5aceac7a2cfb6a8d;hpb=94dc30801f06da3ae0bb9c3bb73ff29dbbf96b6e;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 d1615dbb..37c579dc 100644 --- a/src/charging-station/ui-server/UIHttpServer.ts +++ b/src/charging-station/ui-server/UIHttpServer.ts @@ -39,17 +39,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) as ServerResponse; - 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); } } @@ -61,13 +70,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, @@ -99,9 +113,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 { @@ -116,16 +128,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: