4 int active_requests = 0;
10 #ifndef NO_CURL_EASY_DUPHANDLE
13 char curl_errorstr[CURL_ERROR_SIZE];
15 int curl_ssl_verify = -1;
16 char *ssl_cert = NULL;
17 #if LIBCURL_VERSION_NUM >= 0x070902
20 #if LIBCURL_VERSION_NUM >= 0x070908
21 char *ssl_capath = NULL;
23 char *ssl_cainfo = NULL;
24 long curl_low_speed_limit = -1;
25 long curl_low_speed_time = -1;
27 struct curl_slist *pragma_header;
29 struct active_request_slot *active_queue_head = NULL;
31 size_t fread_buffer(void *ptr, size_t eltsize, size_t nmemb,
32 struct buffer *buffer)
34 size_t size = eltsize * nmemb;
35 if (size > buffer->size - buffer->posn)
36 size = buffer->size - buffer->posn;
37 memcpy(ptr, buffer->buffer + buffer->posn, size);
42 size_t fwrite_buffer(const void *ptr, size_t eltsize,
43 size_t nmemb, struct buffer *buffer)
45 size_t size = eltsize * nmemb;
46 if (size > buffer->size - buffer->posn) {
47 buffer->size = buffer->size * 3 / 2;
48 if (buffer->size < buffer->posn + size)
49 buffer->size = buffer->posn + size;
50 buffer->buffer = xrealloc(buffer->buffer, buffer->size);
52 memcpy(buffer->buffer + buffer->posn, ptr, size);
58 size_t fwrite_null(const void *ptr, size_t eltsize,
59 size_t nmemb, struct buffer *buffer)
62 return eltsize * nmemb;
65 static void finish_active_slot(struct active_request_slot *slot);
68 static void process_curl_messages(void)
71 struct active_request_slot *slot;
72 CURLMsg *curl_message = curl_multi_info_read(curlm, &num_messages);
74 while (curl_message != NULL) {
75 if (curl_message->msg == CURLMSG_DONE) {
76 int curl_result = curl_message->data.result;
77 slot = active_queue_head;
78 while (slot != NULL &&
79 slot->curl != curl_message->easy_handle)
82 curl_multi_remove_handle(curlm, slot->curl);
83 slot->curl_result = curl_result;
84 finish_active_slot(slot);
86 fprintf(stderr, "Received DONE message for unknown request!\n");
89 fprintf(stderr, "Unknown CURL message received: %d\n",
90 (int)curl_message->msg);
92 curl_message = curl_multi_info_read(curlm, &num_messages);
97 static int http_options(const char *var, const char *value)
99 if (!strcmp("http.sslverify", var)) {
100 if (curl_ssl_verify == -1) {
101 curl_ssl_verify = git_config_bool(var, value);
106 if (!strcmp("http.sslcert", var)) {
107 if (ssl_cert == NULL) {
108 ssl_cert = xmalloc(strlen(value)+1);
109 strcpy(ssl_cert, value);
113 #if LIBCURL_VERSION_NUM >= 0x070902
114 if (!strcmp("http.sslkey", var)) {
115 if (ssl_key == NULL) {
116 ssl_key = xmalloc(strlen(value)+1);
117 strcpy(ssl_key, value);
122 #if LIBCURL_VERSION_NUM >= 0x070908
123 if (!strcmp("http.sslcapath", var)) {
124 if (ssl_capath == NULL) {
125 ssl_capath = xmalloc(strlen(value)+1);
126 strcpy(ssl_capath, value);
131 if (!strcmp("http.sslcainfo", var)) {
132 if (ssl_cainfo == NULL) {
133 ssl_cainfo = xmalloc(strlen(value)+1);
134 strcpy(ssl_cainfo, value);
139 #ifdef USE_CURL_MULTI
140 if (!strcmp("http.maxrequests", var)) {
141 if (max_requests == -1)
142 max_requests = git_config_int(var, value);
147 if (!strcmp("http.lowspeedlimit", var)) {
148 if (curl_low_speed_limit == -1)
149 curl_low_speed_limit = (long)git_config_int(var, value);
152 if (!strcmp("http.lowspeedtime", var)) {
153 if (curl_low_speed_time == -1)
154 curl_low_speed_time = (long)git_config_int(var, value);
158 /* Fall back on the default ones */
159 return git_default_config(var, value);
162 static CURL* get_curl_handle(void)
164 CURL* result = curl_easy_init();
166 curl_easy_setopt(result, CURLOPT_SSL_VERIFYPEER, curl_ssl_verify);
167 #if LIBCURL_VERSION_NUM >= 0x070907
168 curl_easy_setopt(result, CURLOPT_NETRC, CURL_NETRC_OPTIONAL);
171 if (ssl_cert != NULL)
172 curl_easy_setopt(result, CURLOPT_SSLCERT, ssl_cert);
173 #if LIBCURL_VERSION_NUM >= 0x070902
175 curl_easy_setopt(result, CURLOPT_SSLKEY, ssl_key);
177 #if LIBCURL_VERSION_NUM >= 0x070908
178 if (ssl_capath != NULL)
179 curl_easy_setopt(result, CURLOPT_CAPATH, ssl_capath);
181 if (ssl_cainfo != NULL)
182 curl_easy_setopt(result, CURLOPT_CAINFO, ssl_cainfo);
183 curl_easy_setopt(result, CURLOPT_FAILONERROR, 1);
185 if (curl_low_speed_limit > 0 && curl_low_speed_time > 0) {
186 curl_easy_setopt(result, CURLOPT_LOW_SPEED_LIMIT,
187 curl_low_speed_limit);
188 curl_easy_setopt(result, CURLOPT_LOW_SPEED_TIME,
189 curl_low_speed_time);
192 curl_easy_setopt(result, CURLOPT_FOLLOWLOCATION, 1);
194 if (getenv("GIT_CURL_VERBOSE"))
195 curl_easy_setopt(result, CURLOPT_VERBOSE, 1);
197 curl_easy_setopt(result, CURLOPT_USERAGENT, GIT_USER_AGENT);
204 char *low_speed_limit;
205 char *low_speed_time;
207 curl_global_init(CURL_GLOBAL_ALL);
209 pragma_header = curl_slist_append(pragma_header, "Pragma: no-cache");
211 #ifdef USE_CURL_MULTI
213 char *http_max_requests = getenv("GIT_HTTP_MAX_REQUESTS");
214 if (http_max_requests != NULL)
215 max_requests = atoi(http_max_requests);
218 curlm = curl_multi_init();
220 fprintf(stderr, "Error creating curl multi handle.\n");
225 if (getenv("GIT_SSL_NO_VERIFY"))
228 ssl_cert = getenv("GIT_SSL_CERT");
229 #if LIBCURL_VERSION_NUM >= 0x070902
230 ssl_key = getenv("GIT_SSL_KEY");
232 #if LIBCURL_VERSION_NUM >= 0x070908
233 ssl_capath = getenv("GIT_SSL_CAPATH");
235 ssl_cainfo = getenv("GIT_SSL_CAINFO");
237 low_speed_limit = getenv("GIT_HTTP_LOW_SPEED_LIMIT");
238 if (low_speed_limit != NULL)
239 curl_low_speed_limit = strtol(low_speed_limit, NULL, 10);
240 low_speed_time = getenv("GIT_HTTP_LOW_SPEED_TIME");
241 if (low_speed_time != NULL)
242 curl_low_speed_time = strtol(low_speed_time, NULL, 10);
244 git_config(http_options);
246 if (curl_ssl_verify == -1)
249 #ifdef USE_CURL_MULTI
250 if (max_requests < 1)
251 max_requests = DEFAULT_MAX_REQUESTS;
254 #ifndef NO_CURL_EASY_DUPHANDLE
255 curl_default = get_curl_handle();
259 void http_cleanup(void)
261 struct active_request_slot *slot = active_queue_head;
262 #ifdef USE_CURL_MULTI
266 while (slot != NULL) {
267 #ifdef USE_CURL_MULTI
269 curl_easy_getinfo(slot->curl,
270 CURLINFO_EFFECTIVE_URL,
272 fprintf(stderr, "Waiting for %s\n", wait_url);
273 run_active_slot(slot);
276 if (slot->curl != NULL)
277 curl_easy_cleanup(slot->curl);
281 #ifndef NO_CURL_EASY_DUPHANDLE
282 curl_easy_cleanup(curl_default);
285 #ifdef USE_CURL_MULTI
286 curl_multi_cleanup(curlm);
288 curl_global_cleanup();
292 struct active_request_slot *get_active_slot(void)
294 struct active_request_slot *slot = active_queue_head;
295 struct active_request_slot *newslot;
297 #ifdef USE_CURL_MULTI
300 /* Wait for a slot to open up if the queue is full */
301 while (active_requests >= max_requests) {
302 curl_multi_perform(curlm, &num_transfers);
303 if (num_transfers < active_requests) {
304 process_curl_messages();
309 while (slot != NULL && slot->in_use) {
313 newslot = xmalloc(sizeof(*newslot));
314 newslot->curl = NULL;
316 newslot->next = NULL;
318 slot = active_queue_head;
320 active_queue_head = newslot;
322 while (slot->next != NULL) {
325 slot->next = newslot;
330 if (slot->curl == NULL) {
331 #ifdef NO_CURL_EASY_DUPHANDLE
332 slot->curl = get_curl_handle();
334 slot->curl = curl_easy_duphandle(curl_default);
341 slot->results = NULL;
342 slot->finished = NULL;
343 slot->callback_data = NULL;
344 slot->callback_func = NULL;
345 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, NULL);
346 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, pragma_header);
347 curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, curl_errorstr);
348 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, NULL);
349 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, NULL);
350 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, NULL);
351 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 0);
352 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
357 int start_active_slot(struct active_request_slot *slot)
359 #ifdef USE_CURL_MULTI
360 CURLMcode curlm_result = curl_multi_add_handle(curlm, slot->curl);
362 if (curlm_result != CURLM_OK &&
363 curlm_result != CURLM_CALL_MULTI_PERFORM) {
372 #ifdef USE_CURL_MULTI
373 void step_active_slots(void)
376 CURLMcode curlm_result;
379 curlm_result = curl_multi_perform(curlm, &num_transfers);
380 } while (curlm_result == CURLM_CALL_MULTI_PERFORM);
381 if (num_transfers < active_requests) {
382 process_curl_messages();
388 void run_active_slot(struct active_request_slot *slot)
390 #ifdef USE_CURL_MULTI
397 struct timeval select_timeout;
400 slot->finished = &finished;
405 if (!data_received && slot->local != NULL) {
406 current_pos = ftell(slot->local);
407 if (current_pos > last_pos)
409 last_pos = current_pos;
412 if (slot->in_use && !data_received) {
417 select_timeout.tv_sec = 0;
418 select_timeout.tv_usec = 50000;
419 select(max_fd, &readfds, &writefds,
420 &excfds, &select_timeout);
424 while (slot->in_use) {
425 slot->curl_result = curl_easy_perform(slot->curl);
426 finish_active_slot(slot);
431 static void closedown_active_slot(struct active_request_slot *slot)
437 void release_active_slot(struct active_request_slot *slot)
439 closedown_active_slot(slot);
441 curl_multi_remove_handle(curlm, slot->curl);
442 curl_easy_cleanup(slot->curl);
448 static void finish_active_slot(struct active_request_slot *slot)
450 closedown_active_slot(slot);
451 curl_easy_getinfo(slot->curl, CURLINFO_HTTP_CODE, &slot->http_code);
453 if (slot->finished != NULL)
454 (*slot->finished) = 1;
456 /* Store slot results so they can be read after the slot is reused */
457 if (slot->results != NULL) {
458 slot->results->curl_result = slot->curl_result;
459 slot->results->http_code = slot->http_code;
462 /* Run callback if appropriate */
463 if (slot->callback_func != NULL) {
464 slot->callback_func(slot->callback_data);
468 void finish_all_active_slots(void)
470 struct active_request_slot *slot = active_queue_head;
474 run_active_slot(slot);
475 slot = active_queue_head;