|
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 = pool_malloc(sn->pool, sizeof(PropfindParser)); |
|
78 parser->pool = sn->pool; |
|
79 parser->rq = davrq; |
|
80 parser->property = NULL; |
|
81 parser->davPropTag = 0; |
|
82 |
|
83 while((r = xmlTextReaderRead(reader)) == 1) { |
|
84 int nodetype = xmlTextReaderNodeType(reader); |
|
85 const xmlChar *name = xmlTextReaderConstLocalName(reader); |
|
86 if(nodetype == XML_READER_TYPE_ELEMENT) { |
|
87 if(propfind_begin_elm(parser, reader)) { |
|
88 fprintf(stderr, "propfind xml error\n"); |
|
89 break; |
|
90 } |
|
91 } else if(nodetype == XML_READER_TYPE_END_ELEMENT) { |
|
92 if(propfind_end_elm(parser, reader)) { |
|
93 fprintf(stderr, "propfind xml error\n"); |
|
94 break; |
|
95 } |
|
96 } |
|
97 } |
|
98 |
|
99 pool_free(sn->pool, parser); |
|
100 |
|
101 return davrq; |
|
102 } |
|
103 |
|
104 int propfind_begin_elm(PropfindParser *p, xmlTextReaderPtr elm) { |
|
105 pool_handle_t *pool = p->pool; |
|
106 const xmlChar *ns = xmlTextReaderConstNamespaceUri(elm); |
|
107 const xmlChar *name = xmlTextReaderConstLocalName(elm); |
|
108 |
|
109 if(ns == NULL) { |
|
110 fprintf(stderr, "ERROR: namespace is null!\n"); |
|
111 return -1; |
|
112 } |
|
113 |
|
114 int dav_ns = xstreq(ns, "DAV:") ? 1 : 0; |
|
115 |
|
116 if(dav_ns && xstreq(name, "allprop")) { |
|
117 p->rq->allprop = 1; |
|
118 } else if(dav_ns && xstreq(name, "prop")) { |
|
119 p->davPropTag = 1; |
|
120 } else if(p->davPropTag && !p->property && !p->rq->allprop) { |
|
121 DavProperty *property = pool_malloc(pool, sizeof(DavProperty)); |
|
122 size_t nslen = strlen(ns); |
|
123 size_t namelen = strlen(name); |
|
124 if(nslen > 0) { |
|
125 property->xmlns = xmlnsmap_put(p->rq->nsmap, (char*)ns); |
|
126 } else { |
|
127 property->xmlns = NULL; |
|
128 } |
|
129 |
|
130 property->name = pool_malloc(pool, namelen + 1); |
|
131 property->name[namelen] = 0; |
|
132 memcpy(property->name, name, namelen); |
|
133 |
|
134 // add property to DavRequest |
|
135 UcxDlist *elm = pool_malloc(pool, sizeof(UcxDlist)); |
|
136 elm->prev = NULL; |
|
137 elm->next = NULL; |
|
138 elm->data = property; |
|
139 p->rq->properties = ucx_dlist_concat(p->rq->properties, elm); |
|
140 } |
|
141 |
|
142 return 0; |
|
143 } |
|
144 |
|
145 int propfind_end_elm(PropfindParser *p, xmlTextReaderPtr elm) { |
|
146 pool_handle_t *pool = p->pool; |
|
147 const xmlChar *ns = xmlTextReaderConstNamespaceUri(elm); |
|
148 const xmlChar *name = xmlTextReaderConstLocalName(elm); |
|
149 |
|
150 if(ns == NULL) { |
|
151 fprintf(stderr, "ERROR: namespace is null!\n"); |
|
152 return -1; |
|
153 } |
|
154 |
|
155 // nothing here yet |
|
156 |
|
157 return 0; |
|
158 } |