Skip to content

Commit ece68d8

Browse files
committed
feat(ui): add native read endpoints backing the graph UI
Completes fu/ui-native-api. The previous commit (a7aeca0) switched the graph UI to native /api calls, but a staging bug in the verification script left out the server half. On that commit the UI requested routes that did not exist. src/ui/http_server.c adds three exact GET routes: - /api/projects - /api/schema - /api/snippet They dispatch through cbm_operation_execute, the operation entry point the CLI uses, with format=json forced server-side. - The existing Host/origin checks run before dispatch. - Query parsing is bounded: - parameter names come from a per-operation allowlist - every copy is length-checked - duplicate keys are rejected - numeric and boolean values are validated - strings are JSON-escaped - Schema text errors are returned as JSON error objects. - The routes add no heap allocation sites, wildcard CORS or system() calls. tests/test_httpd.c adds a live-socket integration test against the real HTTP server, backed by on-disk project databases and real source files. It covers: - project and schema paging - snippet continuation and encoded names - malformed, duplicate and overflowing parameters - missing-project JSON errors - foreign-origin rejection - unavailable POST routes Validated: -Werror build and test-runner, scripts/lint.sh --ci, and scripts/test.sh --suites "httpd ui": 84 passed, 1 Windows-only skip.
1 parent a7aeca0 commit ece68d8

2 files changed

Lines changed: 267 additions & 2 deletions

File tree

‎src/ui/http_server.c‎

Lines changed: 114 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
#include <ctype.h>
4545
#include <errno.h>
4646
#include <math.h>
47+
#include <limits.h>
4748
#include <stdatomic.h>
4849
#include <stdarg.h>
4950
#include <stdio.h>
@@ -73,7 +74,7 @@
7374
#define CBM_VERSION "dev"
7475
#endif
7576

76-
/* Max JSON-RPC request body size (1 MB) — transport enforces the same cap. */
77+
/* Max request body size (1 MB) — transport enforces the same cap. */
7778
#define MAX_BODY_SIZE CBM_HTTP_MAX_BODY
7879

7980
/* ── CORS: only allow localhost origins (blocks remote website attacks) ────── */
@@ -1867,6 +1868,105 @@ static bool request_passes_http_security(cbm_http_server_t *srv, cbm_http_conn_t
18671868
return true;
18681869
}
18691870

