libidav/resource.c

Sun, 06 Oct 2024 18:18:04 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 06 Oct 2024 18:18:04 +0200
changeset 49
2f71f4ee247a
parent 20
db263186edf3
permissions
-rw-r--r--

update toolkit, ucx, libidav

1
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 /*
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
3 *
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
4 * Copyright 2018 Olaf Wintermann. All rights reserved.
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
5 *
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
8 *
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
11 *
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
15 *
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
b5bb7b3cd597 add libidav code
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
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
27 */
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
28
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
29 #include <stdio.h>
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
30 #include <stdlib.h>
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
31 #include <string.h>
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
32 #include <stdbool.h>
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
33 #include <libxml/tree.h>
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
34
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
35 #include "utils.h"
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
36 #include "session.h"
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
37 #include "methods.h"
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
38 #include "crypto.h"
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
39 #include <cx/buffer.h>
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
40 #include <cx/utils.h>
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
41 #include <cx/hash_map.h>
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
42 #include <cx/printf.h>
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
43 #include <cx/mempool.h>
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
44 #include <cx/array_list.h>
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
45
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
46 #include "resource.h"
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
47 #include "xml.h"
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
48 #include "davqlexec.h"
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
49
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
50 #define xstreq(a,b) xmlStrEqual(BAD_CAST a, BAD_CAST b)
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
51
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
52 DavResource* dav_resource_new(DavSession *sn, const char *path) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
53 //char *href = util_url_path(url);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
54 //DavResource *res = dav_resource_new_href(sn, href);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
55 char *parent = util_parent_path(path);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
56 const char *name = util_resource_name(path);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
57 char *href = dav_session_create_plain_href(sn, path);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
58
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
59 DavResource *res = dav_resource_new_full(sn, parent, name, href);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
60 free(parent);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
61 return res;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
62 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
63
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
64 DavResource* dav_resource_new_child(DavSession *sn, DavResource *parent, const char *name) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
65 char *path = util_concat_path(parent->path, name);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
66 char *href = dav_session_create_plain_href(sn, path);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
67 DavResource *res = dav_resource_new_full(sn, parent->path, name, href);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
68 free(path);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
69 return res;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
70 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
71
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
72
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
73 DavResource* dav_resource_new_href(DavSession *sn, const char *href) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
74 DavResource *res = cxCalloc(sn->mp->allocator, 1, sizeof(DavResource));
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
75 res->session = sn;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
76
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
77 // set name, path and href
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
78 resource_set_info(res, href);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
79
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
80 // initialize resource data
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
81 res->data = resource_data_new(sn);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
82
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
83 return res;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
84 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
85
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
86 DavResource* dav_resource_new_full(DavSession *sn, const char *parent_path, const char *name, char *href) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
87 cxstring n = cx_str(name);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
88 // the name must not contain path separators
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
89 if(n.length > 0 && href) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
90 for(int i=0;i<n.length-1;i++) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
91 char c = n.ptr[i];
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
92 if(c == '/' || c == '\\') {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
93 n = cx_str(util_resource_name(href));
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
94 break;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
95 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
96 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
97 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
98 // remove trailing '/'
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
99 if(n.length > 0 && n.ptr[n.length-1] == '/') {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
100 n.length--;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
101 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
102
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
103 DavResource *res = cxCalloc(sn->mp->allocator, 1, sizeof(DavResource));
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
104 res->session = sn;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
105
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
106 // set name, path and href
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
107 res->name = cx_strdup_a(sn->mp->allocator, n).ptr;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
108
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
109 char *path = util_concat_path(parent_path, name);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
110 res->path = dav_session_strdup(sn, path);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
111
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
112 res->href = href;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
113
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
114 // initialize resource data
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
115 res->data = resource_data_new(sn);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
116
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
117 // cache href/path
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
118 if(href) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
119 dav_session_cache_path(sn, cx_str(path), cx_str(href));
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
120 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
121 free(path);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
122
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
123 return res;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
124 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
125
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
126 void resource_free_properties(DavSession *sn, CxMap *properties) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
127 if(!properties) return;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
128
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
129 CxIterator i = cxMapIteratorValues(properties);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
130 cx_foreach(DavProperty*, property, i) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
131 // TODO: free everything
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
132 dav_session_free(sn, property);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
133 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
134 cxMapDestroy(properties);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
135 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
136
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
137 void dav_resource_free(DavResource *res) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
138 DavSession *sn = res->session;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
139
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
140 dav_session_free(sn, res->name);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
141 dav_session_free(sn, res->path);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
142 if(res->href) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
143 dav_session_free(sn, res->href);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
144 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
145
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
146 DavResourceData *data = res->data;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
147 resource_free_properties(sn, data->properties);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
148 resource_free_properties(sn, data->crypto_properties);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
149
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
150 if(data->set) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
151 CxIterator i = cxListIterator(data->set);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
152 cx_foreach(DavProperty *, p, i) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
153 dav_session_free(sn, p->ns->name);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
154 if(p->ns->prefix) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
155 dav_session_free(sn, p->ns->prefix);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
156 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
157 dav_session_free(sn, p->ns);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
158
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
159 dav_session_free(sn, p->name);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
160 dav_free_xml_node_sn(sn, p->value);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
161 dav_session_free(sn, p);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
162 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
163 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
164
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
165 if(data->remove) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
166 CxIterator i = cxListIterator(data->remove);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
167 cx_foreach(DavProperty *, p, i) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
168 dav_session_free(sn, p->ns->name);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
169 if(p->ns->prefix) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
170 dav_session_free(sn, p->ns->prefix);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
171 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
172 dav_session_free(sn, p->ns);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
173
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
174 dav_session_free(sn, p->name);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
175 dav_session_free(sn, p);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
176 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
177 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
178
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
179 if(data->crypto_set) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
180 CxIterator i = cxListIterator(data->crypto_set);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
181 cx_foreach(DavProperty *, p, i) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
182 dav_session_free(sn, p->ns->name);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
183 if(p->ns->prefix) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
184 dav_session_free(sn, p->ns->prefix);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
185 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
186 dav_session_free(sn, p->ns);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
187
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
188 dav_session_free(sn, p->name);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
189 dav_free_xml_node_sn(sn, p->value);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
190 dav_session_free(sn, p);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
191 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
192 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
193
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
194 if(data->crypto_remove) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
195 CxIterator i = cxListIterator(data->crypto_remove);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
196 cx_foreach(DavProperty *, p, i) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
197 dav_session_free(sn, p->ns->name);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
198 if(p->ns->prefix) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
199 dav_session_free(sn, p->ns->prefix);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
200 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
201 dav_session_free(sn, p->ns);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
202
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
203 dav_session_free(sn, p->name);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
204 dav_session_free(sn, p);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
205 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
206 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
207
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
208 if(!data->read && data->content) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
209 dav_session_free(sn, data->content);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
210 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
211 dav_session_free(sn, data);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
212
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
213 dav_session_free(sn, res);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
214 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
215
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
216 void dav_resource_free_all(DavResource *res) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
217 DavResource *child = res->children;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
218 dav_resource_free(res);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
219 while(child) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
220 DavResource *next = child->next;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
221 dav_resource_free_all(child);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
222 child = next;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
223 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
224 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
225
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
226 void resource_set_href(DavResource *res, cxstring href) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
227 res->href = cx_strdup_a(res->session->mp->allocator, href).ptr;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
228 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
229
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
230 void resource_set_info(DavResource *res, const char *href_str) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
231 char *url_str = NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
232 curl_easy_getinfo(res->session->handle, CURLINFO_EFFECTIVE_URL, &url_str);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
233 cxstring name = cx_str(util_resource_name(href_str));
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
234 cxstring href = cx_str(href_str);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
235
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
236 cxstring base_href = cx_str(util_url_path(res->session->base_url));
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
237 cxstring path = cx_strsubs(href, base_href.length - 1);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
238
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
239 const CxAllocator *a = res->session->mp->allocator;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
240 CURL *handle = res->session->handle;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
241
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
242 int nlen = 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
243 char *uname = curl_easy_unescape(handle, name.ptr, name.length , &nlen);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
244 int plen = 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
245 char *upath = curl_easy_unescape(handle, path.ptr, path.length, &plen);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
246
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
247 res->name = cx_strdup_a(a, cx_strn(uname, nlen)).ptr;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
248 res->href = cx_strdup_a(a, href).ptr;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
249 res->path = cx_strdup_a(a, cx_strn(upath, plen)).ptr;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
250
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
251 curl_free(uname);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
252 curl_free(upath);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
253 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
254
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
255 DavResourceData* resource_data_new(DavSession *sn) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
256 DavResourceData *data = cxMalloc(
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
257 sn->mp->allocator,
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
258 sizeof(DavResourceData));
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
259 if(!data) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
260 return NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
261 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
262 data->properties = cxHashMapCreate(sn->mp->allocator, CX_STORE_POINTERS, 32);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
263 data->crypto_properties = NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
264 data->set = NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
265 data->remove = NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
266 data->crypto_set = NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
267 data->crypto_remove = NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
268 data->read = NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
269 data->content = NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
270 data->seek = NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
271 data->length = 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
272 return data;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
273 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
274
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
275 char* dav_resource_get_href(DavResource *resource) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
276 if(!resource->href) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
277 resource->href = dav_session_get_href(
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
278 resource->session,
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
279 resource->path);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
280 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
281 return resource->href;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
282 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
283
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
284 void resource_add_prop(DavResource *res, const char *ns, const char *name, DavXmlNode *val) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
285 DavSession *sn = res->session;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
286
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
287 DavNamespace *namespace = dav_session_malloc(sn, sizeof(DavNamespace));
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
288 namespace->prefix = NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
289 namespace->name = dav_session_strdup(sn, ns);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
290
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
291 DavProperty *prop = dav_session_malloc(sn, sizeof(DavProperty));
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
292 prop->name = dav_session_strdup(sn, name);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
293 prop->ns = namespace;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
294 prop->value = val;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
295
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
296 cxmutstr keystr = dav_property_key(ns, name);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
297 CxHashKey key = cx_hash_key(keystr.ptr, keystr.length);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
298 cxMapPut(((DavResourceData*)res->data)->properties, key, prop);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
299 free(keystr.ptr);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
300 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
301
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
302 void resource_add_property(DavResource *res, const char *ns, const char *name, xmlNode *val) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
303 if(!val) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
304 return;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
305 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
306
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
307 resource_add_prop(res, ns, name, dav_convert_xml(res->session, val));
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
308 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
309
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
310 void resource_add_string_property(DavResource *res, char *ns, char *name, char *val) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
311 if(!val) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
312 return;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
313 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
314
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
315 resource_add_prop(res, ns, name, dav_text_node(res->session, val));
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
316 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
317
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
318 void resource_set_crypto_properties(DavResource *res, CxMap *cprops) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
319 DavResourceData *data = res->data;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
320 resource_free_properties(res->session, data->crypto_properties);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
321 data->crypto_properties = cprops;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
322 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
323
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
324 DavXmlNode* resource_get_property(DavResource *res, const char *ns, const char *name) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
325 cxmutstr keystr = dav_property_key(ns, name);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
326 CxHashKey key = cx_hash_key(keystr.ptr, keystr.length);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
327 DavXmlNode *ret = resource_get_property_k(res, key);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
328 free(keystr.ptr);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
329
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
330 return ret;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
331 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
332
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
333 DavXmlNode* resource_get_encrypted_property(DavResource *res, const char *ns, const char *name) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
334 cxmutstr keystr = dav_property_key(ns, name);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
335 CxHashKey key = cx_hash_key(keystr.ptr, keystr.length);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
336 DavXmlNode *ret = resource_get_encrypted_property_k(res, key);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
337 free(keystr.ptr);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
338
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
339 return ret;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
340 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
341
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
342 DavXmlNode* resource_get_property_k(DavResource *res, CxHashKey key) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
343 DavResourceData *data = (DavResourceData*)res->data;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
344 DavProperty *property = cxMapGet(data->properties, key);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
345
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
346 return property ? property->value : NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
347 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
348
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
349 DavXmlNode* resource_get_encrypted_property_k(DavResource *res, CxHashKey key) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
350 DavResourceData *data = (DavResourceData*)res->data;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
351 DavProperty *property = cxMapGet(data->crypto_properties, key);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
352
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
353 return property ? property->value : NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
354 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
355
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
356 cxmutstr dav_property_key(const char *ns, const char *name) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
357 return dav_property_key_a(cxDefaultAllocator, ns, name);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
358 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
359
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
360 cxmutstr dav_property_key_a(const CxAllocator *a, const char *ns, const char *name) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
361 cxstring ns_str = cx_str(ns);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
362 cxstring name_str = cx_str(name);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
363
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
364 return cx_strcat_a(a, 4, ns_str, CX_STR("\0"), name_str, CX_STR("\0"));
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
365 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
366
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
367
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
368
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
369
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
370 void resource_add_child(DavResource *parent, DavResource *child) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
371 child->next = NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
372 if(parent->children) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
373 DavResource *last = parent->children;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
374 while(last->next) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
375 last = last->next;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
376 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
377 last->next = child;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
378 child->prev = last;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
379 } else {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
380 child->prev = NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
381 parent->children = child;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
382 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
383 child->parent = parent;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
384 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
385
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
386 static int resource_cmp(DavResource *res1, DavResource *res2, DavOrderCriterion *cr) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
387 if(!(res1 && res2)) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
388 return 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
389 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
390
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
391 int ret;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
392 if(cr->type == 0) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
393 switch(cr->column.resprop) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
394 case DAVQL_RES_NAME: {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
395 ret = strcmp(res1->name, res2->name);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
396 break;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
397 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
398 case DAVQL_RES_PATH: {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
399 ret = strcmp(res1->path, res2->path);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
400 break;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
401 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
402 case DAVQL_RES_HREF: {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
403 ret = strcmp(res1->href, res2->href);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
404 break;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
405 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
406 case DAVQL_RES_CONTENTLENGTH: {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
407 int c = res1->contentlength == res2->contentlength;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
408 ret = c ? 0 : (res1->contentlength < res2->contentlength?-1:1);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
409 break;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
410 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
411 case DAVQL_RES_CONTENTTYPE: {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
412 ret = strcmp(res1->contenttype, res2->contenttype);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
413 break;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
414 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
415 case DAVQL_RES_CREATIONDATE: {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
416 int c = res1->creationdate == res2->creationdate;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
417 ret = c ? 0 : (res1->creationdate < res2->creationdate?-1:1);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
418 break;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
419 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
420 case DAVQL_RES_LASTMODIFIED: {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
421 int c = res1->lastmodified == res2->lastmodified;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
422 ret = c ? 0 : (res1->lastmodified < res2->lastmodified?-1:1);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
423 break;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
424 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
425 case DAVQL_RES_ISCOLLECTION: {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
426 int c = res1->iscollection == res2->iscollection;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
427 ret = c ? 0 : (res1->iscollection < res2->iscollection?-1:1);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
428 break;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
429 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
430 default: ret = 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
431 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
432 } else if(cr->type == 1) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
433 DavXmlNode *xvalue1 = resource_get_property_k(res1, cr->column.property);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
434 DavXmlNode *xvalue2 = resource_get_property_k(res2, cr->column.property);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
435 char *value1 = dav_xml_getstring(xvalue1);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
436 char *value2 = dav_xml_getstring(xvalue2);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
437 if(!value1) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
438 ret = value2 ? -1 : 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
439 } else if(!value2) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
440 ret = value1 ? 1 : 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
441 } else {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
442 ret = strcmp(value1, value2);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
443 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
444 } else {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
445 return 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
446 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
447
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
448 return cr->descending ? -ret : ret;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
449 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
450
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
451 void resource_add_ordered_child(DavResource *parent, DavResource *child, CxList *ordercr) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
452 if(!ordercr) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
453 resource_add_child(parent, child);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
454 return;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
455 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
456
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
457 child->parent = parent;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
458
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
459 if(!parent->children) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
460 child->next = NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
461 child->prev = NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
462 parent->children = child;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
463 } else {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
464 DavResource *resource = parent->children;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
465 while(resource) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
466 int r = 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
467 CxIterator i = cxListIterator(ordercr);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
468 cx_foreach(DavOrderCriterion*, cr, i) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
469 r = resource_cmp(child, resource, cr);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
470 if(r != 0) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
471 break;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
472 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
473 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
474
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
475 if(r < 0) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
476 // insert child before resource
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
477 child->prev = resource->prev;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
478 child->next = resource;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
479 if(resource->prev) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
480 resource->prev->next = child;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
481 } else {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
482 parent->children = child;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
483 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
484 resource->prev = child;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
485 break;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
486 } if(!resource->next) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
487 // append child
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
488 child->prev = resource;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
489 child->next = NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
490 resource->next = child;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
491 break;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
492 } else {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
493 resource = resource->next;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
494 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
495 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
496 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
497 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
498
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
499 char* dav_get_string_property(DavResource *res, char *name) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
500 char *pns;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
501 char *pname;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
502 dav_get_property_namespace_str(res->session->context, name, &pns, &pname);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
503 if(!pns || !pname) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
504 return NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
505 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
506 return dav_get_string_property_ns(res, pns, pname);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
507 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
508
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
509 char* dav_get_string_property_ns(DavResource *res, char *ns, char *name) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
510 DavXmlNode *prop = dav_get_property_ns(res, ns, name);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
511 if(!prop) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
512 return NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
513 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
514 return dav_xml_getstring(prop);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
515 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
516
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
517 DavXmlNode* dav_get_property(DavResource *res, char *name) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
518 char *pns;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
519 char *pname;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
520 dav_get_property_namespace_str(res->session->context, name, &pns, &pname);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
521 if(!pns || !pname) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
522 return NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
523 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
524 return dav_get_property_ns(res, pns, pname);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
525 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
526
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
527 static DavXmlNode* get_property_ns(DavResource *res, DavBool encrypted, const char *ns, const char *name) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
528 if(!ns || !name) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
529 return NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
530 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
531
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
532 DavResourceData *data = res->data;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
533
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
534 DavXmlNode *property = NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
535 CxList *remove_list = NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
536 CxList *set_list = NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
537
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
538 if(encrypted) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
539 // check if crypto_properties because it will only be created
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
540 // if the resource has encrypted properties
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
541 if(!data->crypto_properties) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
542 return NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
543 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
544 property = resource_get_encrypted_property(res, ns, name);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
545 remove_list = data->crypto_remove;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
546 set_list = data->crypto_set;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
547 } else {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
548 property = resource_get_property(res, ns, name);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
549 remove_list = data->remove;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
550 set_list = data->set;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
551 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
552
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
553 // resource_get_property only returns persistent properties
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
554 // check the remove and set list
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
555 if(property && remove_list) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
556 // if the property is in the remove list, we return NULL
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
557 CxIterator i = cxListIterator(remove_list);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
558 cx_foreach(DavProperty*, p, i) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
559 if(!strcmp(p->name, name) && !strcmp(p->ns->name, ns)) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
560 return NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
561 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
562 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
563 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
564
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
565 // the set list contains property updates
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
566 // we return an updated property if possible
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
567 if(set_list) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
568 CxIterator i = cxListIterator(set_list);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
569 cx_foreach(DavProperty*, p, i) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
570 if(!strcmp(p->name, name) && !strcmp(p->ns->name, ns)) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
571 return p->value; // TODO: fix
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
572 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
573 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
574 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
575
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
576 // no property update
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
577
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
578 return property;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
579 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
580
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
581 DavXmlNode* dav_get_property_ns(DavResource *res, const char *ns, const char *name) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
582 DavXmlNode *property_value = get_property_ns(res, FALSE, ns, name);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
583
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
584 if(!property_value && DAV_DECRYPT_PROPERTIES(res->session)) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
585 property_value = get_property_ns(res, TRUE, ns, name);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
586 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
587
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
588 return property_value;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
589 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
590
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
591 DavXmlNode* dav_get_encrypted_property_ns(DavResource *res, const char *ns, const char *name) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
592 return get_property_ns(res, TRUE, ns, name);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
593 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
594
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
595 static DavProperty* createprop(DavSession *sn, const char *ns, const char *name) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
596 DavProperty *property = dav_session_malloc(sn, sizeof(DavProperty));
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
597 property->name = dav_session_strdup(sn, name);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
598 property->value = NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
599
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
600 DavNamespace *namespace = dav_session_malloc(sn, sizeof(DavNamespace));
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
601 namespace->prefix = NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
602 namespace->name = dav_session_strdup(sn, ns);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
603
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
604 property->ns = namespace;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
605
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
606 return property;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
607 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
608
49
2f71f4ee247a update toolkit, ucx, libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 20
diff changeset
609 int dav_set_string_property(DavResource *res, char *name, char *value) {
1
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
610 char *pns;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
611 char *pname;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
612 dav_get_property_namespace_str(res->session->context, name, &pns, &pname);
49
2f71f4ee247a update toolkit, ucx, libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 20
diff changeset
613 if(!pns) {
2f71f4ee247a update toolkit, ucx, libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 20
diff changeset
614 res->session->errorstr = "Property namespace not found";
2f71f4ee247a update toolkit, ucx, libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 20
diff changeset
615 return 1;
2f71f4ee247a update toolkit, ucx, libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 20
diff changeset
616 }
1
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
617 dav_set_string_property_ns(res, pns, pname, value);
49
2f71f4ee247a update toolkit, ucx, libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 20
diff changeset
618 return 0;
1
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
619 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
620
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
621 static int add2propertylist(const CxAllocator *a, CxList **list, DavProperty *property) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
622 if(!*list) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
623 CxList *newlist = cxLinkedListCreate(a, NULL, CX_STORE_POINTERS);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
624 if(!newlist) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
625 return 1;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
626 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
627 *list = newlist;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
628 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
629 cxListAdd(*list, property);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
630 return 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
631 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
632
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
633 void dav_set_string_property_ns(DavResource *res, char *ns, char *name, char *value) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
634 DavSession *sn = res->session;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
635 const CxAllocator *a = res->session->mp->allocator;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
636 DavResourceData *data = res->data;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
637
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
638 DavProperty *property = createprop(res->session, ns, name);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
639 property->value = dav_text_node(res->session, value);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
640
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
641 if(DAV_ENCRYPT_PROPERTIES(sn) && dav_namespace_is_encrypted(sn->context, ns)) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
642 add2propertylist(a, &data->crypto_set, property);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
643 } else {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
644 add2propertylist(a, &data->set, property);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
645 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
646 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
647
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
648 void dav_set_property(DavResource *res, char *name, DavXmlNode *value) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
649 char *pns;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
650 char *pname;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
651 dav_get_property_namespace_str(res->session->context, name, &pns, &pname);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
652 dav_set_property_ns(res, pns, pname, value);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
653 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
654
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
655 void dav_set_property_ns(DavResource *res, char *ns, char *name, DavXmlNode *value) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
656 DavSession *sn = res->session;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
657 const CxAllocator *a = sn->mp->allocator;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
658 DavResourceData *data = res->data;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
659
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
660 DavProperty *property = createprop(sn, ns, name);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
661 // TODO: this function should copy the value
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
662 // but we also need a function, that doesn't create a copy
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
663 property->value = value;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
664
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
665 if(DAV_ENCRYPT_PROPERTIES(sn) && dav_namespace_is_encrypted(sn->context, ns)) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
666 add2propertylist(a, &data->crypto_set, property);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
667 } else {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
668 add2propertylist(a, &data->set, property);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
669 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
670 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
671
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
672 void dav_remove_property(DavResource *res, char *name) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
673 char *pns;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
674 char *pname;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
675 dav_get_property_namespace_str(res->session->context, name, &pns, &pname);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
676 dav_remove_property_ns(res, pns, pname);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
677 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
678
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
679 void dav_remove_property_ns(DavResource *res, char *ns, char *name) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
680 DavSession *sn = res->session;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
681 DavResourceData *data = res->data;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
682 const CxAllocator *a = res->session->mp->allocator;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
683
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
684 DavProperty *property = createprop(res->session, ns, name);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
685
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
686 if(DAV_ENCRYPT_PROPERTIES(sn) && dav_namespace_is_encrypted(sn->context, ns)) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
687 add2propertylist(a, &data->crypto_remove, property);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
688 } else {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
689 add2propertylist(a, &data->remove, property);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
690 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
691 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
692
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
693 void dav_set_encrypted_property_ns(DavResource *res, char *ns, char *name, DavXmlNode *value) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
694 const CxAllocator *a = res->session->mp->allocator;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
695 DavResourceData *data = res->data;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
696
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
697 DavProperty *property = createprop(res->session, ns, name);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
698 property->value = value; // TODO: copy node?
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
699
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
700 add2propertylist(a, &data->crypto_set, property);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
701 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
702
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
703 void dav_set_encrypted_string_property_ns(DavResource *res, char *ns, char *name, char *value) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
704 const CxAllocator *a = res->session->mp->allocator;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
705 DavResourceData *data = res->data;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
706
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
707 DavProperty *property = createprop(res->session, ns, name);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
708 property->value = dav_text_node(res->session, value);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
709
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
710 add2propertylist(a, &data->crypto_set, property);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
711 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
712
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
713 void dav_remove_encrypted_property_ns(DavResource *res, char *ns, char *name) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
714 DavResourceData *data = res->data;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
715 const CxAllocator *a = res->session->mp->allocator;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
716
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
717 DavProperty *property = createprop(res->session, ns, name);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
718
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
719 add2propertylist(a, &data->crypto_remove, property);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
720 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
721
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
722 static int compare_propname(const void *a, const void *b) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
723 const DavPropName *p1 = a;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
724 const DavPropName *p2 = b;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
725
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
726 int result = strcmp(p1->ns, p2->ns);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
727 if(result) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
728 return result;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
729 } else {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
730 return strcmp(p1->name, p2->name);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
731 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
732 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
733
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
734 DavPropName* dav_get_property_names(DavResource *res, size_t *count) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
735 DavResourceData *data = res->data;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
736
49
2f71f4ee247a update toolkit, ucx, libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 20
diff changeset
737 *count = cxMapSize(data->properties);
1
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
738 DavPropName *names = dav_session_calloc(
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
739 res->session,
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
740 *count,
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
741 sizeof(DavPropName));
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
742
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
743
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
744 CxIterator i = cxMapIteratorValues(data->properties);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
745 cx_foreach(DavProperty*, value, i) {
49
2f71f4ee247a update toolkit, ucx, libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 20
diff changeset
746 DavPropName *name = &names[i.index];
1
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
747 name->ns = value->ns->name;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
748 name->name = value->name;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
749 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
750
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
751 qsort(names, *count, sizeof(DavPropName), compare_propname);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
752
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
753 return names;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
754 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
755
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
756
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
757 void dav_set_content(DavResource *res, void *stream, dav_read_func read_func, dav_seek_func seek_func) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
758 DavResourceData *data = res->data;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
759 data->content = stream;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
760 data->read = read_func;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
761 data->seek = seek_func;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
762 data->length = 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
763 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
764
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
765 void dav_set_content_data(DavResource *res, char *content, size_t length) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
766 DavSession *sn = res->session;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
767 DavResourceData *data = res->data;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
768 data->content = dav_session_malloc(sn, length);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
769 memcpy(data->content, content, length);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
770 data->read = NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
771 data->seek = NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
772 data->length = length;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
773 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
774
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
775 void dav_set_content_length(DavResource *res, size_t length) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
776 DavResourceData *data = res->data;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
777 data->length = length;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
778 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
779
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
780
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
781 int dav_load(DavResource *res) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
782 CxBuffer *rqbuf = create_allprop_propfind_request();
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
783 int ret = dav_propfind(res->session, res, rqbuf);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
784 cxBufferFree(rqbuf);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
785 return ret;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
786 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
787
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
788 int dav_load_prop(DavResource *res, DavPropName *properties, size_t numprop) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
789 CxMempool *mp = cxMempoolCreate(64, NULL);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
790 const CxAllocator *a = mp->allocator;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
791
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
792 CxList *proplist = cxArrayListCreate(a, NULL, sizeof(DavProperty), numprop);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
793 for(size_t i=0;i<numprop;i++) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
794 DavProperty p;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
795 p.name = properties[i].name;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
796 p.ns = cxMalloc(a, sizeof(DavNamespace));
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
797 p.ns->name = properties[i].ns;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
798 if(!strcmp(properties[i].ns, "DAV:")) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
799 p.ns->prefix = "D";
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
800 } else {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
801 p.ns->prefix = cx_asprintf_a(a, "x%d", (int)i).ptr;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
802 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
803 p.value = NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
804 cxListAdd(proplist, &p);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
805 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
806
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
807 CxBuffer *rqbuf = create_propfind_request(res->session, proplist, "propfind", 0);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
808 int ret = dav_propfind(res->session, res, rqbuf);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
809 cxBufferFree(rqbuf);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
810 cxMempoolDestroy(mp);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
811 return ret;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
812 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
813
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
814
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
815 static void init_hash_stream(HashStream *hstr, void *stream, dav_read_func readfn, dav_seek_func seekfn) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
816 hstr->sha = NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
817 hstr->stream = stream;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
818 hstr->read = readfn;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
819 hstr->seek = seekfn;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
820 hstr->error = 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
821 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
822
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
823 static size_t dav_read_h(void *buf, size_t size, size_t nelm, void *stream) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
824 HashStream *s = stream;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
825 if(!s->sha) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
826 s->sha = dav_hash_init();
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
827 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
828
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
829 size_t r = s->read(buf, size, nelm, s->stream);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
830 dav_hash_update(s->sha, buf, r);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
831 return r;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
832 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
833
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
834 static int dav_seek_h(void *stream, long offset, int whence) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
835 HashStream *s = stream;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
836 if(offset == 0 && whence == SEEK_SET) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
837 unsigned char buf[DAV_SHA256_DIGEST_LENGTH];
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
838 dav_hash_final(s->sha, buf);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
839 s->sha = NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
840 } else {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
841 s->error = 1;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
842 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
843 return s->seek(s->stream, offset, whence);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
844 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
845
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
846
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
847 int dav_store(DavResource *res) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
848 DavSession *sn = res->session;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
849 DavResourceData *data = res->data;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
850
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
851 util_set_url(sn, dav_resource_get_href(res));
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
852
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
853 DavLock *lock = dav_get_lock(sn, res->path);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
854 char *locktoken = lock ? lock->token : NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
855
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
856 // store content
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
857 if(data->content) {
20
db263186edf3 improve upload progress window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
858 curl_easy_setopt(sn->handle, CURLOPT_XFERINFOFUNCTION, dav_session_put_progress);
db263186edf3 improve upload progress window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
859 curl_easy_setopt(sn->handle, CURLOPT_XFERINFODATA, res);
db263186edf3 improve upload progress window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
860 curl_easy_setopt(sn->handle, CURLOPT_NOPROGRESS, 0L);
db263186edf3 improve upload progress window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
861
1
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
862 int encryption = DAV_ENCRYPT_CONTENT(sn) && sn->key;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
863 CURLcode ret;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
864 if(encryption) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
865 AESEncrypter *enc = NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
866 CxBuffer *buf = NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
867 if(data->read) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
868 enc = aes_encrypter_new(
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
869 sn->key,
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
870 data->content,
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
871 data->read,
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
872 data->seek);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
873 } else {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
874 buf = cxBufferCreate(data->content, data->length, cxDefaultAllocator, 0);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
875 buf->size = data->length;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
876 enc = aes_encrypter_new(
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
877 sn->key,
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
878 buf,
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
879 (dav_read_func)cxBufferRead,
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
880 (dav_seek_func)cxBufferSeek);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
881 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
882
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
883 // put resource
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
884 ret = do_put_request(
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
885 sn,
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
886 locktoken,
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
887 TRUE,
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
888 enc,
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
889 (dav_read_func)aes_read,
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
890 (dav_seek_func)aes_encrypter_reset,
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
891 0);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
892
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
893 // get sha256 hash
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
894 dav_get_hash(&enc->sha256, (unsigned char*)data->hash);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
895 char *enc_hash = aes_encrypt(data->hash, DAV_SHA256_DIGEST_LENGTH, sn->key);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
896
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
897 aes_encrypter_close(enc);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
898 if(buf) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
899 cxBufferFree(buf);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
900 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
901
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
902 // add crypto properties
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
903 // TODO: store the properties later
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
904 if(resource_add_crypto_info(sn, res->href, res->name, enc_hash)) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
905 free(enc_hash);
20
db263186edf3 improve upload progress window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
906 curl_easy_setopt(sn->handle, CURLOPT_XFERINFOFUNCTION, NULL);
db263186edf3 improve upload progress window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
907 curl_easy_setopt(sn->handle, CURLOPT_NOPROGRESS, 1L);
1
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
908 return 1;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
909 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
910 resource_add_string_property(res, DAV_NS, "crypto-hash", enc_hash);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
911 free(enc_hash);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
912 } else if((sn->flags & DAV_SESSION_STORE_HASH) == DAV_SESSION_STORE_HASH) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
913 HashStream hstr;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
914 CxBuffer *iobuf = NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
915 if(!data->read) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
916 iobuf = cxBufferCreate(data->content, data->length, cxDefaultAllocator, 0);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
917 iobuf->size = data->length;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
918 init_hash_stream(
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
919 &hstr,
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
920 iobuf,
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
921 (dav_read_func)cxBufferRead,
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
922 (dav_seek_func)cxBufferSeek);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
923 } else {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
924 init_hash_stream(
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
925 &hstr,
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
926 data->content,
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
927 data->read,
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
928 data->seek);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
929 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
930
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
931 ret = do_put_request(
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
932 sn,
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
933 locktoken,
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
934 TRUE,
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
935 &hstr,
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
936 dav_read_h,
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
937 (dav_seek_func)dav_seek_h,
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
938 data->length);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
939
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
940 if(hstr.sha) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
941 dav_hash_final(hstr.sha, (unsigned char*)data->hash);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
942 char *hash = util_hexstr((unsigned char*)data->hash, 32);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
943 dav_set_string_property_ns(res, DAV_NS, "content-hash", hash);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
944 free(hash);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
945 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
946 } else {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
947 ret = do_put_request(
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
948 sn,
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
949 locktoken,
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
950 TRUE,
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
951 data->content,
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
952 data->read,
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
953 data->seek,
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
954 data->length);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
955 }
20
db263186edf3 improve upload progress window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
956
db263186edf3 improve upload progress window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
957 curl_easy_setopt(sn->handle, CURLOPT_XFERINFOFUNCTION, NULL);
db263186edf3 improve upload progress window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
958 curl_easy_setopt(sn->handle, CURLOPT_NOPROGRESS, 1L);
1
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
959
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
960 long status = 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
961 curl_easy_getinfo(sn->handle, CURLINFO_RESPONSE_CODE, &status);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
962 if(ret == CURLE_OK && (status >= 200 && status < 300)) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
963 res->session->error = 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
964 // cleanup node data
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
965 if(!data->read) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
966 cxFree(sn->mp->allocator, data->content);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
967 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
968 data->content = NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
969 data->read = NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
970 data->length = 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
971 } else {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
972 dav_session_set_error(sn, ret, status);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
973 return 1;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
974 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
975 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
976
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
977 // generate crypto-prop content
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
978 if(DAV_ENCRYPT_PROPERTIES(sn) && sn->key && (data->crypto_set || data->crypto_remove)) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
979 DavResource *crypto_res = dav_resource_new_href(sn, res->href);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
980 int ret = 1;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
981
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
982 if(crypto_res) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
983 CxBuffer *rqbuf = create_cryptoprop_propfind_request();
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
984 ret = dav_propfind(res->session, res, rqbuf);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
985 cxBufferFree(rqbuf);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
986 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
987
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
988 if(!ret) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
989 DavXmlNode *crypto_prop_node = dav_get_property_ns(crypto_res, DAV_NS, "crypto-prop");
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
990 CxMap *crypto_props = parse_crypto_prop(sn, sn->key, crypto_prop_node);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
991 if(!crypto_props) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
992 // resource hasn't encrypted properties yet
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
993 crypto_props = cxHashMapCreate(cxDefaultAllocator, CX_STORE_POINTERS, 32); // create new map
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
994 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
995
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
996 // remove all properties
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
997 if(data->crypto_remove) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
998 CxIterator i = cxListIterator(data->crypto_remove);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
999 cx_foreach(DavProperty *, property, i) {
49
2f71f4ee247a update toolkit, ucx, libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 20
diff changeset
1000 if(cxMapSize(crypto_props) == 0) {
1
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1001 break; // map already empty, can't remove any more
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1002 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1003
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1004 cxmutstr key = dav_property_key(property->ns->name, property->name);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1005 DavProperty *existing_prop = cxMapGet(crypto_props, cx_hash_key(key.ptr, key.length));
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1006 if(existing_prop) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1007 // TODO: free existing_prop
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1008 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1009 free(key.ptr);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1010 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1011 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1012
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1013 // set properties
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1014 if(data->crypto_set) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1015 CxIterator i = cxListIterator(data->crypto_set);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1016 cx_foreach(DavProperty *, property, i) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1017 cxmutstr keystr = dav_property_key(property->ns->name, property->name);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1018 CxHashKey key = cx_hash_key(keystr.ptr, keystr.length);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1019 DavProperty *existing_prop = cxMapRemoveAndGet(crypto_props, key);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1020 cxMapPut(crypto_props, key, property);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1021 if(existing_prop) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1022 // TODO: free existing_prop
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1023 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1024 free(keystr.ptr);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1025 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1026 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1027
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1028 DavXmlNode *crypto_prop_value = create_crypto_prop(sn, crypto_props);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1029 if(crypto_prop_value) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1030 DavProperty *new_crypto_prop = createprop(sn, DAV_NS, "crypto-prop");
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1031 new_crypto_prop->value = crypto_prop_value;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1032 add2propertylist(sn->mp->allocator, &data->set, new_crypto_prop);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1033 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1034
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1035 dav_resource_free(crypto_res);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1036 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1037
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1038 if(ret) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1039 return 1;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1040 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1041 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1042
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1043 // store properties
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1044 int r = 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1045 sn->error = DAV_OK;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1046 if(data->set || data->remove) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1047 CxBuffer *request = create_proppatch_request(data);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1048 CxBuffer *response = cxBufferCreate(NULL, 1024, cxDefaultAllocator, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1049 //printf("request:\n%.*s\n\n", request->pos, request->space);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1050
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1051 CURLcode ret = do_proppatch_request(sn, locktoken, request, response);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1052 long status = 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1053 curl_easy_getinfo (sn->handle, CURLINFO_RESPONSE_CODE, &status);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1054 if(ret == CURLE_OK && status == 207) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1055 //printf("%s\n", response->space);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1056 // TODO: parse response
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1057 // TODO: cleanup node data correctly
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1058 data->set = NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1059 data->remove = NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1060 } else {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1061 dav_session_set_error(sn, ret, status);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1062 r = -1;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1063 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1064
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1065 cxBufferFree(request);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1066 cxBufferFree(response);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1067 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1068
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1069 return r;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1070 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1071
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1072 #if LIBCURL_VERSION_MAJOR >= 7 && LIBCURL_VERSION_MINOR >= 32
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1073 static void set_progressfunc(DavResource *res) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1074 CURL *handle = res->session->handle;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1075 curl_easy_setopt(handle, CURLOPT_XFERINFOFUNCTION, dav_session_get_progress);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1076 curl_easy_setopt(handle, CURLOPT_XFERINFODATA, res);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1077 curl_easy_setopt(handle, CURLOPT_NOPROGRESS, 0L);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1078 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1079
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1080 static void unset_progressfunc(DavResource *res) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1081 CURL *handle = res->session->handle;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1082 curl_easy_setopt(handle, CURLOPT_XFERINFOFUNCTION, NULL);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1083 curl_easy_setopt(handle, CURLOPT_XFERINFODATA, NULL);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1084 curl_easy_setopt(handle, CURLOPT_NOPROGRESS, 1L);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1085 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1086 #else
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1087 static void set_progressfunc(DavResource *res) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1088
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1089 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1090 static void unset_progressfunc(DavResource *res) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1091
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1092 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1093 #endif
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1094
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1095 int dav_get_content(DavResource *res, void *stream, dav_write_func write_fnc) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1096 DavSession *sn = res->session;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1097 CURL *handle = sn->handle;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1098 util_set_url(res->session, dav_resource_get_href(res));
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1099
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1100 // check encryption
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1101 AESDecrypter *dec = NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1102 DavKey *key = NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1103 if(DAV_DECRYPT_CONTENT(sn)) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1104 char *keyname = dav_get_string_property_ns(res, DAV_NS, "crypto-key");
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1105 if(keyname) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1106 key = dav_context_get_key(sn->context, keyname);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1107 if(key) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1108 dec = aes_decrypter_new(key, stream, write_fnc);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1109 stream = dec;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1110 write_fnc = (dav_write_func)aes_write;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1111 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1112 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1113 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1114
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1115 curl_easy_setopt(handle, CURLOPT_HTTPHEADER, NULL);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1116 curl_easy_setopt(handle, CURLOPT_CUSTOMREQUEST, NULL);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1117 curl_easy_setopt(handle, CURLOPT_PUT, 0L);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1118 curl_easy_setopt(handle, CURLOPT_UPLOAD, 0L);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1119
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1120 curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, write_fnc);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1121 curl_easy_setopt(handle, CURLOPT_WRITEDATA, stream);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1122
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1123 if(sn->get_progress) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1124 set_progressfunc(res);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1125 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1126
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1127 long status = 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1128 CURLcode ret = dav_session_curl_perform(sn, &status);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1129
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1130 if(sn->get_progress) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1131 unset_progressfunc(res);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1132 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1133
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1134 char *hash = NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1135 if(dec) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1136 aes_decrypter_shutdown(dec); // get final bytes
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1137
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1138 // get hash
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1139 unsigned char sha[DAV_SHA256_DIGEST_LENGTH];
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1140 dav_get_hash(&dec->sha256, sha);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1141 hash = util_hexstr(sha, DAV_SHA256_DIGEST_LENGTH);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1142
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1143 aes_decrypter_close(dec);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1144 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1145
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1146 if(ret == CURLE_OK && (status >= 200 && status < 300)) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1147 int verify_failed = 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1148 if(DAV_DECRYPT_CONTENT(sn) && key) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1149 // try to verify the content
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1150 char *res_hash = dav_get_string_property_ns(res, DAV_NS, "crypto-hash");
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1151
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1152 if(res_hash) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1153 size_t len = 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1154 char *dec_hash = aes_decrypt(res_hash, &len, key);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1155 char *hex_hash = util_hexstr((unsigned char*)dec_hash, len);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1156 if(strcmp(hash, hex_hash)) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1157 verify_failed = 1;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1158 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1159 free(dec_hash);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1160 free(hex_hash);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1161 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1162 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1163 if(hash) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1164 free(hash);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1165 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1166
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1167 if(verify_failed) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1168 res->session->error = DAV_CONTENT_VERIFICATION_ERROR;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1169 return 1;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1170 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1171
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1172 res->session->error = DAV_OK;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1173 return 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1174 } else {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1175 if(hash) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1176 free(hash);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1177 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1178 dav_session_set_error(res->session, ret, status);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1179 return 1;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1180 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1181 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1182
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1183 DavResource* dav_create_child(DavResource *parent, char *name) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1184 DavResource *res = dav_resource_new_child(parent->session, parent, name);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1185 if(dav_create(res)) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1186 dav_resource_free(res);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1187 return NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1188 } else {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1189 return res;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1190 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1191 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1192
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1193 int dav_delete(DavResource *res) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1194 CURL *handle = res->session->handle;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1195 util_set_url(res->session, dav_resource_get_href(res));
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1196
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1197 DavLock *lock = dav_get_lock(res->session, res->path);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1198 char *locktoken = lock ? lock->token : NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1199
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1200 CxBuffer *response = cxBufferCreate(NULL, 4096, cxDefaultAllocator, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1201 CURLcode ret = do_delete_request(res->session, locktoken, response);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1202 long status = 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1203 curl_easy_getinfo (handle, CURLINFO_RESPONSE_CODE, &status);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1204 int r = 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1205 if(ret == CURLE_OK && (status >= 200 && status < 300)) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1206 res->session->error = DAV_OK;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1207 res->exists = 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1208
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1209 // TODO: parse response
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1210 // TODO: free res
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1211 } else {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1212 dav_session_set_error(res->session, ret, status);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1213 r = 1;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1214 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1215
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1216 cxBufferFree(response);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1217 return r;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1218 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1219
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1220 static int create_ancestors(DavSession *sn, char *href, char *path) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1221 CURL *handle = sn->handle;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1222 CURLcode code;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1223
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1224 DavLock *lock = dav_get_lock(sn, path);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1225 char *locktoken = lock ? lock->token : NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1226
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1227 long status = 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1228 int ret = 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1229
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1230 if(strlen(path) <= 1) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1231 return 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1232 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1233
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1234 char *p = util_parent_path(path);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1235 char *h = util_parent_path(href);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1236
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1237 for(int i=0;i<2;i++) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1238 util_set_url(sn, h);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1239 code = do_mkcol_request(sn, locktoken);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1240 curl_easy_getinfo(handle, CURLINFO_RESPONSE_CODE, &status);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1241 if(status == 201) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1242 // resource successfully created
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1243 char *name = (char*)util_resource_name(p);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1244 int len = strlen(name);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1245 if(name[len - 1] == '/') {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1246 name[len - 1] = '\0';
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1247 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1248 if(resource_add_crypto_info(sn, h, name, NULL)) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1249 sn->error = DAV_ERROR;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1250 dav_session_set_errstr(sn, "Cannot set crypto properties for ancestor");
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1251 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1252 break;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1253 } else if(status == 405) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1254 // parent already exists
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1255 break;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1256 } else if(status == 409) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1257 // parent doesn't exist
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1258 if(create_ancestors(sn, h, p)) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1259 ret = 1;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1260 break;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1261 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1262 } else {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1263 dav_session_set_error(sn, code, status);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1264 ret = 1;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1265 break;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1266 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1267 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1268
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1269 free(p);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1270 free(h);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1271 return ret;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1272 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1273
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1274 static int create_resource(DavResource *res, int *status) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1275 DavSession *sn = res->session;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1276 CURL *handle = sn->handle;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1277 util_set_url(sn, dav_resource_get_href(res));
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1278
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1279 DavLock *lock = dav_get_lock(res->session, res->path);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1280 char *locktoken = lock ? lock->token : NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1281
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1282 CURLcode code;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1283 if(res->iscollection) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1284 code = do_mkcol_request(sn, locktoken);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1285 } else {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1286 code = do_put_request(sn, locktoken, TRUE, "", NULL, NULL, 0);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1287 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1288 long s = 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1289 curl_easy_getinfo(handle, CURLINFO_RESPONSE_CODE, &s);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1290 *status = s;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1291 if(code == CURLE_OK && (s >= 200 && s < 300)) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1292 sn->error = DAV_OK;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1293 // if the session has encrypted file names, add crypto infos
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1294 if(!resource_add_crypto_info(sn, res->href, res->name, NULL)) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1295 // do a minimal propfind request
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1296 CxBuffer *rqbuf = create_propfind_request(sn, NULL, "propfind", 0);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1297 int ret = dav_propfind(sn, res, rqbuf);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1298 cxBufferFree(rqbuf);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1299 return ret;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1300 } else {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1301 return 1;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1302 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1303 } else {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1304 dav_session_set_error(sn, code, s);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1305 return 1;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1306 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1307 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1308
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1309 int dav_create(DavResource *res) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1310 int status;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1311 if(!create_resource(res, &status)) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1312 // resource successfully created
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1313 res->exists = 1;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1314 return 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1315 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1316
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1317 if(status == 403 || status == 409 || status == 404) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1318 // create intermediate collections
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1319 if(create_ancestors(res->session, res->href, res->path)) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1320 return 1;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1321 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1322 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1323
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1324 return create_resource(res, &status);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1325 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1326
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1327 int dav_exists(DavResource *res) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1328 if(!dav_load_prop(res, NULL, 0)) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1329 res->exists = 1;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1330 return 1;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1331 } else {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1332 if(res->session->error == DAV_NOT_FOUND) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1333 res->exists = 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1334 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1335 return 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1336 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1337 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1338
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1339 static int dav_cp_mv_url(DavResource *res, char *desturl, _Bool copy, _Bool override) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1340 DavSession *sn = res->session;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1341 CURL *handle = sn->handle;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1342 util_set_url(sn, dav_resource_get_href(res));
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1343
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1344 DavLock *lock = dav_get_lock(sn, res->path);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1345 char *locktoken = lock ? lock->token : NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1346
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1347 CURLcode ret = do_copy_move_request(sn, desturl, locktoken, copy, override);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1348
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1349 long status = 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1350 curl_easy_getinfo (handle, CURLINFO_RESPONSE_CODE, &status);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1351 if(ret == CURLE_OK && (status >= 200 && status < 300)) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1352 return 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1353 } else {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1354 dav_session_set_error(sn, ret, status);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1355 return 1;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1356 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1357 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1358
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1359 static int dav_cp_mv(DavResource *res, char *newpath, _Bool copy, _Bool override) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1360 char *dest = dav_session_get_href(res->session, newpath);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1361 char *desturl = util_get_url(res->session, dest);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1362 dav_session_free(res->session, dest);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1363
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1364 int ret = dav_cp_mv_url(res, desturl, copy, override);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1365 free(desturl);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1366 return ret;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1367 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1368
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1369 int dav_copy(DavResource *res, char *newpath) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1370 return dav_cp_mv(res, newpath, true, false);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1371 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1372
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1373 int dav_move(DavResource *res, char *newpath) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1374 return dav_cp_mv(res, newpath, false, false);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1375 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1376
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1377 int dav_copy_o(DavResource *res, char *newpath, DavBool override) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1378 return dav_cp_mv(res, newpath, true, override);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1379 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1380
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1381 int dav_move_o(DavResource *res, char *newpath, DavBool override) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1382 return dav_cp_mv(res, newpath, false, override);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1383 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1384
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1385 int dav_copyto(DavResource *res, char *url, DavBool override) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1386 return dav_cp_mv_url(res, url, true, override);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1387 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1388
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1389 int dav_moveto(DavResource *res, char *url, DavBool override) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1390 return dav_cp_mv_url(res, url, false, override);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1391 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1392
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1393 int dav_lock(DavResource *res) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1394 return dav_lock_t(res, 0);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1395 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1396
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1397 int dav_lock_t(DavResource *res, time_t timeout) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1398 DavSession *sn = res->session;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1399 CURL *handle = sn->handle;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1400 util_set_url(sn, dav_resource_get_href(res));
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1401
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1402 CxBuffer *request = create_lock_request();
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1403 CxBuffer *response = cxBufferCreate(NULL, 512, cxDefaultAllocator, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1404 CURLcode ret = do_lock_request(sn, request, response, timeout);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1405
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1406 //printf("\nlock\n");
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1407 //printf("%.*s\n\n", request->size, request->space);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1408 //printf("%.*s\n\n", response->size, response->space);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1409
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1410 cxBufferFree(request);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1411
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1412 long status = 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1413 curl_easy_getinfo (handle, CURLINFO_RESPONSE_CODE, &status);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1414 if(ret == CURLE_OK && (status >= 200 && status < 300)) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1415 LockDiscovery lock;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1416 int parse_error = parse_lock_response(sn, response, &lock);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1417 cxBufferFree(response);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1418 if(parse_error) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1419 sn->error = DAV_ERROR;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1420 return -1;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1421 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1422
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1423 DavLock *l = dav_create_lock(sn, lock.locktoken, lock.timeout);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1424 free(lock.locktoken);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1425 free(lock.timeout);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1426
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1427 int r = 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1428 if(res->iscollection) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1429 r = dav_add_collection_lock(sn, res->path, l);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1430 } else {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1431 r = dav_add_resource_lock(sn, res->path, l);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1432 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1433
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1434 if(r == 0) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1435 return 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1436 } else {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1437 (void)dav_unlock(res);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1438 sn->error = DAV_ERROR;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1439 dav_destroy_lock(sn, l);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1440 return -1;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1441 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1442 } else {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1443 dav_session_set_error(sn, ret, status);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1444 cxBufferFree(response);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1445 return -1;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1446 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1447 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1448
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1449 int dav_unlock(DavResource *res) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1450 DavSession *sn = res->session;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1451 CURL *handle = sn->handle;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1452 util_set_url(sn, dav_resource_get_href(res));
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1453
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1454 DavLock *lock = dav_get_lock(res->session, res->path);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1455 if(!lock) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1456 sn->error = DAV_ERROR;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1457 return -1;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1458 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1459
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1460 CURLcode ret = do_unlock_request(sn, lock->token);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1461 long status = 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1462 curl_easy_getinfo (handle, CURLINFO_RESPONSE_CODE, &status);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1463 if(ret == CURLE_OK && (status >= 200 && status < 300)) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1464 dav_remove_lock(sn, res->path, lock);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1465 } else {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1466 dav_session_set_error(sn, ret, status);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1467 return 1;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1468 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1469
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1470 return 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1471 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1472
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1473
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1474 int resource_add_crypto_info(DavSession *sn, const char *href, const char *name, const char *hash) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1475 if(!DAV_IS_ENCRYPTED(sn)) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1476 return 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1477 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1478
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1479 CxBuffer *request = create_crypto_proppatch_request(sn, sn->key, name, hash);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1480 CxBuffer *response = cxBufferCreate(NULL, 1024, cxDefaultAllocator, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1481
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1482 util_set_url(sn, href);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1483 // TODO: lock
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1484 CURLcode ret = do_proppatch_request(sn, NULL, request, response);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1485 cxBufferFree(request);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1486 long status = 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1487 curl_easy_getinfo (sn->handle, CURLINFO_RESPONSE_CODE, &status);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1488 if(ret == CURLE_OK && status == 207) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1489 // TODO: parse response
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1490 sn->error = DAV_OK;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1491 cxBufferFree(response);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1492 return 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1493 } else {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1494 dav_session_set_error(sn, ret, status);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1495 cxBufferFree(response);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1496 return 1;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1497 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1498 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1499
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1500 /* ----------------------------- crypto-prop ----------------------------- */
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1501
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1502 DavXmlNode* create_crypto_prop(DavSession *sn, CxMap *properties) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1503 if(!sn->key) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1504 return NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1505 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1506
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1507 CxBuffer *content = cxBufferCreate(NULL, 2048, cxDefaultAllocator, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1508
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1509 // create an xml document containing all properties
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1510 CxMap *nsmap = cxHashMapCreate(cxDefaultAllocator, CX_STORE_POINTERS, 8);
49
2f71f4ee247a update toolkit, ucx, libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 20
diff changeset
1511 cxDefineDestructor(nsmap, free);
1
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1512 cxMapPut(nsmap, cx_hash_key_str("DAV:"), strdup("D"));
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1513
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1514 cxBufferPutString(content, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1515 cxBufferPutString(content, "<D:prop xmlns:D=\"DAV:\">\n");
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1516
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1517 CxIterator i = cxMapIteratorValues(properties);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1518 cx_foreach(DavProperty*, prop, i) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1519 DavXmlNode pnode;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1520 pnode.type = DAV_XML_ELEMENT;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1521 pnode.namespace = prop->ns->name;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1522 pnode.name = prop->name;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1523 pnode.prev = NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1524 pnode.next = NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1525 pnode.children = prop->value;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1526 pnode.parent = NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1527 pnode.attributes = NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1528 pnode.content = NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1529 pnode.contentlength = 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1530
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1531 dav_print_node(content, (cx_write_func)cxBufferWrite, nsmap, &pnode);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1532 cxBufferPut(content, '\n');
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1533 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1534
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1535 cxBufferPutString(content, "</D:prop>");
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1536
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1537 cxMapDestroy(nsmap);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1538
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1539 // encrypt xml document
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1540 char *crypto_prop_content = aes_encrypt(content->space, content->size, sn->key);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1541 cxBufferDestroy(content);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1542
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1543 DavXmlNode *ret = NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1544 if(crypto_prop_content) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1545 ret = dav_text_node(sn, crypto_prop_content);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1546 free(crypto_prop_content);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1547 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1548 return ret;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1549 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1550
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1551 CxMap* parse_crypto_prop(DavSession *sn, DavKey *key, DavXmlNode *node) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1552 if(!node || node->type != DAV_XML_TEXT || node->contentlength == 0) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1553 return NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1554 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1555
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1556 return parse_crypto_prop_str(sn, key, node->content);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1557 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1558
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1559 CxMap* parse_crypto_prop_str(DavSession *sn, DavKey *key, const char *content) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1560 size_t len = 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1561 char *dec_str = aes_decrypt(content, &len, key);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1562
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1563 xmlDoc *doc = xmlReadMemory(dec_str, len, NULL, NULL, 0);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1564 free(dec_str);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1565 if(!doc) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1566 return NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1567 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1568
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1569 int err = 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1570 xmlNode *xml_root = xmlDocGetRootElement(doc);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1571 if(xml_root) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1572 if(
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1573 !xml_root->ns ||
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1574 !xstreq(xml_root->name, "prop") ||
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1575 !xstreq(xml_root->ns->href, "DAV:"))
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1576 {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1577 err = 1;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1578 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1579 } else {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1580 err = 1;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1581 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1582
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1583 if(err) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1584 xmlFreeDoc(doc);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1585 return NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1586 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1587
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1588 // ready to get the properties
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1589 CxMap *map = cxHashMapCreate(cxDefaultAllocator, CX_STORE_POINTERS, 32);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1590 xmlNode *n = xml_root->children;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1591 while(n) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1592 if(n->type == XML_ELEMENT_NODE && n->ns && n->ns->href) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1593 DavProperty *property = dav_session_malloc(sn, sizeof(DavProperty));
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1594 property->name = dav_session_strdup(sn, (const char*)n->name);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1595 property->ns = dav_session_malloc(sn, sizeof(DavNamespace));
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1596 property->ns->name = dav_session_strdup(sn, (const char*)n->ns->href);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1597 property->ns->prefix = n->ns->prefix ?
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1598 dav_session_strdup(sn, (const char*)n->ns->prefix) : NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1599 property->value = n->children ? dav_convert_xml(sn, n->children) : NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1600
49
2f71f4ee247a update toolkit, ucx, libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 20
diff changeset
1601 cxmutstr propkey = dav_property_key(property->ns->name, property->name);
2f71f4ee247a update toolkit, ucx, libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 20
diff changeset
1602 cxMapPut(map, cx_hash_key_cxstr(propkey), property);
2f71f4ee247a update toolkit, ucx, libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 20
diff changeset
1603 cx_strfree(&propkey);
1
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1604 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1605 n = n->next;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1606 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1607
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1608 xmlFreeDoc(doc);
49
2f71f4ee247a update toolkit, ucx, libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 20
diff changeset
1609 if(cxMapSize(map) == 0) {
1
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1610 cxMapDestroy(map);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1611 return NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1612 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1613 return map;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1614 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1615
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1616
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1617 /* ----------------------------- streams ----------------------------- */
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1618
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1619 static size_t in_write(const char *ptr, size_t size, size_t nitems, void *in_stream) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1620 DavInputStream *in = in_stream;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1621 size_t len = size * nitems;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1622
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1623 if(in->alloc < len) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1624 char *newb = realloc(in->buffer, len);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1625 if(!newb) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1626 if(in->buffer) free(in->buffer);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1627 in->eof = 1;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1628 return 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1629 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1630
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1631 in->buffer = newb;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1632 in->alloc = len;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1633 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1634
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1635 memcpy(in->buffer, ptr, len);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1636
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1637 in->size = len;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1638 in->pos = 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1639
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1640 return nitems;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1641 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1642
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1643 /*
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1644 DavInputStream* dav_inputstream_open(DavResource *res) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1645 DavSession *sn = res->session;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1646
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1647 DavInputStream *in = dav_session_malloc(sn, sizeof(DavInputStream));
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1648 if(!in) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1649 return NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1650 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1651 memset(in, 0, sizeof(DavInputStream));
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1652
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1653 in->res = res;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1654
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1655 in->c = curl_easy_duphandle(sn->handle);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1656 char *url = util_get_url(sn, dav_resource_get_href(res));
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1657 curl_easy_setopt(in->c, CURLOPT_URL, url);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1658 free(url);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1659
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1660 in->m = curl_multi_init();
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1661
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1662 curl_easy_setopt(in->c, CURLOPT_HTTPHEADER, NULL);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1663 curl_easy_setopt(in->c, CURLOPT_CUSTOMREQUEST, NULL);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1664 curl_easy_setopt(in->c, CURLOPT_PUT, 0L);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1665 curl_easy_setopt(in->c, CURLOPT_UPLOAD, 0L);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1666
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1667 curl_multi_add_handle(in->m, in->c);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1668
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1669 dav_write_func write_fnc = (dav_write_func)in_write;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1670 void *stream = in;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1671
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1672 // check encryption
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1673 AESDecrypter *dec = NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1674 DavKey *key = NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1675 if(DAV_DECRYPT_CONTENT(sn)) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1676 char *keyname = dav_get_string_property_ns(res, DAV_NS, "crypto-key");
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1677 if(keyname) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1678 key = dav_context_get_key(sn->context, keyname);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1679 if(key) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1680 dec = aes_decrypter_new(key, stream, write_fnc);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1681 stream = dec;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1682 write_fnc = (dav_write_func)aes_write;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1683 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1684 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1685 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1686
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1687 curl_easy_setopt(in->c, CURLOPT_WRITEFUNCTION, write_fnc);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1688 curl_easy_setopt(in->c, CURLOPT_WRITEDATA, stream);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1689
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1690 in->dec = dec;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1691
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1692 return in;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1693 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1694
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1695 size_t dav_read(void *buf, size_t size, size_t nitems, DavInputStream *in) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1696 size_t len = in->size - in->pos;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1697 size_t rl = size * nitems;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1698 if(len > 0) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1699 len = rl > len ? len : rl;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1700 len -= len % size;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1701 memcpy(buf, in->buffer + in->pos, len);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1702 in->pos += len;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1703 return len / size;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1704 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1705 in->size = 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1706
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1707 if(in->eof) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1708 if(in->dec) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1709 aes_decrypter_shutdown(in->dec); // get final bytes
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1710 aes_decrypter_close(in->dec);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1711 in->dec = NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1712 } else {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1713 return 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1714 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1715 } else {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1716 int running;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1717 while(!in->eof && in->size == 0) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1718 CURLMcode r = curl_multi_perform(in->m, &running);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1719 if(r != CURLM_OK || running == 0) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1720 in->eof = 1;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1721 break;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1722 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1723
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1724 int numfds;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1725 if(curl_multi_poll(in->m, NULL, 0, 5000, &numfds) != CURLM_OK) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1726 in->eof = 1;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1727 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1728 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1729 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1730
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1731 return in->size > 0 ? dav_read(buf, size, nitems, in) : 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1732 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1733
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1734 void dav_inputstream_close(DavInputStream *in) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1735 curl_multi_cleanup(in->m);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1736 curl_easy_cleanup(in->c);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1737 if(in->buffer) free(in->buffer);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1738 dav_session_free(in->res->session, in);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1739 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1740
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1741
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1742 static size_t out_read(char *ptr, size_t size, size_t nitems, void *out_stream) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1743 DavOutputStream *out = out_stream;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1744 size_t len = size * nitems;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1745 size_t available = out->size - out->pos;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1746 if(available == 0) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1747 return 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1748 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1749
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1750 size_t r = len > available ? available : len;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1751 r -= r % size;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1752 memcpy(ptr, out->buffer + out->pos, r);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1753
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1754 out->pos += r;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1755
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1756 return r / size;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1757 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1758
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1759 static size_t dummy_write(void *buf, size_t s, size_t n, void *data) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1760 return s*n;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1761 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1762
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1763 DavOutputStream* dav_outputstream_open(DavResource *res) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1764 DavSession *sn = res->session;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1765
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1766 DavOutputStream *out = dav_session_malloc(sn, sizeof(DavOutputStream));
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1767 if(!out) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1768 return NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1769 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1770 memset(out, 0, sizeof(DavOutputStream));
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1771
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1772 out->res = res;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1773
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1774 out->c = curl_easy_duphandle(sn->handle);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1775 char *url = util_get_url(sn, dav_resource_get_href(res));
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1776 curl_easy_setopt(out->c, CURLOPT_URL, url);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1777 free(url);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1778
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1779 out->m = curl_multi_init();
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1780 curl_multi_add_handle(out->m, out->c);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1781
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1782 void *stream = out;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1783 dav_read_func read_fnc = (dav_read_func)out_read;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1784
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1785 // if encryption or hashing in enabled, we need a stream wrapper
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1786 if(DAV_ENCRYPT_CONTENT(sn) && sn->key) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1787 AESEncrypter *enc = aes_encrypter_new(sn->key, out, (dav_read_func)out_read, NULL);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1788 out->enc = enc;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1789 stream = enc;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1790 read_fnc = (dav_read_func)aes_read;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1791 } else if((sn->flags & DAV_SESSION_STORE_HASH) == DAV_SESSION_STORE_HASH) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1792 HashStream *hstr = dav_session_malloc(sn, sizeof(HashStream));
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1793 out->hstr = hstr;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1794 init_hash_stream(hstr, out, (dav_read_func)out_read, NULL);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1795 stream = hstr;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1796 read_fnc = (dav_read_func)dav_read_h;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1797 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1798
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1799 curl_easy_setopt(out->c, CURLOPT_HEADERFUNCTION, NULL);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1800 curl_easy_setopt(out->c, CURLOPT_HTTPHEADER, NULL);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1801 curl_easy_setopt(out->c, CURLOPT_CUSTOMREQUEST, NULL);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1802 curl_easy_setopt(out->c, CURLOPT_PUT, 1L);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1803 curl_easy_setopt(out->c, CURLOPT_UPLOAD, 1L);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1804 curl_easy_setopt(out->c, CURLOPT_READFUNCTION, read_fnc);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1805 curl_easy_setopt(out->c, CURLOPT_READDATA, stream);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1806 curl_easy_setopt(out->c, CURLOPT_SEEKFUNCTION, NULL);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1807 curl_easy_setopt(out->c, CURLOPT_INFILESIZE, -1);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1808 curl_easy_setopt(out->c, CURLOPT_INFILESIZE_LARGE, -1L);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1809
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1810 curl_easy_setopt(out->c, CURLOPT_WRITEFUNCTION, dummy_write);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1811 curl_easy_setopt(out->c, CURLOPT_WRITEDATA, NULL);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1812
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1813 return out;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1814 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1815
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1816 size_t dav_write(const void *buf, size_t size, size_t nitems, DavOutputStream *out) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1817 if(out->eof) return 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1818
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1819 out->buffer = buf;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1820 out->size = size * nitems;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1821 out->pos = 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1822
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1823 int running;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1824 while(!out->eof && (out->size == 0 || out->size - out->pos > 0)) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1825 CURLMcode r = curl_multi_perform(out->m, &running);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1826 if(r != CURLM_OK || running == 0) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1827 out->eof = 1;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1828 break;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1829 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1830
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1831 int numfds;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1832 if(curl_multi_poll(out->m, NULL, 0, 5000, &numfds) != CURLM_OK) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1833 out->eof = 1;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1834 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1835 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1836
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1837 return (out->size - out->pos) / size;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1838 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1839
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1840 int dav_outputstream_close(DavOutputStream *out) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1841 DavSession *sn = out->res->session;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1842 DavResource *res = out->res;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1843 DavResourceData *data = res->data;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1844
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1845 int ret = 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1846
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1847 dav_write(NULL, 1, 0, out);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1848
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1849 curl_multi_cleanup(out->m);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1850 curl_easy_cleanup(out->c);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1851
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1852 int store = 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1853 if(out->enc) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1854 // get sha256 hash
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1855 char hash[32];
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1856 dav_get_hash(&out->enc->sha256, (unsigned char*)data->hash);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1857 aes_encrypter_close(out->enc);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1858 char *enc_hash = aes_encrypt(hash, DAV_SHA256_DIGEST_LENGTH, sn->key);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1859 // add crypto properties
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1860 if(resource_add_crypto_info(sn, out->res->href, out->res->name, enc_hash)) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1861 ret = 1;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1862 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1863 free(enc_hash);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1864 } else if(out->hstr) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1865 dav_hash_final(out->hstr->sha, (unsigned char*)data->hash);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1866 char *hash = util_hexstr((unsigned char*)data->hash, 32);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1867 dav_set_string_property_ns(res, DAV_NS, "content-hash", hash);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1868 free(hash);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1869 dav_session_free(sn, out->hstr);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1870 store = 1;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1871 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1872
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1873 if(store) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1874 ret = dav_store(out->res);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1875 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1876
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1877 dav_session_free(out->res->session, out);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1878
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1879 return ret;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1880 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1881
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1882 */

mercurial