Skip to content

Commit fb63521

Browse files
committed
Fix serve_a_folder (Now should be Header + Body)
1 parent d213bf4 commit fb63521

File tree

1 file changed

+117
-97
lines changed
  • examples/C/serve_a_folder

1 file changed

+117
-97
lines changed

examples/C/serve_a_folder/main.c

Lines changed: 117 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -10,135 +10,155 @@
1010

1111
void exit_app(webui_event_t* e) {
1212

13-
// Close all opened windows
14-
webui_exit();
13+
// Close all opened windows
14+
webui_exit();
1515
}
1616

1717
void events(webui_event_t* e) {
1818

19-
// This function gets called every time
20-
// there is an event
21-
22-
if (e->event_type == WEBUI_EVENT_CONNECTED)
23-
printf("Connected. \n");
24-
else if (e->event_type == WEBUI_EVENT_DISCONNECTED)
25-
printf("Disconnected. \n");
26-
else if (e->event_type == WEBUI_EVENT_MOUSE_CLICK)
27-
printf("Click. \n");
28-
else if (e->event_type == WEBUI_EVENT_NAVIGATION) {
29-
const char* url = webui_get_string(e);
30-
printf("Starting navigation to: %s \n", url);
31-
32-
// Because we used `webui_bind(MyWindow, "", events);`
33-
// WebUI will block all `href` link clicks and sent here instead.
34-
// We can then control the behaviour of links as needed.
35-
webui_navigate(e->window, url);
36-
}
19+
// This function gets called every time
20+
// there is an event
21+
22+
if (e->event_type == WEBUI_EVENT_CONNECTED)
23+
printf("Connected. \n");
24+
else if (e->event_type == WEBUI_EVENT_DISCONNECTED)
25+
printf("Disconnected. \n");
26+
else if (e->event_type == WEBUI_EVENT_MOUSE_CLICK)
27+
printf("Click. \n");
28+
else if (e->event_type == WEBUI_EVENT_NAVIGATION) {
29+
const char* url = webui_get_string(e);
30+
printf("Starting navigation to: %s \n", url);
31+
32+
// Because we used `webui_bind(MyWindow, "", events);`
33+
// WebUI will block all `href` link clicks and sent here instead.
34+
// We can then control the behaviour of links as needed.
35+
webui_navigate(e->window, url);
36+
}
3737
}
3838

3939
void switch_to_second_page(webui_event_t* e) {
4040

41-
// This function gets called every
42-
// time the user clicks on "SwitchToSecondPage"
41+
// This function gets called every
42+
// time the user clicks on "SwitchToSecondPage"
4343

44-
// Switch to `/second.html` in the same opened window.
45-
webui_show(e->window, "second.html");
44+
// Switch to `/second.html` in the same opened window.
45+
webui_show(e->window, "second.html");
4646
}
4747

4848
void show_second_window(webui_event_t* e) {
4949

50-
// This function gets called every
51-
// time the user clicks on "OpenNewWindow"
50+
// This function gets called every
51+
// time the user clicks on "OpenNewWindow"
5252

53-
// Show a new window, and navigate to `/second.html`
54-
// if it's already open, then switch in the same window
55-
webui_show(MySecondWindow, "second.html");
53+
// Show a new window, and navigate to `/second.html`
54+
// if it's already open, then switch in the same window
55+
webui_show(MySecondWindow, "second.html");
5656
}
5757

