Fixed issue with responses not getting sent

This commit is contained in:
nolan 2025-06-19 12:35:00 -04:00
parent 978472ed21
commit 2ed31347db

View File

@ -114,7 +114,7 @@ void updateUtilization(){
} }
enum MHD_Result sendPage(struct MHD_Connection *connection, char* page, char *mimetype){ enum MHD_Result sendPage(struct MHD_Connection *connection, char* page, char *mimetype){
struct MHD_Response *response = MHD_create_response_from_buffer(strlen(page), (void *) page, MHD_RESPMEM_PERSISTENT); struct MHD_Response *response = MHD_create_response_from_buffer(strlen(page), (void *) page, MHD_RESPMEM_MUST_COPY);
MHD_add_response_header(response, "Content-Type", mimetype); MHD_add_response_header(response, "Content-Type", mimetype);
MHD_add_response_header(response, "Access-Control-Allow-Origin", "*"); MHD_add_response_header(response, "Access-Control-Allow-Origin", "*");
enum MHD_Result result = MHD_queue_response(connection, MHD_HTTP_OK, response); enum MHD_Result result = MHD_queue_response(connection, MHD_HTTP_OK, response);
@ -123,27 +123,24 @@ enum MHD_Result sendPage(struct MHD_Connection *connection, char* page, char *mi
} }
int answerConnection(void *cls, struct MHD_Connection *connection, const char *url, const char *method, const char *version, const char *uploadData, size_t *uploadDataSize, void **con_ls){ int answerConnection(void *cls, struct MHD_Connection *connection, const char *url, const char *method, const char *version, const char *uploadData, size_t *uploadDataSize, void **con_ls){
if (strcmp(url, "/server") == 0){ cJSON *response = cJSON_CreateObject();
cJSON *response = cJSON_CreateObject(); cJSON_AddStringToObject(response, "uptime", uptime);
cJSON_AddStringToObject(response, "uptime", uptime); cJSON_AddNumberToObject(response, "cpuCount", coreCount);
cJSON_AddNumberToObject(response, "cpuCount", coreCount); for (short int i = 0; i < coreCount + 1; i++){
for (short int i = 0; i < coreCount + 1; i++){ char *buf = malloc(32);
char *buf = malloc(32); sprintf(buf, "cpu%i", i);
sprintf(buf, "cpu%i", i); cJSON_AddNumberToObject(response, buf, (int) (cpuUtilization[i] + 0.5));
cJSON_AddNumberToObject(response, buf, (int) (cpuUtilization[i] + 0.5)); free(buf);
free(buf);
}
cJSON_AddNumberToObject(response, "totalRam", maxRam);
cJSON_AddNumberToObject(response, "usedRam", usedRam);
cJSON_AddNumberToObject(response, "totalSwap", maxSwap);
cJSON_AddNumberToObject(response, "usedSwap", usedSwap);
enum MHD_Result result = sendPage(connection, cJSON_Print(response), "application/json");
cJSON_Delete(response);
return result;
} }
return MHD_NO; cJSON_AddNumberToObject(response, "totalRam", maxRam);
cJSON_AddNumberToObject(response, "usedRam", usedRam);
cJSON_AddNumberToObject(response, "totalSwap", maxSwap);
cJSON_AddNumberToObject(response, "usedSwap", usedSwap);
enum MHD_Result result = sendPage(connection, cJSON_Print(response), "application/json");
cJSON_Delete(response);
return result;
} }
void readConfig(){ void readConfig(){