src/server/webdav/parser.c

Tue, 09 Jul 2013 20:56:01 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Tue, 09 Jul 2013 20:56:01 +0200
changeset 86
49bb6c8ceb2b
parent 85
b62e77d8e80c
child 87
bdec069d2239
permissions
-rw-r--r--

replaced usage of deprecated openldap functions

/*
 * 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 <unistd.h>

#include "parser.h"


void xmlerrorfnc(void * ctx, const char * msg, ... ) {
    // nothing
}

static int xinit = 0;

PropfindRequest* dav_parse_propfind2(
        Session *sn,
        Request *rq,
        char *xml,
        size_t len)
{
    if(!xinit) {
        xmlGenericErrorFunc fnc = xmlerrorfnc;
        initGenericErrorDefaultFunc(&fnc);
        xinit = 1;
    }
    
    PropfindRequest *davrq = (PropfindRequest*)pool_calloc(
            sn->pool,
            1,
            sizeof(PropfindRequest));
    davrq->nsmap = xmlnsmap_create(sn->pool);
    xmlnsmap_put(davrq->nsmap, (char*)"DAV:");
    davrq->allprop = 0;
    davrq->propname = 0;
    davrq->prop = 0;
    davrq->properties = NULL;
    davrq->forbiddenProps = NULL;
    davrq->notFoundProps = NULL;
    davrq->mgrdata = NULL;
    
    
    xmlTextReaderPtr reader = xmlReaderForMemory(xml, len, NULL, NULL, 0);
    int r = 0;
    
    if(!reader) {
        fprintf(stderr, "Cannot create xmlReader\n");
        return davrq;
    }
    
    PropfindParser *parser = pool_malloc(sn->pool, sizeof(PropfindParser));
    parser->pool = sn->pool;
    parser->rq = davrq;
    parser->property = NULL;
    parser->davPropTag = 0;
    
    while((r = xmlTextReaderRead(reader)) == 1) {
         int nodetype = xmlTextReaderNodeType(reader);
         const xmlChar *name = xmlTextReaderConstLocalName(reader);
         if(nodetype == XML_READER_TYPE_ELEMENT) {
             if(propfind_begin_elm(parser, reader)) {
                 fprintf(stderr, "propfind xml error\n");
                 break;
             }
         } else if(nodetype == XML_READER_TYPE_END_ELEMENT) {
             if(propfind_end_elm(parser, reader)) {
                 fprintf(stderr, "propfind xml error\n");
                 break;
             }
         }
    }
    
    pool_free(sn->pool, parser);
    
    return davrq;
}

int propfind_begin_elm(PropfindParser *p, xmlTextReaderPtr elm) {
    pool_handle_t *pool = p->pool;
    const xmlChar *ns = xmlTextReaderConstNamespaceUri(elm);
    const xmlChar *name = xmlTextReaderConstLocalName(elm);
    
    if(ns == NULL) {
        fprintf(stderr, "ERROR: namespace is null!\n");
        return -1;
    }
    
    int dav_ns = xstreq(ns, "DAV:") ? 1 : 0;
    
    if(dav_ns && xstreq(name, "allprop")) {
        p->rq->allprop = 1;
    } else if(dav_ns && xstreq(name, "prop")) {
        p->davPropTag = 1;
    } else if(p->davPropTag && !p->property && !p->rq->allprop) {
        DavProperty *property = pool_malloc(pool, sizeof(DavProperty));
        size_t nslen = strlen(ns);
        size_t namelen = strlen(name);
        if(nslen > 0) {
            property->xmlns = xmlnsmap_put(p->rq->nsmap, (char*)ns);
        } else {
            property->xmlns = NULL;
        }

        property->name = pool_malloc(pool, namelen + 1);
        property->name[namelen] = 0;
        memcpy(property->name, name, namelen);
        
        // add property to DavRequest
        UcxDlist *elm = pool_malloc(pool, sizeof(UcxDlist));
        elm->prev = NULL;
        elm->next = NULL;
        elm->data = property;
        p->rq->properties = ucx_dlist_concat(p->rq->properties, elm);
    }
    
    return 0;
}

int propfind_end_elm(PropfindParser *p, xmlTextReaderPtr elm) {
    pool_handle_t *pool = p->pool;
    const xmlChar *ns = xmlTextReaderConstNamespaceUri(elm);
    const xmlChar *name = xmlTextReaderConstLocalName(elm);
    
    if(ns == NULL) {
        fprintf(stderr, "ERROR: namespace is null!\n");
        return -1;
    }
    
    // nothing here yet
    
    return 0;
}

mercurial