+
+gboolean curl_update_object(CarddavData* dav, VCard* vcard, GString* buf) {
+ gboolean res = FALSE;
+
+ CURLcode curlres = 0;
+ Buffers* buffers = buffers_new();
+ struct curl_slist *list = NULL;
+ gchar* etag = NULL;
+
+ GSList* uid = VCard_get_property(vcard, "UID");
+ if (uid == NULL) {
+ if (buf)
+ buf = g_string_assign(buf, "VCard missing UID");
+ return TRUE;
+ }
+ VCardProperty* prop = (VCardProperty*) uid->data;
+ gchar* extra_url = g_strconcat(prop->value, ".vcf", NULL);
+ vcard_slist_property_free(uid);
+ CURL* curl = prepare_curl(dav, buffers, PUT, extra_url);
+ g_free(extra_url);
+ if (curl) {
+ etag = VCard_get_etag(vcard);
+ if (etag) {
+ gchar* match = g_strconcat("If-Match: \"", etag, "\"", NULL);
+ g_free(etag);
+ list = curl_slist_append(list, match);
+ list = curl_slist_append(list, "Content-Type: text/vcard");
+ curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);
+ gchar* card = VCard_get_as_string(vcard);
+ curl_easy_setopt(curl, CURLOPT_POSTFIELDS, card);
+ curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, -1L);
+ curlres = curl_easy_perform(curl);
+ if (curlres != 0) {
+ if (buf) {
+ buf = g_string_assign(buf, buffers->error_buf);
+ }
+ res = TRUE;
+ }
+ else {
+ long code;
+ curlres = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
+ if (code != 204) {
+ if (buf) {
+ buf = g_string_assign(buf, buffers->data->memory);
+ g_string_append_printf(buf, "\nERROR status: ");
+ g_string_append_printf(buf, "%ld", code);
+ }
+ res = TRUE;
+ } else {
+ gchar* etag = get_etag_header(buffers->headers->memory);
+ if (etag) {
+ VCard_set_etag(vcard, etag);
+ g_free(etag);
+ } else {
+ /*
+ * Server did not respond a successful 201 with
+ * a the objects ETag. rfc6352 (6.3.2.3) only say
+ * SHOULD so make a request to the server for the
+ * objects ETag
+ */
+ GSList* uid = VCard_get_property(vcard, "UID");
+ prop = (VCardProperty*) uid->data;
+ etag = curl_get_etag(dav, prop->value, buf);
+ vcard_slist_property_free(uid);
+ if (etag == NULL) {
+ if (buf) {
+ buf = g_string_assign(buf, "Session result");
+ if (buffers->data->size > 0) {
+ g_string_append_printf(buf, "\nData\n");
+ g_string_append_printf(buf, buffers->data->memory);
+ }
+ if (buffers->headers->size > 0) {
+ g_string_append_printf(buf, "\nHeaders\n");
+ g_string_append_printf(buf, buffers->headers->memory);
+ }
+ }
+ res = TRUE;
+ } else {
+ VCard_set_etag(vcard, etag);
+ g_free(etag);
+ }
+ }
+ }
+ }
+ print_buffers(NULL);
+ curl_slist_free_all(list);
+ g_free(card);
+ g_free(match);
+ } else {
+ if (buf) {
+ buf = g_string_assign(buf, "ERROR: Missing ETag");
+ }
+ res = TRUE;
+ }
+ curl_easy_cleanup(curl);
+ } else {
+ res = TRUE;
+ }
+ buffers_free(buffers);
+
+ return res;
+}
+
+/*
+ * DELETE /calendars/johndoe/home/132456762153245.ics HTTP/1.1
+ * If-Match: "2134-314" (ETag)
+ *
+ * HTTP/1.1 204 No Content
+*/
+gboolean curl_delete_object(CarddavData* dav, VCard* vcard, GString* buf) {
+ gboolean res = FALSE;
+
+ CURLcode curlres = 0;
+ Buffers* buffers = buffers_new();
+ struct curl_slist *list = NULL;
+ gchar* etag = NULL;
+
+ GSList* uid = VCard_get_property(vcard, "UID");
+ if (uid == NULL) {
+ if (buf)
+ buf = g_string_assign(buf, "VCard missing UID");
+ return TRUE;
+ }
+ VCardProperty* prop = (VCardProperty*) uid->data;
+ gchar* extra_url = g_strconcat(prop->value, ".vcf", NULL);
+ vcard_slist_property_free(uid);
+ CURL* curl = prepare_curl(dav, buffers, DELETE, extra_url);
+ g_free(extra_url);
+ if (curl) {
+ etag = VCard_get_etag(vcard);
+ if (etag) {
+ gchar* match = g_strconcat("If-Match: \"", etag, "\"", NULL);
+ g_free(etag);
+ list = curl_slist_append(list, match);
+ //list = curl_slist_append(list, "Content-Type: text/vcard");
+ curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);
+ curlres = curl_easy_perform(curl);
+ if (curlres != 0) {
+ if (buf) {
+ buf = g_string_assign(buf, buffers->error_buf);
+ }
+ res = TRUE;
+ }
+ else {
+ long code;
+ curlres = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
+ if (code != 204) {
+ if (buf) {
+ buf = g_string_assign(buf, buffers->data->memory);
+ g_string_append_printf(buf, "\nERROR status: ");
+ g_string_append_printf(buf, "%ld", code);
+ }
+ res = TRUE;
+ } else {
+ buf = g_string_assign(buf, "Deleted: ");
+ g_string_append_printf(buf, "%ld", code);
+ }
+ }
+ print_buffers(NULL);
+ curl_slist_free_all(list);
+ g_free(match);
+ } else {
+ if (buf) {
+ buf = g_string_assign(buf, "ERROR: Missing ETag");
+ }
+ res = TRUE;
+ }
+ curl_easy_cleanup(curl);
+ } else {
+ res = TRUE;
+ }
+ buffers_free(buffers);
+
+ return res;
+}
+