libidav/methods.c

changeset 354
067ea2315a8a
parent 331
9ca1e4706acc
child 361
b6f2462ee055
equal deleted inserted replaced
353:e554f3d72d9e 354:067ea2315a8a
41 #define xstreq(a,b) xmlStrEqual(BAD_CAST a, BAD_CAST b) 41 #define xstreq(a,b) xmlStrEqual(BAD_CAST a, BAD_CAST b)
42 42
43 /* ----------------------------- PROPFIND ----------------------------- */ 43 /* ----------------------------- PROPFIND ----------------------------- */
44 44
45 CURLcode do_propfind_request( 45 CURLcode do_propfind_request(
46 CURL *handle, 46 DavSession *sn,
47 UcxBuffer *request, 47 UcxBuffer *request,
48 UcxBuffer *response) 48 UcxBuffer *response)
49 { 49 {
50 CURL *handle = sn->handle;
50 curl_easy_setopt(handle, CURLOPT_CUSTOMREQUEST, "PROPFIND"); 51 curl_easy_setopt(handle, CURLOPT_CUSTOMREQUEST, "PROPFIND");
51 52
52 // always try to get information about possible children 53 // always try to get information about possible children
53 int depth = 1; 54 int depth = 1;
54 55
79 curl_easy_setopt(handle, CURLOPT_HTTPHEADER, headers); 80 curl_easy_setopt(handle, CURLOPT_HTTPHEADER, headers);
80 81
81 // reset buffers and perform request 82 // reset buffers and perform request
82 request->pos = 0; 83 request->pos = 0;
83 response->size = response->pos = 0; 84 response->size = response->pos = 0;
84 ret = curl_easy_perform(handle); 85 ret = dav_session_curl_perform_buf(sn, request, response, NULL);
85 curl_slist_free_all(headers); 86 curl_slist_free_all(headers);
86 headers = NULL; 87 headers = NULL;
87 88
88 /* 89 /*
89 * Handle two cases: 90 * Handle two cases:
707 708
708 709
709 /* ----------------------------- PROPPATCH ----------------------------- */ 710 /* ----------------------------- PROPPATCH ----------------------------- */
710 711
711 CURLcode do_proppatch_request( 712 CURLcode do_proppatch_request(
712 CURL *handle, 713 DavSession *sn,
713 char *lock, 714 char *lock,
714 UcxBuffer *request, 715 UcxBuffer *request,
715 UcxBuffer *response) 716 UcxBuffer *response)
716 { 717 {
718 CURL *handle = sn->handle;
717 curl_easy_setopt(handle, CURLOPT_CUSTOMREQUEST, "PROPPATCH"); 719 curl_easy_setopt(handle, CURLOPT_CUSTOMREQUEST, "PROPPATCH");
718 720
719 struct curl_slist *headers = NULL; 721 struct curl_slist *headers = NULL;
720 headers = curl_slist_append(headers, "Content-Type: text/xml"); 722 headers = curl_slist_append(headers, "Content-Type: text/xml");
721 if(lock) { 723 if(lock) {
735 737
736 curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, ucx_buffer_write); 738 curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, ucx_buffer_write);
737 curl_easy_setopt(handle, CURLOPT_WRITEDATA, response); 739 curl_easy_setopt(handle, CURLOPT_WRITEDATA, response);
738 740
739 ucx_buffer_seek(request, 0, SEEK_SET); 741 ucx_buffer_seek(request, 0, SEEK_SET);
740 CURLcode ret = curl_easy_perform(handle); 742 CURLcode ret = dav_session_curl_perform_buf(sn, request, response, NULL);
741 curl_slist_free_all(headers); 743 curl_slist_free_all(headers);
742 return ret; 744 return ret;
743 } 745 }
744 746
745 UcxBuffer* create_proppatch_request(DavResourceData *data) { 747 UcxBuffer* create_proppatch_request(DavResourceData *data) {
913 static size_t dummy_write(void *buf, size_t s, size_t n, void *data) { 915 static size_t dummy_write(void *buf, size_t s, size_t n, void *data) {
914 //fwrite(buf, s, n, stdout); 916 //fwrite(buf, s, n, stdout);
915 return s*n; 917 return s*n;
916 } 918 }
917 919
918 CURLcode do_put_request(CURL *handle, char *lock, DavBool create, void *data, dav_read_func read_func, size_t length) { 920 CURLcode do_put_request(DavSession *sn, char *lock, DavBool create, void *data, dav_read_func read_func, size_t length) {
921 CURL *handle = sn->handle;
919 curl_easy_setopt(handle, CURLOPT_CUSTOMREQUEST, NULL); 922 curl_easy_setopt(handle, CURLOPT_CUSTOMREQUEST, NULL);
920 curl_easy_setopt(handle, CURLOPT_UPLOAD, 1L); 923 curl_easy_setopt(handle, CURLOPT_UPLOAD, 1L);
921 924
922 // clear headers 925 // clear headers
923 struct curl_slist *headers = NULL; 926 struct curl_slist *headers = NULL;
957 curl_easy_setopt(handle, CURLOPT_READDATA, data); 960 curl_easy_setopt(handle, CURLOPT_READDATA, data);
958 961
959 curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, dummy_write); 962 curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, dummy_write);
960 curl_easy_setopt(handle, CURLOPT_WRITEDATA, NULL); 963 curl_easy_setopt(handle, CURLOPT_WRITEDATA, NULL);
961 964
962 CURLcode ret = curl_easy_perform(handle); 965 CURLcode ret = dav_session_curl_perform(sn, NULL);
963 curl_slist_free_all(headers); 966 curl_slist_free_all(headers);
964 if(buf) { 967 if(buf) {
965 ucx_buffer_free(buf); 968 ucx_buffer_free(buf);
966 } 969 }
967 970
968 return ret; 971 return ret;
969 } 972 }
970 973
971 CURLcode do_delete_request(CURL *handle, char *lock, UcxBuffer *response) { 974 CURLcode do_delete_request(DavSession *sn, char *lock, UcxBuffer *response) {
975 CURL *handle = sn->handle;
972 struct curl_slist *headers = NULL; 976 struct curl_slist *headers = NULL;
973 if(lock) { 977 if(lock) {
974 char *url = NULL; 978 char *url = NULL;
975 curl_easy_getinfo(handle, CURLINFO_EFFECTIVE_URL, &url); 979 curl_easy_getinfo(handle, CURLINFO_EFFECTIVE_URL, &url);
976 char *ltheader = ucx_sprintf("If: <%s> (<%s>)", url, lock).ptr; 980 char *ltheader = ucx_sprintf("If: <%s> (<%s>)", url, lock).ptr;
985 curl_easy_setopt(handle, CURLOPT_UPLOAD, 0L); 989 curl_easy_setopt(handle, CURLOPT_UPLOAD, 0L);
986 990
987 curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, ucx_buffer_write); 991 curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, ucx_buffer_write);
988 curl_easy_setopt(handle, CURLOPT_WRITEDATA, response); 992 curl_easy_setopt(handle, CURLOPT_WRITEDATA, response);
989 993
990 CURLcode ret = curl_easy_perform(handle); 994 CURLcode ret = dav_session_curl_perform(sn, NULL);
991 curl_slist_free_all(headers); 995 curl_slist_free_all(headers);
992 return ret; 996 return ret;
993 } 997 }
994 998
995 CURLcode do_mkcol_request(CURL *handle, char *lock) { 999 CURLcode do_mkcol_request(DavSession *sn, char *lock) {
1000 CURL *handle = sn->handle;
996 struct curl_slist *headers = NULL; 1001 struct curl_slist *headers = NULL;
997 if(lock) { 1002 if(lock) {
998 char *url = NULL; 1003 char *url = NULL;
999 curl_easy_getinfo(handle, CURLINFO_EFFECTIVE_URL, &url); 1004 curl_easy_getinfo(handle, CURLINFO_EFFECTIVE_URL, &url);
1000 url = util_parent_path(url); 1005 url = util_parent_path(url);
1012 curl_easy_setopt(handle, CURLOPT_UPLOAD, 0L); 1017 curl_easy_setopt(handle, CURLOPT_UPLOAD, 0L);
1013 1018
1014 curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, dummy_write); 1019 curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, dummy_write);
1015 curl_easy_setopt(handle, CURLOPT_WRITEDATA, NULL); 1020 curl_easy_setopt(handle, CURLOPT_WRITEDATA, NULL);
1016 1021
1017 CURLcode ret = curl_easy_perform(handle); 1022 CURLcode ret = dav_session_curl_perform(sn, NULL);
1018 curl_slist_free_all(headers); 1023 curl_slist_free_all(headers);
1019 return ret; 1024 return ret;
1020 } 1025 }
1021 1026
1022 1027
1023 CURLcode do_head_request(CURL *handle) { 1028 CURLcode do_head_request(DavSession *sn) {
1029 CURL *handle = sn->handle;
1024 curl_easy_setopt(handle, CURLOPT_CUSTOMREQUEST, "HEAD"); 1030 curl_easy_setopt(handle, CURLOPT_CUSTOMREQUEST, "HEAD");
1025 curl_easy_setopt(handle, CURLOPT_UPLOAD, 0L); 1031 curl_easy_setopt(handle, CURLOPT_UPLOAD, 0L);
1026 curl_easy_setopt(handle, CURLOPT_NOBODY, 1L); 1032 curl_easy_setopt(handle, CURLOPT_NOBODY, 1L);
1027 1033
1028 // clear headers 1034 // clear headers
1030 curl_easy_setopt(handle, CURLOPT_HTTPHEADER, headers); 1036 curl_easy_setopt(handle, CURLOPT_HTTPHEADER, headers);
1031 1037
1032 curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, dummy_write); 1038 curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, dummy_write);
1033 curl_easy_setopt(handle, CURLOPT_WRITEDATA, NULL); 1039 curl_easy_setopt(handle, CURLOPT_WRITEDATA, NULL);
1034 1040
1035 CURLcode ret = curl_easy_perform(handle); 1041 CURLcode ret = dav_session_curl_perform(sn, NULL);
1036 curl_easy_setopt(handle, CURLOPT_NOBODY, 0L); 1042 curl_easy_setopt(handle, CURLOPT_NOBODY, 0L);
1037 return ret; 1043 return ret;
1038 } 1044 }
1039 1045
1040 1046
1041 CURLcode do_copy_move_request(CURL *handle, char *dest, char *lock, DavBool copy, DavBool override) { 1047 CURLcode do_copy_move_request(DavSession *sn, char *dest, char *lock, DavBool copy, DavBool override) {
1048 CURL *handle = sn->handle;
1042 if(copy) { 1049 if(copy) {
1043 curl_easy_setopt(handle, CURLOPT_CUSTOMREQUEST, "COPY"); 1050 curl_easy_setopt(handle, CURLOPT_CUSTOMREQUEST, "COPY");
1044 } else { 1051 } else {
1045 curl_easy_setopt(handle, CURLOPT_CUSTOMREQUEST, "MOVE"); 1052 curl_easy_setopt(handle, CURLOPT_CUSTOMREQUEST, "MOVE");
1046 } 1053 }
1066 } else { 1073 } else {
1067 headers = curl_slist_append(headers, "Overwrite: F"); 1074 headers = curl_slist_append(headers, "Overwrite: F");
1068 } 1075 }
1069 curl_easy_setopt(handle, CURLOPT_HTTPHEADER, headers); 1076 curl_easy_setopt(handle, CURLOPT_HTTPHEADER, headers);
1070 1077
1071 CURLcode ret = curl_easy_perform(handle); 1078 CURLcode ret = dav_session_curl_perform(sn, NULL);
1072 free(deststr.ptr); 1079 free(deststr.ptr);
1073 curl_slist_free_all(headers); 1080 curl_slist_free_all(headers);
1074 headers = NULL; 1081 headers = NULL;
1075 curl_easy_setopt(handle, CURLOPT_HTTPHEADER, headers); 1082 curl_easy_setopt(handle, CURLOPT_HTTPHEADER, headers);
1076 return ret; 1083 return ret;
1154 1161
1155 xmlFreeDoc(doc); 1162 xmlFreeDoc(doc);
1156 return ret; 1163 return ret;
1157 } 1164 }
1158 1165
1159 CURLcode do_lock_request(CURL *handle, UcxBuffer *request, UcxBuffer *response, time_t timeout) { 1166 CURLcode do_lock_request(DavSession *sn, UcxBuffer *request, UcxBuffer *response, time_t timeout) {
1167 CURL *handle = sn->handle;
1160 curl_easy_setopt(handle, CURLOPT_CUSTOMREQUEST, "LOCK"); 1168 curl_easy_setopt(handle, CURLOPT_CUSTOMREQUEST, "LOCK");
1161 curl_easy_setopt(handle, CURLOPT_UPLOAD, 1L); 1169 curl_easy_setopt(handle, CURLOPT_UPLOAD, 1L);
1162 request->pos = 0; 1170 request->pos = 0;
1163 1171
1164 // clear headers 1172 // clear headers
1182 curl_easy_setopt(handle, CURLOPT_INFILESIZE, request->size); 1190 curl_easy_setopt(handle, CURLOPT_INFILESIZE, request->size);
1183 1191
1184 curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, ucx_buffer_write); 1192 curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, ucx_buffer_write);
1185 curl_easy_setopt(handle, CURLOPT_WRITEDATA, response); 1193 curl_easy_setopt(handle, CURLOPT_WRITEDATA, response);
1186 1194
1187 CURLcode ret = curl_easy_perform(handle); 1195 CURLcode ret = dav_session_curl_perform_buf(sn, request, response, NULL);
1188 1196
1189 if(headers) { 1197 if(headers) {
1190 curl_slist_free_all(headers); 1198 curl_slist_free_all(headers);
1191 } 1199 }
1192 1200
1193 return ret; 1201 return ret;
1194 } 1202 }
1195 1203
1196 CURLcode do_unlock_request(CURL *handle, char *locktoken) { 1204 CURLcode do_unlock_request(DavSession *sn, char *locktoken) {
1205 CURL *handle = sn->handle;
1197 curl_easy_setopt(handle, CURLOPT_CUSTOMREQUEST, "UNLOCK"); 1206 curl_easy_setopt(handle, CURLOPT_CUSTOMREQUEST, "UNLOCK");
1198 curl_easy_setopt(handle, CURLOPT_UPLOAD, 0L); 1207 curl_easy_setopt(handle, CURLOPT_UPLOAD, 0L);
1199 1208
1200 curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, dummy_write); 1209 curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, dummy_write);
1201 curl_easy_setopt(handle, CURLOPT_WRITEDATA, NULL); 1210 curl_easy_setopt(handle, CURLOPT_WRITEDATA, NULL);
1203 // set lock-token header 1212 // set lock-token header
1204 sstr_t ltheader = ucx_sprintf("Lock-Token: <%s>", locktoken); 1213 sstr_t ltheader = ucx_sprintf("Lock-Token: <%s>", locktoken);
1205 struct curl_slist *headers = curl_slist_append(NULL, ltheader.ptr); 1214 struct curl_slist *headers = curl_slist_append(NULL, ltheader.ptr);
1206 curl_easy_setopt(handle, CURLOPT_HTTPHEADER, headers); 1215 curl_easy_setopt(handle, CURLOPT_HTTPHEADER, headers);
1207 1216
1208 CURLcode ret = curl_easy_perform(handle); 1217 CURLcode ret = dav_session_curl_perform(sn, NULL);
1209 curl_slist_free_all(headers); 1218 curl_slist_free_all(headers);
1210 free(ltheader.ptr); 1219 free(ltheader.ptr);
1211 1220
1212 return ret; 1221 return ret;
1213 } 1222 }

mercurial