libidav/methods.c

changeset 475
52e4171d42ce
parent 373
dcc03142eb5f
child 478
baa63fef5c5c
equal deleted inserted replaced
474:017a4f09e6fa 475:52e4171d42ce
127 ucx_buffer_write(s.ptr, 1, s.length, buf); 127 ucx_buffer_write(s.ptr, 1, s.length, buf);
128 128
129 return buf; 129 return buf;
130 } 130 }
131 131
132 UcxBuffer* create_propfind_request(DavSession *sn, UcxList *properties) { 132 UcxBuffer* create_propfind_request(DavSession *sn, UcxList *properties, char *rootelm, DavBool nocrypt) {
133 UcxBuffer *buf = ucx_buffer_new(NULL, 512, UCX_BUFFER_AUTOEXTEND); 133 UcxBuffer *buf = ucx_buffer_new(NULL, 512, UCX_BUFFER_AUTOEXTEND);
134 sstr_t s; 134 sstr_t s;
135 135
136 int add_crypto_name = 1; 136 int add_crypto_name = 1;
137 int add_crypto_key = 1; 137 int add_crypto_key = 1;
159 } 159 }
160 } 160 }
161 } 161 }
162 162
163 DavNamespace idav_ns; 163 DavNamespace idav_ns;
164 if(add_crypto_name && add_crypto_key && DAV_CRYPTO(sn)) { 164 if(add_crypto_name && add_crypto_key && DAV_CRYPTO(sn) && !nocrypt) {
165 idav_ns.prefix = "idav"; 165 idav_ns.prefix = "idav";
166 idav_ns.name = DAV_NS; 166 idav_ns.name = DAV_NS;
167 ucx_map_cstr_put(namespaces, "idav", &idav_ns); 167 ucx_map_cstr_put(namespaces, "idav", &idav_ns);
168 } 168 }
169 169
170 s = S("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n"); 170 s = S("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n");
171 ucx_buffer_write(s.ptr, 1, s.length, buf); 171 ucx_buffer_write(s.ptr, 1, s.length, buf);
172 172
173 // write root element and namespaces 173 // write root element and namespaces
174 s = S("<D:propfind xmlns:D=\"DAV:\""); 174 ucx_bprintf(buf, "<D:%s xmlns:D=\"DAV:\"", rootelm);
175 ucx_buffer_write(s.ptr, 1, s.length, buf); 175
176 UcxMapIterator mapi = ucx_map_iterator(namespaces); 176 UcxMapIterator mapi = ucx_map_iterator(namespaces);
177 UcxKey key; 177 UcxKey key;
178 DavNamespace *ns; 178 DavNamespace *ns;
179 UCX_MAP_FOREACH(key, ns, mapi) { 179 UCX_MAP_FOREACH(key, ns, mapi) {
180 s = S(" xmlns:"); 180 s = S(" xmlns:");
203 203
204 s = S("<D:resourcetype />\n"); 204 s = S("<D:resourcetype />\n");
205 ucx_buffer_write(s.ptr, 1, s.length, buf); 205 ucx_buffer_write(s.ptr, 1, s.length, buf);
206 206
207 // crypto properties 207 // crypto properties
208 if(DAV_CRYPTO(sn)) { 208 if(DAV_CRYPTO(sn) && !nocrypt) {
209 if(add_crypto_name) { 209 if(add_crypto_name) {
210 ucx_buffer_putc(buf, '<'); 210 ucx_buffer_putc(buf, '<');
211 ucx_buffer_puts(buf, crypto_ns); 211 ucx_buffer_puts(buf, crypto_ns);
212 s = S(":crypto-name />\n"); 212 s = S(":crypto-name />\n");
213 ucx_buffer_write(s.ptr, 1, s.length, buf); 213 ucx_buffer_write(s.ptr, 1, s.length, buf);
240 s = S(" />\n"); 240 s = S(" />\n");
241 ucx_buffer_write(s.ptr, 1, s.length, buf); 241 ucx_buffer_write(s.ptr, 1, s.length, buf);
242 } 242 }
243 243
244 // end 244 // end
245 s = S("</D:prop>\n</D:propfind>\n"); 245 ucx_bprintf(buf, "</D:prop>\n</D:%s>\n", rootelm);
246 ucx_buffer_write(s.ptr, 1, s.length, buf);
247 246
248 ucx_map_free(namespaces); 247 ucx_map_free(namespaces);
249 return buf; 248 return buf;
250 } 249 }
251 250
1221 curl_slist_free_all(headers); 1220 curl_slist_free_all(headers);
1222 free(ltheader.ptr); 1221 free(ltheader.ptr);
1223 1222
1224 return ret; 1223 return ret;
1225 } 1224 }
1225
1226 CURLcode do_simple_request(DavSession *sn, char *method, char *locktoken) {
1227 CURL *handle = sn->handle;
1228 curl_easy_setopt(handle, CURLOPT_CUSTOMREQUEST, method);
1229 curl_easy_setopt(handle, CURLOPT_UPLOAD, 0L);
1230
1231 curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, dummy_write);
1232 curl_easy_setopt(handle, CURLOPT_WRITEDATA, NULL);
1233
1234 // set lock-token header
1235 sstr_t ltheader;
1236 struct curl_slist *headers = NULL;
1237 if(locktoken) {
1238 ltheader = ucx_sprintf("Lock-Token: <%s>", locktoken);
1239 headers = curl_slist_append(NULL, ltheader.ptr);
1240 }
1241 curl_easy_setopt(handle, CURLOPT_HTTPHEADER, headers);
1242
1243 CURLcode ret = dav_session_curl_perform(sn, NULL);
1244 if(locktoken) {
1245 curl_slist_free_all(headers);
1246 free(ltheader.ptr);
1247 }
1248
1249 return ret;
1250 }
1251
1252
1253 CURLcode do_report_request(DavSession *sn, UcxBuffer *request, UcxBuffer *response) {
1254 CURL *handle = sn->handle;
1255 curl_easy_setopt(handle, CURLOPT_CUSTOMREQUEST, "REPORT");
1256
1257 curl_easy_setopt(handle, CURLOPT_UPLOAD, 1);
1258 curl_easy_setopt(handle, CURLOPT_READFUNCTION, ucx_buffer_read);
1259 curl_easy_setopt(handle, CURLOPT_READDATA, request);
1260 curl_easy_setopt(handle, CURLOPT_INFILESIZE, request->size);
1261
1262 curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, ucx_buffer_write);
1263 curl_easy_setopt(handle, CURLOPT_WRITEDATA, response);
1264
1265 struct curl_slist *headers = NULL;
1266 headers = curl_slist_append(headers, "Content-Type: text/xml");
1267 curl_easy_setopt(handle, CURLOPT_HTTPHEADER, headers);
1268
1269 request->pos = 0;
1270 response->size = response->pos = 0;
1271 CURLcode ret = dav_session_curl_perform_buf(sn, request, response, NULL);
1272
1273 curl_slist_free_all(headers);
1274
1275 return ret;
1276 }

mercurial