Cleanup of akihttplistener

This commit is contained in:
Dev 2023-10-30 09:36:19 +00:00
parent 476788fee8
commit dacb72e78d

View File

@ -28,7 +28,7 @@ export class AkiHttpListener implements IHttpListener
public canHandle(_: string, req: IncomingMessage): boolean public canHandle(_: string, req: IncomingMessage): boolean
{ {
return req.method === "GET" || req.method === "PUT" || req.method === "POST"; return ["GET", "PUT", "POST"].includes(req.method);
} }
public handle(sessionId: string, req: IncomingMessage, resp: ServerResponse): void public handle(sessionId: string, req: IncomingMessage, resp: ServerResponse): void
@ -41,7 +41,6 @@ export class AkiHttpListener implements IHttpListener
this.sendResponse(sessionId, req, resp, null, response); this.sendResponse(sessionId, req, resp, null, response);
break; break;
} }
// these are handled almost identically. // these are handled almost identically.
case "POST": case "POST":
case "PUT": case "PUT":
@ -54,7 +53,8 @@ export class AkiHttpListener implements IHttpListener
const buffer = Buffer.alloc(requestLength); const buffer = Buffer.alloc(requestLength);
let written = 0; let written = 0;
req.on("data", (data: any) => { req.on("data", (data: any) =>
{
data.copy(buffer, written, 0); data.copy(buffer, written, 0);
written += data.length; written += data.length;
}); });
@ -65,12 +65,12 @@ export class AkiHttpListener implements IHttpListener
// determine if the payload is compressed. All PUT requests are, and POST requests without // determine if the payload is compressed. All PUT requests are, and POST requests without
// debug = 1 are as well. This should be fixed. // debug = 1 are as well. This should be fixed.
// let compressed = req.headers["content-encoding"] === "deflate"; // let compressed = req.headers["content-encoding"] === "deflate";
let compressed = req.method === "PUT" || req.headers["debug"] !== "1"; const compressed = req.method === "PUT" || req.headers["debug"] !== "1";
const value = compressed ? zlib.inflateSync(buffer) : buffer; const value = compressed ? zlib.inflateSync(buffer) : buffer;
if (req.headers["debug"] === "1") if (req.headers["debug"] === "1")
{ {
console.log(value.toString()); this.logger.debug(value.toString(), true);
} }
const response = this.getResponse(sessionId, req, value); const response = this.getResponse(sessionId, req, value);
@ -82,18 +82,26 @@ export class AkiHttpListener implements IHttpListener
default: default:
{ {
this.logger.warning(this.localisationService.getText("unknown_request") + ": " + req.method); this.logger.warning(`${this.localisationService.getText("unknown_request")}: ${req.method}`);
break; break;
} }
} }
} }
/**
* Send http response to the client
* @param sessionID Player id
* @param req Incoming request
* @param resp Outgoing response
* @param body Buffer
* @param output Server generated response data
*/
public sendResponse(sessionID: string, req: IncomingMessage, resp: ServerResponse, body: Buffer, output: string): void public sendResponse(sessionID: string, req: IncomingMessage, resp: ServerResponse, body: Buffer, output: string): void
{ {
const info = this.getBodyInfo(body); const info = this.getBodyInfo(body);
let handled = false; let handled = false;
// Check if this is a debug request, if so just send the raw response without transformation. // Check if this is a debug request, if so just send the raw response without transformation
if (req.headers["debug"] === "1") if (req.headers["debug"] === "1")
{ {
this.sendJson(resp, output, sessionID); this.sendJson(resp, output, sessionID);