Only compile parallel HTTP support with CURL >= 7.9.8
Only compile parallel HTTP support with CURL >= 7.9.8 Signed-off-by: Nick Hengeveld <nickh@reactrix.com> Signed-off-by: Junio C Hamano <junkio@cox.net>
Nick Hengeveld committed
Oct 10, 2005 at 23:22 UTC
a7a8d3786e6b60a472d0f88f778cacaead122a6c
1 file changed
+57
-5
http-fetch.c
+57
-5
@@ -6,7 +6,10 @@
6
#include <curl/curl.h>
7
#include <curl/easy.h>
8
9
+#if LIBCURL_VERSION_NUM >= 0x070908
10
+#define USE_CURL_MULTI
11
#define DEFAULT_MAX_REQUESTS 5
12
+#endif
13
14
#if LIBCURL_VERSION_NUM < 0x070704
15
#define curl_global_cleanup() do { /* nothing */ } while(0)
@@ -18,11 +21,13 @@
21
#define PREV_BUF_SIZE 4096
22
#define RANGE_HEADER_SIZE 30
23
21
-static int max_requests = DEFAULT_MAX_REQUESTS;
24
static int active_requests = 0;
25
static int data_received;
26
27
+#ifdef USE_CURL_MULTI
28
+static int max_requests = DEFAULT_MAX_REQUESTS;
29
static CURLM *curlm;
30
+#endif
31
static CURL *curl_default;
32
static struct curl_slist *no_pragma_header;
33
static struct curl_slist *no_range_header;
@@ -151,13 +156,17 @@ int relink_or_rename(char *old, char *new) {
156
return 0;
157
}
158
159
+#ifdef USE_CURL_MULTI
160
void process_curl_messages();
161
void process_request_queue();
162
+#endif
163
164
struct active_request_slot *get_active_slot()
165
{
166
struct active_request_slot *slot = active_queue_head;
167
struct active_request_slot *newslot;
168
+
169
+#ifdef USE_CURL_MULTI
170
int num_transfers;
171
172
/* Wait for a slot to open up if the queue is full */
@@ -167,6 +176,7 @@ struct active_request_slot *get_active_slot()
176
process_curl_messages();
177
}
178
}
179
+#endif
180
181
while (slot != NULL && slot->in_use) {
182
slot = slot->next;
@@ -202,6 +212,7 @@ struct active_request_slot *get_active_slot()
212
213
int start_active_slot(struct active_request_slot *slot)
214
{
215
+#ifdef USE_CURL_MULTI
216
CURLMcode curlm_result = curl_multi_add_handle(curlm, slot->curl);
217
218
if (curlm_result != CURLM_OK &&
@@ -210,12 +221,13 @@ int start_active_slot(struct active_request_slot *slot)
221
slot->in_use = 0;
222
return 0;
223
}
213
-
224
+#endif
225
return 1;
226
}
227
228
void run_active_slot(struct active_request_slot *slot)
229
{
230
+#ifdef USE_CURL_MULTI
231
int num_transfers;
232
long last_pos = 0;
233
long current_pos;
@@ -255,6 +267,10 @@ void run_active_slot(struct active_request_slot *slot)
267
&excfds, &select_timeout);
268
}
269
}
270
+#else
271
+ slot->curl_result = curl_easy_perform(slot->curl);
272
+ active_requests--;
273
+#endif
274
}
275
276
void start_request(struct transfer_request *request)
@@ -356,7 +372,7 @@ void start_request(struct transfer_request *request)
372
CURLOPT_HTTPHEADER, range_header);
373
}
374
359
- /* Try to add to multi handle, abort the request on error */
375
+ /* Try to get the request started, abort the request on error */
376
if (!start_active_slot(slot)) {
377
request->state = ABORTED;
378
close(request->local);
@@ -413,6 +429,7 @@ void release_request(struct transfer_request *request)
429
free(request);
430
}
431
432
+#ifdef USE_CURL_MULTI
433
void process_curl_messages()
434
{
435
int num_messages;
@@ -478,6 +495,7 @@ void process_request_queue()
495
request = request->next;
496
}
497
}
498
+#endif
499
500
void prefetch(unsigned char *sha1)
501
{
@@ -505,8 +523,10 @@ void prefetch(unsigned char *sha1)
523
}
524
tail->next = newreq;
525
}
526
+#ifdef USE_CURL_MULTI
527
process_request_queue();
528
process_curl_messages();
529
+#endif
530
}
531
532
static int got_alternates = 0;
@@ -864,13 +884,14 @@ static int fetch_object(struct alt_base *repo, unsigned char *sha1)
884
char *hex = sha1_to_hex(sha1);
885
int ret;
886
struct transfer_request *request = request_queue_head;
867
- int num_transfers;
887
888
while (request != NULL && memcmp(request->sha1, sha1, 20))
889
request = request->next;
890
if (request == NULL)
891
return error("Couldn't find request for %s in the queue", hex);
892
893
+#ifdef USE_CURL_MULTI
894
+ int num_transfers;
895
while (request->state == WAITING) {
896
curl_multi_perform(curlm, &num_transfers);
897
if (num_transfers < active_requests) {
@@ -878,9 +899,30 @@ static int fetch_object(struct alt_base *repo, unsigned char *sha1)
899
process_request_queue();
900
}
901
}
902
+#else
903
+ start_request(request);
904
+#endif
905
882
- if (request->state == ACTIVE)
906
+ while (request->state == ACTIVE) {
907
run_active_slot(request->slot);
908
+#ifndef USE_CURL_MULTI
909
+ request->curl_result = request->slot->curl_result;
910
+ curl_easy_getinfo(request->slot->curl,
911
+ CURLINFO_HTTP_CODE,
912
+ &request->http_code);
913
+ request->slot = NULL;
914
+
915
+ /* Use alternates if necessary */
916
+ if (request->http_code == 404 &&
917
+ request->repo->next != NULL) {
918
+ request->repo = request->repo->next;
919
+ start_request(request);
920
+ } else {
921
+ finish_request(request);
922
+ request->state = COMPLETE;
923
+ }
924
+#endif
925
+ }
926
927
if (request->state == ABORTED) {
928
release_request(request);
@@ -992,16 +1034,22 @@ int main(int argc, char **argv)
1034
arg++;
1035
} else if (!strcmp(argv[arg], "--recover")) {
1036
get_recover = 1;
1037
+#ifdef USE_CURL_MULTI
1038
} else if (argv[arg][1] == 'r') {
1039
max_requests = atoi(argv[arg + 1]);
1040
if (max_requests < 1)
1041
max_requests = DEFAULT_MAX_REQUESTS;
1042
arg++;
1043
+#endif
1044
}
1045
arg++;
1046
}
1047
if (argc < arg + 2) {
1048
+#ifdef USE_CURL_MULTI
1049
+ usage("git-http-fetch [-c] [-t] [-a] [-d] [-v] [-r concurrent-request-limit] [--recover] [-w ref] commit-id url");
1050
+#else
1051
usage("git-http-fetch [-c] [-t] [-a] [-d] [-v] [--recover] [-w ref] commit-id url");
1052
+#endif
1053
return 1;
1054
}
1055
commit_id = argv[arg];
@@ -1009,11 +1057,13 @@ int main(int argc, char **argv)
1057
1058
curl_global_init(CURL_GLOBAL_ALL);
1059
1060
+#ifdef USE_CURL_MULTI
1061
curlm = curl_multi_init();
1062
if (curlm == NULL) {
1063
fprintf(stderr, "Error creating curl multi handle.\n");
1064
return 1;
1065
}
1066
+#endif
1067
no_pragma_header = curl_slist_append(no_pragma_header, "Pragma:");
1068
no_range_header = curl_slist_append(no_range_header, "Range:");
1069
@@ -1061,7 +1111,9 @@ int main(int argc, char **argv)
1111
curl_easy_cleanup(slot->curl);
1112
slot = slot->next;
1113
}
1114
+#ifdef USE_CURL_MULTI
1115
curl_multi_cleanup(curlm);
1116
+#endif
1117
curl_global_cleanup();
1118
return 0;
1119
}