#include <ucx/string.h>
#include <ucx/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) {
sstr_t path = sstr(pblock_findkeyval(pb_key_path, rq->vars));
sstr_t ct;
if(path.ptr[path.length -
1] ==
'/') {
ct = sstrn(
"internal/directory",
18);
}
else {
sstr_t 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 = ucx_map_sstr_get(config->mimetypes->map, ext);
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;
}