dav/webdav.c

Mon, 19 Aug 2013 17:10:22 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Mon, 19 Aug 2013 17:10:22 +0200
changeset 24
a317202ae787
parent 22
e593f7e41be0
child 25
29f77f1fdbf3
permissions
-rw-r--r--

implemented dav_create for non-collection resources

5
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 /*
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
3 *
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
4 * Copyright 2013 Olaf Wintermann. All rights reserved.
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
5 *
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
8 *
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
11 *
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
15 *
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
88625853ae74 new webdav api + repository and key configuration + aes encryption
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
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
27 */
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
28
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
29 #include <stdio.h>
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
30 #include <stdlib.h>
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
31 #include <string.h>
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
32 #include <libxml/tree.h>
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
33
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
34 #include "utils.h"
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
35 #include "webdav.h"
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
36 #include "methods.h"
17
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
37 #include "davql.h"
5
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
38 #include "ucx/buffer.h"
16
5dbef9e07376 added mkdir command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
39 #include "ucx/utils.h"
5
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
40
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
41 #define xstreq(a,b) xmlStrEqual(BAD_CAST a, BAD_CAST b)
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
42
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
43 DavContext* dav_context_new() {
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
44 DavContext *context = malloc(sizeof(DavContext));
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
45 if(!context) {
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
46 return NULL;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
47 }
19
18efd2c2973d some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 17
diff changeset
48 context->sessions = NULL;
5
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
49 context->namespaces = ucx_map_new(16);
21
78935b45e2ce fixed put bugs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 19
diff changeset
50 context->http_proxy = NULL;
78935b45e2ce fixed put bugs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 19
diff changeset
51 context->https_proxy = NULL;
78935b45e2ce fixed put bugs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 19
diff changeset
52 context->no_proxy = NULL;
5
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
53 if(!context->namespaces) {
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
54 free(context);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
55 return NULL;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
56 }
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
57 DavNamespace *davns = malloc(sizeof(DavNamespace));
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
58 if(!davns) {
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
59 ucx_map_free(context->namespaces);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
60 free(context);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
61 return NULL;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
62 }
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
63 davns->prefix = "D";
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
64 davns->name = "DAV:";
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
65 if(ucx_map_cstr_put(context->namespaces, "D", davns)) {
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
66 free(davns);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
67 ucx_map_free(context->namespaces);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
68 free(context);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
69 return NULL;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
70 }
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
71
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
72 return context;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
73 }
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
74
16
5dbef9e07376 added mkdir command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
75 void dav_context_destroy(DavContext *ctx) {
5dbef9e07376 added mkdir command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
76 // destroy all sessions assoziated with this context
5dbef9e07376 added mkdir command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
77 UCX_FOREACH(elm, ctx->sessions) {
5dbef9e07376 added mkdir command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
78 dav_session_destroy(elm->data);
5dbef9e07376 added mkdir command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
79 }
5dbef9e07376 added mkdir command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
80
5dbef9e07376 added mkdir command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
81 UcxMapIterator i = ucx_map_iterator(ctx->namespaces);
5dbef9e07376 added mkdir command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
82 UcxKey k;
5dbef9e07376 added mkdir command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
83 DavNamespace *ns;
19
18efd2c2973d some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 17
diff changeset
84 // TODO: free map elements
16
5dbef9e07376 added mkdir command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
85 ucx_map_free(ctx->namespaces);
5dbef9e07376 added mkdir command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
86 free(ctx);
5dbef9e07376 added mkdir command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
87 }
5dbef9e07376 added mkdir command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
88
5
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
89 int dav_add_namespace(DavContext *context, char *prefix, char *name) {
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
90 DavNamespace *namespace = malloc(sizeof(DavNamespace));
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
91 if(!namespace) {
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
92 return 1;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
93 }
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
94 namespace->prefix = strdup(prefix);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
95 namespace->name = strdup(name);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
96 return ucx_map_cstr_put(context->namespaces, prefix, namespace);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
97 }
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
98
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
99 DavNamespace* dav_get_namespace(DavContext *context, char *prefix) {
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
100 return ucx_map_cstr_get(context->namespaces, prefix);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
101 }
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
102
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
103 DavNamespace* dav_get_namespace_s(DavContext *context, sstr_t prefix) {
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
104 return ucx_map_sstr_get(context->namespaces, prefix);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
105 }
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
106
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
107 DavSession* dav_session_new(DavContext *context, char *base_url) {
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
108 if(!base_url) {
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
109 return NULL;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
110 }
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
111 sstr_t url = sstr(base_url);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
112 if(url.length == 0) {
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
113 return NULL;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
114 }
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
115 DavSession *sn = malloc(sizeof(DavSession));
22
e593f7e41be0 added more error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
116 sn->errorstr = NULL;
24
a317202ae787 implemented dav_create for non-collection resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
117 sn->error = DAV_OK;
5
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
118 if(url.ptr[url.length - 1] == '/') {
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
119 sn->base_url = strdup(base_url);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
120 } else {
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
121 char *url_str = malloc(url.length + 2);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
122 memcpy(url_str, base_url, url.length);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
123 url_str[url.length] = '/';
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
124 url_str[url.length + 1] = '\0';
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
125 sn->base_url = url_str;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
126 }
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
127 sn->context = context;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
128 sn->handle = curl_easy_init();
24
a317202ae787 implemented dav_create for non-collection resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
129 //curl_easy_setopt(sn->handle, CURLOPT_VERBOSE, 1L);
a317202ae787 implemented dav_create for non-collection resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
130 //curl_easy_setopt(sn->handle, CURLOPT_STDERR, stderr);
21
78935b45e2ce fixed put bugs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 19
diff changeset
131
78935b45e2ce fixed put bugs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 19
diff changeset
132 // set proxy
78935b45e2ce fixed put bugs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 19
diff changeset
133 if(sstrprefix(url, S("https"))) {
78935b45e2ce fixed put bugs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 19
diff changeset
134 if(context->https_proxy) {
78935b45e2ce fixed put bugs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 19
diff changeset
135 //printf("use https_proxy: %s\n", context->https_proxy);
78935b45e2ce fixed put bugs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 19
diff changeset
136 curl_easy_setopt(sn->handle, CURLOPT_PROXY, context->https_proxy);
78935b45e2ce fixed put bugs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 19
diff changeset
137 }
78935b45e2ce fixed put bugs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 19
diff changeset
138 } else {
78935b45e2ce fixed put bugs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 19
diff changeset
139 if(context->http_proxy) {
78935b45e2ce fixed put bugs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 19
diff changeset
140 //printf("use http_proxy: %s\n", context->http_proxy);
78935b45e2ce fixed put bugs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 19
diff changeset
141 curl_easy_setopt(sn->handle, CURLOPT_PROXY, context->http_proxy);
78935b45e2ce fixed put bugs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 19
diff changeset
142 }
78935b45e2ce fixed put bugs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 19
diff changeset
143 }
78935b45e2ce fixed put bugs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 19
diff changeset
144 if(context->no_proxy) {
78935b45e2ce fixed put bugs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 19
diff changeset
145 //printf("use no_proxy: %s\n", context->no_proxy);
78935b45e2ce fixed put bugs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 19
diff changeset
146 curl_easy_setopt(sn->handle, CURLOPT_NOPROXY, context->no_proxy);
78935b45e2ce fixed put bugs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 19
diff changeset
147 }
78935b45e2ce fixed put bugs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 19
diff changeset
148 // set url
5
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
149 curl_easy_setopt(sn->handle, CURLOPT_URL, base_url);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
150
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
151 sn->mp = ucx_mempool_new(1024);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
152 sn->allocator = ucx_mempool_allocator(sn->mp);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
153
16
5dbef9e07376 added mkdir command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
154 context->sessions = ucx_list_append(context->sessions, sn);
5dbef9e07376 added mkdir command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
155
5
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
156 return sn;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
157 }
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
158
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
159 DavSession* dav_session_new_auth(DavContext *context, char *base_url, char *user, char *password) {
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
160 DavSession *sn = dav_session_new(context, base_url);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
161 if(!sn) {
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
162 return NULL;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
163 }
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
164 dav_session_set_auth(sn, user, password);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
165 return sn;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
166 }
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
167
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
168 void dav_session_set_auth(DavSession *sn, char *user, char *password) {
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
169 if(user && password) {
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
170 size_t ulen = strlen(user);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
171 size_t plen = strlen(password);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
172 size_t upwdlen = ulen + plen + 2;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
173 char *upwdbuf = malloc(upwdlen);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
174 snprintf(upwdbuf, upwdlen, "%s:%s\0", user, password);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
175 curl_easy_setopt(sn->handle, CURLOPT_USERPWD, upwdbuf);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
176 free(upwdbuf);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
177 }
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
178 }
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
179
13
8a0cc4d90de7 added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
180 void session_set_error(DavSession *sn, CURLcode c, int status) {
8a0cc4d90de7 added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
181 if(status > 0) {
8a0cc4d90de7 added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
182 switch(status) {
8a0cc4d90de7 added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
183 default: sn->error = DAV_ERROR; break;
8a0cc4d90de7 added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
184 case 401: sn->error = DAV_UNAUTHORIZED; break;
8a0cc4d90de7 added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
185 case 403: sn->error = DAV_FORBIDDEN; break;
8a0cc4d90de7 added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
186 case 404: sn->error = DAV_NOT_FOUND; break;
16
5dbef9e07376 added mkdir command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
187 case 405: sn->error = DAV_METHOD_NOT_ALLOWED; break;
5dbef9e07376 added mkdir command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
188 case 409: sn->error = DAV_CONFLICT; break;
13
8a0cc4d90de7 added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
189 }
8a0cc4d90de7 added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
190 } else {
8a0cc4d90de7 added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
191 sn->error = DAV_ERROR;
8a0cc4d90de7 added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
192 }
22
e593f7e41be0 added more error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
193 if(c != CURLE_OK) {
e593f7e41be0 added more error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
194 sn->errorstr = curl_easy_strerror(c);
e593f7e41be0 added more error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
195 } else {
e593f7e41be0 added more error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
196 sn->errorstr = NULL;
e593f7e41be0 added more error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
197 }
13
8a0cc4d90de7 added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
198 }
8a0cc4d90de7 added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
199
16
5dbef9e07376 added mkdir command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
200 void dav_session_destroy(DavSession *sn) {
5dbef9e07376 added mkdir command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
201 // remove session from context
5dbef9e07376 added mkdir command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
202 UcxList *sessions = sn->context->sessions;
5dbef9e07376 added mkdir command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
203 ssize_t i = ucx_list_find(sessions, sn, ucx_ptrcmp, NULL);
5dbef9e07376 added mkdir command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
204 if(i > 0) {
5dbef9e07376 added mkdir command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
205 UcxList *elm = ucx_list_get(sessions, i);
5dbef9e07376 added mkdir command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
206 if(elm) {
5dbef9e07376 added mkdir command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
207 sn->context->sessions = ucx_list_remove(sessions, elm);
5dbef9e07376 added mkdir command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
208 }
5dbef9e07376 added mkdir command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
209 }
5dbef9e07376 added mkdir command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
210
5dbef9e07376 added mkdir command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
211 ucx_mempool_destroy(sn->mp);
5dbef9e07376 added mkdir command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
212 curl_easy_cleanup(sn->handle);
5dbef9e07376 added mkdir command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
213 free(sn->base_url);
5dbef9e07376 added mkdir command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
214 free(sn);
5dbef9e07376 added mkdir command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
215 }
5dbef9e07376 added mkdir command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
216
5
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
217 DavResource* dav_get(DavSession *sn, char *path, char *properties) {
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
218 char *url = util_concat_path(sn->base_url, path);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
219
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
220 CURL *handle = sn->handle;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
221 curl_easy_setopt(handle, CURLOPT_URL, url);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
222 free(url);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
223
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
224 UcxList *proplist = NULL;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
225 if(properties) {
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
226 proplist = parse_properties_string(sn->context, sstr(properties));
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
227 }
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
228 UcxBuffer *rqbuf = create_propfind_request(proplist);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
229 UcxBuffer *rpbuf = ucx_buffer_new(NULL, 4096, UCX_BUFFER_AUTOEXTEND);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
230
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
231 //fwrite(rqbuf->space, 1, rqbuf->size, stdout);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
232 //printf("\n");
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
233
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
234 DavResource *resource = NULL;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
235 CURLcode ret = do_propfind_request(handle, rqbuf, rpbuf);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
236 int status = 0;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
237 curl_easy_getinfo (handle, CURLINFO_RESPONSE_CODE, &status);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
238 if(ret == CURLE_OK && status == 207) {
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
239 //printf("response\n%s\n", rpbuf->space);
17
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
240 resource = parse_propfind_response(sn, NULL, rpbuf);
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
241 sn->error = DAV_OK;
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
242 } else {
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
243 session_set_error(sn, ret, status);
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
244 }
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
245 return resource;
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
246 }
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
247
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
248 DavResource* dav_propfind(DavSession *sn, DavResource *root, UcxBuffer *rqbuf, char *path) {
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
249 char *url = util_concat_path(sn->base_url, path);
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
250 CURL *handle = sn->handle;
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
251 curl_easy_setopt(handle, CURLOPT_URL, url);
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
252 free(url);
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
253
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
254 UcxBuffer *rpbuf = ucx_buffer_new(NULL, 4096, UCX_BUFFER_AUTOEXTEND);
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
255 DavResource *resource = root;
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
256 CURLcode ret = do_propfind_request(handle, rqbuf, rpbuf);
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
257 int status = 0;
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
258 curl_easy_getinfo (handle, CURLINFO_RESPONSE_CODE, &status);
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
259 if(ret == CURLE_OK && status == 207) {
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
260 //printf("response\n%s\n", rpbuf->space);
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
261 resource = parse_propfind_response(sn, resource, rpbuf);
13
8a0cc4d90de7 added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
262 sn->error = DAV_OK;
8a0cc4d90de7 added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
263 } else {
8a0cc4d90de7 added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
264 session_set_error(sn, ret, status);
17
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
265 resource = NULL;
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
266 }
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
267 ucx_buffer_free(rpbuf);
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
268 return resource;
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
269 }
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
270
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
271 UcxList* propfind_stack_push(UcxList *stack, DavResource *children) {
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
272 while(children) {
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
273 if(children->iscollection) {
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
274 stack = ucx_list_prepend(stack, children);
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
275 }
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
276 children = children->next;
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
277 }
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
278 return stack;
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
279 }
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
280
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
281 DavResource* dav_get2(DavSession *sn, DavGetQuery *query) {
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
282 char *path;
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
283 int depth = 0;
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
284 if(parse_path_query(query->from, &path, &depth)) {
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
285 sn->error = DAV_ERROR;
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
286 return NULL;
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
287 }
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
288
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
289 sstr_t ps = query->properties;
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
290 UcxBuffer *rqbuf;
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
291 if(!sstrcmp(ps, S("*"))) {
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
292 rqbuf = create_allprop_propfind_request();
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
293 } else if(!sstrcmp(ps, S("-"))) {
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
294 rqbuf = create_propfind_request(NULL);
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
295 } else {
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
296 UcxList *proplist = parse_properties_string(sn->context, ps);
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
297 rqbuf = create_propfind_request(proplist);
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
298 }
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
299
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
300 //fwrite(rqbuf->space, 1, rqbuf->size, stdout);
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
301 //printf("\n");
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
302
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
303 DavResource *resource = dav_propfind(sn, NULL, rqbuf, path);
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
304 free(path);
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
305 int error = 0;
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
306 if(resource && depth == -1) {
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
307 UcxList *stack = NULL; // stack with davResource* elements
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
308 stack = propfind_stack_push(stack, resource->children);
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
309 while(stack) {
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
310 DavResource *sr = stack->data; // get first element from the stack
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
311 stack = ucx_list_remove(stack, stack); // remove first element
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
312 // do propfind request for sr
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
313 sr = dav_propfind(sn, sr, rqbuf, sr->path);
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
314 if(!sr) {
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
315 error = 1;
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
316 printf("subrequest failed\n");
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
317 break;
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
318 }
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
319 stack = propfind_stack_push(stack, sr->children); // add children
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
320 }
5
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
321 }
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
322 return resource;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
323 }
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
324
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
325 UcxList* parse_properties_string(DavContext *context, sstr_t str) {
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
326 UcxList *proplist = NULL;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
327 size_t nprops;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
328 sstr_t *props = sstrsplit(str, S(","), &nprops);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
329 for(int i=0;i<nprops;i++) {
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
330 sstr_t s = props[i];
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
331 sstr_t nsname = sstrchr(s, ':');
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
332 if(nsname.length > 0) {
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
333 sstr_t nspre = sstrsubsl(s, 0, nsname.ptr - s.ptr);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
334 nsname.ptr++;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
335 nsname.length--;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
336
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
337 DavProperty *dp = malloc(sizeof(DavProperty));
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
338 sstr_t pre = sstrdup(sstrtrim(nspre));
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
339 dp->ns = dav_get_namespace(context, pre.ptr);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
340 free(pre.ptr);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
341 dp->name = sstrdup(nsname).ptr;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
342 if(dp->ns && dp->name) {
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
343 proplist = ucx_list_append(proplist, dp);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
344 } else {
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
345 free(dp->name);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
346 free(dp);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
347 }
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
348 }
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
349 free(s.ptr);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
350 }
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
351 free(props);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
352 return proplist;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
353 }
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
354
17
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
355 DavResource* dav_query(DavSession *sn, char *query, ...) {
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
356 va_list ap;
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
357 va_start(ap, query);
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
358 DavQuery q = dav_ql_parse(query, ap);
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
359 va_end(ap);
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
360 DavResource *res = NULL;
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
361 switch(q.command) {
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
362 case DAV_QUERY_GET: {
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
363 res = dav_get2(sn, q.command_data);
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
364 free_get_query(q.command_data);
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
365 break;
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
366 }
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
367 }
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
368 return res;
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
369 }
5
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
370
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
371
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
372
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
373 DavResource* dav_resource_new(DavSession *sn, char *path) {
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
374 char *url = util_concat_path(sn->base_url, path);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
375 char *href = util_url_path(url);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
376 DavResource *res = resource_new_href(sn, href);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
377 free(url);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
378 return res;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
379 }
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
380
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
381 DavResource* resource_new_href(DavSession *sn, char *href) {
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
382 UcxMempool *mp = sn->mp;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
383 UcxAllocator *a = sn->allocator;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
384
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
385 DavResource *res = ucx_mempool_calloc(mp, 1, sizeof(DavResource));
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
386 res->session = sn;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
387
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
388 // set name, path and href
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
389 resource_set_info(res, href);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
390
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
391 // initialize node data
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
392 res->data = node_data_new(sn);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
393
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
394 return res;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
395 }
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
396
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
397 void resource_add_property(DavResource *res, char *ns, char *name, char *value) {
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
398 if(!value) {
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
399 return;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
400 }
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
401 UcxMempool *mp = res->session->mp;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
402 UcxAllocator *a = res->session->allocator;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
403
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
404 UcxKey key = dav_property_key(ns, name);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
405 sstr_t v = sstrdup_a(a, sstr(value));
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
406 ucx_map_put(res->data->properties, key, v.ptr);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
407 free(key.data);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
408 }
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
409
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
410 char* resource_get_property(DavResource *res, char *ns, char *name) {
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
411 UcxKey key = dav_property_key(ns, name);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
412 return ucx_map_get(res->data->properties, key);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
413 }
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
414
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
415 UcxKey dav_property_key(char *ns, char *name) {
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
416 sstr_t ns_str = sstr(ns);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
417 sstr_t name_str = sstr(name);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
418
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
419 sstr_t key;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
420 key.length = ns_str.length + name_str.length + 1;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
421 key.ptr = malloc(key.length + 1);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
422 key = sstrncat(key, 3, ns_str, S(" "), name_str);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
423
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
424 return ucx_key(key.ptr, key.length);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
425 }
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
426
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
427 void resource_add_child(DavResource *parent, DavResource *child) {
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
428 child->next = NULL;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
429 if(parent->children) {
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
430 DavResource *last = parent->children;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
431 while(last->next) {
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
432 last = last->next;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
433 }
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
434 last->next = child;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
435 child->prev = last;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
436 } else {
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
437 child->prev = NULL;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
438 parent->children = child;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
439 }
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
440 }
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
441
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
442 void resource_set_info(DavResource *res, char *href_str) {
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
443 char *url_str = NULL;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
444 curl_easy_getinfo(res->session->handle, CURLINFO_EFFECTIVE_URL, &url_str);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
445 sstr_t name = sstr(util_resource_name(href_str));
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
446 sstr_t href = sstr(href_str);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
447
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
448 sstr_t base_href = sstr(util_url_path(res->session->base_url));
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
449 sstr_t path = sstrsubs(href, base_href.length - 1);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
450
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
451 UcxAllocator *a = res->session->allocator;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
452 res->name = sstrdup_a(a, name).ptr;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
453 res->href = sstrdup_a(a, href).ptr;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
454 res->path = sstrdup_a(a, path).ptr;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
455 }
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
456
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
457 DavNodeData* node_data_new(DavSession *sn) {
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
458 DavNodeData *data = ucx_mempool_malloc(sn->mp, sizeof(DavNodeData));
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
459 if(!data) {
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
460 return NULL;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
461 }
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
462 data->properties = ucx_map_new_a(sn->allocator, 32);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
463 data->set = NULL;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
464 data->remove = NULL;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
465 data->content = NULL;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
466 data->read = NULL;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
467 data->length = 0;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
468 return data;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
469 }
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
470
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
471 int dav_load(DavResource *res) {
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
472 DavSession *sn = res->session;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
473 // clean map
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
474 UcxKey key;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
475 void *value;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
476 UcxMapIterator i = ucx_map_iterator(res->data->properties);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
477 UCX_MAP_FOREACH(key, value, i) {
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
478 ucx_map_remove(res->data->properties, key);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
479 }
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
480
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
481 char *url = util_concat_path(sn->base_url, res->path);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
482
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
483 CURL *handle = sn->handle;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
484 curl_easy_setopt(handle, CURLOPT_URL, url);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
485 free(url);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
486
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
487 UcxBuffer *rqbuf = create_allprop_propfind_request();
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
488 UcxBuffer *rpbuf = ucx_buffer_new(NULL, 4096, UCX_BUFFER_AUTOEXTEND);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
489
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
490 //fwrite(rpbuf->space, 1, rpbuf->size, stdout);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
491 //printf("\n");
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
492
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
493 CURLcode ret = do_propfind_request(handle, rqbuf, rpbuf);
13
8a0cc4d90de7 added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
494 int status = 0;
8a0cc4d90de7 added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
495 curl_easy_getinfo (handle, CURLINFO_RESPONSE_CODE, &status);
5
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
496 if(ret == CURLE_OK) {
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
497 //printf("response\n%s\n", rpbuf->space);
11
5db6178d8b58 added usage info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
498 // TODO: use parse_propfind_response()
5
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
499 xmlDoc *doc = xmlReadMemory(rpbuf->space, rpbuf->size, url, NULL, 0);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
500 if(!doc) {
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
501 return 1;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
502 }
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
503
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
504 xmlNode *xml_root = xmlDocGetRootElement(doc);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
505 xmlNode *node = xml_root->children;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
506 while(node) {
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
507 if(node->type == XML_ELEMENT_NODE) {
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
508 if(xstreq(node->name, "response")) {
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
509 parse_response_tag(res, node);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
510 }
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
511 }
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
512 node = node->next;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
513 }
11
5db6178d8b58 added usage info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
514
5db6178d8b58 added usage info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
515 set_davprops(res);
13
8a0cc4d90de7 added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
516 } else {
8a0cc4d90de7 added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
517 session_set_error(sn, ret, status);
5
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
518 }
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
519 return 0;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
520 }
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
521
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
522 int dav_store(DavResource *res) {
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
523 DavSession *sn = res->session;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
524
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
525 char *url = util_concat_path(sn->base_url, res->path);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
526 CURL *handle = res->session->handle;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
527 curl_easy_setopt(handle, CURLOPT_URL, url);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
528 free(url);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
529
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
530 DavNodeData *data = res->data;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
531
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
532 // store content
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
533 if(data->content) {
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
534 CURLcode ret = do_put_request(handle, data->content, data->read, data->length);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
535 int status = 0;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
536 curl_easy_getinfo(handle, CURLINFO_RESPONSE_CODE, &status);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
537 if(ret == CURLE_OK && (status >= 200 && status < 300)) {
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
538 res->session->error = 0;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
539 // cleanup node data
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
540 if(!data->read) {
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
541 ucx_mempool_free(sn->mp, data->content);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
542 }
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
543 data->content = NULL;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
544 data->read = NULL;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
545 data->length = 0;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
546 } else {
13
8a0cc4d90de7 added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
547 session_set_error(sn, ret, status);
5
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
548 return 1;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
549 }
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
550 }
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
551
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
552 // store properties
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
553 if(data->set || data->remove) {
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
554 UcxBuffer *request = create_proppatch_request(data);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
555 UcxBuffer *response = ucx_buffer_new(NULL, 1024, UCX_BUFFER_AUTOEXTEND);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
556
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
557 CURLcode ret = do_proppatch_request(handle, request, response);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
558 int status = 0;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
559 curl_easy_getinfo (handle, CURLINFO_RESPONSE_CODE, &status);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
560 if(ret == CURLE_OK && status == 207) {
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
561 //printf("%s\n", response->space);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
562 // TODO: parse response
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
563 // TODO: cleanup node data correctly
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
564 data->set = NULL;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
565 data->remove = NULL;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
566 } else {
13
8a0cc4d90de7 added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
567 session_set_error(sn, ret, status);
5
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
568 return 1;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
569 }
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
570 }
13
8a0cc4d90de7 added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
571 sn->error = DAV_OK;
5
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
572 return 0;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
573 }
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
574
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
575 char* dav_get_property_ns(DavResource *res, char *ns, char *name) {
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
576 char *property = resource_get_property(res, ns, name);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
577 if(property && res->data->remove) {
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
578 // TODO
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
579 } else if(!property && res->data->set) {
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
580 // TODO
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
581 }
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
582 return property;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
583 }
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
584
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
585 void dav_set_property_ns(DavResource *res, char *ns, char *name, char *value) {
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
586 UcxAllocator *a = res->session->allocator;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
587
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
588 DavProperty *property = a->malloc(a->pool, sizeof(DavProperty));
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
589 property->name = sstrdup_a(a, sstr(name)).ptr;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
590 property->value = sstrdup_a(a, sstr(value)).ptr;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
591 DavNamespace *namespace = a->malloc(a->pool, sizeof(DavNamespace));
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
592 namespace->prefix = NULL;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
593 namespace->name = sstrdup_a(a, sstr(ns)).ptr;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
594 property->ns = namespace;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
595
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
596 res->data->set = ucx_list_append_a(a, res->data->set, property);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
597 }
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
598
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
599 void dav_remove_property_ns(DavResource *res, char *ns, char *name, char *value) {
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
600 UcxAllocator *a = res->session->allocator;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
601
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
602 DavProperty *property = a->malloc(a->pool, sizeof(DavProperty));
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
603 property->name = sstrdup_a(a, sstr(name)).ptr;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
604 property->value = NULL;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
605 DavNamespace *namespace = a->malloc(a->pool, sizeof(DavNamespace));
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
606 namespace->prefix = NULL;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
607 namespace->name = sstrdup_a(a, sstr(ns)).ptr;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
608 property->ns = namespace;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
609
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
610 res->data->remove = ucx_list_append_a(a, res->data->remove, property);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
611 }
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
612
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
613
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
614 void dav_set_content(DavResource *res, void *stream, dav_read_func read_func) {
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
615 DavNodeData *data = res->data;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
616 data->content = stream;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
617 data->read = read_func;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
618 data->length = 0;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
619 }
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
620
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
621 void dav_set_content_data(DavResource *res, char *content, size_t length) {
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
622 DavSession *sn = res->session;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
623 DavNodeData *data = res->data;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
624 data->content = content;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
625 data->read = NULL;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
626 data->length = length;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
627 }
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
628
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
629 int dav_get_content(DavResource *res, void *stream, dav_write_func write_func) {
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
630 char *url = util_concat_path(res->session->base_url, res->path);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
631 CURL *handle = res->session->handle;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
632 curl_easy_setopt(handle, CURLOPT_URL, url);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
633 free(url);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
634
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
635 curl_easy_setopt(handle, CURLOPT_SSL_VERIFYPEER, 0);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
636 curl_easy_setopt(handle, CURLOPT_CUSTOMREQUEST, NULL);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
637 curl_easy_setopt(handle, CURLOPT_PUT, 0L);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
638 curl_easy_setopt(handle, CURLOPT_UPLOAD, 0L);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
639
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
640 curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, write_func);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
641 curl_easy_setopt(handle, CURLOPT_WRITEDATA, stream);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
642
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
643 CURLcode ret = curl_easy_perform(handle);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
644 int status = 0;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
645 curl_easy_getinfo (handle, CURLINFO_RESPONSE_CODE, &status);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
646 if(ret == CURLE_OK && (status >= 200 && status < 300)) {
13
8a0cc4d90de7 added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
647 res->session->error = DAV_OK;
5
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
648 return 0;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
649 } else {
13
8a0cc4d90de7 added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
650 session_set_error(res->session, ret, status);
5
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
651 return 1;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
652 }
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
653 }
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
654
15
182af08b4813 added remove command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 13
diff changeset
655 int dav_delete(DavResource *res) {
182af08b4813 added remove command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 13
diff changeset
656 char *url = util_concat_path(res->session->base_url, res->path);
182af08b4813 added remove command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 13
diff changeset
657 CURL *handle = res->session->handle;
182af08b4813 added remove command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 13
diff changeset
658 curl_easy_setopt(handle, CURLOPT_URL, url);
182af08b4813 added remove command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 13
diff changeset
659 free(url);
182af08b4813 added remove command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 13
diff changeset
660
182af08b4813 added remove command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 13
diff changeset
661 UcxBuffer *response = ucx_buffer_new(NULL, 4096, UCX_BUFFER_AUTOEXTEND);
182af08b4813 added remove command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 13
diff changeset
662 CURLcode ret = do_delete_request(handle, response);
182af08b4813 added remove command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 13
diff changeset
663 int status = 0;
182af08b4813 added remove command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 13
diff changeset
664 curl_easy_getinfo (handle, CURLINFO_RESPONSE_CODE, &status);
182af08b4813 added remove command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 13
diff changeset
665 if(ret == CURLE_OK && (status >= 200 && status < 300)) {
182af08b4813 added remove command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 13
diff changeset
666 res->session->error = DAV_OK;
182af08b4813 added remove command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 13
diff changeset
667
182af08b4813 added remove command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 13
diff changeset
668 // TODO: parse response
182af08b4813 added remove command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 13
diff changeset
669 // TODO: free res
182af08b4813 added remove command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 13
diff changeset
670
182af08b4813 added remove command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 13
diff changeset
671 return 0;
182af08b4813 added remove command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 13
diff changeset
672 } else {
182af08b4813 added remove command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 13
diff changeset
673 session_set_error(res->session, ret, status);
182af08b4813 added remove command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 13
diff changeset
674 return 1;
182af08b4813 added remove command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 13
diff changeset
675 }
182af08b4813 added remove command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 13
diff changeset
676 }
16
5dbef9e07376 added mkdir command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
677
5dbef9e07376 added mkdir command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
678 int dav_create(DavResource *res) {
5dbef9e07376 added mkdir command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
679 char *url = util_concat_path(res->session->base_url, res->path);
5dbef9e07376 added mkdir command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
680 char *parent = util_parent_path(res->path);
5dbef9e07376 added mkdir command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
681
5dbef9e07376 added mkdir command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
682 DavSession *sn = res->session;
5dbef9e07376 added mkdir command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
683 DavResource *parent_res = dav_get(sn, parent, NULL);
5dbef9e07376 added mkdir command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
684 if(!parent_res && sn->error == DAV_NOT_FOUND) {
5dbef9e07376 added mkdir command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
685 parent_res = dav_resource_new(sn, parent);
5dbef9e07376 added mkdir command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
686 parent_res->iscollection = 1;
5dbef9e07376 added mkdir command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
687 int r = dav_create(parent_res);
5dbef9e07376 added mkdir command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
688 if(r) {
5dbef9e07376 added mkdir command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
689 free(parent);
5dbef9e07376 added mkdir command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
690 return r;
5dbef9e07376 added mkdir command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
691 }
24
a317202ae787 implemented dav_create for non-collection resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
692 } else if(parent_res && !parent_res->iscollection) {
16
5dbef9e07376 added mkdir command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
693 sn->error = DAV_FORBIDDEN;
5dbef9e07376 added mkdir command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
694 return 1;
5dbef9e07376 added mkdir command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
695 } else if(sn->error != DAV_OK) {
5dbef9e07376 added mkdir command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
696 return 1;
5dbef9e07376 added mkdir command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
697 }
5dbef9e07376 added mkdir command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
698
5dbef9e07376 added mkdir command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
699 CURL *handle = res->session->handle;
5dbef9e07376 added mkdir command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
700 curl_easy_setopt(handle, CURLOPT_URL, url);
5dbef9e07376 added mkdir command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
701 free(url);
5dbef9e07376 added mkdir command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
702 free(parent);
5dbef9e07376 added mkdir command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
703
24
a317202ae787 implemented dav_create for non-collection resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
704 // create new collection or do an empty put request
a317202ae787 implemented dav_create for non-collection resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
705 CURLcode ret;
a317202ae787 implemented dav_create for non-collection resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
706 if(res->iscollection) {
a317202ae787 implemented dav_create for non-collection resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
707 ret = do_mkcol_request(handle);
a317202ae787 implemented dav_create for non-collection resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
708 } else {
a317202ae787 implemented dav_create for non-collection resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
709 ret = do_put_request(handle, "", NULL, 0);
a317202ae787 implemented dav_create for non-collection resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
710 }
16
5dbef9e07376 added mkdir command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
711 int status = 0;
5dbef9e07376 added mkdir command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
712 curl_easy_getinfo (handle, CURLINFO_RESPONSE_CODE, &status);
5dbef9e07376 added mkdir command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
713 if(ret == CURLE_OK && (status >= 200 && status < 300)) {
5dbef9e07376 added mkdir command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
714 res->session->error = DAV_OK;
5dbef9e07376 added mkdir command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
715 return 0;
5dbef9e07376 added mkdir command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
716 } else {
5dbef9e07376 added mkdir command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
717 session_set_error(res->session, ret, status);
5dbef9e07376 added mkdir command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
718 return 1;
5dbef9e07376 added mkdir command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
719 }
5dbef9e07376 added mkdir command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
720 }

mercurial