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

Function handle_browse

src/ui/http_server.c:1268–1357  ·  view source on GitHub ↗

GET /api/browse?path=/some/dir — list subdirectories for file picker */

Source from the content-addressed store, hash-verified

1266
1267/* GET /api/browse?path=/some/dir — list subdirectories for file picker */
1268static void handle_browse(cbm_http_conn_t *c, const cbm_http_req_t *req) {
1269 char path[1024] = {0};
1270 const char *home = cbm_get_home_dir();
1271 if (!cbm_http_query_param(req->query, "path", path, (int)sizeof(path)) || path[0] == '\0') {
1272 /* Default to home directory */
1273 if (home)
1274 snprintf(path, sizeof(path), "%s", home);
1275 else
1276 snprintf(path, sizeof(path), "/");
1277 }
1278
1279 /* The browser UI may send Windows backslash separators (e.g.
1280 * "D:\projects\demo"). Normalize to forward slashes before the cbm_is_dir
1281 * gate, exactly as the MCP repo_path handler and cbm_project_name_from_path
1282 * already do — otherwise a real D:/ directory is rejected (#548). */
1283 cbm_normalize_path_sep(path);
1284
1285 if (!cbm_is_dir(path)) {
1286 cbm_http_replyf(c, 400, g_cors_json, "{\"error\":\"not a directory\"}");
1287 return;
1288 }
1289
1290 cbm_dir_t *dir = cbm_opendir(path);
1291 if (!dir) {
1292 cbm_http_replyf(c, 403, g_cors_json, "{\"error\":\"cannot open directory\"}");
1293 return;
1294 }
1295
1296 /* Build JSON response */
1297 char buf[32768];
1298 int pos = 0;
1299 http_appendf(buf, sizeof(buf), &pos, "{\"path\":\"%s\",\"dirs\":[", path);
1300
1301 cbm_dirent_t *ent;
1302 int count = 0;
1303 while ((ent = cbm_readdir(dir)) != NULL) {
1304 /* Skip hidden dirs and . / .. */
1305 if (ent->name[0] == '.')
1306 continue;
1307
1308 /* Check if it's actually a directory */
1309 char full[2048];
1310 snprintf(full, sizeof(full), "%s/%s", path, ent->name);
1311 if (!cbm_is_dir(full))
1312 continue;
1313
1314 if (count > 0)
1315 buf[pos++] = ',';
1316 /* Escape directory name to prevent XSS (e.g., names with quotes/angle brackets) */
1317 {
1318 char esc[512];
1319 cbm_json_escape(esc, (int)sizeof(esc), ent->name);
1320 http_appendf(buf, sizeof(buf), &pos, "\"%s\"", esc);
1321 }
1322 if (pos >= (int)sizeof(buf)) {
1323 pos = (int)sizeof(buf) - 1;
1324 }
1325 count++;

Callers 1

dispatch_requestFunction · 0.85

Calls 11

cbm_get_home_dirFunction · 0.85
cbm_http_query_paramFunction · 0.85
cbm_normalize_path_sepFunction · 0.85
cbm_is_dirFunction · 0.85
cbm_http_replyfFunction · 0.85
cbm_opendirFunction · 0.85
http_appendfFunction · 0.85
cbm_readdirFunction · 0.85
cbm_json_escapeFunction · 0.85
cbm_closedirFunction · 0.85
append_roots_jsonFunction · 0.85

Tested by

no test coverage detected