#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cx/mempool.h>
#include "mimeconf.h"
#include "logging.h"
#define MIMECONFIG_MAX_TOKENS 4096
MimeConfig *load_mime_config(
const char *file) {
FILE *in = fopen(file,
"r");
if(in ==
NULL) {
ws_cfg_log(
LOG_FAILURE,
"Cannot load mime config file %s: %s", file, strerror(errno));
return NULL;
}
MimeConfig *conf = malloc(
sizeof(MimeConfig));
conf->parser.parse = mimeconf_parse;
conf->directives_begin =
NULL;
conf->directives_end =
NULL;
conf->ntypes =
0;
int r = cfg_parse_basic_file((ConfigParser*)conf, in);
if(r !=
0) {
ws_cfg_log(
LOG_FAILURE,
"Cannot parse mime config file %s", file);
fclose(in);
free(conf);
return NULL;
}
fclose(in);
return conf;
}
void free_mime_config(MimeConfig *conf) {
cxMempoolDestroy(conf->parser.mp->data);
free(conf);
}
int mimeconf_parse(
void *p, ConfigLine *begin, ConfigLine *end, cxmutstr line) {
MimeConfig *conf = p;
CxAllocator *mp = conf->parser.mp;
MimeDirective *dir =
OBJ_NEW_N(mp, MimeDirective);
ConfigParam *params = cfg_param_list(line, mp);
for(ConfigParam *param=params;param;param=param->next) {
if(!cx_strcmp(cx_strcast(param->name), cx_str(
"type"))) {
dir->type = param->value;
}
else if(!cx_strcmp(cx_strcast(param->name), cx_str(
"exts"))) {
dir->nextensions = cx_strsplit_a(mp, cx_strcast(param->value), cx_strn(
",",
1),
MIMECONFIG_MAX_TOKENS, &dir->extensions);
}
}
CFG_MIME_ADD(&conf->directives_begin, &conf->directives_end, dir);
conf->ntypes++;
return 0;
}