1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
#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);
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;
}
|