dav/propfind.c

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

mercurial