GET /api/adr?project=X — get ADR content for a project */
| 1360 | |
| 1361 | /* GET /api/adr?project=X — get ADR content for a project */ |
| 1362 | static void handle_adr_get(cbm_http_conn_t *c, const cbm_http_req_t *req) { |
| 1363 | char name[256] = {0}; |
| 1364 | if (!cbm_http_query_param(req->query, "project", name, (int)sizeof(name)) || name[0] == '\0') { |
| 1365 | cbm_http_replyf(c, 400, g_cors_json, "{\"error\":\"missing project\"}"); |
| 1366 | return; |
| 1367 | } |
| 1368 | |
| 1369 | char db_path[1024]; |
| 1370 | db_path_for_project(name, db_path, sizeof(db_path)); |
| 1371 | |
| 1372 | cbm_store_t *store = cbm_store_open_path_query(db_path); |
| 1373 | if (!store) { |
| 1374 | cbm_http_replyf(c, 200, g_cors_json, "{\"has_adr\":false}"); |
| 1375 | return; |
| 1376 | } |
| 1377 | |
| 1378 | cbm_adr_t adr; |
| 1379 | memset(&adr, 0, sizeof(adr)); |
| 1380 | if (cbm_store_adr_get(store, name, &adr) == CBM_STORE_OK && adr.content) { |
| 1381 | /* Escape content for JSON — simple: replace quotes and newlines */ |
| 1382 | size_t clen = strlen(adr.content); |
| 1383 | size_t buf_size = clen * 2 + 256; |
| 1384 | char *buf = malloc(buf_size); |
| 1385 | if (buf) { |
| 1386 | int pos = snprintf(buf, buf_size, "{\"has_adr\":true,\"content\":\""); |
| 1387 | for (size_t i = 0; i < clen && (size_t)pos < buf_size - 10; i++) { |
| 1388 | char ch = adr.content[i]; |
| 1389 | if (ch == '"') { |
| 1390 | buf[pos++] = '\\'; |
| 1391 | buf[pos++] = '"'; |
| 1392 | } else if (ch == '\\') { |
| 1393 | buf[pos++] = '\\'; |
| 1394 | buf[pos++] = '\\'; |
| 1395 | } else if (ch == '\n') { |
| 1396 | buf[pos++] = '\\'; |
| 1397 | buf[pos++] = 'n'; |
| 1398 | } else if (ch == '\r') { /* skip */ |
| 1399 | } else if (ch == '\t') { |
| 1400 | buf[pos++] = '\\'; |
| 1401 | buf[pos++] = 't'; |
| 1402 | } else { |
| 1403 | buf[pos++] = ch; |
| 1404 | } |
| 1405 | } |
| 1406 | http_appendf(buf, buf_size, &pos, "\",\"updated_at\":\"%s\"}", |
| 1407 | adr.updated_at ? adr.updated_at : ""); |
| 1408 | cbm_http_replyf(c, 200, g_cors_json, "%s", buf); |
| 1409 | free(buf); |
| 1410 | } else { |
| 1411 | cbm_http_replyf(c, 500, g_cors, "oom"); |
| 1412 | } |
| 1413 | cbm_store_adr_free(&adr); |
| 1414 | } else { |
| 1415 | cbm_http_replyf(c, 200, g_cors_json, "{\"has_adr\":false}"); |
| 1416 | } |
| 1417 | cbm_store_close(store); |
| 1418 | } |
| 1419 |
no test coverage detected