comparison hg-web/main.c @ 108:f07abbcd2ec5

[HgWeb] Will probably hold off on using it since it is not urgent.
author June Park <parkjune1995@gmail.com>
date Sat, 03 Jan 2026 17:29:12 -0800
parents 1423044e3d58
children 1c446ab6f945
comparison
equal deleted inserted replaced
107:1423044e3d58 108:f07abbcd2ec5
267 267
268 Seobeo_Request_Entry* ApiGetReadme(Seobeo_Request_Entry *req, Dowa_Arena *arena) { 268 Seobeo_Request_Entry* ApiGetReadme(Seobeo_Request_Entry *req, Dowa_Arena *arena) {
269 return ApiGetFile(req, arena); 269 return ApiGetFile(req, arena);
270 } 270 }
271 271
272 Seobeo_Request_Entry* ApiHgWireProtocol(Seobeo_Request_Entry *req, Dowa_Arena *arena)
273 {
274 Seobeo_Request_Entry *resp = NULL;
275
276 void *cmd_kv = Dowa_HashMap_Get_Ptr(req, "query_cmd");
277 const char *cmd = cmd_kv ? ((Seobeo_Request_Entry*)cmd_kv)->value : "";
278 if (strlen(cmd) == 0)
279 {
280 Dowa_HashMap_Push_Arena(resp, "status", "404", arena);
281 return resp;
282 }
283 Seobeo_Log(SEOBEO_DEBUG, "cmd: %s\n", cmd);
284
285 char command[MAX_PATH];
286 snprintf(command, sizeof(command), "hg -R %s serve --stdio 2>&1", REPO_ROOT);
287
288 FILE *hg_pipe = popen(command, "r+");
289 if (!hg_pipe) {
290 Seobeo_Log(SEOBEO_DEBUG, "Failed to open pipe\n");
291 return resp;
292 }
293
294 // 2. Write the command
295 fprintf(hg_pipe, "capabilities\n");
296 fflush(hg_pipe);
297
298 // 3. Read the response
299 int buffer_size = 1024 * 1024 * 5;
300 char *output = Dowa_Arena_Allocate(arena, buffer_size);
301 if (fgets(output, buffer_size, hg_pipe) != NULL) {
302 Seobeo_Log(SEOBEO_DEBUG, "SUCCESS! Received: %s\n", output);
303 } else {
304 Seobeo_Log(SEOBEO_DEBUG, "FAILURE: No output received from hg.\n");
305 }
306
307 // 4. Close and check exit code
308 int status = pclose(hg_pipe);
309 Seobeo_Log(SEOBEO_DEBUG, "Process exited with status: %d\n", status);
310
311 Seobeo_Log(SEOBEO_DEBUG, "body: %s\n", output);
312
313 Dowa_HashMap_Push_Arena(resp, "status", "200", arena);
314 Dowa_HashMap_Push_Arena(resp, "content-type", "application/mercurial-0.2", arena);
315 Dowa_HashMap_Push_Arena(resp, "body", output, arena);
316
317 return resp;
318 }
319
272 int main(void) { 320 int main(void) {
273 Seobeo_Router_Init(); 321 Seobeo_Router_Init();
274 322
275 Seobeo_Router_Register("GET", "/api/repo/list", ApiListDirectory); 323 Seobeo_Router_Register("GET", "/api/repo/list", ApiListDirectory);
276 Seobeo_Router_Register("GET", "/api/repo/file", ApiGetFile); 324 Seobeo_Router_Register("GET", "/api/repo/file", ApiGetFile);
277 Seobeo_Router_Register("GET", "/api/repo/readme", ApiGetReadme); 325 Seobeo_Router_Register("GET", "/api/repo/readme", ApiGetReadme);
278 326
327 Seobeo_Router_Register("GET", "/repo", ApiHgWireProtocol);
328 Seobeo_Router_Register("POST", "/repo", ApiHgWireProtocol);
329
279 printf("Starting on Port 6970...\n"); 330 printf("Starting on Port 6970...\n");
280 printf("Repository: %s\n", REPO_ROOT); 331 printf("Repository: %s\n", REPO_ROOT);
281 332
282 int result = Seobeo_Web_Server_Start("hg-web/src", "6970", SEOBEO_MODE_EDGE, 4); 333 int result = Seobeo_Web_Server_Start("hg-web/src", "6970", SEOBEO_MODE_EDGE, 4);
283 334