src/server/config/mimeconf.c

changeset 39
de4bc3cd2d36
child 44
3da1f7b6847f
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/server/config/mimeconf.c	Sun Dec 30 15:49:44 2012 +0100
@@ -0,0 +1,97 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2012 Olaf Wintermann. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "mimeconf.h"
+
+MimeConfig *load_mime_config(char *file) {
+    FILE *in = fopen(file, "r");
+    if(in == NULL) {
+        return NULL;
+    }
+    
+    MimeConfig *conf = malloc(sizeof(MimeConfig));
+    conf->parser.parse = mimeconf_parse;
+    conf->file = file;
+    conf->directives = NULL;
+    conf->ntypes = 0;
+
+    int r = cfg_parse_basic_file((ConfigParser*)conf, in);
+    if(r != 0) {
+        // TODO: free
+        return NULL;
+    }
+
+    return conf;
+}
+
+void free_mime_config(MimeConfig *conf) {
+    if(conf->directives != NULL) {
+        ucx_list_free(conf->directives);
+    }
+    if(conf->parser.lines != NULL) {
+        ucx_dlist_free(conf->parser.lines);
+    }
+    ucx_mempool_free(conf->parser.mp);
+    free(conf);
+}
+
+int mimeconf_parse(void *p, ConfigLine *begin, ConfigLine *end, sstr_t line) {
+    MimeConfig *conf = p;
+    UcxMempool *mp = conf->parser.mp;
+    
+    // parse mime directive
+    MimeDirective *dir = ucx_mempool_calloc(mp, 1, sizeof(MimeDirective));
+    
+    UcxList *params = cfg_param_list(line, mp);
+    UCX_FOREACH(UcxList*, params, pl) {
+        ConfigParam *param = pl->data;
+        
+        if(!sstrcmp(param->name, sstr("type"))) {
+            dir->type = param->value;
+        } else if(!sstrcmp(param->name, sstr("exts"))) {
+            // comma-separated file extensions
+            
+            size_t nx = 0;
+            sstr_t *exts = sstrsplit(param->value, sstrn(",", 1), &nx);
+            for(int i=0;i<nx;i++) {
+                dir->exts = ucx_list_append(dir->exts, exts[i].ptr);
+            }
+            
+            free(exts);
+        }
+    }
+    
+    conf->directives = ucx_list_append(conf->directives, dir);
+    conf->ntypes++;
+
+    return 0;
+}

mercurial