src/server/webdav/parser.c

changeset 107
7e81699d1f77
parent 106
b122f34ddc80
child 108
2a394ccdd778
equal deleted inserted replaced
106:b122f34ddc80 107:7e81699d1f77
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2013 Olaf Wintermann. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <unistd.h>
32
33 #include "parser.h"
34
35
36 void xmlerrorfnc(void * ctx, const char * msg, ... ) {
37 // nothing
38 }
39
40 static int xinit = 0;
41
42 PropfindRequest* dav_parse_propfind2(
43 Session *sn,
44 Request *rq,
45 char *xml,
46 size_t len)
47 {
48 if(!xinit) {
49 xmlGenericErrorFunc fnc = xmlerrorfnc;
50 initGenericErrorDefaultFunc(&fnc);
51 xinit = 1;
52 }
53
54 PropfindRequest *davrq = (PropfindRequest*)pool_calloc(
55 sn->pool,
56 1,
57 sizeof(PropfindRequest));
58 davrq->nsmap = xmlnsmap_create(sn->pool);
59 xmlnsmap_put(davrq->nsmap, (char*)"DAV:");
60 davrq->allprop = 0;
61 davrq->propname = 0;
62 davrq->prop = 0;
63 davrq->properties = NULL;
64 davrq->forbiddenProps = NULL;
65 davrq->notFoundProps = NULL;
66 davrq->mgrdata = NULL;
67
68
69 xmlTextReaderPtr reader = xmlReaderForMemory(xml, len, NULL, NULL, 0);
70 int r = 0;
71
72 if(!reader) {
73 fprintf(stderr, "Cannot create xmlReader\n");
74 return davrq;
75 }
76
77 PropfindParser parser;
78 parser.pool = sn->pool;
79 parser.rq = davrq;
80 parser.davPropTag = 0;
81
82 while((r = xmlTextReaderRead(reader)) == 1) {
83 int nodetype = xmlTextReaderNodeType(reader);
84 if(nodetype == XML_READER_TYPE_ELEMENT) {
85 if(propfind_begin_elm(&parser, reader)) {
86 fprintf(stderr, "propfind xml error\n");
87 break;
88 }
89 }
90 }
91
92 return davrq;
93 }
94
95 int propfind_begin_elm(PropfindParser *p, xmlTextReaderPtr elm) {
96 pool_handle_t *pool = p->pool;
97 const xmlChar *ns = xmlTextReaderConstNamespaceUri(elm);
98 const xmlChar *name = xmlTextReaderConstLocalName(elm);
99 int depth = xmlTextReaderDepth(elm);
100
101 // check namespace
102 int dav_ns = 0;
103 if(ns) {
104 dav_ns = xstreq(ns, "DAV:") ? 1 : 0;
105 }
106
107 if(dav_ns && xstreq(name, "allprop") && depth == 1) {
108 p->rq->allprop = 1;
109 } else if(dav_ns && xstreq(name, "prop") && depth == 1) {
110 p->davPropTag = 1;
111 } else if(p->davPropTag && !p->rq->allprop && depth == 2) {
112 DavProperty *property = pool_malloc(pool, sizeof(DavProperty));
113 property->xmlns = xmlnsmap_put(p->rq->nsmap, (char*)ns);
114 property->name = pool_strdup(pool, (const char*)name);
115
116 // add property to DavRequest
117 UcxList *elm = pool_malloc(pool, sizeof(UcxList));
118 elm->prev = NULL;
119 elm->next = NULL;
120 elm->data = property;
121 p->rq->properties = ucx_list_concat(p->rq->properties, elm);
122 }
123
124 return 0;
125 }
126

mercurial