Mercurial
comparison hg-web/main.c @ 193:9f4429c49733 hg-web
[HgWeb] Making progress....
| author | MrJuneJune <me@mrjunejune.com> |
|---|---|
| date | Sun, 25 Jan 2026 20:04:55 -0800 |
| parents | a2725419f988 |
| children |
comparison
equal
deleted
inserted
replaced
| 192:b818a4561a3c | 193:9f4429c49733 |
|---|---|
| 125 Dowa_HashMap_Push_Arena(resp, "body", temp1, arena); | 125 Dowa_HashMap_Push_Arena(resp, "body", temp1, arena); |
| 126 Dowa_HashMap_Push_Arena(resp, "content-length", temp2, arena); | 126 Dowa_HashMap_Push_Arena(resp, "content-length", temp2, arena); |
| 127 return resp; | 127 return resp; |
| 128 } | 128 } |
| 129 | 129 |
| 130 Seobeo_Request_Entry* ApiGetGraph(Seobeo_Request_Entry *req, Dowa_Arena *arena) | |
| 131 { | |
| 132 Seobeo_Request_Entry *resp = NULL; | |
| 133 | |
| 134 void *path_kv = Dowa_HashMap_Get_Ptr(req, "QueryString"); | |
| 135 const char *rel_path = path_kv ? ((Seobeo_Request_Entry*)path_kv)->value : ""; | |
| 136 Seobeo_Log(SEOBEO_INFO, "ApiGetGraph: rel_path='%s'\n", rel_path); | |
| 137 void *graph_id_kv = Dowa_HashMap_Get_Ptr(req, ":graph_id"); | |
| 138 char *graph_id = ((Seobeo_Request_Entry*)graph_id_kv)->value; | |
| 139 Seobeo_Log(SEOBEO_INFO, "ApiGetGraph: graph_id='%s'\n", graph_id); | |
| 140 char *decoded_path = Dowa_Arena_Allocate(arena, strlen(rel_path) + 1); | |
| 141 Seobeo_Url_Decode(decoded_path, rel_path); | |
| 142 char *safe_path = sanitize_path(decoded_path, arena); | |
| 143 | |
| 144 Seobeo_Log(SEOBEO_INFO, "ApiGetGraph: safe_path='%s'\n", safe_path); | |
| 145 | |
| 146 if (strlen(safe_path) == 0) | |
| 147 { | |
| 148 Dowa_HashMap_Push_Arena(resp, "status", "400", arena); | |
| 149 Dowa_HashMap_Push_Arena(resp, "content-type", "text/plain", arena); | |
| 150 Dowa_HashMap_Push_Arena(resp, "body", "File path required", arena); | |
| 151 return resp; | |
| 152 } | |
| 153 | |
| 154 char hg_path[MAX_PATH]; | |
| 155 // void *graph_id_kv = Dowa_HashMap_Get_Ptr(req, ":graph_id"); | |
| 156 // char *graph_id = ((Seobeo_Request_Entry*)graph_id_kv)->value; | |
| 157 snprintf(hg_path, sizeof(hg_path), "/graph/%s?%s", graph_id, safe_path); | |
| 158 Seobeo_Client_Response *hg_response = hg_proxy_request("GET", hg_path, NULL, NULL); | |
| 159 | |
| 160 Seobeo_Log(SEOBEO_DEBUG, "ApiGetGraph: status=%i body_len=%zu\n", hg_response->status_code, hg_response->body_length); | |
| 161 | |
| 162 char status[4]; | |
| 163 snprintf(status, 4, "%i", hg_response->status_code); | |
| 164 | |
| 165 if (!hg_response->body) | |
| 166 { | |
| 167 Dowa_HashMap_Push_Arena(resp, "status", "502", arena); | |
| 168 Dowa_HashMap_Push_Arena(resp, "content-type", "text/plain", arena); | |
| 169 Dowa_HashMap_Push_Arena(resp, "body", "Failed to connect to hg serve", arena); | |
| 170 return resp; | |
| 171 } | |
| 172 | |
| 173 if (hg_response->status_code != 200) | |
| 174 { | |
| 175 Seobeo_Log(SEOBEO_DEBUG, "ApiGetGraph: error hg_response: %s\n", hg_response->body); | |
| 176 Dowa_HashMap_Push_Arena(resp, "status", status, arena); | |
| 177 Dowa_HashMap_Push_Arena(resp, "content-type", "text/plain", arena); | |
| 178 Dowa_HashMap_Push_Arena(resp, "body", hg_response->body, arena); | |
| 179 return resp; | |
| 180 } | |
| 181 | |
| 182 | |
| 183 char *temp1 = Dowa_Arena_Copy(arena, hg_response->body, hg_response->body_length); | |
| 184 char *temp2 = Dowa_Arena_Allocate(arena, 256); | |
| 185 snprintf(temp2, 256, "%zu", hg_response->body_length); | |
| 186 | |
| 187 Dowa_HashMap_Push_Arena(resp, "status", "200", arena); | |
| 188 Dowa_HashMap_Push_Arena(resp, "content-type", "text/plain", arena); | |
| 189 Dowa_HashMap_Push_Arena(resp, "body", temp1, arena); | |
| 190 Dowa_HashMap_Push_Arena(resp, "content-length", temp2, arena); | |
| 191 | |
| 192 return resp; | |
| 193 } | |
| 194 | |
| 130 Seobeo_Request_Entry* ApiGetFile(Seobeo_Request_Entry *req, Dowa_Arena *arena) | 195 Seobeo_Request_Entry* ApiGetFile(Seobeo_Request_Entry *req, Dowa_Arena *arena) |
| 131 { | 196 { |
| 132 Seobeo_Request_Entry *resp = NULL; | 197 Seobeo_Request_Entry *resp = NULL; |
| 133 | 198 |
| 134 void *path_kv = Dowa_HashMap_Get_Ptr(req, "query_path"); | 199 void *path_kv = Dowa_HashMap_Get_Ptr(req, "query_path"); |
| 343 Dowa_HashMap_Push_Arena(resp, "content-length", temp2, arena); | 408 Dowa_HashMap_Push_Arena(resp, "content-length", temp2, arena); |
| 344 | 409 |
| 345 return resp; | 410 return resp; |
| 346 } | 411 } |
| 347 | 412 |
| 413 Seobeo_Request_Entry* GetReactHome(Seobeo_Request_Entry *req, Dowa_Arena *arena) | |
| 414 { | |
| 415 size_t file_size = 0; | |
| 416 char *html = Seobeo_Web_LoadFile("/index.html", &file_size); | |
| 417 | |
| 418 printf("%s", html); | |
| 419 Seobeo_Request_Entry *resp = NULL; | |
| 420 Dowa_HashMap_Push_Arena(resp, "status", "200", arena); | |
| 421 Dowa_HashMap_Push_Arena(resp, "content-type", "text/html", arena); | |
| 422 Dowa_HashMap_Push_Arena(resp, "body", html, arena); | |
| 423 return resp; | |
| 424 } | |
| 425 | |
| 348 int main(void) { | 426 int main(void) { |
| 349 Seobeo_Router_Init(); | 427 Seobeo_Router_Init(); |
| 350 | 428 |
| 429 | |
| 430 Seobeo_Router_Register("GET", "/", GetReactHome); | |
| 431 Seobeo_Router_Register("GET", "/directories", GetReactHome); | |
| 432 Seobeo_Router_Register("GET", "/graph", GetReactHome); | |
| 433 | |
| 351 Seobeo_Router_Register("GET", "/api/repo/list", ApiListDirectory); | 434 Seobeo_Router_Register("GET", "/api/repo/list", ApiListDirectory); |
| 352 Seobeo_Router_Register("GET", "/api/repo/file", ApiGetFile); | 435 Seobeo_Router_Register("GET", "/api/repo/file", ApiGetFile); |
| 436 Seobeo_Router_Register("GET", "/api/graph/:graph_id", ApiGetGraph); | |
| 353 Seobeo_Router_Register("GET", "/api/repo/readme", ApiGetReadme); | 437 Seobeo_Router_Register("GET", "/api/repo/readme", ApiGetReadme); |
| 354 | 438 |
| 355 // Use streaming handler for hg wire protocol... | 439 // Use streaming handler for hg wire protocol... |
| 356 Seobeo_Router_Register_Stream("GET", "/repo", StreamHgWireProtocol); | 440 Seobeo_Router_Register_Stream("GET", "/repo", StreamHgWireProtocol); |
| 357 Seobeo_Router_Register_Stream("POST", "/repo", StreamHgWireProtocol); | 441 Seobeo_Router_Register_Stream("POST", "/repo", StreamHgWireProtocol); |