From 82742d5d13dc7b0691a79c79f8e62782fcb16e10 Mon Sep 17 00:00:00 2001 From: justanothercatgirl Date: Thu, 27 Mar 2025 12:52:11 +0300 Subject: Doing SQL (work in progress) --- src/common.c | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 src/common.c (limited to 'src/common.c') 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 + +#include +#include +#include + +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; +} -- cgit v1.2.3-70-g09d2