libidav/methods.c

changeset 207
de23f8881e9f
parent 185
cd42cccee550
child 208
1fb26aca5093
equal deleted inserted replaced
206:527d0fde484e 207:de23f8881e9f
960 } 960 }
961 961
962 962
963 CURLcode do_head_request(CURL *handle) { 963 CURLcode do_head_request(CURL *handle) {
964 curl_easy_setopt(handle, CURLOPT_CUSTOMREQUEST, "HEAD"); 964 curl_easy_setopt(handle, CURLOPT_CUSTOMREQUEST, "HEAD");
965 curl_easy_setopt(handle, CURLOPT_PUT, 0L);
966 curl_easy_setopt(handle, CURLOPT_UPLOAD, 0L); 965 curl_easy_setopt(handle, CURLOPT_UPLOAD, 0L);
967 curl_easy_setopt(handle, CURLOPT_NOBODY, 1L); 966 curl_easy_setopt(handle, CURLOPT_NOBODY, 1L);
967
968 // clear headers
969 struct curl_slist *headers = NULL;
970 curl_easy_setopt(handle, CURLOPT_HTTPHEADER, headers);
968 971
969 curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, dummy_write); 972 curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, dummy_write);
970 curl_easy_setopt(handle, CURLOPT_WRITEDATA, NULL); 973 curl_easy_setopt(handle, CURLOPT_WRITEDATA, NULL);
971 974
972 CURLcode ret = curl_easy_perform(handle); 975 CURLcode ret = curl_easy_perform(handle);
1004 headers = NULL; 1007 headers = NULL;
1005 curl_easy_setopt(handle, CURLOPT_HTTPHEADER, headers); 1008 curl_easy_setopt(handle, CURLOPT_HTTPHEADER, headers);
1006 return ret; 1009 return ret;
1007 } 1010 }
1008 1011
1012
1013 UcxBuffer* create_lock_request() {
1014 UcxBuffer *buf = ucx_buffer_new(NULL, 512, UCX_BUFFER_AUTOEXTEND);
1015 sstr_t s;
1016
1017 s = S("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n");
1018 ucx_buffer_write(s.ptr, 1, s.length, buf);
1019
1020 s = S("<D:lockinfo xmlns:D=\"DAV:\">\n"
1021 "<D:lockscope><D:exclusive/></D:lockscope>\n"
1022 "<D:locktype><D:write/></D:locktype>\n"
1023 "<D:owner><D:href>http://davutils.org/libidav/</D:href></D:owner>\n");
1024 ucx_buffer_write(s.ptr, 1, s.length, buf);
1025
1026 s = S("</D:lockinfo>\n");
1027 ucx_buffer_write(s.ptr, 1, s.length, buf);
1028
1029 return buf;
1030 }
1031
1032 int parse_lock_response(DavSession *sn, UcxBuffer *response, LockDiscovery *lock) {
1033 lock->locktoken = NULL;
1034 lock->timeout = NULL;
1035
1036 xmlDoc *doc = xmlReadMemory(response->space, response->size, NULL, NULL, 0);
1037 if(!doc) {
1038 sn->error = DAV_ERROR;
1039 return -1;
1040 }
1041
1042 char *timeout = NULL;
1043 char *locktoken = NULL;
1044
1045 int ret = -1;
1046 xmlNode *xml_root = xmlDocGetRootElement(doc);
1047 DavBool lockdiscovery = 0;
1048 if(xml_root) {
1049 xmlNode *node = xml_root->children;
1050 while(node) {
1051 if(node->type == XML_ELEMENT_NODE) {
1052 if(xstreq(node->name, "lockdiscovery")) {
1053 node = node->children;
1054 lockdiscovery = 1;
1055 continue;
1056 }
1057
1058 if(lockdiscovery) {
1059 if(xstreq(node->name, "timeout")) {
1060 timeout = util_xml_get_text(node);
1061 } else if(xstreq(node->name, "locktoken")) {
1062 xmlNode *n = node->children;
1063 while(n) {
1064 if(xstreq(n->name, "href")) {
1065 locktoken = util_xml_get_text(n);
1066 break;
1067 }
1068 n = n->next;
1069 }
1070 }
1071 }
1072 }
1073 node = node->next;
1074 }
1075 }
1076
1077 if(timeout && locktoken) {
1078 lock->timeout = strdup(timeout);
1079 lock->locktoken = strdup(locktoken);
1080 ret = 0;
1081 }
1082
1083 xmlFreeDoc(doc);
1084 return ret;
1085 }
1086
1087 CURLcode do_lock_request(CURL *handle, UcxBuffer *request, UcxBuffer *response) {
1088 curl_easy_setopt(handle, CURLOPT_CUSTOMREQUEST, "LOCK");
1089 curl_easy_setopt(handle, CURLOPT_UPLOAD, 1L);
1090 request->pos = 0;
1091
1092 // clear headers
1093 struct curl_slist *headers = NULL;
1094 curl_easy_setopt(handle, CURLOPT_HTTPHEADER, headers);
1095
1096 curl_easy_setopt(handle, CURLOPT_UPLOAD, 1);
1097 curl_easy_setopt(handle, CURLOPT_READFUNCTION, ucx_buffer_read);
1098 curl_easy_setopt(handle, CURLOPT_READDATA, request);
1099 curl_easy_setopt(handle, CURLOPT_INFILESIZE, request->size);
1100
1101 curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, ucx_buffer_write);
1102 curl_easy_setopt(handle, CURLOPT_WRITEDATA, response);
1103
1104 CURLcode ret = curl_easy_perform(handle);
1105
1106 return ret;
1107 }
1108
1109 CURLcode do_unlock_request(CURL *handle, char *locktoken) {
1110 curl_easy_setopt(handle, CURLOPT_CUSTOMREQUEST, "UNLOCK");
1111 curl_easy_setopt(handle, CURLOPT_UPLOAD, 0L);
1112
1113 // set lock-token header
1114 sstr_t ltheader = ucx_sprintf("Lock-Token: <%s>", locktoken);
1115 struct curl_slist *headers = curl_slist_append(NULL, ltheader.ptr);
1116 curl_easy_setopt(handle, CURLOPT_HTTPHEADER, headers);
1117
1118 CURLcode ret = curl_easy_perform(handle);
1119 curl_slist_free_all(headers);
1120 free(ltheader.ptr);
1121
1122 return ret;
1123 }

mercurial