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

Function handle_adr_get

src/ui/http_server.c:761–817  ·  view source on GitHub ↗

GET /api/adr?project=X — get ADR content for a project */

Source from the content-addressed store, hash-verified

759
760/* GET /api/adr?project=X — get ADR content for a project */
761static void handle_adr_get(cbm_http_conn_t *c, const cbm_http_req_t *req) {
762 char name[256] = {0};
763 if (!cbm_http_query_param(req->query, "project", name, (int)sizeof(name)) || name[0] == '\0') {
764 cbm_http_replyf(c, 400, g_cors_json, "{\"error\":\"missing project\"}");
765 return;
766 }
767
768 char db_path[1024];
769 db_path_for_project(name, db_path, sizeof(db_path));
770
771 cbm_store_t *store = cbm_store_open_path_query(db_path);
772 if (!store) {
773 cbm_http_replyf(c, 200, g_cors_json, "{\"has_adr\":false}");
774 return;
775 }
776
777 cbm_adr_t adr;
778 memset(&adr, 0, sizeof(adr));
779 if (cbm_store_adr_get(store, name, &adr) == CBM_STORE_OK && adr.content) {
780 /* Escape content for JSON — simple: replace quotes and newlines */
781 size_t clen = strlen(adr.content);
782 size_t buf_size = clen * 2 + 256;
783 char *buf = malloc(buf_size);
784 if (buf) {
785 int pos = snprintf(buf, buf_size, "{\"has_adr\":true,\"content\":\"");
786 for (size_t i = 0; i < clen && (size_t)pos < buf_size - 10; i++) {
787 char ch = adr.content[i];
788 if (ch == '"') {
789 buf[pos++] = '\\';
790 buf[pos++] = '"';
791 } else if (ch == '\\') {
792 buf[pos++] = '\\';
793 buf[pos++] = '\\';
794 } else if (ch == '\n') {
795 buf[pos++] = '\\';
796 buf[pos++] = 'n';
797 } else if (ch == '\r') { /* skip */
798 } else if (ch == '\t') {
799 buf[pos++] = '\\';
800 buf[pos++] = 't';
801 } else {
802 buf[pos++] = ch;
803 }
804 }
805 http_appendf(buf, buf_size, &pos, "\",\"updated_at\":\"%s\"}",
806 adr.updated_at ? adr.updated_at : "");
807 cbm_http_replyf(c, 200, g_cors_json, "%s", buf);
808 free(buf);
809 } else {
810 cbm_http_replyf(c, 500, g_cors, "oom");
811 }
812 cbm_store_adr_free(&adr);
813 } else {
814 cbm_http_replyf(c, 200, g_cors_json, "{\"has_adr\":false}");
815 }
816 cbm_store_close(store);
817}
818

Callers 1

dispatch_requestFunction · 0.85

Calls 8

cbm_http_query_paramFunction · 0.85
cbm_http_replyfFunction · 0.85
db_path_for_projectFunction · 0.85
cbm_store_adr_getFunction · 0.85
http_appendfFunction · 0.85
cbm_store_adr_freeFunction · 0.85
cbm_store_closeFunction · 0.85

Tested by

no test coverage detected