annotate seobeo/s_web.c @ 100:65e5a5b89a4e

[Seobeo] Migrated everything to this page.
author June Park <parkjune1995@gmail.com>
date Sat, 03 Jan 2026 07:48:07 -0800
parents 70401cf61e97
children 3468e2fe8d88
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
1 #include "seobeo/seobeo.h"
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
2
71
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
3 // Global folder path for serving files
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
4 static char g_folder_path[512] = ".";
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
5
91
19cccf6e866a Added Epi photo reels.
June Park <parkjune1995@gmail.com>
parents: 82
diff changeset
6 int Seobeo_Web_GenerateRequestHeader(
19cccf6e866a Added Epi photo reels.
June Park <parkjune1995@gmail.com>
parents: 82
diff changeset
7 void *buffer, const char *host,
19cccf6e866a Added Epi photo reels.
June Park <parkjune1995@gmail.com>
parents: 82
diff changeset
8 const char *path)
17
d97ec3ded2ae [Seobeo] Few changes...
June Park <parkjune1995@gmail.com>
parents: 16
diff changeset
9 {
d97ec3ded2ae [Seobeo] Few changes...
June Park <parkjune1995@gmail.com>
parents: 16
diff changeset
10 return sprintf(
d97ec3ded2ae [Seobeo] Few changes...
June Park <parkjune1995@gmail.com>
parents: 16
diff changeset
11 buffer,
d97ec3ded2ae [Seobeo] Few changes...
June Park <parkjune1995@gmail.com>
parents: 16
diff changeset
12 "GET %s HTTP/1.1\r\n"
d97ec3ded2ae [Seobeo] Few changes...
June Park <parkjune1995@gmail.com>
parents: 16
diff changeset
13 "Host: %s\r\n"
d97ec3ded2ae [Seobeo] Few changes...
June Park <parkjune1995@gmail.com>
parents: 16
diff changeset
14 "Connection: close\r\n"
18
fa2b8af609d9 [Seobeo] Fixed a bug with pathing. Support SSL.
June Park <parkjune1995@gmail.com>
parents: 17
diff changeset
15 "accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7\r\n"
fa2b8af609d9 [Seobeo] Fixed a bug with pathing. Support SSL.
June Park <parkjune1995@gmail.com>
parents: 17
diff changeset
16 "accept-language: en-GB,en;q=0.9,en-US;q=0.8,ko;q=0.7\r\n"
fa2b8af609d9 [Seobeo] Fixed a bug with pathing. Support SSL.
June Park <parkjune1995@gmail.com>
parents: 17
diff changeset
17 "if-modified-since: Sat, 02 Aug 2025 22:51:58 GMT\r\n"
fa2b8af609d9 [Seobeo] Fixed a bug with pathing. Support SSL.
June Park <parkjune1995@gmail.com>
parents: 17
diff changeset
18 "if-none-match: W/\"688e968e-5700\"\r\n"
fa2b8af609d9 [Seobeo] Fixed a bug with pathing. Support SSL.
June Park <parkjune1995@gmail.com>
parents: 17
diff changeset
19 "priority: u=0, i\r\n"
fa2b8af609d9 [Seobeo] Fixed a bug with pathing. Support SSL.
June Park <parkjune1995@gmail.com>
parents: 17
diff changeset
20 "sec-ch-ua: \"Chromium\";v=\"140\", \"Not=A?Brand\";v=\"24\", \"Google Chrome\";v=\"140\"\r\n"
fa2b8af609d9 [Seobeo] Fixed a bug with pathing. Support SSL.
June Park <parkjune1995@gmail.com>
parents: 17
diff changeset
21 "sec-ch-ua-mobile: 0\r\n"
fa2b8af609d9 [Seobeo] Fixed a bug with pathing. Support SSL.
June Park <parkjune1995@gmail.com>
parents: 17
diff changeset
22 "sec-ch-ua-platform: \"macOS\"\r\n"
fa2b8af609d9 [Seobeo] Fixed a bug with pathing. Support SSL.
June Park <parkjune1995@gmail.com>
parents: 17
diff changeset
23 "sec-fetch-dest: document\r\n"
fa2b8af609d9 [Seobeo] Fixed a bug with pathing. Support SSL.
June Park <parkjune1995@gmail.com>
parents: 17
diff changeset
24 "sec-fetch-mode: navigate\r\n"
fa2b8af609d9 [Seobeo] Fixed a bug with pathing. Support SSL.
June Park <parkjune1995@gmail.com>
parents: 17
diff changeset
25 "sec-fetch-site: none\r\n"
fa2b8af609d9 [Seobeo] Fixed a bug with pathing. Support SSL.
June Park <parkjune1995@gmail.com>
parents: 17
diff changeset
26 "sec-fetch-user: 1\r\n"
fa2b8af609d9 [Seobeo] Fixed a bug with pathing. Support SSL.
June Park <parkjune1995@gmail.com>
parents: 17
diff changeset
27 "upgrade-insecure-requests: 1\r\n"
17
d97ec3ded2ae [Seobeo] Few changes...
June Park <parkjune1995@gmail.com>
parents: 16
diff changeset
28 "\r\n",
d97ec3ded2ae [Seobeo] Few changes...
June Park <parkjune1995@gmail.com>
parents: 16
diff changeset
29 path, host
d97ec3ded2ae [Seobeo] Few changes...
June Park <parkjune1995@gmail.com>
parents: 16
diff changeset
30 );
d97ec3ded2ae [Seobeo] Few changes...
June Park <parkjune1995@gmail.com>
parents: 16
diff changeset
31 }
d97ec3ded2ae [Seobeo] Few changes...
June Park <parkjune1995@gmail.com>
parents: 16
diff changeset
32
77
c348ac875294 [Seobeo] Server side rendering.
June Park <parkjune1995@gmail.com>
parents: 76
diff changeset
33 char* Seobeo_Web_LoadFile(const char *file_path, size_t *p_file_size)
71
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
34 {
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
35 char full_path[1024];
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
36 snprintf(full_path, sizeof(full_path), "%s/%s", g_folder_path, file_path);
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
37
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
38 FILE *p_file = fopen(full_path, "rb");
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
39 if (!p_file)
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
40 return NULL;
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
41
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
42 fseek(p_file, 0, SEEK_END);
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
43 size_t file_size = ftell(p_file);
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
44 fseek(p_file, 0, SEEK_SET);
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
45
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
46 char *p_content = (char*)malloc(file_size + 1);
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
47 if (!p_content)
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
48 {
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
49 fclose(p_file);
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
50 return NULL;
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
51 }
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
52
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
53 fread(p_content, 1, file_size, p_file);
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
54 p_content[file_size] = '\0';
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
55 fclose(p_file);
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
56
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
57 if (p_file_size)
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
58 *p_file_size = file_size;
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
59
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
60 return p_content;
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
61 }
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
62
76
35b1abc37969 Updating my website to use seobeo library.
June Park <parkjune1995@gmail.com>
parents: 72
diff changeset
63 void Seobeo_Web_Header_Generate(
35b1abc37969 Updating my website to use seobeo library.
June Park <parkjune1995@gmail.com>
parents: 72
diff changeset
64 void *buffer, int status,
35b1abc37969 Updating my website to use seobeo library.
June Park <parkjune1995@gmail.com>
parents: 72
diff changeset
65 const char *content_type, const int content_length)
7
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
66 {
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
67 const char *status_text;
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
68 switch(status)
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
69 {
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
70 case HTTP_OK: status_text = "OK"; break;
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
71 case HTTP_CREATED: status_text = "Created"; break;
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
72 case HTTP_MOVED_PERMANENTLY: status_text = "Moved Permanently"; break;
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
73 case HTTP_FOUND: status_text = "Found"; break;
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
74 case HTTP_BAD_REQUEST: status_text = "Bad Request"; break;
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
75 case HTTP_UNAUTHORIZED: status_text = "Unauthorized"; break;
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
76 case HTTP_FORBIDDEN: status_text = "Forbidden"; break;
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
77 case HTTP_NOT_FOUND: status_text = "Not Found"; break;
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
78 case HTTP_INTERNAL_ERROR: status_text = "Internal Server Error"; break;
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
79 default: status_text = "Unknown"; break;
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
80 }
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
81
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
82 sprintf(
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
83 buffer,
18
fa2b8af609d9 [Seobeo] Fixed a bug with pathing. Support SSL.
June Park <parkjune1995@gmail.com>
parents: 17
diff changeset
84 "HTTP/1.1 %d %s\r\n"
7
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
85 "Content-Type: %s\r\n"
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
86 "Content-Length: %d\r\n"
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
87 "Connection: close\r\n"
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
88 "\r\n",
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
89 status, status_text, content_type, content_length
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
90 );
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
91 }
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
92
66
a0f0ad5e42eb [Misc] taking out capital P stuff.
June Park <parkjune1995@gmail.com>
parents: 65
diff changeset
93 void Seobeo_Web_HandleClientRequest(Seobeo_Handle *p_cli_handle,
71
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
94 Seobeo_Cache_Entry *p_html_cache)
7
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
95 {
96
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
96 Seobeo_Log(SEOBEO_INFO, "Client is from %s\n", p_cli_handle->host);
71
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
97 Dowa_Arena *p_request_arena = Dowa_Arena_Create(1*1024*1024); // 1MB for request parsing
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
98 if (!p_request_arena) { perror("Dowa_Arena_Create request"); goto clean_up; }
7
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
99
96
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
100 Dowa_Arena *p_response_arena = Dowa_Arena_Create(5*1024*1024); // 5MB for response
71
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
101 if (!p_response_arena) { perror("Dowa_Arena_Create response"); goto clean_up; }
7
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
102
71
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
103 void *p_response_header = Dowa_Arena_Allocate(p_response_arena, (size_t)1024*5); // 5Kb
7
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
104 if (!p_response_header) { perror("Dowa_Arena_Allocate"); goto clean_up; }
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
105
71
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
106 Seobeo_Request_Entry *p_req_map = NULL;
92
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 91
diff changeset
107 int parse_result = Seobeo_Web_Header_Parse(p_cli_handle, &p_req_map, p_request_arena);
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 91
diff changeset
108
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 91
diff changeset
109 if (parse_result != 0 && parse_result != 1)
7
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
110 {
96
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
111 Seobeo_Log(SEOBEO_ERROR, "Seobeo_Web_Header_Parse failed with code %d\n", parse_result);
92
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 91
diff changeset
112 fflush(stdout);
19
875bb6e10db7 [Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents: 18
diff changeset
113 Seobeo_Web_Header_Generate(p_response_header,
22
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
114 HTTP_BAD_REQUEST,
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
115 "text/plain", 0);
7
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
116 Seobeo_Handle_Queue(p_cli_handle,
22
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
117 (const uint8*)p_response_header,
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
118 (uint32)strlen(p_response_header));
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
119 Seobeo_Handle_Flush(p_cli_handle);
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
120 goto clean_up;
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
121 }
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
122
96
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
123 Seobeo_Log(SEOBEO_DEBUG, "Parse completed with code %d\n", parse_result);
92
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 91
diff changeset
124 fflush(stdout);
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 91
diff changeset
125
71
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
126 void *p_method_kv = Dowa_HashMap_Get_Ptr(p_req_map, "HTTP_Method");
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
127 const char *method = p_method_kv ? ((Seobeo_Request_Entry*)p_method_kv)->value : NULL;
22
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
128
96
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
129 Seobeo_Log(SEOBEO_DEBUG, "Parsed request, method=%s\n", method ? method : "NULL");
92
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 91
diff changeset
130 fflush(stdout);
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 91
diff changeset
131
22
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
132 if (!method)
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
133 {
96
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
134 Seobeo_Log(SEOBEO_ERROR, "No HTTP method found in request\n");
92
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 91
diff changeset
135 fflush(stdout);
22
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
136 Seobeo_Web_Header_Generate(p_response_header,
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
137 HTTP_BAD_REQUEST,
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
138 "text/plain", 0);
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
139 Seobeo_Handle_Queue(p_cli_handle,
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
140 (const uint8*)p_response_header,
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
141 (uint32)strlen(p_response_header));
7
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
142 Seobeo_Handle_Flush(p_cli_handle);
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
143 goto clean_up;
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
144 }
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
145
72
4532ce6d9eb8 [Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents: 71
diff changeset
146 // Extract path
4532ce6d9eb8 [Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents: 71
diff changeset
147 void *p_path_kv = Dowa_HashMap_Get_Ptr(p_req_map, "Path");
4532ce6d9eb8 [Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents: 71
diff changeset
148 const char *path = p_path_kv ? ((Seobeo_Request_Entry*)p_path_kv)->value : "/";
4532ce6d9eb8 [Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents: 71
diff changeset
149
4532ce6d9eb8 [Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents: 71
diff changeset
150 // --- Try to match API route first ---
4532ce6d9eb8 [Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents: 71
diff changeset
151 Seobeo_Route_Handler handler = Seobeo_Router_Find_Handler(method, path, &p_req_map, p_request_arena);
4532ce6d9eb8 [Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents: 71
diff changeset
152 if (handler != NULL)
4532ce6d9eb8 [Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents: 71
diff changeset
153 {
4532ce6d9eb8 [Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents: 71
diff changeset
154 Seobeo_Request_Entry *p_response_map = handler(p_req_map, p_response_arena);
4532ce6d9eb8 [Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents: 71
diff changeset
155 Seobeo_Router_Send_Response(p_cli_handle, p_response_map, p_response_arena);
4532ce6d9eb8 [Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents: 71
diff changeset
156 goto clean_up;
4532ce6d9eb8 [Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents: 71
diff changeset
157 }
4532ce6d9eb8 [Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents: 71
diff changeset
158
4532ce6d9eb8 [Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents: 71
diff changeset
159 // --- Handle different HTTP methods (static files fallback) ---
22
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
160 if (strcmp(method, "GET") == 0)
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
161 {
71
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
162 void *p_kv = Dowa_HashMap_Get_Ptr(p_req_map, "Path");
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
163 const char *path = p_kv ? ((Seobeo_Request_Entry*)p_kv)->value : NULL;
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
164 char *file_path = Dowa_Arena_Allocate(p_response_arena, (size_t)5 * 1024); // 5Kb only for path
7
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
165
22
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
166 if (!path || strcmp(path, "/") == 0)
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
167 {
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
168 strcpy(file_path, "index.html");
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
169 }
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
170 else
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
171 {
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
172 size_t L = strlen(path);
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
173 if (path[0] == '/')
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
174 {
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
175 if (strchr(path, '.') == NULL)
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
176 snprintf(file_path, 512, "%.*s/index.html", (int)(L-1), path+1);
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
177 else
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
178 snprintf(file_path, 512, "%.*s", (int)(L-1), path+1);
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
179 }
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
180 else
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
181 strcpy(file_path, path);
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
182 }
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
183
71
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
184 // Check if file is in cache, load if not
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
185 void *p_file_kv = Dowa_HashMap_Get_Ptr(p_html_cache, file_path);
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
186 const char *file_content = NULL;
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
187 size_t body_size = 0;
18
fa2b8af609d9 [Seobeo] Fixed a bug with pathing. Support SSL.
June Park <parkjune1995@gmail.com>
parents: 17
diff changeset
188
71
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
189 if (p_file_kv)
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
190 {
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
191 // File is cached
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
192 file_content = ((Seobeo_Cache_Entry*)p_file_kv)->value;
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
193 body_size = strlen(file_content);
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
194 }
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
195 else
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
196 {
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
197 file_content = Seobeo_Web_LoadFile(file_path, &body_size);
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
198 if (file_content)
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
199 Dowa_HashMap_Push(p_html_cache, file_path, file_content);
22
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
200 }
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
201
71
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
202 if (!file_content)
22
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
203 {
19
875bb6e10db7 [Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents: 18
diff changeset
204 Seobeo_Web_Header_Generate(p_response_header,
22
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
205 HTTP_NOT_FOUND,
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
206 "text/html", 0);
18
fa2b8af609d9 [Seobeo] Fixed a bug with pathing. Support SSL.
June Park <parkjune1995@gmail.com>
parents: 17
diff changeset
207 Seobeo_Handle_Queue(p_cli_handle,
22
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
208 (const uint8*)p_response_header,
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
209 (uint32)strlen(p_response_header));
18
fa2b8af609d9 [Seobeo] Fixed a bug with pathing. Support SSL.
June Park <parkjune1995@gmail.com>
parents: 17
diff changeset
210 Seobeo_Handle_Flush(p_cli_handle);
fa2b8af609d9 [Seobeo] Fixed a bug with pathing. Support SSL.
June Park <parkjune1995@gmail.com>
parents: 17
diff changeset
211 goto clean_up;
fa2b8af609d9 [Seobeo] Fixed a bug with pathing. Support SSL.
June Park <parkjune1995@gmail.com>
parents: 17
diff changeset
212 }
7
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
213
72
4532ce6d9eb8 [Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents: 71
diff changeset
214 // Serve static file
22
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
215 const char *mime = "application/octet-stream";
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
216 if (strstr(file_path, ".html")) mime = "text/html; charset=utf-8";
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
217 else if (strstr(file_path, ".css")) mime = "text/css";
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
218 else if (strstr(file_path, ".js")) mime = "application/javascript";
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
219 else if (strstr(file_path, ".png")) mime = "image/png";
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
220 else if (strstr(file_path, ".jpg") || strstr(file_path, ".jpeg")) mime = "image/jpeg";
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
221 else if (strstr(file_path, ".gif")) mime = "image/gif";
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
222 else if (strstr(file_path, ".svg")) mime = "image/svg+xml";
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
223 else if (strstr(file_path, ".ico")) mime = "image/x-icon";
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
224 else if (strstr(file_path, ".json")) mime = "application/json";
100
65e5a5b89a4e [Seobeo] Migrated everything to this page.
June Park <parkjune1995@gmail.com>
parents: 96
diff changeset
225 else if (strstr(file_path, ".wasm")) mime = "application/wasm";
7
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
226
96
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
227 Seobeo_Log(SEOBEO_DEBUG, "File path: %s\nBody Size: %zu\n", file_path, body_size);
22
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
228
19
875bb6e10db7 [Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents: 18
diff changeset
229 Seobeo_Web_Header_Generate(p_response_header,
22
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
230 HTTP_OK,
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
231 mime,
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
232 body_size);
7
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
233
22
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
234 Seobeo_Handle_Queue(p_cli_handle,
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
235 (const uint8*)p_response_header,
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
236 (uint32)strlen(p_response_header));
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
237 Seobeo_Handle_Queue(p_cli_handle,
71
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
238 (const uint8*)file_content,
22
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
239 (uint32)body_size);
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
240 Seobeo_Handle_Flush(p_cli_handle);
96
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
241 Seobeo_Log(SEOBEO_DEBUG, "Request handled successfully\n");
22
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
242 }
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
243 else
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
244 {
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
245 Seobeo_Web_Header_Generate(p_response_header,
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
246 HTTP_FORBIDDEN,
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
247 "text/plain", 0);
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
248 Seobeo_Handle_Queue(p_cli_handle,
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
249 (const uint8*)p_response_header,
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
250 (uint32)strlen(p_response_header));
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
251 Seobeo_Handle_Flush(p_cli_handle);
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
252 }
33
c0f6c8c7829f [Seobeo] Linux epoll. Set the client socket to be nonblocking so that it doesn't stop loading when two different threads handle different client calls. I might have problem with socket not being cleaned up properly so need to check that.
MrJuneJune <me@mrjunejune.com>
parents: 22
diff changeset
253 goto clean_up;
7
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
254
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
255 clean_up:
96
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
256 Seobeo_Log(SEOBEO_INFO, "Clean up all Arenas\n");
62
ea9ef388ab97 [Seobeo] Fixed issues with epoll or kqeue in different threads. Initizlied the event looop inside of the thread itself.
June Park <parkjune1995@gmail.com>
parents: 36
diff changeset
257 if (p_cli_handle)
ea9ef388ab97 [Seobeo] Fixed issues with epoll or kqeue in different threads. Initizlied the event looop inside of the thread itself.
June Park <parkjune1995@gmail.com>
parents: 36
diff changeset
258 Seobeo_Handle_Destroy(p_cli_handle);
71
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
259 if (p_request_arena)
72
4532ce6d9eb8 [Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents: 71
diff changeset
260 Dowa_Arena_Free(p_request_arena);
62
ea9ef388ab97 [Seobeo] Fixed issues with epoll or kqeue in different threads. Initizlied the event looop inside of the thread itself.
June Park <parkjune1995@gmail.com>
parents: 36
diff changeset
261 if (p_response_arena)
72
4532ce6d9eb8 [Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents: 71
diff changeset
262 Dowa_Arena_Free(p_response_arena);
36
84672efec192 [Zenbu] WIP fixing issues regarding to using edge only. I think there is a problem where socket closes before sending back the info.
MrJuneJune <me@mrjunejune.com>
parents: 34
diff changeset
263 return;
7
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
264 }
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
265
22
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
266
71
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
267 int Seobeo_Web_Header_Parse(Seobeo_Handle *p_handle, Seobeo_Request_Entry **pp_map, Dowa_Arena *p_arena)
7
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
268 {
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
269 // 1) Fill read_buffer until we see "\r\n\r\n"
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
270 while (1)
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
271 {
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
272 int r = Seobeo_Handle_Read(p_handle);
71
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
273 if (r < 0)
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
274 return -1; // fatal error
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
275 if (r == -2)
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
276 return -2; // connection closed TODO: Add this as part of Handle struct.
7
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
277
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
278 if (p_handle->read_buffer_len >= 4 &&
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
279 strstr((char*)p_handle->read_buffer, "\r\n\r\n") != NULL)
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
280 {
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
281 break;
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
282 }
71
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
283 if (r == 0)
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
284 return 1; // EAGAIN, try again later TODO: Add this as part of Handle struct.
7
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
285 }
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
286
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
287 // 2) Parse request‐line "METHOD SP PATH SP VERSION CRLF"
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
288 char *buf = (char*)p_handle->read_buffer;
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
289 char *hdr_end = strstr(buf, "\r\n\r\n");
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
290 size_t hdr_len = hdr_end - buf + 4;
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
291
92
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 91
diff changeset
292 // Debug: Print the first line of the request
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 91
diff changeset
293 char *first_line_end = strstr(buf, "\r\n");
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 91
diff changeset
294 if (first_line_end)
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 91
diff changeset
295 {
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 91
diff changeset
296 size_t first_line_len = first_line_end - buf;
96
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
297 Seobeo_Log(SEOBEO_DEBUG, "Request line (first %zu bytes)\n", first_line_len > 200 ? 200 : first_line_len);
92
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 91
diff changeset
298 fflush(stdout);
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 91
diff changeset
299 }
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 91
diff changeset
300
22
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
301 // This seems kinda bad ?
7
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
302 char method[16], path[256], version[16];
92
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 91
diff changeset
303 int scan_result = sscanf(buf, "%15s %255s %15s", method, path, version);
96
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
304 Seobeo_Log(SEOBEO_DEBUG, "sscanf returned %d (method='%s', path='%s', version='%s')\n",
92
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 91
diff changeset
305 scan_result,
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 91
diff changeset
306 scan_result >= 1 ? method : "N/A",
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 91
diff changeset
307 scan_result >= 2 ? path : "N/A",
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 91
diff changeset
308 scan_result >= 3 ? version : "N/A");
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 91
diff changeset
309 fflush(stdout);
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 91
diff changeset
310
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 91
diff changeset
311 if (scan_result != 3)
7
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
312 {
96
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
313 Seobeo_Log(SEOBEO_ERROR, "Failed to parse request line\n");
92
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 91
diff changeset
314 fflush(stdout);
7
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
315 return -1;
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
316 }
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
317
71
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
318 // Copy strings to arena and store in hashmap
96
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
319 Seobeo_Log(SEOBEO_DEBUG, "Allocating method_copy\n");
92
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 91
diff changeset
320 fflush(stdout);
71
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
321 char *method_copy = Dowa_Arena_Allocate(p_arena, strlen(method) + 1);
96
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
322 if (!method_copy) { Seobeo_Log(SEOBEO_ERROR, "Failed to allocate method_copy\n"); return -1; }
71
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
323 strcpy(method_copy, method);
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
324
96
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
325 Seobeo_Log(SEOBEO_DEBUG, "Allocating version_copy\n");
92
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 91
diff changeset
326 fflush(stdout);
71
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
327 char *version_copy = Dowa_Arena_Allocate(p_arena, strlen(version) + 1);
96
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
328 if (!version_copy) { Seobeo_Log(SEOBEO_ERROR, "Failed to allocate version_copy\n"); return -1; }
71
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
329 strcpy(version_copy, version);
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
330
96
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
331 Seobeo_Log(SEOBEO_DEBUG, "Pushing HTTP_Method and Version to map\n");
92
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 91
diff changeset
332 fflush(stdout);
71
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
333 Dowa_HashMap_Push_Arena(*pp_map, "HTTP_Method", method_copy, p_arena);
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
334 Dowa_HashMap_Push_Arena(*pp_map, "Version", version_copy, p_arena);
96
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
335 Seobeo_Log(SEOBEO_DEBUG, "Map now has %zu entries\n", Dowa_Array_Length(*pp_map));
92
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 91
diff changeset
336 fflush(stdout);
7
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
337
22
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
338 // 1) Separate raw path and query string
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
339 char *raw_path = path;
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
340 char *query_start = strchr(raw_path, '?');
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
341 char *query_str = NULL;
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
342
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
343 if (query_start)
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
344 {
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
345 *query_start = '\0'; // now raw_path ends before '?'
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
346 query_str = query_start + 1;
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
347 }
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
348
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
349 // push only the clean path
71
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
350 char *path_copy = Dowa_Arena_Allocate(p_arena, strlen(raw_path) + 1);
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
351 if (!path_copy) return -1;
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
352 strcpy(path_copy, raw_path);
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
353 Dowa_HashMap_Push_Arena(*pp_map, "Path", path_copy, p_arena);
22
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
354
71
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
355 // 2) If there *is* a query, tokenize into the same map with "query_" prefix
22
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
356 if (query_str && *query_str)
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
357 {
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
358 char *cur = query_str;
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
359 while (cur && *cur)
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
360 {
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
361 char *next_amp = strchr(cur, '&');
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
362 char *pair_end = next_amp ? next_amp : cur + strlen(cur);
71
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
363
22
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
364 char *eq = memchr(cur, '=', pair_end - cur);
71
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
365 if (eq)
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
366 {
22
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
367 size_t key_len = eq - cur;
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
368 size_t val_len = pair_end - (eq + 1);
71
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
369
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
370 char key_buf[256];
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
371 snprintf(key_buf, sizeof(key_buf), "query_%.*s", (int)key_len, cur);
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
372
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
373 char *val_copy = Dowa_Arena_Allocate(p_arena, val_len + 1);
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
374 if (!val_copy) return -1;
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
375 memcpy(val_copy, eq + 1, val_len);
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
376 val_copy[val_len] = '\0';
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
377
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
378 Dowa_HashMap_Push_Arena(*pp_map, key_buf, val_copy, p_arena);
22
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
379 }
71
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
380
22
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
381 cur = next_amp ? next_amp + 1 : NULL;
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
382 }
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
383 }
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
384
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
385 // int qp = Dowa_HashMap_Get_Position(map, "QueryParams");
65
ecb6ee6a22c3 [Misc] I will no longer drink cool aids of capital P.
June Park <parkjune1995@gmail.com>
parents: 62
diff changeset
386 // Dowa_HashEntry *p_qp_entry = map->entries[qp];
22
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
387 // printf("query param key: %s\n", p_qp_entry->key);
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
388 // printf("query param value: %s\n",(char *)Dowa_HashMap_Get(p_qp_entry->buffer, "hello"));
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
389
7
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
390 // 3) Parse each header line until the blank line
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
391 char *line = buf + strlen(method) + 1 + strlen(path) + 1 + strlen(version) + 2;
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
392 while (line < hdr_end)
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
393 {
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
394 char *next = strstr(line, "\r\n");
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
395 if (!next) break;
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
396
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
397 // split at colon
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
398 char *colon = memchr(line, ':', next - line);
22
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
399 if (colon)
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
400 {
7
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
401 size_t key_len = colon - line;
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
402 size_t value_len = next - colon - 1;
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
403
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
404 char *val_start = colon + 1;
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
405 if (*val_start == ' ')
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
406 {
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
407 val_start++;
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
408 value_len--;
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
409 }
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
410
71
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
411 char *key = Dowa_Arena_Allocate(p_arena, key_len + 1);
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
412 if (!key) return -1;
7
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
413 memcpy(key, line, key_len);
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
414 key[key_len] = '\0';
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
415
71
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
416 char *val = Dowa_Arena_Allocate(p_arena, value_len + 1);
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
417 if (!val) return -1;
7
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
418 memcpy(val, val_start, value_len);
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
419 val[value_len] = '\0';
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
420
71
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
421 // Both key and value are arena-allocated, hashmap will use them
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
422 Dowa_HashMap_Push_Arena(*pp_map, key, val, p_arena);
7
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
423 }
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
424
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
425 line = next + 2;
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
426 }
71
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
427
7
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
428 Seobeo_Handle_Consume(p_handle, (uint32)hdr_len);
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
429
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
430 // 4) If Content-Length was provided, read that much body
71
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
431 void *p_cl_kv = Dowa_HashMap_Get_Ptr(*pp_map, "Content-Length");
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
432 if (p_cl_kv)
7
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
433 {
71
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
434 const char *content_length_str = ((Seobeo_Request_Entry*)p_cl_kv)->value;
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
435 size_t body_len = atoi(content_length_str);
92
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 91
diff changeset
436
96
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
437 Seobeo_Log(SEOBEO_DEBUG, "Content-Length=%zu, reading body in chunks...\n", body_len);
92
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 91
diff changeset
438 fflush(stdout);
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 91
diff changeset
439
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 91
diff changeset
440 // Allocate buffer for entire body
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 91
diff changeset
441 char *body = Dowa_Arena_Allocate(p_arena, body_len + 1);
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 91
diff changeset
442 if (!body)
7
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
443 {
96
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
444 Seobeo_Log(SEOBEO_ERROR, "Failed to allocate %zu bytes for body\n", body_len);
92
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 91
diff changeset
445 fflush(stdout);
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 91
diff changeset
446 return -1;
7
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
447 }
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
448
92
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 91
diff changeset
449 size_t total_read = 0;
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 91
diff changeset
450
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 91
diff changeset
451 // Read body in chunks
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 91
diff changeset
452 while (total_read < body_len)
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 91
diff changeset
453 {
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 91
diff changeset
454 // Copy what's currently in the read buffer
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 91
diff changeset
455 size_t available = p_handle->read_buffer_len;
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 91
diff changeset
456 size_t to_copy = (body_len - total_read) < available ? (body_len - total_read) : available;
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 91
diff changeset
457
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 91
diff changeset
458 if (to_copy > 0)
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 91
diff changeset
459 {
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 91
diff changeset
460 memcpy(body + total_read, p_handle->read_buffer, to_copy);
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 91
diff changeset
461 total_read += to_copy;
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 91
diff changeset
462 Seobeo_Handle_Consume(p_handle, (uint32)to_copy);
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 91
diff changeset
463
96
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
464 Seobeo_Log(SEOBEO_DEBUG, "Copied %zu bytes, total %zu/%zu\n", to_copy, total_read, body_len);
92
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 91
diff changeset
465 fflush(stdout);
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 91
diff changeset
466 }
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 91
diff changeset
467
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 91
diff changeset
468 // If we still need more data, read another chunk
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 91
diff changeset
469 if (total_read < body_len)
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 91
diff changeset
470 {
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 91
diff changeset
471 int r = Seobeo_Handle_Read(p_handle);
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 91
diff changeset
472 if (r < 0)
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 91
diff changeset
473 {
96
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
474 Seobeo_Log(SEOBEO_ERROR, "Read failed with %d\n", r);
92
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 91
diff changeset
475 fflush(stdout);
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 91
diff changeset
476 return -1;
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 91
diff changeset
477 }
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 91
diff changeset
478 if (r == 0)
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 91
diff changeset
479 {
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 91
diff changeset
480 // No data available yet, continue waiting
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 91
diff changeset
481 // printf("DEBUG: Waiting for more data... (have %zu/%zu bytes)\n", total_read, body_len);
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 91
diff changeset
482 fflush(stdout);
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 91
diff changeset
483 continue;
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 91
diff changeset
484 }
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 91
diff changeset
485 }
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 91
diff changeset
486 }
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 91
diff changeset
487
7
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
488 body[body_len] = '\0';
96
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
489 Seobeo_Log(SEOBEO_DEBUG, "Body fully received (%zu bytes)\n", body_len);
92
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 91
diff changeset
490 fflush(stdout);
7
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
491
71
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
492 // Body is arena-allocated
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
493 Dowa_HashMap_Push_Arena(*pp_map, "Body", body, p_arena);
7
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
494 }
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
495
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
496 return 0; // success; map now holds Method, Path, Version, headers, and optional Body
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
497 }
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
498
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
499 // TODO: Do epoll or kqueue depending on the OS.
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
500 void SigchildHandler(int s)
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
501 {
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
502 (void)s; // quiet unused variable warning
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
503
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
504 // waitpid() might overwrite errno, so we save and restore it:
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
505 int saved_errno = errno;
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
506
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
507 while(waitpid(-1, NULL, WNOHANG) > 0);
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
508
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
509 errno = saved_errno;
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
510 }
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
511
19
875bb6e10db7 [Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents: 18
diff changeset
512 int Seobeo_Web_Server_Start(
7
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
513 const char *folder_path,
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
514 const char *port,
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
515 Seobeo_ServerMode mode,
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
516 int thread_count)
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
517 {
71
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
518 // Store folder path globally
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
519 if (folder_path)
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
520 strncpy(g_folder_path, folder_path, sizeof(g_folder_path) - 1);
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
521
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
522 // Initialize empty cache - files will be loaded on-demand
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 67
diff changeset
523 Seobeo_Cache_Entry *p_html_cache = NULL;
7
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
524
66
a0f0ad5e42eb [Misc] taking out capital P stuff.
June Park <parkjune1995@gmail.com>
parents: 65
diff changeset
525 Seobeo_Handle *p_server_handle =
18
fa2b8af609d9 [Seobeo] Fixed a bug with pathing. Support SSL.
June Park <parkjune1995@gmail.com>
parents: 17
diff changeset
526 Seobeo_Stream_Handle_Server_Create(NULL, port);
7
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
527 if (p_server_handle->socket < 0) return 1;
18
fa2b8af609d9 [Seobeo] Fixed a bug with pathing. Support SSL.
June Park <parkjune1995@gmail.com>
parents: 17
diff changeset
528
96
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
529 Seobeo_Log(SEOBEO_INFO, "Listening on port %s\n", port);
7
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
530
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
531 // Fork‐based fallback
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
532 if (mode == SEOBEO_MODE_FORK)
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
533 {
96
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
534 Seobeo_Log(SEOBEO_INFO, "Server mode: FORK\n");
7
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
535 struct sigaction sa;
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
536 sa.sa_handler = SigchildHandler;
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
537 sigemptyset(&sa.sa_mask);
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
538 sa.sa_flags = SA_RESTART;
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
539 sigaction(SIGCHLD, &sa, NULL);
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
540
34
6c322f9c2cb9 [Birthday] I didn't have nay time to buy birhtday present so w/e/
MrJuneJune <me@mrjunejune.com>
parents: 33
diff changeset
541 while (1)
6c322f9c2cb9 [Birthday] I didn't have nay time to buy birhtday present so w/e/
MrJuneJune <me@mrjunejune.com>
parents: 33
diff changeset
542 {
66
a0f0ad5e42eb [Misc] taking out capital P stuff.
June Park <parkjune1995@gmail.com>
parents: 65
diff changeset
543 Seobeo_Handle *p_cli_handle =
19
875bb6e10db7 [Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents: 18
diff changeset
544 Seobeo_Stream_Handle_Server_Accept(p_server_handle);
36
84672efec192 [Zenbu] WIP fixing issues regarding to using edge only. I think there is a problem where socket closes before sending back the info.
MrJuneJune <me@mrjunejune.com>
parents: 34
diff changeset
545 if (!p_cli_handle) continue;
7
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
546
16
fb2cff495a60 [Seobeo] Fixed the problem with edge server.
June Park <parkjune1995@gmail.com>
parents: 7
diff changeset
547 if (fork() == 0)
fb2cff495a60 [Seobeo] Fixed the problem with edge server.
June Park <parkjune1995@gmail.com>
parents: 7
diff changeset
548 {
36
84672efec192 [Zenbu] WIP fixing issues regarding to using edge only. I think there is a problem where socket closes before sending back the info.
MrJuneJune <me@mrjunejune.com>
parents: 34
diff changeset
549 Seobeo_Web_HandleClientRequest(p_cli_handle,
7
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
550 p_html_cache);
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
551 _exit(0);
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
552 }
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
553 }
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
554 }
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
555
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
556 if (mode == SEOBEO_MODE_EDGE)
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
557 {
96
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
558 Seobeo_Log(SEOBEO_INFO, "Server mode: EDGE\n");
62
ea9ef388ab97 [Seobeo] Fixed issues with epoll or kqeue in different threads. Initizlied the event looop inside of the thread itself.
June Park <parkjune1995@gmail.com>
parents: 36
diff changeset
559 Seobeo_Web_Edge(p_server_handle, thread_count, p_html_cache);
7
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
560 }
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
561
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
562 return -1;
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
563 }
17
d97ec3ded2ae [Seobeo] Few changes...
June Park <parkjune1995@gmail.com>
parents: 16
diff changeset
564
19
875bb6e10db7 [Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents: 18
diff changeset
565 int Seobeo_Web_Client_Get(const char *host,
875bb6e10db7 [Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents: 18
diff changeset
566 const char *port,
875bb6e10db7 [Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents: 18
diff changeset
567 const char *path)
17
d97ec3ded2ae [Seobeo] Few changes...
June Park <parkjune1995@gmail.com>
parents: 16
diff changeset
568 {
66
a0f0ad5e42eb [Misc] taking out capital P stuff.
June Park <parkjune1995@gmail.com>
parents: 65
diff changeset
569 Seobeo_Handle *h = Seobeo_Stream_Handle_Client_Create(host, port, TRUE);
17
d97ec3ded2ae [Seobeo] Few changes...
June Park <parkjune1995@gmail.com>
parents: 16
diff changeset
570 if (!h || h->socket < 0)
d97ec3ded2ae [Seobeo] Few changes...
June Park <parkjune1995@gmail.com>
parents: 16
diff changeset
571 {
18
fa2b8af609d9 [Seobeo] Fixed a bug with pathing. Support SSL.
June Park <parkjune1995@gmail.com>
parents: 17
diff changeset
572 if (h)
fa2b8af609d9 [Seobeo] Fixed a bug with pathing. Support SSL.
June Park <parkjune1995@gmail.com>
parents: 17
diff changeset
573 Seobeo_Handle_Destroy(h);
17
d97ec3ded2ae [Seobeo] Few changes...
June Park <parkjune1995@gmail.com>
parents: 16
diff changeset
574 return -1;
d97ec3ded2ae [Seobeo] Few changes...
June Park <parkjune1995@gmail.com>
parents: 16
diff changeset
575 }
d97ec3ded2ae [Seobeo] Few changes...
June Park <parkjune1995@gmail.com>
parents: 16
diff changeset
576
65
ecb6ee6a22c3 [Misc] I will no longer drink cool aids of capital P.
June Park <parkjune1995@gmail.com>
parents: 62
diff changeset
577 Dowa_Arena *p_request_arena = Dowa_Arena_Create(1 * 1024 * 1024);
18
fa2b8af609d9 [Seobeo] Fixed a bug with pathing. Support SSL.
June Park <parkjune1995@gmail.com>
parents: 17
diff changeset
578 if (!p_request_arena) { perror("Dowa_Arena_Create"); return -1; }
fa2b8af609d9 [Seobeo] Fixed a bug with pathing. Support SSL.
June Park <parkjune1995@gmail.com>
parents: 17
diff changeset
579
17
d97ec3ded2ae [Seobeo] Few changes...
June Park <parkjune1995@gmail.com>
parents: 16
diff changeset
580 void *p_request_header = Dowa_Arena_Allocate(p_request_arena, 4096);
18
fa2b8af609d9 [Seobeo] Fixed a bug with pathing. Support SSL.
June Park <parkjune1995@gmail.com>
parents: 17
diff changeset
581 if (!p_request_header) { perror("Dowa_Arena_Allocate"); return -1; }
17
d97ec3ded2ae [Seobeo] Few changes...
June Park <parkjune1995@gmail.com>
parents: 16
diff changeset
582
d97ec3ded2ae [Seobeo] Few changes...
June Park <parkjune1995@gmail.com>
parents: 16
diff changeset
583 int request_len = Seobeo_Web_GenerateRequestHeader(p_request_header, host, path);
79
5710108c949e [Seobeo] Added Redirect logic.
June Park <parkjune1995@gmail.com>
parents: 78
diff changeset
584
17
d97ec3ded2ae [Seobeo] Few changes...
June Park <parkjune1995@gmail.com>
parents: 16
diff changeset
585 Seobeo_Handle_Queue(h, (uint8 *)p_request_header, (uint32)request_len);
d97ec3ded2ae [Seobeo] Few changes...
June Park <parkjune1995@gmail.com>
parents: 16
diff changeset
586 if (Seobeo_Handle_Flush(h) < 0)
d97ec3ded2ae [Seobeo] Few changes...
June Park <parkjune1995@gmail.com>
parents: 16
diff changeset
587 {
d97ec3ded2ae [Seobeo] Few changes...
June Park <parkjune1995@gmail.com>
parents: 16
diff changeset
588 perror("Seobeo_Handle_Flush");
d97ec3ded2ae [Seobeo] Few changes...
June Park <parkjune1995@gmail.com>
parents: 16
diff changeset
589 Seobeo_Handle_Destroy(h);
d97ec3ded2ae [Seobeo] Few changes...
June Park <parkjune1995@gmail.com>
parents: 16
diff changeset
590 return -1;
d97ec3ded2ae [Seobeo] Few changes...
June Park <parkjune1995@gmail.com>
parents: 16
diff changeset
591 }
d97ec3ded2ae [Seobeo] Few changes...
June Park <parkjune1995@gmail.com>
parents: 16
diff changeset
592
d97ec3ded2ae [Seobeo] Few changes...
June Park <parkjune1995@gmail.com>
parents: 16
diff changeset
593 size_t cap = 1024*8, used = 0;
d97ec3ded2ae [Seobeo] Few changes...
June Park <parkjune1995@gmail.com>
parents: 16
diff changeset
594 char *p_request_body = Dowa_Arena_Allocate(p_request_arena, cap);
d97ec3ded2ae [Seobeo] Few changes...
June Park <parkjune1995@gmail.com>
parents: 16
diff changeset
595 if (!p_request_body) { Seobeo_Handle_Destroy(h); return -1; }
d97ec3ded2ae [Seobeo] Few changes...
June Park <parkjune1995@gmail.com>
parents: 16
diff changeset
596
18
fa2b8af609d9 [Seobeo] Fixed a bug with pathing. Support SSL.
June Park <parkjune1995@gmail.com>
parents: 17
diff changeset
597 while (1)
fa2b8af609d9 [Seobeo] Fixed a bug with pathing. Support SSL.
June Park <parkjune1995@gmail.com>
parents: 17
diff changeset
598 {
17
d97ec3ded2ae [Seobeo] Few changes...
June Park <parkjune1995@gmail.com>
parents: 16
diff changeset
599 int n = Seobeo_Handle_Read(h);
96
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
600 Seobeo_Log(SEOBEO_DEBUG, "Received size: %d bytes\n", n);
17
d97ec3ded2ae [Seobeo] Few changes...
June Park <parkjune1995@gmail.com>
parents: 16
diff changeset
601 if (n > 0)
d97ec3ded2ae [Seobeo] Few changes...
June Park <parkjune1995@gmail.com>
parents: 16
diff changeset
602 {
18
fa2b8af609d9 [Seobeo] Fixed a bug with pathing. Support SSL.
June Park <parkjune1995@gmail.com>
parents: 17
diff changeset
603 // TODO: Maybe directly use arena inside of the struct? idk if that is useful or not...
17
d97ec3ded2ae [Seobeo] Few changes...
June Park <parkjune1995@gmail.com>
parents: 16
diff changeset
604 memcpy(p_request_body + used, h->read_buffer, h->read_buffer_len);
d97ec3ded2ae [Seobeo] Few changes...
June Park <parkjune1995@gmail.com>
parents: 16
diff changeset
605 used += h->read_buffer_len;
d97ec3ded2ae [Seobeo] Few changes...
June Park <parkjune1995@gmail.com>
parents: 16
diff changeset
606 Seobeo_Handle_Consume(h, (uint32)h->read_buffer_len);
18
fa2b8af609d9 [Seobeo] Fixed a bug with pathing. Support SSL.
June Park <parkjune1995@gmail.com>
parents: 17
diff changeset
607 }else if (n == 0)
17
d97ec3ded2ae [Seobeo] Few changes...
June Park <parkjune1995@gmail.com>
parents: 16
diff changeset
608 {
18
fa2b8af609d9 [Seobeo] Fixed a bug with pathing. Support SSL.
June Park <parkjune1995@gmail.com>
parents: 17
diff changeset
609 // Wait
17
d97ec3ded2ae [Seobeo] Few changes...
June Park <parkjune1995@gmail.com>
parents: 16
diff changeset
610 continue;
18
fa2b8af609d9 [Seobeo] Fixed a bug with pathing. Support SSL.
June Park <parkjune1995@gmail.com>
parents: 17
diff changeset
611 }else if (n == -2)
17
d97ec3ded2ae [Seobeo] Few changes...
June Park <parkjune1995@gmail.com>
parents: 16
diff changeset
612 {
18
fa2b8af609d9 [Seobeo] Fixed a bug with pathing. Support SSL.
June Park <parkjune1995@gmail.com>
parents: 17
diff changeset
613 // Debug
17
d97ec3ded2ae [Seobeo] Few changes...
June Park <parkjune1995@gmail.com>
parents: 16
diff changeset
614 // peer closed; we’ve got everything
96
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
615 Seobeo_Log(SEOBEO_DEBUG, "Connection closed by client\n");
17
d97ec3ded2ae [Seobeo] Few changes...
June Park <parkjune1995@gmail.com>
parents: 16
diff changeset
616 break;
18
fa2b8af609d9 [Seobeo] Fixed a bug with pathing. Support SSL.
June Park <parkjune1995@gmail.com>
parents: 17
diff changeset
617 }else
17
d97ec3ded2ae [Seobeo] Few changes...
June Park <parkjune1995@gmail.com>
parents: 16
diff changeset
618 {
72
4532ce6d9eb8 [Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents: 71
diff changeset
619 Dowa_Arena_Free(p_request_arena);
17
d97ec3ded2ae [Seobeo] Few changes...
June Park <parkjune1995@gmail.com>
parents: 16
diff changeset
620 Seobeo_Handle_Destroy(h);
d97ec3ded2ae [Seobeo] Few changes...
June Park <parkjune1995@gmail.com>
parents: 16
diff changeset
621 return -1;
d97ec3ded2ae [Seobeo] Few changes...
June Park <parkjune1995@gmail.com>
parents: 16
diff changeset
622 }
d97ec3ded2ae [Seobeo] Few changes...
June Park <parkjune1995@gmail.com>
parents: 16
diff changeset
623 }
d97ec3ded2ae [Seobeo] Few changes...
June Park <parkjune1995@gmail.com>
parents: 16
diff changeset
624
22
947b81010aba [Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
June Park <parkjune1995@gmail.com>
parents: 21
diff changeset
625 // Debug
96
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
626 Seobeo_Log(SEOBEO_DEBUG, "Request body: %s\n", p_request_body);
72
4532ce6d9eb8 [Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents: 71
diff changeset
627 Dowa_Arena_Free(p_request_arena);
17
d97ec3ded2ae [Seobeo] Few changes...
June Park <parkjune1995@gmail.com>
parents: 16
diff changeset
628 Seobeo_Handle_Destroy(h);
d97ec3ded2ae [Seobeo] Few changes...
June Park <parkjune1995@gmail.com>
parents: 16
diff changeset
629 return 0;
d97ec3ded2ae [Seobeo] Few changes...
June Park <parkjune1995@gmail.com>
parents: 16
diff changeset
630 }
96
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
631
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
632 /* Router logic */
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
633 struct Seobeo_Route_Struct {
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
634 char *method; // "GET", "POST", "PUT", "DELETE"
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
635 char *path_pattern; // "/v1/users/:id/posts/:post_id"
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
636 Seobeo_Route_Handler handler;
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
637
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
638 // Pre-parsed path segments for efficient matching
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
639 char **path_segments; // ["v1", "users", ":id", "posts", ":post_id"]
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
640 boolean *is_param; // [false, false, true, false, true]
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
641 size_t segment_count;
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
642 };
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
643
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
644 static Seobeo_Route *g_routes = NULL;
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
645
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
646 void Seobeo_Router_Init()
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
647 {
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
648 Dowa_Array_Reserve(g_routes, 20);
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
649 }
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
650
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
651 void Seobeo_Router_Register(const char *method, const char *path_pattern, Seobeo_Route_Handler handler)
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
652 {
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
653 Seobeo_Route route = {0};
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
654
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
655 route.method = strdup(method);
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
656 route.path_pattern = strdup(path_pattern);
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
657 route.handler = handler;
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
658 route.path_segments = Dowa_String_Split(path_pattern, "/", strlen(path_pattern), 1, NULL);
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
659 route.segment_count = Dowa_Array_Length(route.path_segments);
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
660 route.is_param = (boolean*)malloc(sizeof(boolean) * route.segment_count);
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
661
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
662 for (size_t i = 0; i < route.segment_count; i++)
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
663 route.is_param[i] = (route.path_segments[i][0] == ':');
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
664
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
665 Dowa_Array_Push(g_routes, route);
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
666 }
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
667
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
668 // Match route and extract path parameters
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
669 static boolean match_route_and_extract(
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
670 Seobeo_Route *route,
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
671 const char *request_path,
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
672 Seobeo_Request_Entry **pp_request_map,
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
673 Dowa_Arena *p_arena)
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
674 {
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
675 Dowa_Arena *p_temp_arena = Dowa_Arena_Create(1024);
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
676 char **request_segments = Dowa_String_Split(request_path, "/", strlen(request_path), 1, p_temp_arena);
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
677 size_t request_segment_count = Dowa_Array_Length(request_segments);
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
678 // Check segment count matches
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
679 if (request_segment_count != route->segment_count)
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
680 {
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
681 Dowa_Arena_Free(p_temp_arena);
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
682 return FALSE;
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
683 }
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
684
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
685 for (size_t i = 0; i < route->segment_count; i++)
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
686 {
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
687 // parameters
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
688 if (route->is_param[i])
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
689 {
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
690 char *param_name = route->path_segments[i]; // e.g., ":id"
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
691 char *param_value = request_segments[i]; // e.g., "123"
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
692
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
693 // Should Copy to arena
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
694 char *key = Dowa_String_Copy_Arena(param_name, p_arena);
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
695 char *value = Dowa_String_Copy_Arena(param_value, p_arena);
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
696 Dowa_HashMap_Push_Arena(*pp_request_map, key, value, p_arena);
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
697 }
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
698 else
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
699 {
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
700 // Does not match.
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
701 if (strcmp(route->path_segments[i], request_segments[i]) != 0)
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
702 {
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
703 Dowa_Arena_Free(p_temp_arena);
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
704 return FALSE;
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
705 }
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
706 }
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
707 }
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
708
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
709 Dowa_Arena_Free(p_temp_arena);
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
710 return TRUE;
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
711 }
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
712
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
713 Seobeo_Route_Handler Seobeo_Router_Find_Handler(const char *method,
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
714 const char *path,
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
715 Seobeo_Request_Entry **pp_request_map,
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
716 Dowa_Arena *p_arena) {
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
717 if (g_routes == NULL)
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
718 {
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
719 return NULL;
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
720 }
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
721
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
722 size_t route_count = Dowa_Array_Length(g_routes);
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
723 for (size_t i = 0; i < route_count; i++)
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
724 {
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
725 Seobeo_Route *route = &g_routes[i];
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
726 if (strcmp(route->method, method) != 0)
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
727 {
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
728 continue;
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
729 }
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
730
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
731 if (match_route_and_extract(route, path, pp_request_map, p_arena))
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
732 {
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
733 return route->handler;
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
734 }
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
735 }
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
736
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
737 return NULL;
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
738 }
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
739
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
740 void Seobeo_Router_Send_Response(
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
741 Seobeo_Handle *p_handle,
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
742 Seobeo_Request_Entry *p_response_map,
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
743 Dowa_Arena *p_arena)
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
744 {
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
745 if (p_response_map == NULL)
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
746 {
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
747 char *header = Dowa_Arena_Allocate(p_arena, 1024);
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
748 Seobeo_Web_Header_Generate(header, HTTP_INTERNAL_ERROR, "text/plain", 21);
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
749 Seobeo_Handle_Queue(p_handle, (uint8_t*)header, strlen(header));
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
750 Seobeo_Handle_Queue(p_handle, (uint8_t*)"Internal Server Error", 21);
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
751 Seobeo_Handle_Flush(p_handle);
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
752 return;
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
753 }
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
754
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
755 // Header
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
756 int status = HTTP_OK;
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
757 void *p_status_kv = Dowa_HashMap_Get_Ptr(p_response_map, "status");
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
758 if (p_status_kv)
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
759 {
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
760 const char *status_str = ((Seobeo_Request_Entry*)p_status_kv)->value;
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
761 status = atoi(status_str);
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
762 }
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
763
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
764 // Body
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
765 const char *body = "";
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
766 void *p_body_kv = Dowa_HashMap_Get_Ptr(p_response_map, "body");
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
767 if (p_body_kv)
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
768 {
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
769 body = ((Seobeo_Request_Entry*)p_body_kv)->value;
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
770 }
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
771
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
772 // Default text plain
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
773 const char *content_type = "text/html";
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
774 void *p_content_type_kv = Dowa_HashMap_Get_Ptr(p_response_map, "content-type");
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
775 if (p_content_type_kv)
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
776 {
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
777 content_type = ((Seobeo_Request_Entry*)p_content_type_kv)->value;
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
778 }
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
779
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
780 // Check for custom content-length (for binary data)
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
781 size_t body_length = strlen(body);
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
782 void *p_content_length_kv = Dowa_HashMap_Get_Ptr(p_response_map, "content-length");
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
783 if (p_content_length_kv)
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
784 {
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
785 const char *content_length_str = ((Seobeo_Request_Entry*)p_content_length_kv)->value;
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
786 body_length = atoi(content_length_str);
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
787 }
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
788
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
789 // All other types are default thoguht to be headers.
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
790 char *header = Dowa_Arena_Allocate(p_arena, 4096);
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
791 Seobeo_Web_Header_Generate(header, status, content_type, body_length);
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
792 for (int i = 0; i < Dowa_Array_Length(p_response_map); i++)
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
793 {
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
794 if (
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
795 strstr(p_response_map[i].key, "status") ||
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
796 strstr(p_response_map[i].key, "body") ||
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
797 strstr(p_response_map[i].key, "content-type") ||
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
798 strstr(p_response_map[i].key, "content-length") // Skip custom content-length
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
799 )
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
800 continue;
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
801
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
802 int32 current_header_len = strlen(header);
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
803 char *temp = malloc(sizeof(char) * 1024);
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
804 sprintf(temp, "%s: %s\r\n\r\n", p_response_map[i].key, p_response_map[i].value);
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
805 memcpy(&header[current_header_len - 2 /* \r\n */], temp, strlen(temp));
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
806 free(temp);
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
807 }
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
808
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
809 Seobeo_Handle_Queue(p_handle, (uint8_t*)header, strlen(header));
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
810 Seobeo_Handle_Queue(p_handle, (uint8_t*)body, body_length); // Use actual body length
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
811 Seobeo_Handle_Flush(p_handle);
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
812 }
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
813
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
814 void Seobeo_Router_Destroy()
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
815 {
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
816 if (g_routes == NULL)
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
817 return;
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
818
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
819 size_t route_count = Dowa_Array_Length(g_routes);
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
820 for (size_t i = 0; i < route_count; i++)
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
821 {
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
822 Seobeo_Route *route = &g_routes[i];
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
823 if (route->method) free(route->method);
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
824 if (route->path_pattern) free(route->path_pattern);
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
825 if (route->path_segments) Dowa_Array_Free(route->path_segments);
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
826 if (route->is_param) free(route->is_param);
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
827 }
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
828 Dowa_Array_Free(g_routes);
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
829 g_routes = NULL;
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
830 }
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
831
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
832 static char *Seobeo_Log_Level_String(Seobeo_Log_Level level)
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
833 {
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
834 switch(level) {
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
835 case SEOBEO_DEBUG: return "DEBUG";
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
836 case SEOBEO_INFO: return "INFO";
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
837 case SEOBEO_WARNING: return "WARNING";
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
838 case SEOBEO_ERROR: return "ERROR";
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
839 default: return "INFO";
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
840 }
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
841 }
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
842
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
843 int Seobeo_Log(Seobeo_Log_Level level, const char * restrict format, ...)
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
844 {
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
845 #ifndef SEOBEO_ENABLE_DEBUG
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
846 if (level == SEOBEO_DEBUG)
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
847 return 0;
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
848 #endif
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
849
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
850 int result;
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
851 va_list args;
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
852 printf("[%s] ", Seobeo_Log_Level_String(level));
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
853 va_start(args, format);
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
854 result = vprintf(format, args);
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
855 va_end(args);
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
856
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
857 return result;
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
858 }
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
859