MCPcopy Create free account
hub / github.com/DeusData/codebase-memory-mcp / handle_ui_log_post

Function handle_ui_log_post

src/ui/http_server.c:887–1043  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

885}
886
887static void handle_ui_log_post(cbm_http_conn_t *c, const cbm_http_req_t *req) {
888 if (req->body_len > UI_LOG_BODY_MAX) {
889 cbm_http_replyf(c, 413, g_cors_json, "{\"error\":\"body exceeds %d bytes\"}",
890 UI_LOG_BODY_MAX);
891 return;
892 }
893 if (req->body_len == 0) {
894 cbm_http_replyf(c, 400, g_cors_json, "{\"error\":\"invalid body\"}");
895 return;
896 }
897 yyjson_doc *doc = yyjson_read(req->body, req->body_len, 0);
898 if (!doc) {
899 cbm_http_replyf(c, 400, g_cors_json, "{\"error\":\"invalid json\"}");
900 return;
901 }
902 yyjson_val *root = yyjson_doc_get_root(doc);
903 yyjson_val *entries = root ? yyjson_obj_get(root, "entries") : NULL;
904 if (!root || !yyjson_is_obj(root) || !entries || !yyjson_is_arr(entries)) {
905 yyjson_doc_free(doc);
906 cbm_http_replyf(c, 400, g_cors_json, "{\"error\":\"entries[] required\"}");
907 return;
908 }
909
910 ui_log_scratch_t *s = cbm_calloc(CBM_MEM_CLASS_OTHER, sizeof(*s));
911 char path[CBM_SZ_1K];
912 if (!s || !cbm_ui_log_file_path(path, sizeof(path))) {
913 cbm_free(CBM_MEM_CLASS_OTHER, s);
914 yyjson_doc_free(doc);
915 cbm_http_replyf(c, 500, g_cors_json, "{\"error\":\"no log path\"}");
916 return;
917 }
918
919 /* Page and session are the same for every entry of a post. */
920 yyjson_val *v_page = yyjson_obj_get(root, "page");
921 yyjson_val *v_session = yyjson_obj_get(root, "session");
922 ui_log_copy_field(s->field, sizeof(s->field),
923 (v_page && yyjson_is_str(v_page)) ? yyjson_get_str(v_page) : "", 256);
924 cbm_json_escape(s->page, (int)sizeof(s->page), s->field);
925 ui_log_copy_field(s->field, sizeof(s->field),
926 (v_session && yyjson_is_str(v_session)) ? yyjson_get_str(v_session) : "",
927 128);
928 cbm_json_escape(s->session, (int)sizeof(s->session), s->field);
929
930 char received[40];
931 ui_log_utc_now(received, sizeof(received));
932
933 int accepted = 0;
934 int duplicates = 0;
935 int dropped = 0;
936 bool file_error = false, storage_error = false;
937
938 cbm_ui_log_init();
939 cbm_mutex_lock(&g_log_mutex);
940 sqlite3 *db = activity_db_locked();
941 if (!db) {
942 cbm_mutex_unlock(&g_log_mutex);
943 cbm_free(CBM_MEM_CLASS_OTHER, s);
944 yyjson_doc_free(doc);

Callers 1

dispatch_requestFunction · 0.85

Calls 15

cbm_http_replyfFunction · 0.85
cbm_callocFunction · 0.85
cbm_ui_log_file_pathFunction · 0.85
cbm_freeFunction · 0.85
ui_log_copy_fieldFunction · 0.85
cbm_json_escapeFunction · 0.85
ui_log_utc_nowFunction · 0.85
cbm_ui_log_initFunction · 0.85
cbm_mutex_lockFunction · 0.85
activity_db_lockedFunction · 0.85
cbm_mutex_unlockFunction · 0.85
cbm_file_sizeFunction · 0.85

Tested by

no test coverage detected