1871+
/* Translate the native read API's bounded query into operation arguments.
1872+
* Only these three read operations are reachable; format and project details
1873+
* are server-owned so the response is always machine-readable. */
1874+
static void handle_read_operation(cbm_http_conn_t *c, const cbm_http_req_t *req,
1875+
cbm_operation_id_t operation) {
1876+
/* The transport caps the entire query at 2 KiB. Sixfold JSON escaping
1877+
* plus keys fits here without adding heap allocations. */
1878+
char args[16384] = "{\"format\":\"json\",\"detail\":\"stats\"";
1879+
size_t used = strlen(args);
1880+
const char *query = req->query;
1881+
while (*query) {
1882+
const char *end = strchr(query, '&');
1883+
if (!end)
1884+
end = query + strlen(query);
1885+
const char *eq = memchr(query, '=', (size_t)(end - query));
1886+
char key[32], value[2048], escaped[12288];
1887+
size_t key_len = eq ? (size_t)(eq - query) : 0;
1888+
if (!key_len || key_len >= sizeof(key))
1889+
goto bad_query;
1890+
memcpy(key, query, key_len);
1891+
key[key_len] = '\0';
1892+
char pair[2048];
1893+
size_t pair_len = (size_t)(end - query);
1894+
if (pair_len >= sizeof(pair))
1895+
goto bad_query;
1896+
memcpy(pair, query, pair_len);
1897+
pair[pair_len] = '\0';
1898+
if (!cbm_http_query_param(pair, key, value, (int)sizeof(value)) || !value[0])
1899+
goto bad_query;
1900+
1901+
bool string_arg = strcmp(key, "project") == 0 && operation != CBM_OPERATION_PROJECTS;
1902+
bool number_arg = operation != CBM_OPERATION_SNIPPET &&
1903+
(strcmp(key, "limit") == 0 || strcmp(key, "offset") == 0);
1904+
bool bool_arg = false;
1905+
if (operation == CBM_OPERATION_SNIPPET) {
1906+
string_arg =
1907+
string_arg || strcmp(key, "qualified_name") == 0 || strcmp(key, "source_mode") == 0;
1908+
number_arg = strcmp(key, "start_line") == 0 || strcmp(key, "max_lines") == 0 ||
1909+
strcmp(key, "member_limit") == 0 || strcmp(key, "member_offset") == 0 ||
1910+
strcmp(key, "max_output_tokens") == 0;
1911+
bool_arg = strcmp(key, "include_neighbors") == 0;
1912+
}
1913+
if (!string_arg && !number_arg && !bool_arg)
1914+
goto bad_query;
1915+
/* Reject duplicate keys rather than depending on JSON lookup order. */
1916+
char marker[40];
1917+
snprintf(marker, sizeof(marker), ",\"%s\":", key);
1918+
if (strstr(args, marker))
1919+
goto bad_query;
1920+
int n;
1921+
if (string_arg) {
1922+
cbm_json_escape(escaped, (int)sizeof(escaped), value);
1923+
n = snprintf(args + used, sizeof(args) - used, ",\"%s\":\"%s\"", key, escaped);
1924+
} else {
1925+
if (number_arg) {
1926+
for (const char *p = value; *p; p++) {
1927+
if (*p < '0' || *p > '9')
1928+
goto bad_query;
1929+
}
1930+
errno = 0;
1931+
unsigned long parsed = strtoul(value, NULL, 10);
1932+
if (errno || parsed > INT_MAX)
1933+
goto bad_query;
1934+
n = snprintf(args + used, sizeof(args) - used, ",\"%s\":%lu", key, parsed);
1935+
} else {
1936+
if (strcmp(value, "true") != 0 && strcmp(value, "false") != 0)
1937+
goto bad_query;
1938+
n = snprintf(args + used, sizeof(args) - used, ",\"%s\":%s", key, value);
1939+
}
1940+
}
1941+
if (n < 0 || (size_t)n >= sizeof(args) - used - 2)
1942+
goto bad_query;
1943+
used += (size_t)n;
1944+
query = *end ? end + 1 : end;
1945+
}
1946+
if (operation != CBM_OPERATION_PROJECTS && !strstr(args, ",\"project\":"))
1947+
goto bad_query;
1948+
if (operation == CBM_OPERATION_SNIPPET && !strstr(args, ",\"qualified_name\":"))
1949+
goto bad_query;
1950+
args[used++] = '}';
1951+
args[used] = '\0';
1952+
cbm_operation_result_t result = cbm_operation_execute(NULL, operation, args);
1953+
/* Schema reports failures as plain text; the other read operations already
1954+
* return JSON error objects. Keep the native API's error contract JSON. */
1955+
if (result.is_error && result.payload && operation == CBM_OPERATION_SCHEMA) {
1956+
char escaped[4096];
1957+
cbm_json_escape(escaped, (int)sizeof(escaped), result.payload);
1958+
cbm_http_replyf(c, 400, g_cors_json, "{\"error\":\"%s\"}", escaped);
1959+
} else {
1960+
cbm_http_replyf(c, result.payload ? (result.is_error ? 400 : 200) : 500, g_cors_json, "%s",
1961+
result.payload ? result.payload : "{\"error\":\"operation failed\"}");
1962+
}
1963+
cbm_operation_result_dispose(&result);
1964+
return;
1965+
1966+
bad_query:
1967+
cbm_http_replyf(c, 400, g_cors_json, "%s", "{\"error\":\"invalid query parameters\"}");
1968+
}
1969+
18701970
static void dispatch_request(cbm_http_server_t *srv, cbm_http_conn_t *c,
18711971
const cbm_http_req_t *req) {
18721972
if (!request_passes_http_security(srv, c, req))
@@ -1890,6 +1990,19 @@ static void dispatch_request(cbm_http_server_t *srv, cbm_http_conn_t *c,
18901990
return;
18911991
}
18921992

1993+
if (is_get && strcmp(req->path, "/api/projects") == 0) {
1994+
handle_read_operation(c, req, CBM_OPERATION_PROJECTS);
1995+
return;
1996+
}
1997+
if (is_get && strcmp(req->path, "/api/schema") == 0) {
1998+
handle_read_operation(c, req, CBM_OPERATION_SCHEMA);
1999+
return;
2000+
}
2001+
if (is_get && strcmp(req->path, "/api/snippet") == 0) {
2002+
handle_read_operation(c, req, CBM_OPERATION_SNIPPET);
2003+
return;
2004+
}
2005+
18932006
/* GET /api/layout → 3D graph layout */
18942007
if (is_get && cbm_http_path_match(req->path, "/api/layout*")) {
18952008
handle_layout(c, req);

‎tests/test_httpd.c‎

Lines changed: 153 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* query-param decoding, route pattern matching.
99
* 2. Live-socket integration tests against the full UI server
1010
* (http_server.c) on an ephemeral port: routing, CORS policy,
11-
* RPC dispatch, transport limits, receive deadline, clean shutdown.
11+
* native read APIs, transport limits, receive deadline, clean shutdown.
1212
*/
1313
#include "../src/foundation/compat.h"
1414
#if defined(__APPLE__)
@@ -28,6 +28,7 @@
2828
#include "ui/http_server.h"
2929
#include <store/store.h>
3030
#include <watcher/watcher.h>
31+
#include <yyjson/yyjson.h>
3132

3233
#include <stdio.h>
3334
#include <stdatomic.h>
@@ -862,6 +863,156 @@ static bool ui_adr_equals(const ui_delete_fixture_t *fx, const char *project,
862863
return equal;
863864
}
864865

866+
/* Real project stores, source files, and HTTP sockets: native routes must
867+
* preserve the operation payloads and their continuation metadata. */
868+
TEST(ui_server_native_read_endpoints) {
869+
ui_delete_fixture_t fx;
870+
ASSERT_EQ(ui_delete_fixture_init(&fx), 0);
871+
char path[1024];
872+
snprintf(path, sizeof(path), "%s/code.c", fx.root_dir);
873+
ASSERT_EQ(th_write_file(path, "first\nsecond\nthird\nfourth\n"), 0);
874+
for (int i = 0; i < 2; i++) {
875+
const char *project = i == 0 ? "alpha" : "beta";
876+
ui_delete_db_path(&fx, project, path, sizeof(path));
877+
cbm_store_t *store = cbm_store_open_path(path);
878+
ASSERT_NOT_NULL(store);
879+
ASSERT_EQ(cbm_store_upsert_project(store, project, fx.root_dir), CBM_STORE_OK);
880+
cbm_node_t node = {.project = project,
881+
.label = "Function",
882+
.name = "demo",
883+
.qualified_name = "app::demo & more",
884+
.file_path = "code.c",
885+
.start_line = 1,
886+
.end_line = 4};
887+
int64_t function = cbm_store_upsert_node(store, &node);
888+
ASSERT_GT(function, 0);
889+
node.label = "Class";
890+
node.name = "Container";
891+
node.qualified_name = "app::Container";
892+
int64_t container = cbm_store_upsert_node(store, &node);
893+
ASSERT_GT(container, 0);
894+
cbm_edge_t edge = {
895+
.project = project, .source_id = function, .target_id = container, .type = "CALLS"};
896+
ASSERT_GT(cbm_store_insert_edge(store, &edge), 0);
897+
cbm_store_close(store);
898+
}
899+
th_server_t ts;
900+
ASSERT_EQ(th_server_start(&ts), 0);
901+
int port = cbm_http_server_port(ts.srv);
902+
bool projects_ok = true, schema_ok = true, snippet_ok = true, errors_ok = true;
903+
for (int kind = 0; kind < 3; kind++) {
904+
int offset = 0;
905+
int pages = kind == 1 ? 3 : 2;
906+
for (int page = 0; page < pages; page++) {
907+
char request[512], response[16384], pool[32768];
908+
if (kind == 0) {
909+
snprintf(request, sizeof(request),
910+
"GET /api/projects?limit=1&offset=%d HTTP/1.1\r\n\r\n", offset);
911+
} else if (kind == 1) {
912+
snprintf(request, sizeof(request),
913+
"GET /api/schema?project=alpha&limit=1&offset=%d HTTP/1.1\r\n\r\n",
914+
offset);
915+
} else {
916+
snprintf(
917+
request, sizeof(request),
918+
"GET /api/snippet?project=alpha&qualified_name=app%%3A%%3Ademo%%20%%26%%20more"
919+
"&source_mode=full&max_lines=2&start_line=%d HTTP/1.1\r\n\r\n",
920+
offset + 1);
921+
}
922+
int n = th_http(port, request, response, sizeof(response));
923+
char *body = n > 0 ? strstr(response, "\r\n\r\n") : NULL;
924+
body = body ? body + 4 : NULL;
925+
yyjson_alc alc;
926+
yyjson_alc_pool_init(&alc, pool, sizeof(pool));
927+
yyjson_doc *doc = body ? yyjson_read_opts(body, strlen(body), 0, &alc, NULL) : NULL;
928+
yyjson_val *root = doc ? yyjson_doc_get_root(doc) : NULL;
929+
bool ok = n > 0 && th_status(response) == 200 && yyjson_is_obj(root) &&
930+
strstr(response, "Content-Type: application/json") != NULL;
931+
bool more = page + 1 < pages;
932+
if (kind == 0) {
933+
yyjson_val *rows = yyjson_obj_get(root, "projects");
934+
const char *name = yyjson_get_str(yyjson_obj_get(yyjson_arr_get(rows, 0), "name"));
935+
ok = ok && yyjson_get_int(yyjson_obj_get(root, "total")) == 2 &&
936+
yyjson_arr_size(rows) == 1 && name &&
937+
strcmp(name, page == 0 ? "alpha" : "beta") == 0;
938+
} else if (kind == 1) {
939+
yyjson_val *rows = yyjson_obj_get(root, page < 2 ? "node_labels" : "edge_types");
940+
const char *name = yyjson_get_str(
941+
yyjson_obj_get(yyjson_arr_get(rows, 0), page < 2 ? "label" : "type"));
942+
const char *expected = page == 0 ? "Class" : page == 1 ? "Function" : "CALLS";
943+
ok = ok && name && strcmp(name, expected) == 0 &&
944+
yyjson_get_int(yyjson_obj_get(root, "total")) == 3 &&
945+
yyjson_get_int(yyjson_obj_get(root, "returned")) == 1 &&
946+
yyjson_arr_size(yyjson_obj_get(root, "node_labels")) +
947+
yyjson_arr_size(yyjson_obj_get(root, "edge_types")) ==
948+
1;
949+
} else {
950+
const char *source = yyjson_get_str(yyjson_obj_get(root, "source"));
951+
ok = ok && source &&
952+
strcmp(source, page == 0 ? "first\nsecond\n" : "third\nfourth\n") == 0 &&
953+
yyjson_get_int(yyjson_obj_get(root, "start_line")) == offset + 1;
954+
}
955+
const char *more_key = kind == 2 ? "source_truncated" : "has_more";
956+
const char *next_key = kind == 2 ? "next_start_line" : "next_offset";
957+
ok = ok && yyjson_get_bool(yyjson_obj_get(root, more_key)) == more;
958+
if (more) {
959+
int next = (int)yyjson_get_int(yyjson_obj_get(root, next_key));
960+
ok = ok && next == (kind == 2 ? 3 : offset + 1);
961+
offset = kind == 2 ? next - 1 : next;
962+
}
963+
if (kind == 0)
964+
projects_ok = projects_ok && ok;
965+
else if (kind == 1)
966+
schema_ok = schema_ok && ok;
967+
else
968+
snippet_ok = snippet_ok && ok;
969+
if (doc)
970+
yyjson_doc_free(doc);
971+
}
972+
}
973+
static const char *const invalid[] = {
974+
"/api/projects?limit=no",
975+
"/api/projects?offset=-1",
976+
"/api/projects?limit=2147483648",
977+
"/api/projects?limit=1&limit=2",
978+
"/api/schema",
979+
"/api/schema?project=missing",
980+
"/api/schema?project=alpha&offset=%XX",
981+
"/api/snippet?project=alpha",
982+
"/api/snippet?project=alpha&qualified_name=missing",
983+
"/api/snippet?project=alpha&qualified_name=demo&include_neighbors=yes",
984+
};
985+
for (size_t i = 0; i < sizeof(invalid) / sizeof(invalid[0]); i++) {
986+
char request[512], response[4096];
987+
snprintf(request, sizeof(request), "GET %s HTTP/1.1\r\n\r\n", invalid[i]);
988+
int n = th_http(port, request, response, sizeof(response));
989+
errors_ok = errors_ok && n > 0 && th_status(response) == 400 &&
990+
strstr(response, "\"error\"") != NULL;
991+
}
992+
static const char *const routes[] = {"projects", "schema", "snippet"};
993+
for (size_t i = 0; i < sizeof(routes) / sizeof(routes[0]); i++) {
994+
char request[512], response[4096];
995+
snprintf(request, sizeof(request),
996+
"GET /api/%s HTTP/1.1\r\nOrigin: https://foreign.invalid\r\n\r\n", routes[i]);
997+
int n = th_http(port, request, response, sizeof(response));
998+
errors_ok = errors_ok && n > 0 && th_status(response) == 403 &&
999+
strstr(response, "Access-Control-Allow-Origin: *") == NULL;
1000+
snprintf(request, sizeof(request),
1001+
"POST /api/%s HTTP/1.1\r\nContent-Type: application/json\r\n"
1002+
"Content-Length: 2\r\n\r\n{}",
1003+
routes[i]);
1004+
n = th_http(port, request, response, sizeof(response));
1005+
errors_ok = errors_ok && n > 0 && th_status(response) == 404;
1006+
}
1007+
th_server_stop(&ts);
1008+
ui_delete_fixture_cleanup(&fx);
1009+
ASSERT_TRUE(projects_ok);
1010+
ASSERT_TRUE(schema_ok);
1011+
ASSERT_TRUE(snippet_ok);
1012+
ASSERT_TRUE(errors_ok);
1013+
PASS();
1014+
}
1015+
8651016
TEST(ui_server_readiness_proof_is_exact_and_generation_bound) {
8661017
static const char challenge[] =
8671018
"202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f";
@@ -2378,6 +2529,7 @@ SUITE(httpd) {
23782529
/* Full UI server */
23792530
RUN_TEST(ui_server_readiness_proof_is_exact_and_generation_bound);
23802531
RUN_TEST(ui_server_rejects_non_loopback_host);
2532+
RUN_TEST(ui_server_native_read_endpoints);
23812533
RUN_TEST(ui_server_unknown_path_404);
23822534
RUN_TEST(ui_server_process_kill_route_is_unavailable);
23832535
RUN_TEST(ui_server_routes_indexing_through_joinable_daemon_executor);

0 commit comments

Comments
 (0)