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 #include <cx/string.h>
30 #include <cx/map.h>
31
32 #include "objecttype.h"
33 #include "../util/pblock.h"
34
35 #include "../daemon/config.h"
36 #include "../daemon/session.h"
37
38 int object_type_by_extension(pblock *pb, Session *sn, Request *rq) {
39 cxstring path = cx_str(pblock_findkeyval(pb_key_path, rq->vars));
40
41
42 cxstring ct;
43 if(path.ptr[path.length -
1] ==
'/') {
44
45 ct = (cxstring)
CX_STR(
OBJTYPE_INTERNAL_DIRECTORY);
46 }
else {
47 cxstring ext;
48 ext.length =
0;
49 for(
int i=path.length -
1;i>=
0;i--) {
50 if(path.ptr[i] ==
'.') {
51 ext.ptr = path.ptr + i +
1;
52 ext.length = path.length - i -
1;
53 break;
54 }
55 }
56
57 if(ext.length ==
0) {
58
59 return REQ_NOACTION;
60 }
61
62
63 ServerConfiguration *config = session_get_config(sn);
64
65 WS_ASSERT(config);
66 WS_ASSERT(config->mimetypes);
67 WS_ASSERT(config->mimetypes->map);
68
69 char *type = cxMapGet(config->mimetypes->map, cx_hash_key_bytes((
const unsigned char*)ext.ptr, ext.length));
70
71 if(!type) {
72 return REQ_NOACTION;
73 }
74
75 ct.ptr = type;
76 ct.length = strlen(type);
77 }
78
79 pblock_kvinsert(pb_key_content_type, ct.ptr, ct.length, rq->srvhdrs);
80 return REQ_PROCEED;
81 }
82
83 int object_type_force_type(pblock *pb, Session *sn, Request *rq) {
84 char *type = pblock_findkeyval(pb_key_type, pb);
85
86 if(!type) {
87 return REQ_NOACTION;
88 }
89
90 char *ct = pblock_findkeyval(pb_key_content_type, rq->srvhdrs);
91 if(ct) {
92 return REQ_NOACTION;
93 }
94
95 pblock_kvinsert(pb_key_content_type, type, strlen(type), rq->srvhdrs);
96
97 return REQ_PROCEED;
98 }
99