dav/propfind.c

changeset 1
1bcaac272cdf
child 3
323689ada09d
equal deleted inserted replaced
0:0f94d369bb02 1:1bcaac272cdf
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2012 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 <string.h>
32
33 #include "utils.h"
34 #include "propfind.h"
35
36 Propfind* dav_propfind(CURL *handle) {
37 Propfind *response = malloc(sizeof(Propfind));
38 response->resource = NULL;
39 response->children = NULL;
40
41 curl_easy_setopt(handle, CURLOPT_CUSTOMREQUEST, "PROPFIND");
42
43 struct curl_slist *headers = NULL;
44 headers = curl_slist_append(headers, "Content-Type: text/xml");
45 curl_easy_setopt(handle, CURLOPT_HTTPHEADER, headers);
46
47 UcxBuffer *rqbuf = create_propfind_request_body();
48 UcxBuffer *rpbuf = ucx_buffer_new(NULL, 4096, UCX_BUFFER_AUTOEXTEND);
49 //printf("request\n%s\n", body->space);
50
51 curl_easy_setopt(handle, CURLOPT_UPLOAD, 1);
52 curl_easy_setopt(handle, CURLOPT_READFUNCTION, ucx_buffer_read);
53 curl_easy_setopt(handle, CURLOPT_READDATA, rqbuf);
54 curl_easy_setopt(handle, CURLOPT_INFILESIZE, rqbuf->size);
55
56 curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, ucx_buffer_write);
57 curl_easy_setopt(handle, CURLOPT_WRITEDATA, rpbuf);
58
59 ucx_buffer_seek(rqbuf, 0, SEEK_SET);
60 CURLcode res = curl_easy_perform(handle);
61 if(res == CURLE_OK) {
62 //printf("response\n%s\n", rpbuf->space);
63
64 char *url = NULL;
65 curl_easy_getinfo(handle, CURLINFO_EFFECTIVE_URL, &url);
66 response->url = util_url_path(url);
67
68 parse_propfind(response, rpbuf);
69
70 response->url = strdup(url);
71 }
72 curl_easy_reset(handle);
73
74 return response;
75 }
76
77 UcxBuffer* create_propfind_request_body() {
78 UcxBuffer *buf = ucx_buffer_new(NULL, 512, 0);
79 sstr_t s;
80
81 s = ST("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n");
82 ucx_buffer_write(s.ptr, 1, s.length, buf);
83
84 s = ST("<D:propfind xmlns:D=\"DAV:\">\n");
85 ucx_buffer_write(s.ptr, 1, s.length, buf);
86
87 s = ST("<D:prop>\n");
88 ucx_buffer_write(s.ptr, 1, s.length, buf);
89
90 s = ST("<D:creationdate />\n<D:getlastmodified />\n");
91 ucx_buffer_write(s.ptr, 1, s.length, buf);
92
93 s = ST("<D:getcontentlength />\n<D:getcontenttype />\n");
94 ucx_buffer_write(s.ptr, 1, s.length, buf);
95
96 s = ST("<D:resourcetype />\n");
97 ucx_buffer_write(s.ptr, 1, s.length, buf);
98
99 s = ST("</D:prop>\n</D:propfind>\n");
100 ucx_buffer_write(s.ptr, 1, s.length, buf);
101
102 return buf;
103 }
104
105 int parse_propfind(Propfind *propfind, UcxBuffer *response) {
106 xmlTextReaderPtr reader = xmlReaderForMemory(
107 response->space,
108 response->size,
109 propfind->url,
110 NULL,
111 0);
112
113 int ret = 0;
114 if(reader != NULL) {
115 int r;
116 while((r = xmlTextReaderRead(reader)) == 1) {
117 int nodetype = xmlTextReaderNodeType(reader);
118 if(nodetype == XML_READER_TYPE_ELEMENT) {
119 const xmlChar *name = xmlTextReaderConstLocalName(reader);
120 if(!xmlStrcmp(name, BAD_CAST "response")) {
121 r = parse_response(reader, propfind);
122 if(r != 0) {
123 ret = -1;
124 break;
125 }
126 }
127 }
128 }
129 xmlFreeTextReader(reader);
130 }
131
132 return ret;
133 }
134
135 int parse_response(xmlTextReaderPtr reader, Propfind *propfind) {
136 DavResource *res = calloc(sizeof(DavResource), 1);
137 if(res == NULL) {
138 return -1;
139 }
140
141 /*
142 * 0: href
143 * 1: creationdate
144 * 2: getlastmodified
145 * 3: getcontentlength
146 * 4: getcontenttype
147 * 5: resourcetype
148 */
149 int valuetype = -1;
150
151 int r;
152 while((r = xmlTextReaderRead(reader)) == 1) {
153 int nodetype = xmlTextReaderNodeType(reader);
154
155 if(nodetype == XML_READER_TYPE_END_ELEMENT) {
156 const xmlChar *name = xmlTextReaderConstLocalName(reader);
157 if(!xmlStrcmp(name, BAD_CAST "response")) {
158 break;
159 }
160 }
161
162 switch(nodetype) {
163 case XML_READER_TYPE_ELEMENT: {
164 const xmlChar *name = xmlTextReaderConstLocalName(reader);
165 if(!xmlStrcmp(name, BAD_CAST "href")) {
166 valuetype = 0;
167 } else if(!xmlStrcmp(name, BAD_CAST "creationdate")) {
168 valuetype = 1;
169 } else if(!xmlStrcmp(name, BAD_CAST "getlastmodified")) {
170 valuetype = 2;
171 } else if(!xmlStrcmp(name, BAD_CAST "getcontentlength")) {
172 valuetype = 3;
173 } else if(!xmlStrcmp(name, BAD_CAST "getcontenttype")) {
174 valuetype = 4;
175 } else if(!xmlStrcmp(name, BAD_CAST "resourcetype")) {
176 valuetype = 5;
177 } else if(!xmlStrcmp(name, BAD_CAST "collection")
178 && valuetype == 5) {
179 res->collection = 1;
180 } else {
181 valuetype = -1;
182 }
183 break;
184 }
185 case XML_READER_TYPE_TEXT: {
186 const xmlChar *cvalue = xmlTextReaderConstValue(reader);
187 if(cvalue == NULL) {
188 break;
189 }
190 char *value = strdup((char*)cvalue);
191 switch(valuetype) {
192 case 0: {
193 res->href = value;
194 res->name = util_resource_name(value);
195 break;
196 }
197 case 1: {
198 res->creationdate = 0; // TODO
199 break;
200 }
201 case 2: {
202 res->lastmodified = 0; // TODO
203 break;
204 }
205 case 3: {
206 res->contentlength = atoi(value);
207 break;
208 }
209 case 4: {
210 res->contenttype = value;
211 //break;
212 }
213 }
214 //break;
215 }
216 }
217 }
218
219 if(!strcmp(res->href, propfind->url)) {
220 propfind->resource = res;
221 } else {
222 propfind->children = ucx_dlist_append(propfind->children, res);
223 }
224
225 return 0;
226 }

mercurial