src/server/config/mimeconf.c

Mon, 06 Mar 2017 17:32:26 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Mon, 06 Mar 2017 17:32:26 +0100
changeset 179
ef6827505bd2
parent 101
7fbcdbad0baa
child 255
b5d15a4a19f5
permissions
-rw-r--r--

merge srvctrl into default branch

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2013 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 <ucx/mempool.h>
#include "mimeconf.h"

typedef struct {
    ucx_destructor destructor;
    void           *ptr;
} ucx_regdestr;

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) {
        fclose(in);
        free(conf);
        // TODO: free
        return NULL;
    }
    
    fclose(in);

    return conf;
}

void free_mime_config(MimeConfig *conf) {
    ucx_mempool_destroy(conf->parser.mp->pool);
    free(conf);
}

int mimeconf_parse(void *p, ConfigLine *begin, ConfigLine *end, sstr_t line) {
    MimeConfig *conf = p;
    UcxAllocator *mp = conf->parser.mp;
    
    // parse mime directive
    MimeDirective *dir = OBJ_NEW_N(mp, MimeDirective);
    
    UcxList *params = cfg_param_list(line, mp);
    UCX_FOREACH(pl, params) {
        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
            
            ssize_t nx = 0;
            sstr_t *exts = sstrsplit(param->value, sstrn(",", 1), &nx);
            for(int i=0;i<nx;i++) {
                sstr_t extstr = sstrdup_a(mp, exts[i]);
                dir->exts = ucx_list_append_a(mp, dir->exts, extstr.ptr);
                free(exts[i].ptr);
            }
            free(exts);
        }
    }
    
    conf->directives = ucx_list_append_a(mp, conf->directives, dir);
    conf->ntypes++;

    return 0;
}

mercurial