Mercurial
changeset 79:5710108c949e
[Seobeo] Added Redirect logic.
| author | June Park <parkjune1995@gmail.com> |
|---|---|
| date | Thu, 01 Jan 2026 05:57:03 -0800 |
| parents | e7bf9e002850 |
| children | d55157451947 |
| files | mrjunejune/main.c mrjunejune/pages/resume/index.html mrjunejune/pages/tools/markdown_to_html/index.html seobeo/s_router.c seobeo/s_web.c seobeo/seobeo.h |
| diffstat | 6 files changed, 42 insertions(+), 14 deletions(-) [+] |
line wrap: on
line diff
--- a/mrjunejune/main.c Wed Dec 31 15:07:43 2025 -0800 +++ b/mrjunejune/main.c Thu Jan 01 05:57:03 2026 -0800 @@ -104,20 +104,25 @@ return resp; } +CREATE_REDIRECT_HANDLER(HomePage, "http://localhost:6969/") +CREATE_REDIRECT_HANDLER(Resume, "http://localhost:6969/resume") +CREATE_REDIRECT_HANDLER(Tools, "http://localhost:6969/tools") +CREATE_REDIRECT_HANDLER(MarkDownToHtml, "http://localhost:6969/tools/markdown_to_html") + int main(void) { Seobeo_Router_Init(); Seobeo_Router_Register("GET", "/", GetHomePage); - Seobeo_Router_Register("GET", "/index.html", GetHomePage); + Seobeo_Router_Register("GET", "/index.html", GetRedirectHomePage); Seobeo_Router_Register("GET", "/resume", GetResume); - Seobeo_Router_Register("GET", "/resume/index.html", GetResume); + Seobeo_Router_Register("GET", "/resume/index.html", GetRedirectResume); Seobeo_Router_Register("GET", "/tools", GetTools); - Seobeo_Router_Register("GET", "/tools/index.html", GetTools); + Seobeo_Router_Register("GET", "/tools/index.html", GetRedirectTools); Seobeo_Router_Register("GET", "/tools/markdown_to_html", GetMDToHTML); - Seobeo_Router_Register("GET", "/tools/markdown_to_html/index.html", GetMDToHTML); + Seobeo_Router_Register("GET", "/tools/markdown_to_html/index.html", GetRedirectMarkDownToHtml); Seobeo_Web_Server_Start("mrjunejune/pages", "6969", SEOBEO_MODE_EDGE, 2); }
--- a/mrjunejune/pages/resume/index.html Wed Dec 31 15:07:43 2025 -0800 +++ b/mrjunejune/pages/resume/index.html Thu Jan 01 05:57:03 2026 -0800 @@ -232,7 +232,6 @@ </div> <div id="footer"></div> </main> - <script src="/dark-mode.js"></script> <script href="index.js"></script> </body> </html>
--- a/mrjunejune/pages/tools/markdown_to_html/index.html Wed Dec 31 15:07:43 2025 -0800 +++ b/mrjunejune/pages/tools/markdown_to_html/index.html Thu Jan 01 05:57:03 2026 -0800 @@ -68,7 +68,6 @@ </div> </div> - <script src="/dark-mode.js"></script> <script src="/markdown_to_html.js"></script> <script> function convert() {
--- a/seobeo/s_router.c Wed Dec 31 15:07:43 2025 -0800 +++ b/seobeo/s_router.c Thu Jan 01 05:57:03 2026 -0800 @@ -28,8 +28,6 @@ route.method = strdup(method); route.path_pattern = strdup(path_pattern); route.handler = handler; - - // save it as global variable route.path_segments = Dowa_String_Split(path_pattern, "/", strlen(path_pattern), 1, NULL); route.segment_count = Dowa_Array_Length(route.path_segments); route.is_param = (boolean*)malloc(sizeof(boolean) * route.segment_count); @@ -112,9 +110,10 @@ return NULL; } -void Seobeo_Router_Send_Response(Seobeo_Handle *p_handle, - Seobeo_Request_Entry *p_response_map, - Dowa_Arena *p_arena) +void Seobeo_Router_Send_Response( + Seobeo_Handle *p_handle, + Seobeo_Request_Entry *p_response_map, + Dowa_Arena *p_arena) { if (p_response_map == NULL) { @@ -151,8 +150,24 @@ content_type = ((Seobeo_Request_Entry*)p_content_type_kv)->value; } - char *header = Dowa_Arena_Allocate(p_arena, 1024); + // All other types are default thoguht to be headers. + char *header = Dowa_Arena_Allocate(p_arena, 4096); Seobeo_Web_Header_Generate(header, status, content_type, strlen(body)); + for (int i = 0; i < Dowa_Array_Length(p_response_map); i++) + { + if ( + strstr(p_response_map[i].key, "status") || + strstr(p_response_map[i].key, "body") || + strstr(p_response_map[i].key, "content-type") + ) + continue; + + int32 current_header_len = strlen(header); + char *temp = malloc(sizeof(char) * 1024); + sprintf(temp, "%s: %s\r\n\r\n", p_response_map[i].key, p_response_map[i].value); + memcpy(&header[current_header_len - 2 /* \r\n */], temp, strlen(temp)); + free(temp); + } Seobeo_Handle_Queue(p_handle, (uint8_t*)header, strlen(header)); Seobeo_Handle_Queue(p_handle, (uint8_t*)body, strlen(body));
--- a/seobeo/s_web.c Wed Dec 31 15:07:43 2025 -0800 +++ b/seobeo/s_web.c Thu Jan 01 05:57:03 2026 -0800 @@ -500,8 +500,7 @@ if (!p_request_header) { perror("Dowa_Arena_Allocate"); return -1; } int request_len = Seobeo_Web_GenerateRequestHeader(p_request_header, host, path); - // DEBUG - // printf("request: %s\n", (char *)p_request_header); + Seobeo_Handle_Queue(h, (uint8 *)p_request_header, (uint32)request_len); if (Seobeo_Handle_Flush(h) < 0) {
--- a/seobeo/seobeo.h Wed Dec 31 15:07:43 2025 -0800 +++ b/seobeo/seobeo.h Thu Jan 01 05:57:03 2026 -0800 @@ -45,6 +45,17 @@ #define HTTP_NOT_FOUND 404 #define HTTP_INTERNAL_ERROR 500 +#define CREATE_REDIRECT_HANDLER(name, target_url) \ +Seobeo_Request_Entry* GetRedirect##name(Seobeo_Request_Entry *req, Dowa_Arena *arena) \ +{ \ + Seobeo_Request_Entry *resp = NULL; \ + Dowa_HashMap_Push_Arena(resp, "status", "301", arena); \ + Dowa_HashMap_Push_Arena(resp, "content-type", "text/plain", arena); \ + Dowa_HashMap_Push_Arena(resp, "Body", "", arena); \ + Dowa_HashMap_Push_Arena(resp, "Location", target_url, arena); \ + return resp; \ +} + extern volatile sig_atomic_t stop_server; // --- TCP --- //