src/server/webdav/parser.c

Sat, 17 Oct 2015 23:05:23 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sat, 17 Oct 2015 23:05:23 +0200
changeset 103
d3b514e2ddbd
parent 91
fac51f87def0
permissions
-rw-r--r--

added date header to response

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

mercurial