aboutsummaryrefslogtreecommitdiffstats
path: root/src/common.c
diff options
context:
space:
mode:
authorjustanothercatgirl <sotov@twistea.su>2025-03-27 12:52:11 +0300
committerjustanothercatgirl <sotov@twistea.su>2025-03-27 12:52:11 +0300
commit82742d5d13dc7b0691a79c79f8e62782fcb16e10 (patch)
tree48d077549ed667d2a54171b82eb7608cb8edaf3e /src/common.c
parent8542ec17d3df989f3df9fd03af7a447bf730dc13 (diff)
Doing SQL (work in progress)
Diffstat (limited to 'src/common.c')
-rw-r--r--src/common.c40
1 files changed, 40 insertions, 0 deletions
diff --git a/src/common.c b/src/common.c
new file mode 100644
index 0000000..7d8d836
--- /dev/null
+++ b/src/common.c
@@ -0,0 +1,40 @@
+#include "common.h"
+#include "mime.h"
+
+#include <log.h>
+
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+
+char *duplicate(const char* x) {
+ size_t len = strlen(x);
+ char* ret = malloc(len+1);
+ memcpy(ret, x, len);
+ ret[len] = '\0';
+ return ret;
+}
+// either get a file in a directory %URL or www/%URL.html
+struct MHD_Response *get_from_file(const char* path) {
+ int fd = open(path, O_RDONLY);
+ if (fd < 0) {
+ size_t psz = strlen(path);
+ char *wwwdx = malloc(psz + 4 + 5 + 1);
+ strcpy(wwwdx, "www/");
+ strcpy(wwwdx + 4, path);
+ strcpy(wwwdx + 4 + psz, ".html");
+ fd = open(wwwdx, O_RDONLY);
+ free(wwwdx);
+ if (fd < 0) return NULL;
+ }
+ struct stat st;
+ if (fstat(fd, &st) < 0) return NULL;
+ struct MHD_Response *res = MHD_create_response_from_fd(st.st_size, fd);
+ size_t len, lastdot;
+ for (len = lastdot = 0; path[len]; ++len)
+ if (len[path] == '.') lastdot = len;
+ if (lastdot++)
+ MHD_add_response_header(res, "Content-Type", mimetype_get(path + lastdot, len - lastdot));
+ LDEBUGF("mimetype = %s\n", mimetype_get(path + lastdot, len - lastdot));
+ return res;
+}