replaced propfind parser with new libxml2 parser

Tue, 09 Jul 2013 17:16:26 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Tue, 09 Jul 2013 17:16:26 +0200
changeset 85
b62e77d8e80c
parent 84
afd57ce39ec9
child 86
49bb6c8ceb2b

replaced propfind parser with new libxml2 parser

src/server/daemon/config.c file | annotate | diff | comparison | revisions
src/server/daemon/configmanager.c file | annotate | diff | comparison | revisions
src/server/daemon/configmanager.h file | annotate | diff | comparison | revisions
src/server/daemon/webserver.c file | annotate | diff | comparison | revisions
src/server/webdav/objs.mk file | annotate | diff | comparison | revisions
src/server/webdav/parser.c file | annotate | diff | comparison | revisions
src/server/webdav/parser.h file | annotate | diff | comparison | revisions
src/server/webdav/webdav.c file | annotate | diff | comparison | revisions
--- a/src/server/daemon/config.c	Mon Jul 08 11:10:54 2013 +0200
+++ b/src/server/daemon/config.c	Tue Jul 09 17:16:26 2013 +0200
@@ -62,11 +62,11 @@
     printf("load_init_conf\n");
 
     InitConfig *cfg = load_init_config(file);
-    UcxMempool *mp = cfg->parser.mp;
     if(cfg == NULL) {
         fprintf(stderr, "Cannot load init.conf\n");
         return 1;
     }
+    UcxMempool *mp = cfg->parser.mp;
 
     cfg_pool = pool_create(); // one pool for one Configuration
     UcxDlist *dirs = cfg->directives;
--- a/src/server/daemon/configmanager.c	Mon Jul 08 11:10:54 2013 +0200
+++ b/src/server/daemon/configmanager.c	Tue Jul 09 17:16:26 2013 +0200
@@ -90,7 +90,7 @@
     return 0;
 }
 
-int cfgmgr_load_config() {
+int cfgmgr_load_config(ServerConfiguration **set_cfg) {
     int cfgreload = 0;
     
     /* check config files */  
@@ -126,6 +126,7 @@
         config = current_config;
     }
     
+    *set_cfg = config;
     ServerConfiguration *old_conf = NULL;
     if(current_config != config) {
         old_conf = current_config;
--- a/src/server/daemon/configmanager.h	Mon Jul 08 11:10:54 2013 +0200
+++ b/src/server/daemon/configmanager.h	Tue Jul 09 17:16:26 2013 +0200
@@ -50,7 +50,7 @@
 void cfgmgr_attach_file(ConfigFile *cf);
 ConfigFile* cfgmgr_get_file(sstr_t name);
 int cfgmgr_reload_file(ConfigFile *f, ServerConfiguration *conf, int *reload);
-int cfgmgr_load_config();
+int cfgmgr_load_config(ServerConfiguration **cfg);
 
 ServerConfiguration* cfgmgr_get_server_config();
 
--- a/src/server/daemon/webserver.c	Mon Jul 08 11:10:54 2013 +0200
+++ b/src/server/daemon/webserver.c	Tue Jul 09 17:16:26 2013 +0200
@@ -71,7 +71,8 @@
 
     // load server.conf
     init_configuration_manager();
-    if(cfgmgr_load_config() != 0) {
+    ServerConfiguration *cfg;
+    if(cfgmgr_load_config(&cfg) != 0) {
         fprintf(stderr, "Cannot load configuration\n");
         return -1;
     }
@@ -80,7 +81,6 @@
     auth_cache_init();
     
     // create tmp dir and pid file
-    ServerConfiguration *cfg = cfgmgr_get_server_config();
     char *mkdir_cmd = NULL;
     asprintf(&mkdir_cmd, "mkdir -p %s", cfg->tmp.ptr);
     system(mkdir_cmd);
--- a/src/server/webdav/objs.mk	Mon Jul 08 11:10:54 2013 +0200
+++ b/src/server/webdav/objs.mk	Tue Jul 09 17:16:26 2013 +0200
@@ -32,6 +32,7 @@
 
 DAVOBJ = webdav.o
 DAVOBJ += persistence.o
+DAVOBJ += parser.o
 
 DAVOBJS = $(DAVOBJ:%=$(DAV_OBJPRE)%)
 DAVSOURCE = $(DAVOBJ:%.o=webdav/%.c)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/server/webdav/parser.c	Tue Jul 09 17:16:26 2013 +0200
@@ -0,0 +1,158 @@
+/*
+ * 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;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/server/webdav/parser.h	Tue Jul 09 17:16:26 2013 +0200
@@ -0,0 +1,66 @@
+/*
+ * 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.
+ */
+
+#ifndef PARSER_H
+#define	PARSER_H
+
+#include <libxml/parser.h>
+#include <libxml/xmlreader.h>
+
+#include "webdav.h"
+
+#ifdef	__cplusplus
+extern "C" {
+#endif
+
+#define xstreq(a,b) xmlStrEqual(BAD_CAST a, BAD_CAST b)
+
+typedef struct PropfindParser {
+    PropfindRequest *rq;
+    pool_handle_t   *pool;
+    DavProperty     *property;
+    int             davPropTag;
+} PropfindParser;
+    
+    
+PropfindRequest* dav_parse_propfind2(
+        Session *sn,
+        Request *rq,
+        char *xml,
+        size_t len);
+
+int propfind_begin_elm(PropfindParser *parser, xmlTextReaderPtr elm);
+int propfind_end_elm(PropfindParser *parser, xmlTextReaderPtr elm);
+
+
+#ifdef	__cplusplus
+}
+#endif
+
+#endif	/* PARSER_H */
+
--- a/src/server/webdav/webdav.c	Mon Jul 08 11:10:54 2013 +0200
+++ b/src/server/webdav/webdav.c	Tue Jul 09 17:16:26 2013 +0200
@@ -41,6 +41,7 @@
 #include "../daemon/protocol.h"
 
 #include "davparser.h"
+#include "parser.h"
 #include "persistence.h"
 
 static UcxMap *pmgr_map; // char*, PersistenceManager
@@ -252,7 +253,7 @@
      */
     DavCollection *collection = rq->davCollection;
     
-    PropfindRequest *davrq = dav_parse_propfind(sn, rq, xml_body, xml_len);
+    PropfindRequest *davrq = dav_parse_propfind2(sn, rq, xml_body, xml_len);  
     davrq->sn = sn;
     davrq->rq = rq;
     davrq->out = sbuf_new(512);

mercurial