#include <cx/string.h>
#include <cx/map.h>
#include "objecttype.h"
#include "../util/pblock.h"
#include "../daemon/config.h"
#include "../daemon/session.h"
int object_type_by_extension(pblock *pb, Session *sn, Request *rq) {
cxstring path = cx_str(pblock_findkeyval(pb_key_path, rq->vars));
cxstring ct;
if(path.ptr[path.length -
1] ==
'/') {
ct = (cxstring)
CX_STR(
OBJTYPE_INTERNAL_DIRECTORY);
}
else {
cxstring ext;
ext.length =
0;
for(
int i=path.length -
1;i>=
0;i--) {
if(path.ptr[i] ==
'.') {
ext.ptr = path.ptr + i +
1;
ext.length = path.length - i -
1;
break;
}
}
if(ext.length ==
0) {
return REQ_NOACTION;
}
ServerConfiguration *config = session_get_config(sn);
WS_ASSERT(config);
WS_ASSERT(config->mimetypes);
WS_ASSERT(config->mimetypes->map);
char *type = cxMapGet(config->mimetypes->map, cx_hash_key_bytes((
const unsigned char*)ext.ptr, ext.length));
if(!type) {
return REQ_NOACTION;
}
ct.ptr = type;
ct.length = strlen(type);
}
pblock_kvinsert(pb_key_content_type, ct.ptr, ct.length, rq->srvhdrs);
return REQ_PROCEED;
}
int object_type_force_type(pblock *pb, Session *sn, Request *rq) {
char *type = pblock_findkeyval(pb_key_type, pb);
if(!type) {
return REQ_NOACTION;
}
char *ct = pblock_findkeyval(pb_key_content_type, rq->srvhdrs);
if(ct) {
return REQ_NOACTION;
}
pblock_kvinsert(pb_key_content_type, type, strlen(type), rq->srvhdrs);
return REQ_PROCEED;
}