libidav/session.c

Thu, 03 Oct 2024 18:08:29 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Thu, 03 Oct 2024 18:08:29 +0200
changeset 46
31bc86844659
parent 18
af411868ab9b
child 49
2f71f4ee247a
permissions
-rw-r--r--

implement action_newfile

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
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
33 #include <cx/buffer.h>
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
34 #include <cx/utils.h>
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
35 #include <cx/mempool.h>
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
36 #include <cx/hash_map.h>
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
37
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
38 #include "utils.h"
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
39 #include "session.h"
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
40 #include "resource.h"
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
41 #include "methods.h"
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
42
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
43 DavSession* dav_session_new(DavContext *context, char *base_url) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
44 if(!base_url) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
45 return NULL;
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 cxstring url = cx_str(base_url);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
48 if(url.length == 0) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
49 return NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
50 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
51 DavSession *sn = malloc(sizeof(DavSession));
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
52 memset(sn, 0, sizeof(DavSession));
18
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
53 sn->mp = cxBasicMempoolCreate(DAV_SESSION_MEMPOOL_SIZE);
1
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
54 sn->pathcache = cxHashMapCreate(sn->mp->allocator, CX_STORE_POINTERS, DAV_PATH_CACHE_SIZE);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
55 sn->key = NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
56 sn->errorstr = NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
57 sn->error = DAV_OK;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
58 sn->flags = 0;
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 dav_session_set_baseurl(sn, base_url);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
61
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
62 sn->handle = curl_easy_init();
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
63 curl_easy_setopt(sn->handle, CURLOPT_FOLLOWLOCATION, 1L);
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 // lock manager is created on-demand
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
66 sn->locks = NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
67
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
68 // set proxy
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
69 DavProxy *proxy = cx_strprefix(url, CX_STR("https")) ? context->https_proxy
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
70 : context->http_proxy;
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 if (proxy->url) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
73 curl_easy_setopt(sn->handle, CURLOPT_PROXY, proxy->url);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
74 if (proxy->username) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
75 curl_easy_setopt(sn->handle, CURLOPT_PROXYUSERNAME,
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
76 proxy->username);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
77 if (proxy->password) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
78 curl_easy_setopt(sn->handle, CURLOPT_PROXYPASSWORD,
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
79 proxy->password);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
80 } else {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
81 // TODO: prompt
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 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
84 if(proxy->no_proxy) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
85 curl_easy_setopt(sn->handle, CURLOPT_NOPROXY,
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
86 proxy->no_proxy);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
87 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
88 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
89
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
90 // set url
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
91 #if LIBCURL_VERSION_NUM >= 0x072D00
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
92 curl_easy_setopt(sn->handle, CURLOPT_DEFAULT_PROTOCOL, "http");
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
93 #endif
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
94 curl_easy_setopt(sn->handle, CURLOPT_URL, base_url);
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 // add to context
18
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
97 dav_context_add_session(context, sn);
1
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
98 sn->context = context;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
99
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
100 return sn;
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 DavSession* dav_session_new_auth(
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
104 DavContext *context,
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
105 char *base_url,
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
106 char *user,
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
107 char *password)
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 DavSession *sn = dav_session_new(context, base_url);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
110 if(!sn) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
111 return NULL;
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 dav_session_set_auth(sn, user, password);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
114 return sn;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
115 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
116
18
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
117 DavSession* dav_session_clone(DavSession *sn) {
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
118 CURL *newhandle = curl_easy_duphandle(sn->handle);
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
119
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
120 DavSession *newsn = malloc(sizeof(DavSession));
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
121 memset(newsn, 0, sizeof(DavSession));
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
122 newsn->mp = cxMempoolCreate(DAV_SESSION_MEMPOOL_SIZE, NULL);
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
123 newsn->pathcache = cxHashMapCreate(sn->mp->allocator, CX_STORE_POINTERS, DAV_PATH_CACHE_SIZE);
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
124 newsn->key = sn->key;
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
125 newsn->errorstr = NULL;
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
126 newsn->error = DAV_OK;
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
127 newsn->flags = 0;
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
128
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
129 newsn->handle = newhandle;
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
130
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
131 newsn->base_url = cx_strdup_a(newsn->mp->allocator, cx_str(sn->base_url)).ptr;
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
132 newsn->auth_prompt = sn->auth_prompt;
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
133 newsn->authprompt_userdata = sn->authprompt_userdata;
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
134 newsn->logfunc = sn->logfunc;
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
135 newsn->get_progress = sn->get_progress;
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
136 newsn->put_progress = sn->put_progress;
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
137 newsn->progress_userdata = sn->progress_userdata;
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
138
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
139 // add to context
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
140 dav_context_add_session(sn->context, newsn);
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
141 newsn->context = sn->context;
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
142
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
143 return newsn;
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
144 }
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
145
2
fbdfaacc4182 update ucx, libidav and add first gui code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
146 void dav_session_set_auth(DavSession *sn, const char *user, const char *password) {
1
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
147 if(user && password) {
2
fbdfaacc4182 update ucx, libidav and add first gui code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
148 dav_session_set_auth_s(sn, cx_str(user), cx_str(password));
fbdfaacc4182 update ucx, libidav and add first gui code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
149 }
fbdfaacc4182 update ucx, libidav and add first gui code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
150 }
fbdfaacc4182 update ucx, libidav and add first gui code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
151
fbdfaacc4182 update ucx, libidav and add first gui code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
152 void dav_session_set_auth_s(DavSession *sn, cxstring user, cxstring password) {
fbdfaacc4182 update ucx, libidav and add first gui code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
153 if(user.length > 0 && password.length > 0) {
fbdfaacc4182 update ucx, libidav and add first gui code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
154 size_t upwdlen = user.length + password.length + 2;
1
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
155 char *upwdbuf = malloc(upwdlen);
2
fbdfaacc4182 update ucx, libidav and add first gui code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
156 snprintf(upwdbuf, upwdlen, "%.*s:%.*s", (int)user.length, user.ptr, (int)password.length, password.ptr);
1
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
157 curl_easy_setopt(sn->handle, CURLOPT_USERPWD, upwdbuf);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
158 free(upwdbuf);
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 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
161
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
162 void dav_session_set_baseurl(DavSession *sn, char *base_url) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
163 const CxAllocator *a = sn->mp->allocator;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
164 if(sn->base_url) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
165 cxFree(a, sn->base_url);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
166 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
167
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
168 cxstring url = cx_str(base_url);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
169 if(url.ptr[url.length - 1] == '/') {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
170 cxmutstr url_m = cx_strdup_a(a, cx_str(base_url));
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
171 sn->base_url = url_m.ptr;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
172 } else {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
173 char *url_str = cxMalloc(a, url.length + 2);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
174 memcpy(url_str, base_url, url.length);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
175 url_str[url.length] = '/';
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
176 url_str[url.length + 1] = '\0';
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
177 sn->base_url = url_str;
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 }
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 void dav_session_enable_encryption(DavSession *sn, DavKey *key, int flags) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
182 sn->key = key;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
183 // TODO: review sanity
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
184 if(flags != 0) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
185 sn->flags |= flags;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
186 } else {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
187 sn->flags |= DAV_SESSION_ENCRYPT_CONTENT;
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 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
190
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
191 void dav_session_set_authcallback(DavSession *sn, dav_auth_func func, void *userdata) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
192 sn->auth_prompt = func;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
193 sn->authprompt_userdata = userdata;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
194 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
195
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
196 void dav_session_set_progresscallback(DavSession *sn, dav_progress_func get, dav_progress_func put, void *userdata) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
197 sn->get_progress = get;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
198 sn->put_progress = put;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
199 sn->progress_userdata = userdata;
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
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
202 CURLcode dav_session_curl_perform(DavSession *sn, long *status) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
203 return dav_session_curl_perform_buf(sn, NULL, NULL, status);
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 CURLcode dav_session_curl_perform_buf(DavSession *sn, CxBuffer *request, CxBuffer *response, long *status) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
207 CURLcode ret = curl_easy_perform(sn->handle);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
208 long http_status;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
209 curl_easy_getinfo(sn->handle, CURLINFO_RESPONSE_CODE, &http_status);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
210 if(ret == CURLE_OK) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
211 if(sn->logfunc) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
212 char *log_method;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
213 char *log_url;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
214 curl_easy_getinfo(sn->handle, CURLINFO_EFFECTIVE_URL, &log_url);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
215 curl_easy_getinfo(sn->handle, CURLINFO_EFFECTIVE_METHOD , &log_method);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
216 char *log_reqbody = NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
217 size_t log_reqbodylen = 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
218 char *log_rpbody = NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
219 size_t log_rpbodylen = 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
220 if(request) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
221 log_reqbody = request->space;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
222 log_reqbodylen = request->size;
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 if(response) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
225 log_rpbody = response->space;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
226 log_rpbodylen = response->size;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
227 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
228 sn->logfunc(sn, log_method, log_url, log_reqbody, log_reqbodylen, http_status, log_rpbody, log_rpbodylen);
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
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
231 if(http_status == 401 && sn->auth_prompt) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
232 if(!sn->auth_prompt(sn, sn->authprompt_userdata)) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
233 if(request) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
234 cxBufferSeek(request, 0, SEEK_SET);
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 if(response) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
237 cxBufferSeek(response, 0, SEEK_SET);
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 ret = curl_easy_perform(sn->handle);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
240 curl_easy_getinfo(sn->handle, CURLINFO_RESPONSE_CODE, &http_status);
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 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
243
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
244 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
245
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
246 if(status) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
247 *status = http_status;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
248 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
249 return ret;
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
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
252 int dav_session_get_progress(void *clientp, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
253 DavResource *res = clientp;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
254 DavSession *sn = res->session;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
255 if(sn->get_progress) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
256 sn->get_progress(res, (int64_t)dltotal, (int64_t)dlnow, sn->progress_userdata);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
257 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
258 return 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
259 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
260
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
261 int dav_session_put_progress(void *clientp, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
262 DavResource *res = clientp;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
263 DavSession *sn = res->session;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
264 if(sn->put_progress) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
265 sn->put_progress(res, (int64_t)ultotal, (int64_t)ulnow, sn->progress_userdata);
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 return 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
268 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
269
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
270 void dav_session_set_error(DavSession *sn, CURLcode c, int status) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
271 if(status > 0) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
272 switch(status) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
273 default: {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
274 switch(c) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
275 default: sn->error = DAV_ERROR;
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 break;
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 case 401: sn->error = DAV_UNAUTHORIZED; break;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
280 case 403: sn->error = DAV_FORBIDDEN; break;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
281 case 404: sn->error = DAV_NOT_FOUND; break;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
282 case 405: sn->error = DAV_METHOD_NOT_ALLOWED; break;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
283 case 407: sn->error = DAV_PROXY_AUTH_REQUIRED; break;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
284 case 409: sn->error = DAV_CONFLICT; break;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
285 case 412: sn->error = DAV_PRECONDITION_FAILED; break;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
286 case 413: sn->error = DAV_REQUEST_ENTITY_TOO_LARGE; break;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
287 case 414: sn->error = DAV_REQUEST_URL_TOO_LONG; break;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
288 case 423: sn->error = DAV_LOCKED; break;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
289 case 511: sn->error = DAV_NET_AUTH_REQUIRED; break;
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 } else {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
292 switch(c) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
293 case CURLE_UNSUPPORTED_PROTOCOL: sn->error = DAV_UNSUPPORTED_PROTOCOL; break;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
294 case CURLE_COULDNT_RESOLVE_PROXY: sn->error = DAV_COULDNT_RESOLVE_PROXY; break;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
295 case CURLE_COULDNT_RESOLVE_HOST: sn->error = DAV_COULDNT_RESOLVE_HOST; break;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
296 case CURLE_COULDNT_CONNECT: sn->error = DAV_COULDNT_CONNECT; break;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
297 case CURLE_OPERATION_TIMEDOUT: sn->error = DAV_TIMEOUT; break;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
298 case CURLE_SSL_CONNECT_ERROR:
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
299 case CURLE_PEER_FAILED_VERIFICATION:
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
300 case CURLE_SSL_ENGINE_NOTFOUND:
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
301 case CURLE_SSL_ENGINE_SETFAILED:
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
302 case CURLE_SSL_CERTPROBLEM:
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
303 case CURLE_SSL_CIPHER:
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
304 //#ifndef CURLE_SSL_CACERT
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
305 // case CURLE_SSL_CACERT:
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
306 //#endif
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
307 case CURLE_SSL_CACERT_BADFILE:
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
308 case CURLE_SSL_SHUTDOWN_FAILED:
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
309 case CURLE_SSL_CRL_BADFILE:
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
310 case CURLE_SSL_ISSUER_ERROR: sn->error = DAV_SSL_ERROR; break;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
311 default: sn->error = DAV_ERROR; break;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
312 }
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(c != CURLE_OK) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
315 dav_session_set_errstr(sn, curl_easy_strerror(c));
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
316 } else {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
317 dav_session_set_errstr(sn, NULL);
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 }
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 void dav_session_set_errstr(DavSession *sn, const char *str) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
322 if(sn->errorstr) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
323 dav_session_free(sn, sn->errorstr);
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 char *errstr = NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
326 if(str) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
327 errstr = dav_session_strdup(sn, str);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
328 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
329 sn->errorstr = errstr;
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
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
332 void dav_session_destroy(DavSession *sn) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
333 // remove session from context
18
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
334 if (dav_context_remove_session(sn->context, sn)) {
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
335 fprintf(stderr, "Error: session not found in ctx->sessions\n");
1
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
336 dav_session_destructor(sn);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
337 }
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
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
340 void dav_session_destructor(DavSession *sn) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
341 cxMempoolDestroy(sn->mp);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
342 curl_easy_cleanup(sn->handle);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
343 free(sn);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
344 }
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
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
347 void* dav_session_malloc(DavSession *sn, size_t size) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
348 return cxMalloc(sn->mp->allocator, size);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
349 }
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 void* dav_session_calloc(DavSession *sn, size_t nelm, size_t size) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
352 return cxCalloc(sn->mp->allocator, nelm, size);
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 void* dav_session_realloc(DavSession *sn, void *ptr, size_t size) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
356 return cxRealloc(sn->mp->allocator, ptr, size);
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
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
359 void dav_session_free(DavSession *sn, void *ptr) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
360 cxFree(sn->mp->allocator, ptr);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
361 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
362
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
363 char* dav_session_strdup(DavSession *sn, const char *str) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
364 return cx_strdup_a(sn->mp->allocator, cx_str((char*)str)).ptr;
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 char* dav_session_create_plain_href(DavSession *sn, const char *path) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
369 if(!DAV_ENCRYPT_NAME(sn) && !DAV_DECRYPT_NAME(sn)) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
370 // non encrypted file names
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
371 char *url = util_path_to_url(sn, path);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
372 char *href = dav_session_strdup(sn, util_url_path(url));
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
373 free(url);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
374 return href;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
375 } else {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
376 return NULL;
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 char* dav_session_get_href(DavSession *sn, const char *path) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
381 if(DAV_DECRYPT_NAME(sn) || DAV_ENCRYPT_NAME(sn)) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
382 cxstring p = cx_str(path);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
383 CxBuffer href;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
384 CxBuffer pbuf;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
385 cxBufferInit(&href, NULL, 256, cxDefaultAllocator, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
386 cxBufferInit(&pbuf, NULL, 256, cxDefaultAllocator, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
387
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
388 int start = 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
389 int begin = 0;
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 // check path cache
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
392 char *cp = strdup(path);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
393 //printf("cp: %s\n", cp);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
394 while(strlen(cp) > 1) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
395 char *cached = cxMapGet(sn->pathcache, cx_hash_key_str(cp));
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
396 if(cached) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
397 start = strlen(cp);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
398 begin = start;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
399 cxBufferPutString(&href, cached);
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 } else {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
402 // check, if the parent path is cached
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
403 char *f = cp;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
404 cp = util_parent_path(cp);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
405 free(f);
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 free(cp);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
409 if(href.pos == 0) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
410 // if there are no cached elements we have to add the base url path
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
411 // to the href buffer
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
412 cxBufferPutString(&href, util_url_path(sn->base_url));
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 // create resource for name lookup
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
416 cxmutstr rp = cx_strdup(cx_strn(path, start));
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
417 DavResource *root = dav_resource_new(sn, rp.ptr);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
418 free(rp.ptr);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
419 resource_set_href(root, cx_strn(href.space, href.pos));
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
420
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
421 // create request buffer for propfind requests
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
422 CxBuffer *rqbuf = create_basic_propfind_request();
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
423
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
424 cxstring remaining = cx_strsubs(p, start);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
425 CxStrtokCtx elms = cx_strtok(remaining, CX_STR("/"), INT_MAX);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
426 DavResource *res = root;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
427 cxBufferPutString(&pbuf, res->path);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
428 // iterate over all remaining path elements
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
429 cxstring elm;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
430 while(cx_strtok_next(&elms, &elm)) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
431 if(elm.length > 0) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
432 //printf("elm: %.*s\n", elm.length, elm.ptr);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
433 DavResource *child = dav_find_child(sn, res, rqbuf, elm.ptr);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
434
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
435 // if necessary add a path separator
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
436 if(pbuf.space[pbuf.pos-1] != '/') {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
437 if(href.space[href.pos-1] != '/') {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
438 cxBufferPut(&href, '/');
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
439 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
440 cxBufferPut(&pbuf, '/');
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
441 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
442 // add last path/href to the cache
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
443 cxstring pp = cx_strn(pbuf.space, pbuf.size);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
444 cxstring hh = cx_strn(href.space, href.size);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
445 dav_session_cache_path(sn, pp, hh);
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 cxBufferWrite(elm.ptr, 1, elm.length, &pbuf);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
448 if(child) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
449 // href is already URL encoded, so don't encode again
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
450 cxBufferPutString(&href, util_resource_name(child->href));
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
451 res = child;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
452 } else if(DAV_ENCRYPT_NAME(sn)) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
453 char *random_name = util_random_str();
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
454 cxBufferPutString(&href, random_name);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
455 free(random_name);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
456 } else {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
457 // path is not URL encoded, so we have to do this here
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
458 cxstring resname = cx_str(util_resource_name((const char*)path));
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
459 // the name of collections ends with
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
460 // a trailing slash, which MUST NOT be encoded
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
461 if(resname.ptr[resname.length-1] == '/') {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
462 char *esc = curl_easy_escape(sn->handle,
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
463 resname.ptr, resname.length-1);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
464 cxBufferWrite(esc, 1, strlen(esc), &href);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
465 cxBufferPut(&href, '/');
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
466 curl_free(esc);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
467 } else {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
468 char *esc = curl_easy_escape(sn->handle,
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
469 resname.ptr, resname.length);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
470 cxBufferWrite(esc, 1, strlen(esc), &href);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
471 curl_free(esc);
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 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
476
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
477 // if necessary add a path separator
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
478 if(p.ptr[p.length-1] == '/') {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
479 if(href.space[href.pos-1] != '/') {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
480 cxBufferPut(&href, '/');
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
481 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
482 cxBufferPut(&pbuf, '/');
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 // add the final path to the cache
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
485 cxstring pp = cx_strn(pbuf.space, pbuf.size);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
486 cxstring hh = cx_strn(href.space, href.size);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
487 dav_session_cache_path(sn, pp, hh);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
488
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
489 cxmutstr href_str = cx_strdup_a(
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
490 sn->mp->allocator,
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
491 cx_strn(href.space, href.size));
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
492
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
493 // cleanup
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
494 dav_resource_free_all(root);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
495 cxBufferFree(rqbuf);
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 cxBufferDestroy(&pbuf);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
498 cxBufferDestroy(&href);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
499
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
500 return href_str.ptr;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
501 } else {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
502 return dav_session_create_plain_href(sn, path);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
503 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
504 }
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 DavResource* dav_find_child(DavSession *sn, DavResource *res, CxBuffer *rqbuf, const char *name) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
507 if(res && !dav_propfind(sn, res, rqbuf)) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
508 DavResource *child = res->children;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
509 while(child) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
510 if(!strcmp(child->name, name)) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
511 return child;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
512 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
513 child = child->next;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
514 }
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 return NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
517 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
518
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
519 void dav_session_cache_path(DavSession *sn, cxstring path, cxstring href) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
520 CxHashKey path_key = cx_hash_key(path.ptr, path.length);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
521 char *elm = cxMapGet(sn->pathcache, path_key);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
522 if(!elm) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
523 cxmutstr href_s = cx_strdup_a(sn->mp->allocator, href);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
524 cxMapPut(sn->pathcache, path_key, href_s.ptr);
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
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
528
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
529 DavLock* dav_create_lock(DavSession *sn, const char *token, char *timeout) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
530 DavLock *lock = dav_session_malloc(sn, sizeof(DavLock));
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
531 lock->path = NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
532 lock->token = dav_session_strdup(sn, token);
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 // TODO: timeout
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
535
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
536 return lock;
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
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
539 void dav_destroy_lock(DavSession *sn, DavLock *lock) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
540 dav_session_free(sn, lock->token);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
541 if(lock->path) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
542 dav_session_free(sn, lock->path);
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 dav_session_free(sn, lock);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
545 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
546
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
547
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
548 static int dav_lock_cmp(void const *left, void const *right) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
549 const DavLock *l = left;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
550 const DavLock *r = right;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
551 return strcmp(l->path, r->path);
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
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
554 static int create_lock_manager(DavSession *sn) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
555 // create lock manager
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
556 DavLockManager *locks = cxMalloc(sn->mp->allocator, sizeof(DavLockManager));
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
557 locks->resource_locks = cxHashMapCreate(sn->mp->allocator, CX_STORE_POINTERS, 16);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
558 locks->collection_locks = cxLinkedListCreate(sn->mp->allocator, dav_lock_cmp, CX_STORE_POINTERS);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
559 sn->locks = locks;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
560 return 0;
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 static DavLockManager* get_lock_manager(DavSession *sn) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
564 DavLockManager *locks = sn->locks;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
565 if(!locks) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
566 if(create_lock_manager(sn)) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
567 return NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
568 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
569 locks = sn->locks;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
570 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
571 return locks;
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 int dav_add_resource_lock(DavSession *sn, const char *path, DavLock *lock) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
575 DavLockManager *locks = get_lock_manager(sn);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
576 if(!locks) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
577 return -1;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
578 }
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 CxHashKey path_key = cx_hash_key_str(path);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
581 if(cxMapGet(locks->resource_locks, path_key)) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
582 return -1;
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
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
585 cxMapPut(locks->resource_locks, path_key, lock);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
586 return 0;
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
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
589 int dav_add_collection_lock(DavSession *sn, const char *path, DavLock *lock) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
590 DavLockManager *locks = get_lock_manager(sn);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
591 if(!locks) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
592 return -1;
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 lock->path = dav_session_strdup(sn, path);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
596 cxListAdd(locks->collection_locks, lock);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
597 cxListSort(locks->collection_locks);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
598
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
599 return 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
600 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
601
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
602 DavLock* dav_get_lock(DavSession *sn, const char *path) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
603 DavLockManager *locks = get_lock_manager(sn);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
604 if(!locks) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
605 return NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
606 }
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 cxstring p = cx_str(path);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
609
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
610 DavLock *lock = cxMapGet(locks->resource_locks, cx_hash_key(p.ptr, p.length));
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
611 if(lock) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
612 return lock;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
613 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
614
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
615 CxIterator i = cxListIterator(locks->collection_locks);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
616 cx_foreach(DavLock*, col_lock, i) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
617 int cmd = strcmp(path, col_lock->path);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
618 if(cmd == 0) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
619 return col_lock;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
620 } else if(cx_strprefix(p, cx_str(col_lock->path))) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
621 return col_lock;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
622 } else if(cmd > 0) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
623 break;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
624 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
625 }
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 return NULL;
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
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
630 void dav_remove_lock(DavSession *sn, const char *path, DavLock *lock) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
631 DavLockManager *locks = get_lock_manager(sn);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
632 if(!locks) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
633 return;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
634 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
635
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
636 if(cxMapRemoveAndGet(locks->resource_locks, cx_hash_key_str(path))) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
637 return;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
638 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
639
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
640 CxMutIterator i = cxListMutIterator(locks->collection_locks);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
641 int rm = 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
642 cx_foreach(DavLock* , cl, i) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
643 if(rm) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
644 break;
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 if(cl == lock) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
647 cxIteratorFlagRemoval(i);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
648 rm = 1;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
649 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
650 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
651 }

mercurial