libidav/utils.c

changeset 40
a95ee94b9204
parent 39
3e55bed345f9
child 43
03076907b58a
equal deleted inserted replaced
39:3e55bed345f9 40:a95ee94b9204
29 #include <time.h> 29 #include <time.h>
30 #include <stdio.h> 30 #include <stdio.h>
31 #include <stdlib.h> 31 #include <stdlib.h>
32 #include <string.h> 32 #include <string.h>
33 #include <ucx/string.h> 33 #include <ucx/string.h>
34 #include <ucx/buffer.h>
34 #include <libxml/tree.h> 35 #include <libxml/tree.h>
35 #include <curl/curl.h> 36 #include <curl/curl.h>
36 37
37 #include <openssl/sha.h> 38 #include <openssl/sha.h>
38 #include <openssl/hmac.h> 39 #include <openssl/hmac.h>
39 #include <openssl/evp.h> 40 #include <openssl/evp.h>
40 #include <openssl/bio.h> 41 #include <openssl/bio.h>
41 #include <openssl/buffer.h> 42 #include <openssl/buffer.h>
42 43
43 #include "utils.h" 44 #include "utils.h"
45 #include "webdav.h"
44 46
45 47
46 time_t util_parse_creationdate(char *str) { 48 time_t util_parse_creationdate(char *str) {
47 // example: 2012-11-29T21:35:35Z 49 // example: 2012-11-29T21:35:35Z
48 if(!str) { 50 if(!str) {
169 } else { 171 } else {
170 url = sstrncat(url, 2, base, path); 172 url = sstrncat(url, 2, base, path);
171 } 173 }
172 174
173 return url.ptr; 175 return url.ptr;
176 }
177
178 void util_set_url(DavSession *sn, char *path) {
179 if(path) {
180 char *url = util_path_to_url(sn, path);
181 curl_easy_setopt(sn->handle, CURLOPT_URL, url);
182 free(url);
183 } else {
184 curl_easy_setopt(sn->handle, CURLOPT_URL, sn->base_url);
185 }
186 }
187
188 char* util_path_to_url(DavSession *sn, char *path) {
189 UcxBuffer *url = ucx_buffer_new(NULL, 256, UCX_BUFFER_AUTOEXTEND);
190
191 // add base url
192 ucx_buffer_write(sn->base_url, 1, strlen(sn->base_url), url);
193 // remove trailing slash
194 ucx_buffer_seek(url, -1, SEEK_CUR);
195
196 sstr_t p = sstr(path);
197 size_t ntk = 0;
198 sstr_t *tks = sstrsplit(p, S("/"), &ntk);
199
200 for(int i=0;i<ntk;i++) {
201 sstr_t node = tks[i];
202 if(node.length > 0) {
203 // TODO: implement file name encryption
204 char *esc = curl_easy_escape(sn->handle, node.ptr, node.length);
205 ucx_buffer_putc(url, '/');
206 ucx_buffer_write(esc, 1, strlen(esc), url);
207 curl_free(esc);
208 free(node.ptr);
209 }
210 }
211 free(tks);
212 if(path[p.length-1] == '/') {
213 ucx_buffer_putc(url, '/');
214 }
215 ucx_buffer_putc(url, 0);
216
217 // only free the buffer struct and return the buffer space
218 char *space = url->space;
219 free(url);
220 return space;
174 } 221 }
175 222
176 char* util_parent_path(char *path) { 223 char* util_parent_path(char *path) {
177 char *name = util_resource_name(path); 224 char *name = util_resource_name(path);
178 size_t namelen = strlen(name); 225 size_t namelen = strlen(name);

mercurial