libidav/resource.c

Mon, 26 Aug 2013 14:44:21 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Mon, 26 Aug 2013 14:44:21 +0200
changeset 34
1b87b3b1921e
parent 33
0bbbb0341606
child 40
a95ee94b9204
permissions
-rw-r--r--

removed debug code

33
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 /*
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
3 *
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
4 * Copyright 2013 Olaf Wintermann. All rights reserved.
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
5 *
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
8 *
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
11 *
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
15 *
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0bbbb0341606 some code cleanup and bug fixes
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
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
27 */
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
28
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
29 #include <stdio.h>
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
30 #include <stdlib.h>
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
31 #include <string.h>
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
32 #include <libxml/tree.h>
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
33
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
34 #include "utils.h"
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
35 #include "methods.h"
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
36 #include "davql.h"
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
37 #include "ucx/buffer.h"
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
38 #include "ucx/utils.h"
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
39
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
40 #include "resource.h"
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
41
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
42 #define xstreq(a,b) xmlStrEqual(BAD_CAST a, BAD_CAST b)
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
43
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
44 DavResource* dav_resource_new(DavSession *sn, char *path) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
45 char *url = util_concat_path(sn->base_url, path);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
46 char *href = util_url_path(url);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
47 DavResource *res = dav_resource_new_href(sn, href);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
48 free(url);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
49 return res;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
50 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
51
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
52
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
53 DavResource* dav_resource_new_href(DavSession *sn, char *href) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
54 UcxMempool *mp = sn->mp;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
55 UcxAllocator *a = sn->allocator;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
56
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
57 DavResource *res = ucx_mempool_calloc(mp, 1, sizeof(DavResource));
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
58 res->session = sn;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
59
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
60 // set name, path and href
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
61 resource_set_info(res, href);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
62
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
63 // initialize resource data
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
64 res->data = resource_data_new(sn);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
65
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
66 return res;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
67 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
68
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
69 void resource_set_info(DavResource *res, char *href_str) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
70 char *url_str = NULL;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
71 curl_easy_getinfo(res->session->handle, CURLINFO_EFFECTIVE_URL, &url_str);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
72 sstr_t name = sstr(util_resource_name(href_str));
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
73 sstr_t href = sstr(href_str);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
74
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
75 sstr_t base_href = sstr(util_url_path(res->session->base_url));
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
76 sstr_t path = sstrsubs(href, base_href.length - 1);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
77
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
78 UcxAllocator *a = res->session->allocator;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
79 res->name = sstrdup_a(a, name).ptr;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
80 res->href = sstrdup_a(a, href).ptr;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
81 res->path = sstrdup_a(a, path).ptr;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
82 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
83
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
84 DavResourceData* resource_data_new(DavSession *sn) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
85 DavResourceData *data = ucx_mempool_malloc(
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
86 sn->mp,
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
87 sizeof(DavResourceData));
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
88 if(!data) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
89 return NULL;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
90 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
91 data->properties = ucx_map_new_a(sn->allocator, 32);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
92 data->set = NULL;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
93 data->remove = NULL;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
94 data->content = NULL;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
95 data->read = NULL;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
96 data->length = 0;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
97 return data;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
98 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
99
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
100 void resource_add_property(DavResource *res, char *ns, char *name, char *val) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
101 if(!val) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
102 return;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
103 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
104 UcxAllocator *a = res->session->allocator;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
105
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
106 UcxKey key = dav_property_key(ns, name);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
107 sstr_t v = sstrdup_a(a, sstr(val));
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
108 ucx_map_put(((DavResourceData*)res->data)->properties, key, v.ptr);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
109 free(key.data);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
110 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
111
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
112 char* resource_get_property(DavResource *res, char *ns, char *name) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
113 UcxKey key = dav_property_key(ns, name);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
114 DavResourceData *data = (DavResourceData*)res->data;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
115 char *property = ucx_map_get(data->properties, key);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
116 free(key.data);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
117 return property;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
118 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
119
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
120 UcxKey dav_property_key(char *ns, char *name) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
121 sstr_t ns_str = sstr(ns);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
122 sstr_t name_str = sstr(name);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
123
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
124 sstr_t key;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
125 key.length = ns_str.length + name_str.length + 1;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
126 key.ptr = malloc(key.length + 1);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
127 key = sstrncat(key, 3, ns_str, S(" "), name_str);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
128
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
129 return ucx_key(key.ptr, key.length);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
130 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
131
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
132
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
133
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
134
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
135 void resource_add_child(DavResource *parent, DavResource *child) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
136 child->next = NULL;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
137 if(parent->children) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
138 DavResource *last = parent->children;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
139 while(last->next) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
140 last = last->next;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
141 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
142 last->next = child;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
143 child->prev = last;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
144 } else {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
145 child->prev = NULL;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
146 parent->children = child;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
147 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
148 child->parent = parent;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
149 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
150
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
151 char* dav_get_property(DavResource *res, char *name) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
152 char *pns;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
153 char *pname;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
154 dav_get_property_namespace(res->session->context, name, &pns, &pname);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
155 return dav_get_property_ns(res, pns, pname);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
156 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
157
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
158 char* dav_get_property_ns(DavResource *res, char *ns, char *name) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
159 char *property = resource_get_property(res, ns, name);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
160 DavResourceData *data = res->data;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
161 // resource_get_property only returns persistent properties
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
162 // check the remove and set list
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
163 if(property) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
164 // if the property is in the remove list, we return NULL
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
165 UCX_FOREACH(elm, data->remove) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
166 DavProperty *p = elm->data;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
167 if(!strcmp(p->name, name) && !strcmp(p->ns->name, ns)) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
168 return NULL;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
169 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
170 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
171 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
172 // the set list contains property updates
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
173 // we return an updated property if possible
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
174 UCX_FOREACH(elm, data->set) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
175 DavProperty *p = elm->data;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
176 if(!strcmp(p->name, name) && !strcmp(p->ns->name, ns)) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
177 return p->value;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
178 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
179 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
180 // no property update
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
181 return property;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
182 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
183
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
184 void dav_set_property(DavResource *res, char *name, char *value) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
185 char *pns;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
186 char *pname;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
187 dav_get_property_namespace(res->session->context, name, &pns, &pname);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
188 dav_set_property_ns(res, pns, pname, value);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
189 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
190
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
191 void dav_set_property_ns(DavResource *res, char *ns, char *name, char *value) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
192 UcxAllocator *a = res->session->allocator;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
193 DavResourceData *data = res->data;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
194
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
195 DavProperty *property = a->malloc(a->pool, sizeof(DavProperty));
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
196 property->name = sstrdup_a(a, sstr(name)).ptr;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
197 property->value = sstrdup_a(a, sstr(value)).ptr;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
198 DavNamespace *namespace = a->malloc(a->pool, sizeof(DavNamespace));
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
199 namespace->prefix = NULL;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
200 namespace->name = sstrdup_a(a, sstr(ns)).ptr;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
201 property->ns = namespace;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
202
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
203 data->set = ucx_list_append_a(a, data->set, property);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
204 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
205
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
206 void dav_remove_property(DavResource *res, char *name) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
207 char *pns;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
208 char *pname;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
209 dav_get_property_namespace(res->session->context, name, &pns, &pname);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
210 dav_remove_property_ns(res, pns, pname);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
211 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
212
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
213 void dav_remove_property_ns(DavResource *res, char *ns, char *name) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
214 UcxAllocator *a = res->session->allocator;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
215 DavResourceData *data = res->data;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
216
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
217 DavProperty *property = a->malloc(a->pool, sizeof(DavProperty));
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
218 property->name = sstrdup_a(a, sstr(name)).ptr;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
219 property->value = NULL;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
220 DavNamespace *namespace = a->malloc(a->pool, sizeof(DavNamespace));
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
221 namespace->prefix = NULL;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
222 namespace->name = sstrdup_a(a, sstr(ns)).ptr;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
223 property->ns = namespace;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
224
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
225 data->remove = ucx_list_append_a(a, data->remove, property);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
226 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
227
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
228
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
229 void dav_set_content(DavResource *res, void *stream, dav_read_func read_func) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
230 DavResourceData *data = res->data;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
231 data->content = stream;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
232 data->read = read_func;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
233 data->length = 0;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
234 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
235
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
236 void dav_set_content_data(DavResource *res, char *content, size_t length) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
237 DavSession *sn = res->session;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
238 DavResourceData *data = res->data;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
239 data->content = content;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
240 data->read = NULL;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
241 data->length = length;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
242 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
243
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
244
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
245 int dav_load(DavResource *res) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
246 DavSession *sn = res->session;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
247 DavResourceData *data = res->data;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
248 // clean map
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
249 UcxKey key;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
250 void *value;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
251 UcxMapIterator i = ucx_map_iterator(data->properties);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
252 UCX_MAP_FOREACH(key, value, i) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
253 ucx_map_remove(data->properties, key);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
254 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
255
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
256 char *url = util_concat_path(sn->base_url, res->path);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
257
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
258 CURL *handle = sn->handle;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
259 curl_easy_setopt(handle, CURLOPT_URL, url);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
260 free(url);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
261
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
262 UcxBuffer *rqbuf = create_allprop_propfind_request();
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
263 UcxBuffer *rpbuf = ucx_buffer_new(NULL, 4096, UCX_BUFFER_AUTOEXTEND);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
264
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
265 //fwrite(rpbuf->space, 1, rpbuf->size, stdout);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
266 //printf("\n");
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
267
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
268 CURLcode ret = do_propfind_request(handle, rqbuf, rpbuf);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
269 int status = 0;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
270 curl_easy_getinfo (handle, CURLINFO_RESPONSE_CODE, &status);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
271 if(ret == CURLE_OK) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
272 //printf("response\n%s\n", rpbuf->space);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
273 // TODO: use parse_propfind_response()
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
274 xmlDoc *doc = xmlReadMemory(rpbuf->space, rpbuf->size, url, NULL, 0);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
275 if(!doc) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
276 return 1;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
277 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
278
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
279 xmlNode *xml_root = xmlDocGetRootElement(doc);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
280 xmlNode *node = xml_root->children;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
281 while(node) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
282 if(node->type == XML_ELEMENT_NODE) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
283 if(xstreq(node->name, "response")) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
284 parse_response_tag(res, node, NULL, 0);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
285 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
286 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
287 node = node->next;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
288 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
289
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
290 set_davprops(res);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
291 } else {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
292 session_set_error(sn, ret, status);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
293 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
294 return 0;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
295 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
296
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
297 int dav_store(DavResource *res) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
298 DavSession *sn = res->session;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
299 DavResourceData *data = res->data;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
300
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
301 char *url = util_concat_path(sn->base_url, res->path);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
302 CURL *handle = res->session->handle;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
303 curl_easy_setopt(handle, CURLOPT_URL, url);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
304 free(url);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
305
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
306 // store content
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
307 if(data->content) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
308 CURLcode ret = do_put_request(handle, data->content, data->read, data->length);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
309 int status = 0;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
310 curl_easy_getinfo(handle, CURLINFO_RESPONSE_CODE, &status);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
311 if(ret == CURLE_OK && (status >= 200 && status < 300)) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
312 res->session->error = 0;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
313 // cleanup node data
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
314 if(!data->read) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
315 ucx_mempool_free(sn->mp, data->content);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
316 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
317 data->content = NULL;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
318 data->read = NULL;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
319 data->length = 0;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
320 } else {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
321 session_set_error(sn, ret, status);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
322 return 1;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
323 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
324 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
325
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
326 // store properties
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
327 if(data->set || data->remove) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
328 UcxBuffer *request = create_proppatch_request(data);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
329 UcxBuffer *response = ucx_buffer_new(NULL, 1024, UCX_BUFFER_AUTOEXTEND);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
330
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
331 CURLcode ret = do_proppatch_request(handle, request, response);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
332 int status = 0;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
333 curl_easy_getinfo (handle, CURLINFO_RESPONSE_CODE, &status);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
334 if(ret == CURLE_OK && status == 207) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
335 //printf("%s\n", response->space);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
336 // TODO: parse response
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
337 // TODO: cleanup node data correctly
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
338 data->set = NULL;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
339 data->remove = NULL;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
340 } else {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
341 session_set_error(sn, ret, status);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
342 return 1;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
343 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
344 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
345 sn->error = DAV_OK;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
346 return 0;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
347 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
348
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
349 int dav_get_content(DavResource *res, void *stream, dav_write_func write_fnc) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
350 char *url = util_concat_path(res->session->base_url, res->path);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
351 CURL *handle = res->session->handle;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
352 curl_easy_setopt(handle, CURLOPT_URL, url);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
353 free(url);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
354
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
355 curl_easy_setopt(handle, CURLOPT_SSL_VERIFYPEER, 0);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
356 curl_easy_setopt(handle, CURLOPT_CUSTOMREQUEST, NULL);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
357 curl_easy_setopt(handle, CURLOPT_PUT, 0L);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
358 curl_easy_setopt(handle, CURLOPT_UPLOAD, 0L);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
359
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
360 curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, write_fnc);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
361 curl_easy_setopt(handle, CURLOPT_WRITEDATA, stream);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
362
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
363 CURLcode ret = curl_easy_perform(handle);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
364 int status = 0;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
365 curl_easy_getinfo (handle, CURLINFO_RESPONSE_CODE, &status);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
366 if(ret == CURLE_OK && (status >= 200 && status < 300)) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
367 res->session->error = DAV_OK;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
368 return 0;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
369 } else {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
370 session_set_error(res->session, ret, status);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
371 return 1;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
372 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
373 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
374
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
375 DavResource* dav_create_child(DavResource *parent, char *name) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
376 // TODO
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
377 return NULL;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
378 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
379
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
380 int dav_delete(DavResource *res) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
381 char *url = util_concat_path(res->session->base_url, res->path);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
382 CURL *handle = res->session->handle;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
383 curl_easy_setopt(handle, CURLOPT_URL, url);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
384 free(url);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
385
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
386 UcxBuffer *response = ucx_buffer_new(NULL, 4096, UCX_BUFFER_AUTOEXTEND);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
387 CURLcode ret = do_delete_request(handle, response);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
388 int status = 0;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
389 curl_easy_getinfo (handle, CURLINFO_RESPONSE_CODE, &status);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
390 if(ret == CURLE_OK && (status >= 200 && status < 300)) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
391 res->session->error = DAV_OK;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
392
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
393 // TODO: parse response
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
394 // TODO: free res
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
395
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
396 return 0;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
397 } else {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
398 session_set_error(res->session, ret, status);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
399 return 1;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
400 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
401 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
402
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
403 int dav_create(DavResource *res) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
404 char *url = util_concat_path(res->session->base_url, res->path);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
405 char *parent = util_parent_path(res->path);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
406
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
407 DavSession *sn = res->session;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
408 DavResource *parent_res = dav_get(sn, parent, NULL);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
409 if(!parent_res && sn->error == DAV_NOT_FOUND) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
410 parent_res = dav_resource_new(sn, parent);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
411 parent_res->iscollection = 1;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
412 int r = dav_create(parent_res);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
413 if(r) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
414 free(parent);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
415 return r;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
416 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
417 } else if(parent_res && !parent_res->iscollection) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
418 sn->error = DAV_FORBIDDEN;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
419 return 1;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
420 } else if(sn->error != DAV_OK) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
421 return 1;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
422 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
423
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
424 CURL *handle = res->session->handle;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
425 curl_easy_setopt(handle, CURLOPT_URL, url);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
426 free(url);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
427 free(parent);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
428
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
429 // create new collection or do an empty put request
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
430 CURLcode ret;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
431 if(res->iscollection) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
432 ret = do_mkcol_request(handle);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
433 } else {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
434 ret = do_put_request(handle, "", NULL, 0);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
435 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
436 int status = 0;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
437 curl_easy_getinfo (handle, CURLINFO_RESPONSE_CODE, &status);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
438 if(ret == CURLE_OK && (status >= 200 && status < 300)) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
439 res->session->error = DAV_OK;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
440 } else {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
441 session_set_error(res->session, ret, status);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
442 return 1;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
443 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
444
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
445 // do an minimal propfind request
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
446 UcxBuffer *rqbuf = create_propfind_request(NULL);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
447 UcxBuffer *rpbuf = ucx_buffer_new(NULL, 4096, UCX_BUFFER_AUTOEXTEND);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
448
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
449 //fwrite(rpbuf->space, 1, rpbuf->size, stdout);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
450 //printf("\n");
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
451
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
452 ret = do_propfind_request(handle, rqbuf, rpbuf);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
453 status = 0;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
454 curl_easy_getinfo (handle, CURLINFO_RESPONSE_CODE, &status);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
455 if(ret == CURLE_OK && (status >= 200 && status < 300)) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
456 //printf("response\n%s\n", rpbuf->space);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
457 // TODO: use parse_propfind_response()
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
458 xmlDoc *doc = xmlReadMemory(rpbuf->space, rpbuf->size, url, NULL, 0);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
459 if(!doc) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
460 return 1;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
461 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
462
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
463 xmlNode *xml_root = xmlDocGetRootElement(doc);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
464 xmlNode *node = xml_root->children;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
465 while(node) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
466 if(node->type == XML_ELEMENT_NODE) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
467 if(xstreq(node->name, "response")) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
468 parse_response_tag(res, node, NULL, 0);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
469 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
470 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
471 node = node->next;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
472 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
473
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
474 set_davprops(res);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
475 return 0;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
476 } else {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
477 session_set_error(sn, ret, status);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
478 return 1;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
479 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
480 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
481
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
482 int dav_exists(DavResource *res) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
483 DavSession *sn = res->session;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
484 char *url = util_concat_path(sn->base_url, res->path);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
485 CURL *handle = sn->handle;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
486 curl_easy_setopt(handle, CURLOPT_URL, url);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
487 free(url);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
488
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
489 CURLcode ret = do_head_request(handle);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
490 int status = 0;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
491 curl_easy_getinfo (handle, CURLINFO_RESPONSE_CODE, &status);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
492 if(ret == CURLE_OK && (status >= 200 && status < 300)) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
493 return 1;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
494 } else {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
495 session_set_error(sn, ret, status);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
496 return 0;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
497 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
498 }

mercurial