http-push: refactor curl_easy_setup madness
We were doing (nearly) the same thing all over the place, in slightly different orders, different variable names, etc. Refactor most calls into two helper functions, one for GET and one for everything else, that do the heavy lifting leaving most callsites a lot cleaner in the process. Note that the setting of CURLOPT_PUT at the callsites of curl_setup_http() which previously didn't do it (eg. locking_available(), remote_ls()) is safe, since that option is deprecated in libcurl in place of, and has the same effect as, CURLOPT_UPLOAD. Signed-off-by: Dan McGee <dpmcgee@gmail.com> Signed-off-by: Tay Ray Chuan <rctay89@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Dan McGee committed
May 3, 2011 at 23:47 UTC
ebaaf316ca5b7317a945a1590e8dab4e39932fbb
1 file changed
+48
-96
http-push.c
+48
-96
@@ -197,6 +197,34 @@ static char *xml_entities(const char *s)
197
return strbuf_detach(&buf, NULL);
198
}
199
200
+static void curl_setup_http_get(CURL *curl, const char *url,
201
+ const char *custom_req)
202
+{
203
+ curl_easy_setopt(curl, CURLOPT_HTTPGET, 1);
204
+ curl_easy_setopt(curl, CURLOPT_URL, url);
205
+ curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, custom_req);
206
+ curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite_null);
207
+}
208
+
209
+static void curl_setup_http(CURL *curl, const char *url,
210
+ const char *custom_req, struct buffer *buffer,
211
+ curl_write_callback write_fn)
212
+{
213
+ curl_easy_setopt(curl, CURLOPT_PUT, 1);
214
+ curl_easy_setopt(curl, CURLOPT_URL, url);
215
+ curl_easy_setopt(curl, CURLOPT_INFILE, buffer);
216
+ curl_easy_setopt(curl, CURLOPT_INFILESIZE, buffer->buf.len);
217
+ curl_easy_setopt(curl, CURLOPT_READFUNCTION, fread_buffer);
218
+#ifndef NO_CURL_IOCTL
219
+ curl_easy_setopt(curl, CURLOPT_IOCTLFUNCTION, ioctl_buffer);
220
+ curl_easy_setopt(curl, CURLOPT_IOCTLDATA, &buffer);
221
+#endif
222
+ curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_fn);
223
+ curl_easy_setopt(curl, CURLOPT_NOBODY, 0);
224
+ curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, custom_req);
225
+ curl_easy_setopt(curl, CURLOPT_UPLOAD, 1);
226
+}
227
+
228
static struct curl_slist *get_dav_token_headers(struct remote_lock *lock, enum dav_header_flag options)
229
{
230
struct strbuf buf = STRBUF_INIT;
@@ -272,11 +300,8 @@ static void start_mkcol(struct transfer_request *request)
300
slot = get_active_slot();
301
slot->callback_func = process_response;
302
slot->callback_data = request;
275
- curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1); /* undo PUT setup */
276
- curl_easy_setopt(slot->curl, CURLOPT_URL, request->url);
303
+ curl_setup_http_get(slot->curl, request->url, DAV_MKCOL);
304
curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, request->errorstr);
278
- curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_MKCOL);
279
- curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
305
306
if (start_active_slot(slot)) {
307
request->slot = slot;
@@ -395,19 +420,8 @@ static void start_put(struct transfer_request *request)
420
slot = get_active_slot();
421
slot->callback_func = process_response;
422
slot->callback_data = request;
398
- curl_easy_setopt(slot->curl, CURLOPT_INFILE, &request->buffer);
399
- curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, request->buffer.buf.len);
400
- curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
401
-#ifndef NO_CURL_IOCTL
402
- curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, ioctl_buffer);
403
- curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, &request->buffer);
404
-#endif
405
- curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
406
- curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
407
- curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PUT);
408
- curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
409
- curl_easy_setopt(slot->curl, CURLOPT_PUT, 1);
410
- curl_easy_setopt(slot->curl, CURLOPT_URL, request->url);
423
+ curl_setup_http(slot->curl, request->url, DAV_PUT,
424
+ &request->buffer, fwrite_null);
425
426
if (start_active_slot(slot)) {
427
request->slot = slot;
@@ -427,13 +441,10 @@ static void start_move(struct transfer_request *request)
441
slot = get_active_slot();
442
slot->callback_func = process_response;
443
slot->callback_data = request;
430
- curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1); /* undo PUT setup */
431
- curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_MOVE);
444
+ curl_setup_http_get(slot->curl, request->url, DAV_MOVE);
445
dav_headers = curl_slist_append(dav_headers, request->dest);
446
dav_headers = curl_slist_append(dav_headers, "Overwrite: T");
447
curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
435
- curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
436
- curl_easy_setopt(slot->curl, CURLOPT_URL, request->url);
448
449
if (start_active_slot(slot)) {
450
request->slot = slot;
@@ -458,10 +469,7 @@ static int refresh_lock(struct remote_lock *lock)
469
470
slot = get_active_slot();
471
slot->results = &results;
461
- curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
462
- curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
463
- curl_easy_setopt(slot->curl, CURLOPT_URL, lock->url);
464
- curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_LOCK);
472
+ curl_setup_http_get(slot->curl, lock->url, DAV_LOCK);
473
curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
474
475
if (start_active_slot(slot)) {
@@ -876,10 +884,7 @@ static struct remote_lock *lock_remote(const char *path, long timeout)
884
ep[1] = '\0';
885
slot = get_active_slot();
886
slot->results = &results;
879
- curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
880
- curl_easy_setopt(slot->curl, CURLOPT_URL, url);
881
- curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_MKCOL);
882
- curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
887
+ curl_setup_http_get(slot->curl, url, DAV_MKCOL);
888
if (start_active_slot(slot)) {
889
run_active_slot(slot);
890
if (results.curl_result != CURLE_OK &&
@@ -909,19 +914,9 @@ static struct remote_lock *lock_remote(const char *path, long timeout)
914
915
slot = get_active_slot();
916
slot->results = &results;
912
- curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer);
913
- curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.buf.len);
914
- curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
915
-#ifndef NO_CURL_IOCTL
916
- curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, ioctl_buffer);
917
- curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, &out_buffer);
918
-#endif
919
- curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
920
- curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
921
- curl_easy_setopt(slot->curl, CURLOPT_URL, url);
922
- curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
923
- curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_LOCK);
917
+ curl_setup_http(slot->curl, url, DAV_LOCK, &out_buffer, fwrite_buffer);
918
curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
919
+ curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
920
921
lock = xcalloc(1, sizeof(*lock));
922
lock->timeout = -1;
@@ -987,9 +982,7 @@ static int unlock_remote(struct remote_lock *lock)
982
983
slot = get_active_slot();
984
slot->results = &results;
990
- curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
991
- curl_easy_setopt(slot->curl, CURLOPT_URL, lock->url);
992
- curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_UNLOCK);
985
+ curl_setup_http_get(slot->curl, lock->url, DAV_UNLOCK);
986
curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
987
988
if (start_active_slot(slot)) {
@@ -1167,19 +1160,10 @@ static void remote_ls(const char *path, int flags,
1160
1161
slot = get_active_slot();
1162
slot->results = &results;
1170
- curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer);
1171
- curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.buf.len);
1172
- curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
1173
-#ifndef NO_CURL_IOCTL
1174
- curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, ioctl_buffer);
1175
- curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, &out_buffer);
1176
-#endif
1177
- curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
1178
- curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
1179
- curl_easy_setopt(slot->curl, CURLOPT_URL, url);
1180
- curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
1181
- curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PROPFIND);
1163
+ curl_setup_http(slot->curl, url, DAV_PROPFIND,
1164
+ &out_buffer, fwrite_buffer);
1165
curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1166
+ curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
1167
1168
if (start_active_slot(slot)) {
1169
run_active_slot(slot);
@@ -1250,19 +1234,10 @@ static int locking_available(void)
1234
1235
slot = get_active_slot();
1236
slot->results = &results;
1253
- curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer);
1254
- curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.buf.len);
1255
- curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
1256
-#ifndef NO_CURL_IOCTL
1257
- curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, ioctl_buffer);
1258
- curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, &out_buffer);
1259
-#endif
1260
- curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
1261
- curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
1262
- curl_easy_setopt(slot->curl, CURLOPT_URL, repo->url);
1263
- curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
1264
- curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PROPFIND);
1237
+ curl_setup_http(slot->curl, repo->url, DAV_PROPFIND,
1238
+ &out_buffer, fwrite_buffer);
1239
curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1240
+ curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
1241
1242
if (start_active_slot(slot)) {
1243
run_active_slot(slot);
@@ -1436,19 +1411,9 @@ static int update_remote(unsigned char *sha1, struct remote_lock *lock)
1411
1412
slot = get_active_slot();
1413
slot->results = &results;
1439
- curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer);
1440
- curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.buf.len);
1441
- curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
1442
-#ifndef NO_CURL_IOCTL
1443
- curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, ioctl_buffer);
1444
- curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, &out_buffer);
1445
-#endif
1446
- curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
1447
- curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PUT);
1414
+ curl_setup_http(slot->curl, lock->url, DAV_PUT,
1415
+ &out_buffer, fwrite_null);
1416
curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1449
- curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
1450
- curl_easy_setopt(slot->curl, CURLOPT_PUT, 1);
1451
- curl_easy_setopt(slot->curl, CURLOPT_URL, lock->url);
1417
1418
if (start_active_slot(slot)) {
1419
run_active_slot(slot);
@@ -1572,19 +1537,9 @@ static void update_remote_info_refs(struct remote_lock *lock)
1537
1538
slot = get_active_slot();
1539
slot->results = &results;
1575
- curl_easy_setopt(slot->curl, CURLOPT_INFILE, &buffer);
1576
- curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, buffer.buf.len);
1577
- curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
1578
-#ifndef NO_CURL_IOCTL
1579
- curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, ioctl_buffer);
1580
- curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, &buffer);
1581
-#endif
1582
- curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
1583
- curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PUT);
1540
+ curl_setup_http(slot->curl, lock->url, DAV_PUT,
1541
+ &buffer, fwrite_null);
1542
curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1585
- curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
1586
- curl_easy_setopt(slot->curl, CURLOPT_PUT, 1);
1587
- curl_easy_setopt(slot->curl, CURLOPT_URL, lock->url);
1543
1544
if (start_active_slot(slot)) {
1545
run_active_slot(slot);
@@ -1742,10 +1697,7 @@ static int delete_remote_branch(const char *pattern, int force)
1697
sprintf(url, "%s%s", repo->url, remote_ref->name);
1698
slot = get_active_slot();
1699
slot->results = &results;
1745
- curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
1746
- curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
1747
- curl_easy_setopt(slot->curl, CURLOPT_URL, url);
1748
- curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_DELETE);
1700
+ curl_setup_http_get(slot->curl, url, DAV_DELETE);
1701
if (start_active_slot(slot)) {
1702
run_active_slot(slot);
1703
free(url);