Fix bug in HTTP response parsing

This commit is contained in:
Logan Cusano
2023-06-18 22:53:23 -04:00
parent 597546b73d
commit e062cf5794
2 changed files with 23 additions and 22 deletions

View File

@@ -5,11 +5,16 @@ const log = new DebugBuilder("client", "httpRequests");
require('dotenv').config(); require('dotenv').config();
// Modules // Modules
const http = require("http"); const http = require("http");
const { isJsonString } = require("./utilities.js");
exports.requestOptions = class requestOptions { exports.requestOptions = class requestOptions {
constructor(path, method, hostname = undefined, headers = undefined, port = undefined) { constructor(path, method, hostname = undefined, headers = undefined, port = undefined) {
if (method === "POST"){ if (method === "POST"){
this.hostname = hostname ?? process.env.SERVER_HOSTNAME ?? process.env.SERVER_IP; log.DEBUG("Hostname Vars: ", hostname, process.env.SERVER_HOSTNAME, process.env.SERVER_IP);
if (hostname) this.hostname = hostname;
if (process.env.SERVER_HOSTNAME) this.hostname = process.env.SERVER_HOSTNAME;
if (process.env.SERVER_IP) this.hostname = process.env.SERVER_IP;
if (!this.hostname) throw new Error("No server hostname / IP was given when creating a request");
this.path = path; this.path = path;
this.port = port ?? process.env.SERVER_PORT; this.port = port ?? process.env.SERVER_PORT;
this.method = method; this.method = method;
@@ -31,31 +36,27 @@ exports.sendHttpRequest = function sendHttpRequest(requestOptions, data, callbac
// Create the request // Create the request
const req = http.request(requestOptions, res => { const req = http.request(requestOptions, res => {
res.on('data', (data) => { res.on('data', (data) => {
if (res.statusCode >= 200 && res.statusCode <= 299) { log.DEBUG("Response data from new node: ", data);
const responseObject = { const responseObject = {
"statusCode": res.statusCode, "statusCode": res.statusCode,
"body": JSON.parse(data) "body": (isJsonString(data.toString())) ? JSON.parse(data.toString()) : data.toString()
}; };
log.DEBUG("Response Object: ", responseObject); log.DEBUG("Response Object: ", responseObject);
callback(responseObject); callback(responseObject);
}
if (res.statusCode >= 300) {
const responseObject = {
"statusCode": res.statusCode,
"body": data
};
log.DEBUG("Response Object: ", responseObject);
callback(responseObject);
}
}) })
}).on('error', err => { }).on('error', err => {
log.ERROR('Error: ', err.message) if (err.code === "ECONNREFUSED"){
// Bot refused connection, assumed offline
log.WARN("Connection Refused");
}
else log.ERROR('Error: ', err.message, err);
callback(undefined);
// TODO need to handle if the server is down // TODO need to handle if the server is down
}) })
// Write the data to the request and send it // Write the data to the request and send it
req.write(data) req.write(data);
req.end() req.end();
} }
exports.onHttpError = function onHttpError(httpStatusCode) { exports.onHttpError = function onHttpError(httpStatusCode) {

View File

@@ -40,7 +40,7 @@ exports.sendHttpRequest = function sendHttpRequest(requestOptions, data, callbac
res.on('data', (data) => { res.on('data', (data) => {
const responseObject = { const responseObject = {
"statusCode": res.statusCode, "statusCode": res.statusCode,
"body": (isJsonString(data.toString)) ? JSON.parse(data) : data.toString() "body": (isJsonString(data.toString())) ? JSON.parse(data.toString()) : data.toString()
}; };
log.DEBUG("Response Object: ", responseObject); log.DEBUG("Response Object: ", responseObject);
callback(responseObject); callback(responseObject);