28 |
28 |
29 #include <stdio.h> |
29 #include <stdio.h> |
30 #include <stdlib.h> |
30 #include <stdlib.h> |
31 #include <string.h> |
31 #include <string.h> |
32 |
32 |
33 #include <ucx/mempool.h> |
33 #include <cx/mempool.h> |
34 #include "mimeconf.h" |
34 #include "mimeconf.h" |
35 |
35 |
36 typedef struct { |
36 #define MIMECONFIG_MAX_TOKENS 4096 |
37 ucx_destructor destructor; |
|
38 void *ptr; |
|
39 } ucx_regdestr; |
|
40 |
37 |
41 MimeConfig *load_mime_config(const char *file) { |
38 MimeConfig *load_mime_config(const char *file) { |
42 FILE *in = fopen(file, "r"); |
39 FILE *in = fopen(file, "r"); |
43 if(in == NULL) { |
40 if(in == NULL) { |
44 log_ereport(LOG_FAILURE, "Cannot load mime config file %s: %s", file, strerror(errno)); |
41 log_ereport(LOG_FAILURE, "Cannot load mime config file %s: %s", file, strerror(errno)); |
45 return NULL; |
42 return NULL; |
46 } |
43 } |
47 |
44 |
48 MimeConfig *conf = malloc(sizeof(MimeConfig)); |
45 MimeConfig *conf = malloc(sizeof(MimeConfig)); |
49 conf->parser.parse = mimeconf_parse; |
46 conf->parser.parse = mimeconf_parse; |
50 conf->directives = NULL; |
47 conf->directives_begin = NULL; |
|
48 conf->directives_end = NULL; |
51 conf->ntypes = 0; |
49 conf->ntypes = 0; |
52 int r = cfg_parse_basic_file((ConfigParser*)conf, in); |
50 int r = cfg_parse_basic_file((ConfigParser*)conf, in); |
53 if(r != 0) { |
51 if(r != 0) { |
54 log_ereport(LOG_FAILURE, "Cannot parse mime config file %s", file); |
52 log_ereport(LOG_FAILURE, "Cannot parse mime config file %s", file); |
55 fclose(in); |
53 fclose(in); |
62 |
60 |
63 return conf; |
61 return conf; |
64 } |
62 } |
65 |
63 |
66 void free_mime_config(MimeConfig *conf) { |
64 void free_mime_config(MimeConfig *conf) { |
67 ucx_mempool_destroy(conf->parser.mp->pool); |
65 //ucx_mempool_destroy(conf->parser.mp->pool); |
68 free(conf); |
66 free(conf); |
69 } |
67 } |
70 |
68 |
71 int mimeconf_parse(void *p, ConfigLine *begin, ConfigLine *end, sstr_t line) { |
69 int mimeconf_parse(void *p, ConfigLine *begin, ConfigLine *end, cxmutstr line) { |
72 MimeConfig *conf = p; |
70 MimeConfig *conf = p; |
73 UcxAllocator *mp = conf->parser.mp; |
71 CxAllocator *mp = conf->parser.mp; |
74 |
72 |
75 // parse mime directive |
73 // parse mime directive |
76 MimeDirective *dir = OBJ_NEW_N(mp, MimeDirective); |
74 MimeDirective *dir = OBJ_NEW_N(mp, MimeDirective); |
77 |
75 |
78 UcxList *params = cfg_param_list(line, mp); |
76 ConfigParam *params = cfg_param_list(line, mp); |
79 UCX_FOREACH(pl, params) { |
77 for(ConfigParam *param=params;param;param=param->next) { |
80 ConfigParam *param = pl->data; |
78 if(!cx_strcmp(cx_strcast(param->name), cx_str("type"))) { |
81 |
|
82 if(!sstrcmp(param->name, sstr("type"))) { |
|
83 dir->type = param->value; |
79 dir->type = param->value; |
84 } else if(!sstrcmp(param->name, sstr("exts"))) { |
80 } else if(!cx_strcmp(cx_strcast(param->name), cx_str("exts"))) { |
85 // comma-separated file extensions |
81 // comma-separated file extensions |
86 |
82 dir->nextensions = cx_strsplit_a(mp, cx_strcast(param->value), cx_strn(",", 1), MIMECONFIG_MAX_TOKENS, &dir->extensions); |
87 ssize_t nx = 0; |
|
88 sstr_t *exts = sstrsplit(param->value, sstrn(",", 1), &nx); |
|
89 for(int i=0;i<nx;i++) { |
|
90 sstr_t extstr = sstrdup_a(mp, exts[i]); |
|
91 dir->exts = ucx_list_append_a(mp, dir->exts, extstr.ptr); |
|
92 free(exts[i].ptr); |
|
93 } |
|
94 free(exts); |
|
95 } |
83 } |
96 } |
84 } |
97 |
85 |
98 conf->directives = ucx_list_append_a(mp, conf->directives, dir); |
86 CFG_MIME_ADD(&conf->directives_begin, &conf->directives_end, dir); |
|
87 |
99 conf->ntypes++; |
88 conf->ntypes++; |
100 |
89 |
101 return 0; |
90 return 0; |
102 } |
91 } |