libidav/webdav.c

Sun, 28 Jan 2024 16:31:34 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 28 Jan 2024 16:31:34 +0100
changeset 3
f154867f54dc
parent 1
b5bb7b3cd597
child 18
af411868ab9b
permissions
-rw-r--r--

add nav buttons, update toolkit

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 <libxml/tree.h>
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
33
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
34 #include "utils.h"
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
35 #include "webdav.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 <cx/buffer.h>
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
39 #include <cx/utils.h>
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
40 #include <cx/linked_list.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/compare.h>
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
43 #include "davqlparser.h"
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
44 #include "davqlexec.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
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
47 DavContext* dav_context_new(void) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
48 // initialize
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
49 DavContext *context = calloc(1, sizeof(DavContext));
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
50 if(!context) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
51 return NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
52 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
53 context->sessions = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_intptr, CX_STORE_POINTERS);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
54 context->sessions->destructor_data = dav_session_destructor;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
55 context->http_proxy = calloc(1, sizeof(DavProxy));
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
56 if(!context->http_proxy) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
57 dav_context_destroy(context);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
58 return NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
59 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
60 context->https_proxy = calloc(1, sizeof(DavProxy));
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
61 if(!context->https_proxy) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
62 dav_context_destroy(context);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
63 return NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
64 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
65 context->namespaces = cxHashMapCreate(cxDefaultAllocator, CX_STORE_POINTERS, 16);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
66 if(!context->namespaces) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
67 dav_context_destroy(context);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
68 return NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
69 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
70 context->namespaceinfo = cxHashMapCreate(cxDefaultAllocator, CX_STORE_POINTERS, 16);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
71 if(!context->namespaceinfo) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
72 dav_context_destroy(context);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
73 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
74 context->keys = cxHashMapCreate(cxDefaultAllocator, CX_STORE_POINTERS, 16);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
75 if(!context->keys) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
76 dav_context_destroy(context);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
77 return NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
78 }
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 // add DAV: namespace
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
81 if(dav_add_namespace(context, "D", "DAV:")) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
82 dav_context_destroy(context);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
83 return NULL;
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
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
87 // add idav namespace
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
88 if(dav_add_namespace(context, "idav", DAV_NS)) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
89 dav_context_destroy(context);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
90 return NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
91 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
92
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
93 // add idavprops namespace
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
94 if(dav_add_namespace(context, "idavprops", DAV_PROPS_NS)) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
95 dav_context_destroy(context);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
96 return NULL;
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
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
99 return context;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
100 }
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 void dav_context_destroy(DavContext *ctx) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
103 // destroy all sessions assoziated with this context
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
104 // ctx->sessions destructor must be dav_session_destructor
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
105 cxListDestroy(ctx->sessions);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
106
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
107 if(ctx->http_proxy) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
108 free(ctx->http_proxy);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
109 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
110 if(ctx->https_proxy) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
111 free(ctx->https_proxy);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
112 }
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 if(ctx->namespaces) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
115 CxIterator i = cxMapIteratorValues(ctx->namespaces);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
116 cx_foreach(DavNamespace*, ns, i) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
117 if(!ns) continue;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
118 if(ns->prefix) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
119 free(ns->prefix);
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 if(ns->name) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
122 free(ns->name);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
123 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
124 free(ns);
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 cxMapDestroy(ctx->namespaces);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
127 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
128 if(ctx->namespaceinfo) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
129 // TODO: implement
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
130 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
131 if(ctx->keys) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
132 CxIterator i = cxMapIteratorValues(ctx->keys);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
133 cx_foreach(DavKey*, key, i) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
134 if(!key) continue;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
135 if(key->name) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
136 free(key->name);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
137 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
138 if(key->data) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
139 free(key->data);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
140 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
141 free(key);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
142 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
143 cxMapDestroy(ctx->keys);
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 free(ctx);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
147 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
148
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
149 void dav_context_add_key(DavContext *context, DavKey *key) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
150 cxMapPut(context->keys, cx_hash_key_str(key->name), key);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
151 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
152
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
153 DavKey* dav_context_get_key(DavContext *context, const char *name) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
154 if(name) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
155 return cxMapGet(context->keys, cx_hash_key_str(name));
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 return NULL;
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
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
160 int dav_add_namespace(DavContext *context, const char *prefix, const char *name) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
161 DavNamespace *namespace = malloc(sizeof(DavNamespace));
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
162 if(!namespace) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
163 return 1;
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
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
166 char *p = strdup(prefix);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
167 char *n = strdup(name);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
168
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
169 int err = 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
170 if(p && n) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
171 namespace->prefix = p;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
172 namespace->name = n;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
173 err = cxMapPut(context->namespaces, cx_hash_key_str(prefix), namespace);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
174 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
175
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
176 if(err) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
177 free(namespace);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
178 if(p) free(p);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
179 if(n) free(n);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
180 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
181
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
182 return err;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
183 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
184
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
185 DavNamespace* dav_get_namespace(DavContext *context, const char *prefix) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
186 return cxMapGet(context->namespaces, cx_hash_key_str(prefix));
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
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
189 DavNamespace* dav_get_namespace_s(DavContext *context, cxstring prefix) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
190 return cxMapGet(context->namespaces, cx_hash_key(prefix.ptr, prefix.length));
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 int dav_enable_namespace_encryption(DavContext *context, const char *ns, DavBool encrypt) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
194 CxHashKey hkey = cx_hash_key_str(ns);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
195 DavNSInfo *info = cxMapGet(context->namespaceinfo, hkey);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
196 if(!info) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
197 info = calloc(1, sizeof(DavNSInfo));
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
198 info->encrypt = encrypt;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
199 cxMapPut(context->namespaceinfo, hkey, info);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
200 } else {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
201 info->encrypt = encrypt;
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 return 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
204 }
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 int dav_namespace_is_encrypted(DavContext *context, const char *ns) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
207 DavNSInfo *info = cxMapGet(context->namespaceinfo, cx_hash_key_str(ns));
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
208 if(info) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
209 return info->encrypt;
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 return 0;
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
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
214 void dav_get_property_namespace_str(
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
215 DavContext *ctx,
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
216 char *prefixed_name,
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
217 char **ns,
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
218 char **name)
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
219 {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
220 // TODO: rewrite using dav_get_property_ns
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
221
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
222 char *pname = strchr(prefixed_name, ':');
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
223 char *pns = "DAV:";
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
224 if(pname) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
225 DavNamespace *ns = dav_get_namespace_s(
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
226 ctx,
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
227 cx_strn(prefixed_name, pname-prefixed_name));
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
228 if(ns) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
229 pns = ns->name;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
230 pname++;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
231 } else {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
232 pns = NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
233 pname = NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
234 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
235 } else {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
236 pname = prefixed_name;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
237 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
238 *ns = pns;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
239 *name = pname;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
240 }
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 DavNamespace* dav_get_property_namespace(
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
243 DavContext *ctx,
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
244 char *prefixed_name,
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
245 char **name)
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 char *pname = strchr(prefixed_name, ':');
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
248 if(pname) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
249 DavNamespace *ns = dav_get_namespace_s(
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
250 ctx,
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
251 cx_strn(prefixed_name, pname-prefixed_name));
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
252 if(ns) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
253 *name = pname +1;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
254 return ns;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
255 } else {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
256 *name = NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
257 return NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
258 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
259 } else {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
260 *name = prefixed_name;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
261 return dav_get_namespace_s(ctx, cx_str("D"));
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
262 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
263 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
264
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
265 // TODO: add sstr_t version of dav_get_property_ns
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
266
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
267 void dav_set_effective_href(DavSession *sn, DavResource *resource) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
268 char *eff_url;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
269 curl_easy_getinfo(sn->handle, CURLINFO_EFFECTIVE_URL, &eff_url);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
270 if(eff_url) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
271 const char *href = util_url_path(eff_url);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
272 if(strcmp(href, resource->href)) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
273 dav_session_free(sn, resource->href);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
274 resource->href = dav_session_strdup(sn, href);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
275 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
276 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
277 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
278
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
279 DavResource* dav_get(DavSession *sn, char *path, const char *properties) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
280 CURL *handle = sn->handle;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
281 DavResource *resource = dav_resource_new(sn, path);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
282 util_set_url(sn, dav_resource_get_href(resource));
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 CxList *proplist = NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
285 if(properties) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
286 proplist = parse_properties_string(sn->context, cx_str(properties));
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
287 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
288 CxBuffer *rqbuf = create_propfind_request(sn, proplist, "propfind", 0);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
289 CxBuffer *rpbuf = 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
290
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
291 //fwrite(rqbuf->space, 1, rqbuf->size, stdout);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
292 //printf("\n");
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
293
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
294 CURLcode ret = do_propfind_request(sn, rqbuf, rpbuf);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
295 long status = 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
296 curl_easy_getinfo (handle, CURLINFO_RESPONSE_CODE, &status);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
297 if(ret == CURLE_OK && status == 207) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
298 dav_set_effective_href(sn, resource);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
299
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
300 //printf("response\n%s\n", rpbuf->space);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
301 // TODO: use PropfindParser
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
302 resource = parse_propfind_response(sn, resource, rpbuf);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
303 resource->exists = 1;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
304 sn->error = DAV_OK;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
305 } else {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
306 dav_session_set_error(sn, ret, status);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
307 dav_resource_free(resource);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
308 resource = NULL;
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
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
311 cxBufferFree(rqbuf);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
312 cxBufferFree(rpbuf);
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 if(proplist) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
315 CxIterator i = cxListIterator(proplist);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
316 cx_foreach(DavProperty*, p, i) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
317 free(p->name);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
318 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
319 cxListDestroy(proplist);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
320 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
321
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
322 return resource;
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
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
325
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
326 int dav_propfind(DavSession *sn, DavResource *root, CxBuffer *rqbuf) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
327 // clean resource properties
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
328 DavResourceData *data = root->data;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
329 cxMapClear(data->properties); // TODO: free existing content
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
330
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
331 CURL *handle = sn->handle;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
332 util_set_url(sn, dav_resource_get_href(root));
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
333
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
334 CxBuffer *rpbuf = 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
335 DavResource *resource = root;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
336 CURLcode ret = do_propfind_request(sn, rqbuf, rpbuf);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
337 long status = 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
338 long error = 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
339 curl_easy_getinfo (handle, CURLINFO_RESPONSE_CODE, &status);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
340 if(ret == CURLE_OK && status == 207) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
341 //printf("response\n%s\n", rpbuf->space);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
342 dav_set_effective_href(sn, resource);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
343 // TODO: use PropfindParser
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
344 resource = parse_propfind_response(sn, resource, rpbuf);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
345 sn->error = DAV_OK;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
346 root->exists = 1;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
347 } else {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
348 dav_session_set_error(sn, ret, status);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
349 error = 1;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
350 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
351 cxBufferFree(rpbuf);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
352 return error;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
353 }
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 CxList* parse_properties_string(DavContext *context, cxstring str) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
356 CxList *proplist = cxLinkedListCreateSimple(sizeof(DavProperty));
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
357
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
358 CxStrtokCtx tok = cx_strtok(str, cx_str(","), INT_MAX);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
359 cxstring s;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
360 while(cx_strtok_next(&tok, &s)) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
361 cxstring nsname = cx_strchr(s, ':');
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
362 if(nsname.length > 0) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
363 cxstring nspre = cx_strsubsl(s, 0, nsname.ptr - s.ptr);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
364 nsname.ptr++;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
365 nsname.length--;
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 DavProperty dp;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
368 cxstring pre = cx_strtrim(nspre);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
369 dp.ns = dav_get_namespace_s(context, pre);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
370 dp.name = cx_strdup(nsname).ptr;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
371 dp.value = NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
372 if(dp.ns && dp.name) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
373 cxListAdd(proplist, &dp);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
374 } else {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
375 free(dp.name);
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 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
378 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
379
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
380 return proplist;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
381 }
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 DavResource* dav_query(DavSession *sn, char *query, ...) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
384 DavQLStatement *stmt = dav_parse_statement(cx_str(query));
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
385 if(!stmt) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
386 sn->error = DAV_ERROR;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
387 return NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
388 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
389 if(stmt->errorcode != 0) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
390 sn->error = DAV_QL_ERROR;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
391 dav_free_statement(stmt);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
392 return NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
393 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
394
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
395 va_list ap;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
396 va_start(ap, query);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
397 DavResult result = dav_statement_execv(sn, stmt, ap);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
398 va_end(ap);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
399
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
400 dav_free_statement(stmt);
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 if(result.status == -1) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
403 if(result.result) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
404 dav_resource_free(result.result);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
405 result.result = NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
406 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
407 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
408
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
409 return result.result;
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
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
412
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
413
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 void dav_verbose_log(
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
416 DavSession *sn,
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
417 const char *method,
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
418 const char *url,
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
419 const char *request_body,
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
420 size_t request_bodylen,
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
421 int status,
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
422 const char *response_body,
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
423 size_t response_bodylen)
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 fprintf(stderr, "# method: %s url: %s status: %d\n", method, url, status);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
426 fprintf(stderr, "# request len: %d\n%.*s\n", (int)request_bodylen, (int)request_bodylen, request_body);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
427 fprintf(stderr, "# response len: %d\n%.*s\n", (int)response_bodylen, (int)response_bodylen, response_body);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
428 }

mercurial