#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); char *wwwdx = NULL; if (fd < 0) { size_t psz = strlen(path); wwwdx = malloc(psz + 4 + 5 + 1); strcpy(wwwdx, "www/"); strcpy(wwwdx + 4, path); strcpy(wwwdx + 4 + psz, ".html"); fd = open(wwwdx, O_RDONLY); path = 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)); if (wwwdx) free(wwwdx); return res; }