X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2Fui-server%2FUIHttpServer.ts;h=fb967ef4ecb0403d54b074738e11dcfb8446af05;hb=8f9060ba0f4e2e22053ceb34f357bb8a1263d889;hp=eb3550830682f45cff76aff8559df71b34e855f5;hpb=13a6f27c10768faa05acf33fd8e0637511d49e3e;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 eb355083..fb967ef4 100644 --- a/src/charging-station/ui-server/UIHttpServer.ts +++ b/src/charging-station/ui-server/UIHttpServer.ts @@ -1,9 +1,9 @@ -import type { IncomingMessage, RequestListener, ServerResponse } from 'node:http' +import type { IncomingMessage, ServerResponse } from 'node:http' import { StatusCodes } from 'http-status-codes' import { AbstractUIServer } from './AbstractUIServer.js' -import { UIServerUtils } from './UIServerUtils.js' +import { isProtocolAndVersionSupported } from './UIServerUtils.js' import { BaseError } from '../../exception/index.js' import { ApplicationProtocolVersion, @@ -16,7 +16,14 @@ import { ResponseStatus, type UIServerConfiguration } from '../../types/index.js' -import { Constants, generateUUID, isNotEmptyString, logPrefix, logger } from '../../utils/index.js' +import { + Constants, + JSONStringifyWithMapSupport, + generateUUID, + isNotEmptyString, + logPrefix, + logger +} from '../../utils/index.js' const moduleName = 'UIHttpServer' @@ -33,7 +40,7 @@ export class UIHttpServer extends AbstractUIServer { } public start (): void { - this.httpServer.on('request', this.requestListener.bind(this) as RequestListener) + this.httpServer.on('request', this.requestListener.bind(this)) this.startHttpServer() } @@ -54,7 +61,7 @@ export class UIHttpServer extends AbstractUIServer { .writeHead(this.responseStatusToStatusCode(payload.status), { 'Content-Type': 'application/json' }) - .end(JSON.stringify(payload)) + .end(JSONStringifyWithMapSupport(payload)) } else { logger.error( `${this.logPrefix(moduleName, 'sendResponse')} Response for unknown request id: ${uuid}` @@ -88,7 +95,7 @@ export class UIHttpServer extends AbstractUIServer { 'WWW-Authenticate': 'Basic realm=users' }) .end(`${StatusCodes.UNAUTHORIZED} Unauthorized`) - .destroy() + res.destroy() req.destroy() } }) @@ -102,7 +109,7 @@ export class UIHttpServer extends AbstractUIServer { this.responseHandlers.set(uuid, res) try { const fullProtocol = `${protocol}${version}` - if (!UIServerUtils.isProtocolAndVersionSupported(fullProtocol)) { + if (!isProtocolAndVersionSupported(fullProtocol)) { throw new BaseError(`Unsupported UI protocol version: '${fullProtocol}'`) } this.registerProtocolVersionUIService(version) @@ -119,10 +126,22 @@ export class UIHttpServer extends AbstractUIServer { bodyBuffer.push(chunk) }) .on('end', () => { - const body = JSON.parse(Buffer.concat(bodyBuffer).toString()) as RequestPayload + let requestPayload: RequestPayload | undefined + try { + requestPayload = JSON.parse(Buffer.concat(bodyBuffer).toString()) as RequestPayload + } catch (error) { + this.sendResponse( + this.buildProtocolResponse(uuid, { + status: ResponseStatus.FAILURE, + errorMessage: (error as Error).message, + errorStack: (error as Error).stack + }) + ) + return + } this.uiServices .get(version) - ?.requestHandler(this.buildProtocolRequest(uuid, procedureName, body)) + ?.requestHandler(this.buildProtocolRequest(uuid, procedureName, requestPayload)) .then((protocolResponse?: ProtocolResponse) => { if (protocolResponse != null) { this.sendResponse(protocolResponse)