/* * api.c - the CGI front end for plugin_memory: the browser page and the JSON * actions it talks to. * * There is deliberately no second implementation of the graph operations here. * Every action maps onto one of the nine tools in tools.c, so the page and an * agent going through --mcp see exactly the same thing - the same skips, the * same not-found messages, the same relation rules. When the two are separate * they drift, and the drift only ever shows up in production. * * Parameters are read from the QUERY_STRING *and* from a JSON body, because * the UI posts JSON (it must: an observation can be any text) while a person * debugging with curl expects a query string. `action` and `format` are kept * in separate buffers on purpose - reading one into the other's variable * silently degrades an action request to the default action. * * MIT License. Copyright (c) 2026 SCORPIOX INC. */ #if defined(_WIN32) && !defined(_WIN32_WINNT) # define _WIN32_WINNT 0x0601 #endif #if !defined(_WIN32) # define _POSIX_C_SOURCE 200809L # define _DEFAULT_SOURCE 1 # if defined(__APPLE__) # define _DARWIN_C_SOURCE 1 # endif #endif #include #include #include #include #include #if defined(_WIN32) # define WIN32_LEAN_AND_MEAN # include #else # include #endif #include "memory.h" #include "bot_theme_plugin.h" #include "ui_html.h" /* ------------------------------------------------------------------ */ /* Query string */ /* ------------------------------------------------------------------ */ static void url_decode(char *s) { char *o = s; if (!s) return; while (*s) { if (*s == '%' && isxdigit((unsigned char)s[1]) && isxdigit((unsigned char)s[2])) { int hi = (s[1] <= '9') ? s[1] - '0' : (tolower((unsigned char)s[1]) - 'a' + 10); int lo = (s[2] <= '9') ? s[2] - '0' : (tolower((unsigned char)s[2]) - 'a' + 10); *o++ = (char)((hi << 4) | lo); s += 3; } else if (*s == '+') { *o++ = ' '; s++; } else { *o++ = *s++; } } *o = '\0'; } static int get_query_val(const char *qs, const char *key, char *out, size_t outsz) { size_t klen; const char *p = qs; if (out && outsz) out[0] = '\0'; if (!qs || !*qs || !key || !out || outsz == 0) return 0; klen = strlen(key); while (p && *p) { const char *eq = strchr(p, '='); const char *amp = strchr(p, '&'); if (!eq || (amp && eq > amp)) { p = amp ? amp + 1 : NULL; continue; } if ((size_t)(eq - p) == klen && strncmp(p, key, klen) == 0) { const char *vs = eq + 1; size_t n = amp ? (size_t)(amp - vs) : strlen(vs); if (n >= outsz) n = outsz - 1; memcpy(out, vs, n); out[n] = '\0'; url_decode(out); return 1; } p = amp ? amp + 1 : NULL; } return 0; } /* ------------------------------------------------------------------ */ /* Request body */ /* ------------------------------------------------------------------ */ static char g_body[REQ_BODY_MAX]; /* Read the body only when CONTENT_LENGTH says there is one: a CGI handler * that reads stdin unconditionally hangs on an inherited empty stdin. */ static int read_request_body(void) { const char *cl = getenv("CONTENT_LENGTH"); long len; size_t got; g_body[0] = '\0'; if (!cl || !*cl) return 0; len = strtol(cl, NULL, 10); if (len <= 0) return 0; if ((size_t)len >= sizeof g_body) return -1; got = fread(g_body, 1, (size_t)len, stdin); g_body[got] = '\0'; return (int)got; } static yyjson_doc *g_body_doc = NULL; static yyjson_val *body_root(void) { return g_body_doc ? yyjson_doc_get_root(g_body_doc) : NULL; } /* One parameter: query string first, then the JSON body. Strings are * returned in `out`; `has` says whether either source had it. */ static int param_str(const char *qs, const char *key, char *out, size_t outsz) { if (out && outsz) out[0] = '\0'; if (qs && get_query_val(qs, key, out, outsz) && out[0]) return 1; { yyjson_val *root = body_root(); yyjson_val *v = root ? yyjson_obj_get(root, key) : NULL; if (v && yyjson_is_str(v)) { str_copy(out, outsz, yyjson_get_str(v)); return 1; } } return 0; } /* An array parameter (e.g. names[], observations[]) from either source. A * query string accepts a JSON array value or a |-separated list. */ static yyjson_val *body_arr(const char *key) { yyjson_val *root = body_root(); yyjson_val *v = root ? yyjson_obj_get(root, key) : NULL; return (v && yyjson_is_arr(v)) ? v : NULL; } /* ------------------------------------------------------------------ */ /* Node authentication */ /* */ /* The node's :8150 is LAN-reachable, and this plugin can read and write the * operator's knowledge graph, so it must not answer a bare curl. Three * credential forms are accepted because the three callers differ: bot-web * proxies with a header, the plugin iframe appends ?pwd=, and a script may * send a bearer token. --meta stays open so /plugins can still discover us. */ /* ------------------------------------------------------------------ */ static void require_node_auth(void) { const char *np = getenv("BOT_PASSWORD"); const char *xp, *auth, *qs; char qpwd[256]; if (!np || !*np) return; /* node has no password set */ xp = getenv("HTTP_X_BOT_PASSWORD"); if (xp && strcmp(xp, np) == 0) return; auth = getenv("HTTP_AUTHORIZATION"); if (auth) { while (*auth == ' ') auth++; if (strncmp(auth, "Bearer ", 7) == 0) { auth += 7; while (*auth == ' ') auth++; if (strcmp(auth, np) == 0) return; } } qs = getenv("QUERY_STRING"); if (qs && get_query_val(qs, "pwd", qpwd, sizeof qpwd) && strcmp(qpwd, np) == 0) return; fputs("Status: 401 Unauthorized\r\n" "Content-Type: application/json; charset=utf-8\r\n" "Cache-Control: no-store\r\n" "X-Content-Type-Options: nosniff\r\n\r\n" "{\"ok\":false,\"error\":\"invalid node password\"}\n", stdout); exit(0); } /* ------------------------------------------------------------------ */ /* Responses */ /* ------------------------------------------------------------------ */ static void json_header(void) { fputs("Content-Type: application/json; charset=utf-8\r\n" "Cache-Control: no-store\r\n" "X-Content-Type-Options: nosniff\r\n\r\n", stdout); } static void json_raw(const char *json) { json_header(); fputs(json ? json : "{\"ok\":true}", stdout); fputc('\n', stdout); fflush(stdout); } static void json_err(int code, const char *msg) { buf_t b; const char *status = NULL; if (code == 400) status = "Status: 400 Bad Request\r\n"; else if (code == 404) status = "Status: 404 Not Found\r\n"; else if (code == 413) status = "Status: 413 Payload Too Large\r\n"; else status = "Status: 500 Internal Server Error\r\n"; buf_init(&b); buf_puts(&b, status); buf_puts(&b, "Content-Type: application/json; charset=utf-8\r\n" "Cache-Control: no-store\r\n" "X-Content-Type-Options: nosniff\r\n\r\n" "{\"ok\":false,\"status\":"); { char t[8]; snprintf(t, sizeof t, "%d", code); buf_puts(&b, t); } buf_puts(&b, ",\"error\":"); json_str(&b, msg ? msg : "error"); buf_puts(&b, "}"); fputs(buf_str(&b), stdout); fputc('\n', stdout); fflush(stdout); buf_free(&b); } /* Run a tool and answer with its result, mapping the tool's own error to a * 200 + ok:false so the UI can show the message the reference server gives * rather than a bare HTTP status. */ /* Run one tool for an HTTP action. The UI's arguments were assembled into a * mutable document, so serialize it and re-parse: the tools read the * immutable yyjson_val API, and a cast between the two types would be a * silent type-lie that only yyjson's internals could expose. At these sizes * one round trip costs nothing, and both types stay exactly what they are. */ static void run_tool(const char *name, yyjson_mut_doc *md, yyjson_mut_val *root) { tool_out_t t; char *s; char *packed = NULL; yyjson_doc *parsed = NULL; yyjson_val *args = NULL; if (md && root) { packed = yyjson_mut_write(md, 0, NULL); if (packed) { parsed = yyjson_read(packed, strlen(packed), 0); if (parsed) args = yyjson_doc_get_root(parsed); } } tool_out_init(&t); if (tool_dispatch(name, args, &t) != 0) { tool_free(&t); free(packed); if (parsed) yyjson_doc_free(parsed); json_err(400, "unknown action"); return; } s = tool_api_result(&t); json_raw(s); free(s); tool_free(&t); free(packed); if (parsed) yyjson_doc_free(parsed); } /* ------------------------------------------------------------------ */ /* Args assembled from the request */ /* ------------------------------------------------------------------ */ /* Build a mutable doc so we can graft arrays taken from the body together * with scalars from the query string, and hand the tools one object either * way. The doc owns the memory the tools read during dispatch. */ typedef struct { yyjson_mut_doc *md; yyjson_mut_val *root; } args_t; static void args_init(args_t *a) { memset(a, 0, sizeof *a); a->md = yyjson_mut_doc_new(NULL); if (!a->md) return; a->root = yyjson_mut_obj(a->md); if (a->root) yyjson_mut_doc_set_root(a->md, a->root); } static void args_free(args_t *a) { if (a && a->md) yyjson_mut_doc_free(a->md); memset(a, 0, sizeof *a); } static void args_add_str(args_t *a, const char *k, const char *v) { if (a->root) yyjson_mut_obj_add_str(a->md, a->root, k, v ? v : ""); } static void args_add_int(args_t *a, const char *k, long v) { if (a->root) yyjson_mut_obj_add_sint(a->md, a->root, k, (int64_t)v); } /* A string array: from the body when present, else a |-separated query value, * else a repeated single value. */ static void args_add_arr(args_t *a, const char *k, const char *qval) { yyjson_val *src = body_arr(k); (void)qval; yyjson_mut_val *arr = a->root ? yyjson_mut_arr(a->md) : NULL; if (!arr) return; if (src) { size_t i, n = yyjson_arr_size(src); for (i = 0; i < n; i++) { yyjson_val *v = yyjson_arr_get(src, i); if (v && yyjson_is_str(v)) yyjson_mut_arr_add_str(a->md, arr, yyjson_get_str(v)); } } else if (qval && *qval) { /* '|' separated, with \%7C available for a literal pipe. */ const char *p = qval; while (*p) { const char *s = strchr(p, '|'); size_t len = s ? (size_t)(s - p) : strlen(p); char tmp[OBS_TEXT_MAX]; if (len >= sizeof tmp) len = sizeof tmp - 1; memcpy(tmp, p, len); tmp[len] = '\0'; yyjson_mut_arr_add_str(a->md, arr, tmp); if (!s) break; p = s + 1; } } if (a->root) yyjson_mut_obj_add_val(a->md, a->root, k, arr); } /* ------------------------------------------------------------------ */ /* Actions */ /* ------------------------------------------------------------------ */ static void do_stats(const char *qs) { long long size = 0; int exists = 0; size_t i, n_obs = 0, types_seen = 0; const char **types = NULL; buf_t b; (void)qs; if (graph_load() != 0) { json_err(500, g_data_err); return; } graph_path_info(&size, &exists); for (i = 0; i < G.n_ent; i++) n_obs += G.ent[i].n_obs; /* distinct types, for the filter chips */ types = (const char **)calloc(G.n_ent ? G.n_ent : 1, sizeof *types); buf_init(&b); buf_puts(&b, "{\"ok\":true,\"entities\":"); { char t[32]; snprintf(t, sizeof t, "%zu", G.n_ent); buf_puts(&b, t); } buf_puts(&b, ",\"relations\":"); { char t[32]; snprintf(t, sizeof t, "%zu", G.n_rel); buf_puts(&b, t); } buf_puts(&b, ",\"observations\":"); { char t[32]; snprintf(t, sizeof t, "%zu", n_obs); buf_puts(&b, t); } buf_puts(&b, ",\"unreadable_lines\":"); { char t[32]; snprintf(t, sizeof t, "%zu", G.n_bad); buf_puts(&b, t); } buf_puts(&b, ",\"file_exists\":"); buf_puts(&b, exists ? "true" : "false"); buf_puts(&b, ",\"file_bytes\":"); { char t[32]; snprintf(t, sizeof t, "%lld", size); buf_puts(&b, t); } buf_puts(&b, ",\"types\":["); if (types) { for (i = 0; i < G.n_ent; i++) { size_t j; int seen = 0; for (j = 0; j < types_seen; j++) if (strcmp(types[j], G.ent[i].etype) == 0) { seen = 1; break; } if (seen) continue; if (types_seen >= 200) break; types[types_seen++] = G.ent[i].etype; if (types_seen > 1) buf_putc(&b, ','); json_str(&b, G.ent[i].etype); } } buf_puts(&b, "]}"); json_raw(buf_str(&b)); buf_free(&b); free((void *)types); } static void do_graph(const char *qs) { char types[4096], lim[32]; args_t a; args_init(&a); if (param_str(qs, "entityTypes", types, sizeof types)) args_add_arr(&a, "entityTypes", types); if (param_str(qs, "limit", lim, sizeof lim)) args_add_int(&a, "limit", strtol(lim, NULL, 10)); run_tool("read_graph", a.md, a.root); args_free(&a); } static void do_search(const char *qs) { char q[512], lim[32], ol[32]; args_t a; if (!param_str(qs, "q", q, sizeof q) || !q[0]) { json_err(400, "missing q"); return; } args_init(&a); args_add_str(&a, "query", q); if (param_str(qs, "limit", lim, sizeof lim)) args_add_int(&a, "limit", strtol(lim, NULL, 10)); if (param_str(qs, "observationLimit", ol, sizeof ol)) args_add_int(&a, "observationLimit", strtol(ol, NULL, 10)); run_tool("search_nodes", a.md, a.root); args_free(&a); } static void do_entity(const char *qs) { char name[SX_NAME_MAX * 2]; args_t a; if (!param_str(qs, "name", name, sizeof name) || !name[0]) { json_err(400, "missing name"); return; } args_init(&a); { yyjson_mut_val *arr = yyjson_mut_arr(a.md); yyjson_mut_arr_add_strcpy(a.md, arr, name); yyjson_mut_obj_add_val(a.md, a.root, "names", arr); } run_tool("open_nodes", a.md, a.root); args_free(&a); } static void do_add_entity(const char *qs) { char name[SX_NAME_MAX * 2], type[SX_TYPE_MAX * 2]; args_t a; yyjson_mut_val *ents, *ent; yyjson_val *bobs; if (!param_str(qs, "name", name, sizeof name) || !name[0]) { json_err(400, "missing name"); return; } if (!param_str(qs, "entityType", type, sizeof type) || !type[0]) { json_err(400, "missing entityType"); return; } args_init(&a); ents = yyjson_mut_arr(a.md); ent = yyjson_mut_obj(a.md); yyjson_mut_obj_add_strcpy(a.md, ent, "name", name); yyjson_mut_obj_add_strcpy(a.md, ent, "entityType", type); bobs = body_arr("observations"); if (!bobs) { char obs[OBS_TEXT_MAX * 4]; if (param_str(qs, "observations", obs, sizeof obs)) { const char *p = obs; yyjson_mut_val *arr = yyjson_mut_arr(a.md); while (*p) { const char *s = strchr(p, '|'); size_t len = s ? (size_t)(s - p) : strlen(p); char tmp[OBS_TEXT_MAX]; if (len >= sizeof tmp) len = sizeof tmp - 1; memcpy(tmp, p, len); tmp[len] = '\0'; if (*tmp) yyjson_mut_arr_add_strcpy(a.md, arr, tmp); if (!s) break; p = s + 1; } yyjson_mut_obj_add_val(a.md, ent, "observations", arr); } else { yyjson_mut_obj_add_val(a.md, ent, "observations", yyjson_mut_arr(a.md)); } } else { yyjson_mut_val *arr = yyjson_mut_arr(a.md); size_t i, n = yyjson_arr_size(bobs); for (i = 0; i < n; i++) { yyjson_val *v = yyjson_arr_get(bobs, i); if (v && yyjson_is_str(v)) yyjson_mut_arr_add_strcpy(a.md, arr, yyjson_get_str(v)); } yyjson_mut_obj_add_val(a.md, ent, "observations", arr); } yyjson_mut_arr_add_val(ents, ent); yyjson_mut_obj_add_val(a.md, a.root, "entities", ents); run_tool("create_entities", a.md, a.root); args_free(&a); } static void do_add_relation(const char *qs) { char from[SX_NAME_MAX * 2], to[SX_NAME_MAX * 2], type[SX_TYPE_MAX * 2]; args_t a; yyjson_mut_val *rels, *rel; if (!param_str(qs, "from", from, sizeof from) || !from[0]) { json_err(400, "missing from"); return; } if (!param_str(qs, "to", to, sizeof to) || !to[0]) { json_err(400, "missing to"); return; } if (!param_str(qs, "relationType", type, sizeof type) || !type[0]) { json_err(400, "missing relationType"); return; } args_init(&a); rels = yyjson_mut_arr(a.md); rel = yyjson_mut_obj(a.md); yyjson_mut_obj_add_strcpy(a.md, rel, "from", from); yyjson_mut_obj_add_strcpy(a.md, rel, "to", to); yyjson_mut_obj_add_strcpy(a.md, rel, "relationType", type); yyjson_mut_arr_add_val(rels, rel); yyjson_mut_obj_add_val(a.md, a.root, "relations", rels); run_tool("create_relations", a.md, a.root); args_free(&a); } static void do_add_obs(const char *qs) { char name[SX_NAME_MAX * 2]; args_t a; yyjson_mut_val *obs, *one, *arr; yyjson_val *bcontents; if (!param_str(qs, "name", name, sizeof name) || !name[0]) { json_err(400, "missing name"); return; } args_init(&a); obs = yyjson_mut_arr(a.md); one = yyjson_mut_obj(a.md); arr = yyjson_mut_arr(a.md); yyjson_mut_obj_add_strcpy(a.md, one, "entityName", name); bcontents = body_arr("contents"); if (bcontents) { size_t i, n = yyjson_arr_size(bcontents); for (i = 0; i < n; i++) { yyjson_val *v = yyjson_arr_get(bcontents, i); if (v && yyjson_is_str(v)) yyjson_mut_arr_add_strcpy(a.md, arr, yyjson_get_str(v)); } } else { char contents[OBS_TEXT_MAX * 4]; if (param_str(qs, "contents", contents, sizeof contents)) { const char *p = contents; while (*p) { const char *s = strchr(p, '|'); size_t len = s ? (size_t)(s - p) : strlen(p); char tmp[OBS_TEXT_MAX]; if (len >= sizeof tmp) len = sizeof tmp - 1; memcpy(tmp, p, len); tmp[len] = '\0'; if (*tmp) yyjson_mut_arr_add_strcpy(a.md, arr, tmp); if (!s) break; p = s + 1; } } } yyjson_mut_obj_add_val(a.md, one, "contents", arr); yyjson_mut_arr_add_val(obs, one); yyjson_mut_obj_add_val(a.md, a.root, "observations", obs); run_tool("add_observations", a.md, a.root); args_free(&a); } static void do_delete_entity(const char *qs) { char name[SX_NAME_MAX * 2]; args_t a; yyjson_mut_val *arr; if (!param_str(qs, "name", name, sizeof name) || !name[0]) { json_err(400, "missing name"); return; } args_init(&a); arr = yyjson_mut_arr(a.md); yyjson_mut_arr_add_strcpy(a.md, arr, name); yyjson_mut_obj_add_val(a.md, a.root, "entityNames", arr); run_tool("delete_entities", a.md, a.root); args_free(&a); } static void do_delete_relation(const char *qs) { char from[SX_NAME_MAX * 2], to[SX_NAME_MAX * 2], type[SX_TYPE_MAX * 2]; args_t a; yyjson_mut_val *rels, *rel; if (!param_str(qs, "from", from, sizeof from) || !from[0] || !param_str(qs, "to", to, sizeof to) || !to[0] || !param_str(qs, "relationType", type, sizeof type) || !type[0]) { json_err(400, "from, to and relationType are all required"); return; } args_init(&a); rels = yyjson_mut_arr(a.md); rel = yyjson_mut_obj(a.md); yyjson_mut_obj_add_strcpy(a.md, rel, "from", from); yyjson_mut_obj_add_strcpy(a.md, rel, "to", to); yyjson_mut_obj_add_strcpy(a.md, rel, "relationType", type); yyjson_mut_arr_add_val(rels, rel); yyjson_mut_obj_add_val(a.md, a.root, "relations", rels); run_tool("delete_relations", a.md, a.root); args_free(&a); } static void do_delete_obs(const char *qs) { char name[SX_NAME_MAX * 2]; args_t a; yyjson_mut_val *deletions, *one, *arr; yyjson_val *bobs; if (!param_str(qs, "name", name, sizeof name) || !name[0]) { json_err(400, "missing name"); return; } args_init(&a); deletions = yyjson_mut_arr(a.md); one = yyjson_mut_obj(a.md); arr = yyjson_mut_arr(a.md); yyjson_mut_obj_add_strcpy(a.md, one, "entityName", name); bobs = body_arr("observations"); if (bobs) { size_t i, n = yyjson_arr_size(bobs); for (i = 0; i < n; i++) { yyjson_val *v = yyjson_arr_get(bobs, i); if (v && yyjson_is_str(v)) yyjson_mut_arr_add_strcpy(a.md, arr, yyjson_get_str(v)); } } else { char obs[OBS_TEXT_MAX * 4]; if (param_str(qs, "observations", obs, sizeof obs)) { const char *p = obs; while (*p) { const char *s = strchr(p, '|'); size_t len = s ? (size_t)(s - p) : strlen(p); char tmp[OBS_TEXT_MAX]; if (len >= sizeof tmp) len = sizeof tmp - 1; memcpy(tmp, p, len); tmp[len] = '\0'; if (*tmp) yyjson_mut_arr_add_strcpy(a.md, arr, tmp); if (!s) break; p = s + 1; } } } yyjson_mut_obj_add_val(a.md, one, "observations", arr); yyjson_mut_arr_add_val(deletions, one); yyjson_mut_obj_add_val(a.md, a.root, "deletions", deletions); run_tool("delete_observations", a.md, a.root); args_free(&a); } /* The raw JSONL, for an operator who wants to diff or back up the graph. * Served as text/plain with a filename so the browser offers a download. */ static void do_export(void) { char *text; size_t len = 0; const char *p = graph_path(); if (graph_load() != 0) { json_err(500, g_data_err); return; } { FILE *f = (p && *p) ? fopen(p, "rb") : NULL; text = NULL; if (f) { long sz; fseek(f, 0, SEEK_END); sz = ftell(f); if (sz < 0) sz = 0; if ((size_t)sz > (size_t)FILE_MAX) sz = (long)FILE_MAX; if (sz > 0) fseek(f, 0, SEEK_SET); text = (char *)malloc((size_t)sz + 1); if (text) len = fread(text, 1, (size_t)sz, f); text[len] = '\0'; fclose(f); } } fputs("Content-Type: text/plain; charset=utf-8\r\n" "Cache-Control: no-store\r\n" "X-Content-Type-Options: nosniff\r\n" "Content-Disposition: attachment; filename=\"memory.jsonl\"\r\n\r\n", stdout); if (text && len) fwrite(text, 1, len, stdout); free(text); fflush(stdout); } /* tools/list over HTTP - the same schemas an MCP host gets, so the Setup UI * and an agent can never disagree about what this connector offers. */ static void do_tools(void) { char *t = tools_list_json(); buf_t b; buf_init(&b); buf_puts(&b, "{\"ok\":true,\"connector\":\"" PLUGIN_ID "\",\"kind\":\"mcp\",\"protocol\":\"" MCP_PROTOCOL "\",\"tools\":"); buf_puts(&b, t ? t : "[]"); buf_puts(&b, "}"); json_raw(buf_str(&b)); buf_free(&b); free(t); } /* ------------------------------------------------------------------ */ /* The page */ /* ------------------------------------------------------------------ */ static void emit_ui(void) { const char *p = UI_HTML; while (*p) { if (*p == '\001') { fputs(SX_BOT_THEME_PREPAINT "", stdout); p++; } else if (*p == '\002') { fputs(SX_BOT_THEME_HANDOFF, stdout); p++; } else { putchar(*p++); } } } static void mode_html(void) { fputs("Content-Type: text/html; charset=utf-8\r\n" "Cache-Control: no-store\r\n" "X-Content-Type-Options: nosniff\r\n" "X-Frame-Options: SAMEORIGIN\r\n" "Content-Security-Policy: default-src 'none'; script-src 'unsafe-inline'; " "style-src 'unsafe-inline'; img-src data:; connect-src 'self'; " "form-action 'none'; base-uri 'none'\r\n\r\n", stdout); emit_ui(); } /* ------------------------------------------------------------------ */ /* Entry */ /* ------------------------------------------------------------------ */ static const char *method(void) { const char *m = getenv("REQUEST_METHOD"); return (m && *m) ? m : "GET"; } int api_handle(void) { const char *qs = getenv("QUERY_STRING"); char action[64], act[64], fmt[32]; int got; if (!qs) qs = ""; action[0] = '\0'; act[0] = '\0'; fmt[0] = '\0'; require_node_auth(); got = read_request_body(); if (got < 0) { json_err(413, "the request body is too large"); return 0; } if (got > 0) { g_body_doc = yyjson_read(g_body, strlen(g_body), 0); if (!g_body_doc) { json_err(400, "the request body is not valid JSON"); return 0; } } /* Two buffers on purpose: parsing `format` into `action` would clobber the * action we just parsed and silently answer the default one. */ if (!get_query_val(qs, "action", action, sizeof action)) { if (param_str(qs, "action", action, sizeof action)) { /* body action */ } } if (!action[0]) { if (get_query_val(qs, "format", fmt, sizeof fmt) && strcmp(fmt, "json") == 0) str_copy(action, sizeof action, "graph"); else if (strcmp(method(), "POST") == 0) { json_err(400, "missing action"); return 0; } } str_copy(act, sizeof act, action); if (act[0]) { if (strcmp(act, "stats") == 0) { do_stats(qs); } else if (strcmp(act, "graph") == 0) { do_graph(qs); } else if (strcmp(act, "search") == 0) { do_search(qs); } else if (strcmp(act, "entity") == 0) { do_entity(qs); } else if (strcmp(act, "add_entity") == 0) { do_add_entity(qs); } else if (strcmp(act, "add_relation") == 0){ do_add_relation(qs); } else if (strcmp(act, "add_obs") == 0) { do_add_obs(qs); } else if (strcmp(act, "delete_entity") == 0) { do_delete_entity(qs); } else if (strcmp(act, "delete_relation") == 0) { do_delete_relation(qs); } else if (strcmp(act, "delete_obs") == 0) { do_delete_obs(qs); } else if (strcmp(act, "tools") == 0) { do_tools(); } else if (strcmp(act, "export") == 0) { do_export(); } else json_err(400, "unknown action"); if (g_body_doc) yyjson_doc_free(g_body_doc); return 0; } mode_html(); if (g_body_doc) yyjson_doc_free(g_body_doc); return 0; }