5858
const void* my_files_handler(const char* filename, int* length) {
5959

60-
printf("File: %s \n", filename);
61-
62-
if (!strcmp(filename, "/test.txt")) {
63-
64-
// Const static file example
65-
// Note: The connection will drop if the content
66-
// does not have `<script src="/webui.js"></script>`
67-
return "This is a embedded file content example.";
68-
} else if (!strcmp(filename, "/dynamic.html")) {
69-
70-
// Dynamic file example
71-
72-
// Allocate memory
73-
char* dynamic_content = webui_malloc(1024);
74-
75-
// Generate content
76-
static int count = 0;
77-
sprintf(
78-
dynamic_content,
79-
"<html>"
80-
" This is a dynamic file content example. <br>"
81-
" Count: %d <a href=\"dynamic.html\">[Refresh]</a><br>"
82-
" <script src=\"webui.js\"></script>" // To keep connection with WebUI
83-
"</html>",
84-
++count
85-
);
86-
87-
// Set len (optional)
88-
*length = strlen(dynamic_content);
89-
90-
// By allocating resources using webui_malloc()
91-
// WebUI will automaticaly free the resources.
92-
return dynamic_content;
93-
}
94-
95-
// Other files:
96-
// A NULL return will make WebUI
97-
// looks for the file locally.
98-
return NULL;
60+
printf("File: %s \n", filename);
61+
62+
if (!strcmp(filename, "/test.txt")) {
63+
64+
// Const static file example
65+
return "HTTP/1.1 200 OK\r\n"
66+
"Content-Type: text/html\r\n"
67+
"Content-Length: 99\r\n\r\n"
68+
"<html>"
69+
" This is a static embedded file content example."
70+
" <script src=\"webui.js\"></script>" // To keep connection with WebUI
71+
"</html>";
72+
}
73+
else if (!strcmp(filename, "/dynamic.html")) {
74+
75+
// Dynamic file example
76+
77+
// Allocate memory
78+
char* body = webui_malloc(1024);
79+
char* header_and_body = webui_malloc(1024);
80+
81+
// Generate body
82+
static int count = 0;
83+
sprintf(
84+
body,
85+
"<html>"
86+
" This is a dynamic file content example. <br>"
87+
" Count: %d <a href=\"dynamic.html\">[Refresh]</a><br>"
88+
" <script src=\"webui.js\"></script>" // To keep connection with WebUI
89+
"</html>",
90+
++count
91+
);
92+
93+
// Generate header + body
94+
int body_size = strlen(body);
95+
sprintf(
96+
header_and_body,
97+
"HTTP/1.1 200 OK\r\n"
98+
"Content-Type: text/html\r\n"
99+
"Content-Length: %d\r\n\r\n"
100+
"%s",
101+
body_size, body
102+
);
103+
104+
// Free body buffer
105+
webui_free(body);
106+
107+
// Set len (optional)
108+
*length = strlen(header_and_body);
109+
110+
// By allocating resources using webui_malloc()
111+
// WebUI will automaticaly free the resources.
112+
return header_and_body;
113+
}
114+
115+
// Other files:
116+
// A NULL return will make WebUI
117+
// looks for the file locally.
118+
return NULL;
99119
}
100120

101121
int main() {
102122

103-
// Create new windows
104-
webui_new_window_id(MyWindow);
105-
webui_new_window_id(MySecondWindow);
123+
// Create new windows
124+
webui_new_window_id(MyWindow);
125+
webui_new_window_id(MySecondWindow);
106126

107-
// Bind HTML element IDs with a C functions
108-
webui_bind(MyWindow, "SwitchToSecondPage", switch_to_second_page);
109-
webui_bind(MyWindow, "OpenNewWindow", show_second_window);
110-
webui_bind(MyWindow, "Exit", exit_app);
111-
webui_bind(MySecondWindow, "Exit", exit_app);
127+
// Bind HTML element IDs with a C functions
128+
webui_bind(MyWindow, "SwitchToSecondPage", switch_to_second_page);
129+
webui_bind(MyWindow, "OpenNewWindow", show_second_window);
130+
webui_bind(MyWindow, "Exit", exit_app);
131+
webui_bind(MySecondWindow, "Exit", exit_app);
112132

113-
// Bind events
114-
webui_bind(MyWindow, "", events);
133+
// Bind events
134+
webui_bind(MyWindow, "", events);
115135

116-
// Set the `.ts` and `.js` runtime
117-
// webui_set_runtime(MyWindow, NodeJS);
118-
// webui_set_runtime(MyWindow, Bun);
119-
webui_set_runtime(MyWindow, Deno);
136+
// Set the `.ts` and `.js` runtime
137+
// webui_set_runtime(MyWindow, NodeJS);
138+
// webui_set_runtime(MyWindow, Bun);
139+
webui_set_runtime(MyWindow, Deno);
120140

121-
// Set a custom files handler
122-
webui_set_file_handler(MyWindow, my_files_handler);
141+
// Set a custom files handler
142+
webui_set_file_handler(MyWindow, my_files_handler);
123143

124-
// Set window size
125-
webui_set_size(MyWindow, 800, 800);
144+
// Set window size
145+
webui_set_size(MyWindow, 800, 800);
126146

127-
// Set window position
128-
webui_set_position(MyWindow, 200, 200);
147+
// Set window position
148+
webui_set_position(MyWindow, 200, 200);
129149

130-
// Show a new window
131-
// webui_set_root_folder(MyWindow, "_MY_PATH_HERE_");
132-
// webui_show_browser(MyWindow, "index.html", Chrome);
133-
webui_show(MyWindow, "index.html");
150+
// Show a new window
151+
// webui_set_root_folder(MyWindow, "_MY_PATH_HERE_");
152+
// webui_show_browser(MyWindow, "index.html", Chrome);
153+
webui_show(MyWindow, "index.html");
134154

135-
// Wait until all windows get closed
136-
webui_wait();
155+
// Wait until all windows get closed
156+
webui_wait();
137157

138-
// Free all memory resources (Optional)
139-
webui_clean();
158+
// Free all memory resources (Optional)
159+
webui_clean();
140160

141-
return 0;
161+
return 0;
142162
}
143163

144164
#if defined(_MSC_VER)

0 commit comments

Comments
 (0)