src/server/webdav/parser.c

Sat, 31 Oct 2015 15:01:07 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sat, 31 Oct 2015 15:01:07 +0100
changeset 106
b122f34ddc80
parent 91
fac51f87def0
permissions
-rw-r--r--

added minimal ssl support

/*
 * 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;
    parser.pool = sn->pool;
    parser.rq = davrq;
    parser.davPropTag = 0;
    
    while((r = xmlTextReaderRead(reader)) == 1) {
         int nodetype = xmlTextReaderNodeType(reader);
         if(nodetype == XML_READER_TYPE_ELEMENT) {
             if(propfind_begin_elm(&parser, reader)) {
                 fprintf(stderr, "propfind xml error\n");
                 break;
             }
         }
    }
    
    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);
    int depth = xmlTextReaderDepth(elm);
    
    // check namespace
    int dav_ns = 0;
    if(ns) {
        dav_ns = xstreq(ns, "DAV:") ? 1 : 0;
    }
    
    if(dav_ns && xstreq(name, "allprop") && depth == 1) {
        p->rq->allprop = 1;
    } else if(dav_ns && xstreq(name, "prop") && depth == 1) {
        p->davPropTag = 1;
    } else if(p->davPropTag && !p->rq->allprop && depth == 2) {
        DavProperty *property = pool_malloc(pool, sizeof(DavProperty));
        property->xmlns = xmlnsmap_put(p->rq->nsmap, (char*)ns);
        property->name = pool_strdup(pool, (const char*)name);
        
        // add property to DavRequest
        UcxList *elm = pool_malloc(pool, sizeof(UcxList));
        elm->prev = NULL;
        elm->next = NULL;
        elm->data = property;
        p->rq->properties = ucx_list_concat(p->rq->properties, elm);
    }
    
    return 0;
}

mercurial