]> git.datanom.net - vcard-parser.git/blob - src/curl/curllib.c
Begin adding support for carddav server
[vcard-parser.git] / src / curl / curllib.c
1 /*
2 * curllib.c
3 *
4 * Copyright 2016 Michael Rasmussen <mir@datanom.net>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 * MA 02110-1301, USA.
20 */
21
22 #include <string.h>
23 #include <stdlib.h>
24 #include <curl/curl.h>
25 #include <curl/easy.h>
26 #include "globals.h"
27 #include "curllib.h"
28
29 const char* const CollectionType_string[] = {"unknown", "principal","calendar","addressbook"};
30
31 #define prop_request "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" \
32 "<A:propfind xmlns:A=\"DAV:\">" \
33 "<A:prop>" \
34 "<A:displayname/>" \
35 "<A:current-user-privilege-set/>" \
36 "<A:resourcetype/>" \
37 "<A:getetag/>" \
38 "<A:getlastmodified/>" \
39 "<A:getcontenttype/>" \
40 "</A:prop>" \
41 "</A:propfind>"
42
43 typedef enum {
44 PROPFIND,
45 OPTIONS
46 } Request;
47
48 typedef struct {
49 char * memory;
50 size_t size;
51 } MemoryStruct;
52
53 typedef struct {
54 char trace_ascii; /* 1 or 0 */
55 } Data;
56
57 typedef struct {
58 MemoryStruct* headers;
59 MemoryStruct* data;
60 char error_buf[CURL_ERROR_SIZE];
61 } Buffers;
62
63 static Buffers* buffers_new() {
64 Buffers* buffers = g_new0(Buffers, 1);
65 buffers->data = g_new0(MemoryStruct, 1);
66 buffers->data->size = 0;
67 buffers->headers = g_new0(MemoryStruct, 1);
68 buffers->headers->size = 0;
69
70 return buffers;
71 }
72
73 static void buffers_free(Buffers* buffers) {
74 if (buffers) {
75 if (buffers->data->memory)
76 g_free(buffers->data->memory);
77 g_free(buffers->data);
78 if (buffers->headers->memory)
79 g_free(buffers->headers->memory);
80 g_free(buffers->headers);
81 g_free(buffers);
82 }
83 }
84
85 /**
86 * This function is burrowed from the libcurl documentation
87 * @param text
88 * @param stream
89 * @param ptr
90 * @param size
91 * @param nohex
92 */
93 static void dump(const char* text, FILE* stream, char* ptr, size_t size, char nohex) {
94 size_t i;
95 size_t c;
96
97 unsigned int width=0x10;
98
99 if(nohex)
100 /* without the hex output, we can fit more on screen */
101 width = 0x40;
102 fprintf(stream, "%s, %zd bytes (0x%zx)\n", text, size, size);
103 for(i=0; i<size; i+= width) {
104 fprintf(stream, "%04zx: ", i);
105 if(!nohex) {
106 /* hex not disabled, show it */
107 for(c = 0; c < width; c++) {
108 if(i+c < size)
109 fprintf(stream, "%02x ", ptr[i+c]);
110 else
111 fputs(" ", stream);
112 }
113 }
114 for(c = 0; (c < width) && (i+c < size); c++) {
115 /* check for 0D0A; if found, skip past and start a new line of output */
116 if (nohex && (i+c+1 < size) && ptr[i+c]==0x0D && ptr[i+c+1]==0x0A) {
117 i+=(c+2-width);
118 break;
119 }
120 fprintf(stream, "%c",(ptr[i+c]>=0x20) && (ptr[i+c]<0x80)?ptr[i+c]:'.');
121 /* check again for 0D0A, to avoid an extra \n if it's at width */
122 if (nohex && (i+c+2 < size) && ptr[i+c+1]==0x0D && ptr[i+c+2]==0x0A) {
123 i+=(c+3-width);
124 break;
125 }
126 }
127 fputc('\n', stream); /* newline */
128 }
129 fflush(stream);
130 }
131
132 /**
133 * This function is burrowed from the libcurl documentation
134 * @param handle
135 * @param type
136 * @param data
137 * @param size
138 * @param userp
139 * @return
140 */
141 static int my_trace(CURL* handle, curl_infotype type, char* data, size_t size, void* userp) {
142 Data* config = (Data *)userp;
143 const char* text;
144 (void)handle; /* prevent compiler warning */
145
146 switch (type) {
147 case CURLINFO_TEXT:
148 fprintf(stderr, "== Info: %s", data);
149 default: /* in case a new one is introduced to shock us */
150 return 0;
151 case CURLINFO_HEADER_OUT:
152 text = "=> Send header";
153 break;
154 case CURLINFO_DATA_OUT:
155 text = "=> Send data";
156 break;
157 case CURLINFO_SSL_DATA_OUT:
158 text = "=> Send SSL data";
159 break;
160 case CURLINFO_HEADER_IN:
161 text = "<= Recv header";
162 break;
163 case CURLINFO_DATA_IN:
164 text = "<= Recv data";
165 break;
166 case CURLINFO_SSL_DATA_IN:
167 text = "<= Recv SSL data";
168 break;
169 }
170 dump(text, stderr, data, size, config->trace_ascii);
171 return 0;
172 }
173
174 /**
175 * This function is burrowed from the libcurl documentation
176 * @param ptr
177 * @param size
178 * @return void* to memory region
179 */
180 static void* myrealloc(void* ptr, size_t size) {
181 /* There might be a realloc() out there that doesn't like reallocing
182 * NULL pointers, so we take care of it here
183 * */
184 if(ptr)
185 return realloc(ptr, size);
186 else
187 return malloc(size);
188 }
189
190 /**
191 * This function is burrowed from the libcurl documentation
192 * @param ptr
193 * @param size
194 * @param nmemb
195 * @param data
196 * @return number of written bytes
197 */
198 static size_t WriteMemoryCallback(void* ptr, size_t size, size_t nmemb, void* data) {
199 size_t realsize = size * nmemb;
200 MemoryStruct* mem = (MemoryStruct *)data;
201 mem->memory = (char *)myrealloc(mem->memory, mem->size + realsize + 1);
202
203 if (mem->memory) {
204 memcpy(&(mem->memory[mem->size]), ptr, realsize);
205 mem->size += realsize;
206 mem->memory[mem->size] = 0;
207 }
208 return realsize;
209 }
210
211 static CURL* get_curl(CarddavData* data) {
212 CURL* curl;
213 gchar* userpwd;
214 Data config;
215
216 curl = curl_easy_init();
217 if (curl) {
218 if (data->pwd) {
219 userpwd = g_strconcat(data->user, ":", data->pwd, NULL);
220 curl_easy_setopt(curl, CURLOPT_USERPWD, userpwd);
221 } else {
222 userpwd = g_strconcat(data->user, NULL);
223 curl_easy_setopt(curl, CURLOPT_USERPWD, userpwd);
224 }
225 g_free(userpwd);
226 curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
227 curl_easy_setopt(curl, CURLOPT_USERAGENT, "carddavParser");
228 curl_easy_setopt(curl, CURLOPT_URL, data->url);
229 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
230 curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, WriteMemoryCallback);
231 if (data->debug) {
232 config.trace_ascii = 1;
233 curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, my_trace);
234 curl_easy_setopt(curl, CURLOPT_DEBUGDATA, &config);
235 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
236 }
237 curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
238 curl_easy_setopt(curl, CURLOPT_UNRESTRICTED_AUTH, 1);
239 curl_easy_setopt(curl, CURLOPT_POSTREDIR, CURL_REDIR_POST_ALL);
240 }
241
242 return curl;
243 }
244
245 static CURL* prepare_curl(CarddavData* dav, Buffers* buffers, Request type) {
246 CURL* curl = get_curl(dav);
247 if (curl) {
248 curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)buffers->data);
249 curl_easy_setopt(curl, CURLOPT_WRITEHEADER, (void *)buffers->headers);
250 curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, (char *)buffers->error_buf);
251 switch (type) {
252 case OPTIONS:
253 curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "OPTIONS");
254 break;
255 case PROPFIND:
256 curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PROPFIND");
257 break;
258 }
259 }
260
261 return curl;
262 }
263
264 static gboolean has_support(CarddavData* dav, const gchar* headers) {
265 gboolean support = FALSE;
266
267 g_print("%s\n", headers);
268 gchar* start = g_strstr_len(headers, -1, "DAV");
269 if (start) {
270 gchar** values = g_strsplit(start, ":", 0);
271 if (values && g_strv_length(values) > 1) {
272 gchar** options = g_strsplit(values[1], ",", 0);
273 if (options && g_strv_length(options) > 0) {
274 gchar** tmp = options;
275 while (*tmp) {
276 gchar *opt, *option;
277 gchar* end = g_strstr_len(*tmp, -1, "\r");
278 if (end) {
279 gchar* str = g_strndup(*tmp, end - *tmp);
280 opt = g_ascii_strup(str, -1);
281 g_free(str);
282 option = g_strstrip(opt);
283 } else {
284 opt = g_ascii_strup(*tmp, -1);
285 option = g_strstrip(opt);
286 }
287 if (g_strcmp0(option, "ADDRESSBOOK") == 0)
288 support = TRUE;
289 if (g_strcmp0(option, "EXTENDED-MKCOL") == 0)
290 dav->mkcol = TRUE;
291 g_free(opt);
292 tmp++;
293 }
294 }
295 g_strfreev(options);
296 }
297 g_strfreev(values);
298 }
299
300 return support;
301 }
302
303 static gchar* find_text(const gchar* string, const gchar* start, const gchar* end) {
304 gchar* text = NULL;
305
306 if (start && end) {
307 gchar* s_start = g_strstr_len(string, -1, start);
308 if (s_start) {
309 gchar* s_end = g_strstr_len(s_start, -1, end);
310 if (s_start && s_end) {
311 text = g_strndup(s_start + strlen(start), s_end - (s_start + strlen(start)));
312 }
313 }
314 }
315
316 return text;
317 }
318
319 static gchar* remove_ns(gchar* s) {
320 gchar* s1 = NULL;
321 //gchar* tmp = NULL;
322
323 if (s) {
324 gchar* s_start = g_strstr_len(s, -1, ":");
325 if (s_start) {
326 s1 = g_strndup(s_start + 1, strlen(s_start + 1));
327 } else {
328 s1 = g_strdup(s);
329 }
330 g_strstrip(s1);
331 //g_free(tmp);
332 }
333
334 return s1;
335 }
336
337 static CollectionType get_collection_type(const gchar* s) {
338 CollectionType type = UNKNOWN;
339 int len = sizeof(CollectionType_string)/sizeof(CollectionType_string[0]);
340
341 if (s) {
342 for (int i = 0; i < len; ++i) {
343 if (g_strcmp0(CollectionType_string[i], s) == 0) {
344 type = i;
345 break;
346 }
347 }
348 }
349
350 return type;
351 }
352
353 /*
354 gboolean curl_get_objects(VCalData* vcal, GString* buf) {
355 }
356
357 gboolean curl_set_objects(VCalData* vcal, GString* buf) {
358 }
359
360 gboolean curl_update_objects(VCalData* vcal, GString* buf) {
361 }
362
363 gboolean curl_delete_objects(VCalData* vcal, GString* buf) {
364 }
365
366 gboolean curl_get_freebusy(VCalData* vcal, GString* buf) {
367 }
368 */
369 gboolean curl_get_options(CarddavData* dav, GString* buf) {
370 gboolean res;
371 CURLcode curlres = 0;
372 Buffers* buffers = buffers_new();
373
374 CURL* curl = prepare_curl(dav, buffers, OPTIONS);
375 if (curl) {
376 curlres = curl_easy_perform(curl);
377 if (curlres != 0) {
378 if (buf) {
379 buf = g_string_assign(buf, buffers->error_buf);
380 }
381 res = TRUE;
382 }
383 else {
384 long code;
385 curlres = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
386 if (code != 200) {
387 res = TRUE;
388 }
389 else {
390 res = !has_support(dav, buffers->headers->memory);
391 GString* displayname = g_string_new(NULL);
392 if (!curl_get_displayname(dav, displayname))
393 dav->displayname = g_string_free_and_steal(displayname);
394 else
395 g_string_free(displayname, TRUE);
396 }
397 }
398 curl_easy_cleanup(curl);
399 } else {
400 res = TRUE;
401 }
402 buffers_free(buffers);
403
404 return res;
405 }
406
407 gboolean curl_get_displayname(CarddavData* dav, GString* buf) {
408 gboolean res;
409 CURLcode curlres = 0;
410 Buffers* buffers = buffers_new();
411
412 if (!buf) return TRUE;
413
414 CURL* curl = prepare_curl(dav, buffers, PROPFIND);
415 if (curl) {
416 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, prop_request);
417 curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, -1L);
418 curlres = curl_easy_perform(curl);
419 if (curlres != 0) {
420 buf = g_string_assign(buf, buffers->error_buf);
421 res = TRUE;
422 }
423 else {
424 long code;
425 curlres = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
426 if (code != 207) {
427 res = TRUE;
428 }
429 else {
430 gchar* displayname = find_text(buffers->data->memory, "<displayname>", "</displayname>");
431 if (displayname) {
432 buf = g_string_assign(buf, displayname);
433 g_free(displayname);
434 }
435 res = FALSE;
436 }
437 }
438 curl_easy_cleanup(curl);
439 } else {
440 res = TRUE;
441 }
442
443 buffers_free(buffers);
444
445 return res;
446 }
447
448 gboolean curl_get_resource_type(CarddavData* dav) {
449 Buffers* buffers = buffers_new();
450 gboolean res = TRUE;
451 CURLcode curlres = 0;
452 GPtrArray* types = g_ptr_array_new_null_terminated(1, (GDestroyNotify)g_free, TRUE);
453
454 CURL* curl = prepare_curl(dav, buffers, PROPFIND);
455 if (curl) {
456 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, prop_request);
457 curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, -1L);
458 curlres = curl_easy_perform(curl);
459 if (curlres != 0) {
460 g_ptr_array_add(types, g_strdup(buffers->error_buf));
461 res = TRUE;
462 }
463 else {
464 long code;
465 curlres = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
466 if (code != 207) {
467 res = TRUE;
468 }
469 else {
470 gchar* resourcetype = find_text(buffers->data->memory, "<resourcetype>", "</resourcetype>");
471 if (resourcetype) {
472 gchar* tmp = g_strdup(resourcetype);
473 g_free(resourcetype);
474 resourcetype = find_text(tmp, "<", "/>");
475 gchar* s = remove_ns(resourcetype);
476 g_free(resourcetype);
477 if (s) {
478 g_ptr_array_add(types, g_strdup(s));
479 g_free(s);
480 resourcetype = find_text(tmp, "><", "/>");
481 gchar* s = remove_ns(resourcetype);
482 g_ptr_array_add(types, g_strdup(s));
483 g_free(resourcetype);
484 g_free(s);
485 }
486 g_free(tmp);
487 }
488 res = FALSE;
489 }
490 }
491 curl_easy_cleanup(curl);
492 }
493
494 if (types->len > 0) {
495 gboolean authorized = FALSE;
496 gpointer* types_str = g_ptr_array_free(types, FALSE);
497 gchar** start = (gchar**)types_str;
498 while (*types_str) {
499 CollectionType type = get_collection_type(*types_str);
500 switch (type) {
501 case PRINCIPAL:
502 case CALENDAR:
503 case ADDRESSBOOK:
504 dav->type = type;
505 authorized = TRUE;
506 break;
507 default:
508 break;
509 }
510 types_str++;
511 }
512 if (authorized == FALSE)
513 dav->type = UNKNOWN;
514 g_strfreev(start);
515 }
516
517 buffers_free(buffers);
518
519 return res;
520 }
521
522 void curl_free_CarddavData(CarddavData* dav) {
523 if (dav) {
524 g_free(dav->displayname);
525 g_free(dav->pwd);
526 g_free(dav->url);
527 g_free(dav->user);
528 g_free(dav);
529 }
530 }
531
532 const gchar* get_resource_type_name(CollectionType type) {
533 if (type < TYPE_SIZE)
534 return CollectionType_string[type];
535
536 return NULL;
537 };
This page took 0.596681 seconds and 6 git commands to generate.