#include <curl/curl.h>
#include <curl/easy.h>
+#if LIBCURL_VERSION_NUM >= 0x070908
+#define USE_CURL_MULTI
#define DEFAULT_MAX_REQUESTS 5
+#endif
#if LIBCURL_VERSION_NUM < 0x070704
#define curl_global_cleanup() do { /* nothing */ } while(0)
#define PREV_BUF_SIZE 4096
#define RANGE_HEADER_SIZE 30
-static int max_requests = DEFAULT_MAX_REQUESTS;
static int active_requests = 0;
static int data_received;
+#ifdef USE_CURL_MULTI
+static int max_requests = DEFAULT_MAX_REQUESTS;
static CURLM *curlm;
+#endif
static CURL *curl_default;
static struct curl_slist *no_pragma_header;
static struct curl_slist *no_range_header;
return 0;
}
+#ifdef USE_CURL_MULTI
void process_curl_messages();
void process_request_queue();
+#endif
struct active_request_slot *get_active_slot()
{
struct active_request_slot *slot = active_queue_head;
struct active_request_slot *newslot;
+
+#ifdef USE_CURL_MULTI
int num_transfers;
/* Wait for a slot to open up if the queue is full */
process_curl_messages();
}
}
+#endif
while (slot != NULL && slot->in_use) {
slot = slot->next;
int start_active_slot(struct active_request_slot *slot)
{
+#ifdef USE_CURL_MULTI
CURLMcode curlm_result = curl_multi_add_handle(curlm, slot->curl);
if (curlm_result != CURLM_OK &&
slot->in_use = 0;
return 0;
}
-
+#endif
return 1;
}
void run_active_slot(struct active_request_slot *slot)
{
+#ifdef USE_CURL_MULTI
int num_transfers;
long last_pos = 0;
long current_pos;
&excfds, &select_timeout);
}
}
+#else
+ slot->curl_result = curl_easy_perform(slot->curl);
+ active_requests--;
+#endif
}
void start_request(struct transfer_request *request)
CURLOPT_HTTPHEADER, range_header);
}
- /* Try to add to multi handle, abort the request on error */
+ /* Try to get the request started, abort the request on error */
if (!start_active_slot(slot)) {
request->state = ABORTED;
close(request->local);
free(request);
}
+#ifdef USE_CURL_MULTI
void process_curl_messages()
{
int num_messages;
request = request->next;
}
}
+#endif
void prefetch(unsigned char *sha1)
{
}
tail->next = newreq;
}
+#ifdef USE_CURL_MULTI
process_request_queue();
process_curl_messages();
+#endif
}
static int got_alternates = 0;
char *hex = sha1_to_hex(sha1);
int ret;
struct transfer_request *request = request_queue_head;
- int num_transfers;
while (request != NULL && memcmp(request->sha1, sha1, 20))
request = request->next;
if (request == NULL)
return error("Couldn't find request for %s in the queue", hex);
+#ifdef USE_CURL_MULTI
+ int num_transfers;
while (request->state == WAITING) {
curl_multi_perform(curlm, &num_transfers);
if (num_transfers < active_requests) {
process_request_queue();
}
}
+#else
+ start_request(request);
+#endif
- if (request->state == ACTIVE)
+ while (request->state == ACTIVE) {
run_active_slot(request->slot);
+#ifndef USE_CURL_MULTI
+ request->curl_result = request->slot->curl_result;
+ curl_easy_getinfo(request->slot->curl,
+ CURLINFO_HTTP_CODE,
+ &request->http_code);
+ request->slot = NULL;
+
+ /* Use alternates if necessary */
+ if (request->http_code == 404 &&
+ request->repo->next != NULL) {
+ request->repo = request->repo->next;
+ start_request(request);
+ } else {
+ finish_request(request);
+ request->state = COMPLETE;
+ }
+#endif
+ }
if (request->state == ABORTED) {
release_request(request);
arg++;
} else if (!strcmp(argv[arg], "--recover")) {
get_recover = 1;
+#ifdef USE_CURL_MULTI
} else if (argv[arg][1] == 'r') {
max_requests = atoi(argv[arg + 1]);
if (max_requests < 1)
max_requests = DEFAULT_MAX_REQUESTS;
arg++;
+#endif
}
arg++;
}
if (argc < arg + 2) {
+#ifdef USE_CURL_MULTI
+ usage("git-http-fetch [-c] [-t] [-a] [-d] [-v] [-r concurrent-request-limit] [--recover] [-w ref] commit-id url");
+#else
usage("git-http-fetch [-c] [-t] [-a] [-d] [-v] [--recover] [-w ref] commit-id url");
+#endif
return 1;
}
commit_id = argv[arg];
curl_global_init(CURL_GLOBAL_ALL);
+#ifdef USE_CURL_MULTI
curlm = curl_multi_init();
if (curlm == NULL) {
fprintf(stderr, "Error creating curl multi handle.\n");
return 1;
}
+#endif
no_pragma_header = curl_slist_append(no_pragma_header, "Pragma:");
no_range_header = curl_slist_append(no_range_header, "Range:");
curl_easy_cleanup(slot->curl);
slot = slot->next;
}
+#ifdef USE_CURL_MULTI
curl_multi_cleanup(curlm);
+#endif
curl_global_cleanup();
return 0;
}