#include "Info.h" #include "../Config.h" #include "../Scraping/Scraping.h" #include "../Utility/Utility.h" #include #include #include #include static int append_json(char *buf, size_t size, size_t *used, const char *format, ...) { if (*used >= size) { return -1; } va_list args; va_start(args, format); int written = vsnprintf(buf + *used, size - *used, format, args); va_end(args); if (written < 0 || (size_t)written >= size - *used) { return -1; } *used += (size_t)written; return 0; } int info_handler(UrlParams *params) { (void)params; extern Config global_config; char json[INITIAL_BUFFER_SIZE]; size_t used = 0; if (append_json(json, sizeof(json), &used, "{\"version\":\"%s\",", VERSION) != 0 || append_json(json, sizeof(json), &used, "\"git_remote\":\"%s\",", GIT_REMOTE) != 0 || append_json(json, sizeof(json), &used, "\"locale\":\"%s\",", global_config.default_locale) != 0 || append_json(json, sizeof(json), &used, "\"engines\":[") != 0) { send_response("Failed to build instance info"); return -1; } int first = 1; for (int i = 0; i < ENGINE_COUNT; i++) { if (!ENGINE_REGISTRY[i].enabled) { continue; } if (append_json(json, sizeof(json), &used, "%s{\"id\":\"%s\",\"name\":\"%s\"}", first ? "" : ",", ENGINE_REGISTRY[i].id, ENGINE_REGISTRY[i].name) != 0) { send_response("Failed to build instance info"); return -1; } first = 0; } if (append_json(json, sizeof(json), &used, "]}") != 0) { send_response("Failed to build instance info"); return -1; } serve_data(json, used, "application/json"); return 0; }