@matheusbsbs / matheus-bagliano / commits / 29508e1efb
Fork 0

Isolate shared HTTP request functionality

Move shared HTTP request functionality out of http-fetch and http-push, and replace the two fwrite_buffer/fwrite_buffer_dynamic functions with one fwrite_buffer function that does dynamic buffering. Use slot callbacks to process responses to fetch object transfer requests and push transfer requests, and put all of http-push into an #ifdef check for curl multi support. Signed-off-by: Nick Hengeveld <nickh@reactrix.com> Signed-off-by: Junio C Hamano <junkio@cox.net>

Nick Hengeveld committed Nov 18, 2005 at 11:02 UTC 29508e1efb8d5a18f78ca0f9be45fdde49ef6b76
5 files changed +635 -976
Makefile
+2 -1
@@ -387,7 +387,8 @@ $(SIMPLE_PROGRAMS) : git-%$X : %.o
387 $(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) $(filter %.o,$^) \
388 $(LIB_FILE) $(SIMPLE_LIB)
389
390 -git-http-fetch$X: fetch.o
390 +git-http-fetch$X: fetch.o http.o
391 +git-http-push$X: http.o
392 git-local-fetch$X: fetch.o
393 git-ssh-fetch$X: rsh.o fetch.o
394 git-ssh-upload$X: rsh.o
http-fetch.c
+59 -527
@@ -2,44 +2,14 @@
2 #include "commit.h"
3 #include "pack.h"
4 #include "fetch.h"
5 -
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)
16 -#endif
17 -#if LIBCURL_VERSION_NUM < 0x070800
18 -#define curl_global_init(a) do { /* nothing */ } while(0)
19 -#endif
20 -
21 -#if LIBCURL_VERSION_NUM < 0x070c04
22 -#define NO_CURL_EASY_DUPHANDLE
23 -#endif
5 +#include "http.h"
6
7 #define PREV_BUF_SIZE 4096
8 #define RANGE_HEADER_SIZE 30
9
10 static int got_alternates = -1;
29 -static int active_requests = 0;
30 -static int data_received;
11
32 -#ifdef USE_CURL_MULTI
33 -static int max_requests = -1;
34 -static CURLM *curlm;
35 -#endif
36 -#ifndef NO_CURL_EASY_DUPHANDLE
37 -static CURL *curl_default;
38 -#endif
39 -static struct curl_slist *pragma_header;
12 static struct curl_slist *no_pragma_header;
41 -static struct curl_slist *no_range_header;
42 -static char curl_errorstr[CURL_ERROR_SIZE];
13
14 struct alt_base
15 {
@@ -79,19 +49,6 @@ struct transfer_request
49 struct transfer_request *next;
50 };
51
82 -struct active_request_slot
83 -{
84 - CURL *curl;
85 - FILE *local;
86 - int in_use;
87 - int done;
88 - CURLcode curl_result;
89 - long http_code;
90 - void *callback_data;
91 - void (*callback_func)(void *data);
92 - struct active_request_slot *next;
93 -};
94 -
52 struct alt_request {
53 char *base;
54 char *url;
@@ -101,119 +58,6 @@ struct alt_request {
58 };
59
60 static struct transfer_request *request_queue_head = NULL;
104 -static struct active_request_slot *active_queue_head = NULL;
105 -
106 -static int curl_ssl_verify = -1;
107 -static char *ssl_cert = NULL;
108 -#if LIBCURL_VERSION_NUM >= 0x070902
109 -static char *ssl_key = NULL;
110 -#endif
111 -#if LIBCURL_VERSION_NUM >= 0x070908
112 -static char *ssl_capath = NULL;
113 -#endif
114 -static char *ssl_cainfo = NULL;
115 -static long curl_low_speed_limit = -1;
116 -static long curl_low_speed_time = -1;
117 -
118 -struct buffer
119 -{
120 - size_t posn;
121 - size_t size;
122 - void *buffer;
123 -};
124 -
125 -static int http_options(const char *var, const char *value)
126 -{
127 - if (!strcmp("http.sslverify", var)) {
128 - if (curl_ssl_verify == -1) {
129 - curl_ssl_verify = git_config_bool(var, value);
130 - }
131 - return 0;
132 - }
133 -
134 - if (!strcmp("http.sslcert", var)) {
135 - if (ssl_cert == NULL) {
136 - ssl_cert = xmalloc(strlen(value)+1);
137 - strcpy(ssl_cert, value);
138 - }
139 - return 0;
140 - }
141 -#if LIBCURL_VERSION_NUM >= 0x070902
142 - if (!strcmp("http.sslkey", var)) {
143 - if (ssl_key == NULL) {
144 - ssl_key = xmalloc(strlen(value)+1);
145 - strcpy(ssl_key, value);
146 - }
147 - return 0;
148 - }
149 -#endif
150 -#if LIBCURL_VERSION_NUM >= 0x070908
151 - if (!strcmp("http.sslcapath", var)) {
152 - if (ssl_capath == NULL) {
153 - ssl_capath = xmalloc(strlen(value)+1);
154 - strcpy(ssl_capath, value);
155 - }
156 - return 0;
157 - }
158 -#endif
159 - if (!strcmp("http.sslcainfo", var)) {
160 - if (ssl_cainfo == NULL) {
161 - ssl_cainfo = xmalloc(strlen(value)+1);
162 - strcpy(ssl_cainfo, value);
163 - }
164 - return 0;
165 - }
166 -
167 -#ifdef USE_CURL_MULTI
168 - if (!strcmp("http.maxrequests", var)) {
169 - if (max_requests == -1)
170 - max_requests = git_config_int(var, value);
171 - return 0;
172 - }
173 -#endif
174 -
175 - if (!strcmp("http.lowspeedlimit", var)) {
176 - if (curl_low_speed_limit == -1)
177 - curl_low_speed_limit = (long)git_config_int(var, value);
178 - return 0;
179 - }
180 - if (!strcmp("http.lowspeedtime", var)) {
181 - if (curl_low_speed_time == -1)
182 - curl_low_speed_time = (long)git_config_int(var, value);
183 - return 0;
184 - }
185 -
186 - /* Fall back on the default ones */
187 - return git_default_config(var, value);
188 -}
189 -
190 -static size_t fwrite_buffer(void *ptr, size_t eltsize, size_t nmemb,
191 - struct buffer *buffer)
192 -{
193 - size_t size = eltsize * nmemb;
194 - if (size > buffer->size - buffer->posn)
195 - size = buffer->size - buffer->posn;
196 - memcpy(buffer->buffer + buffer->posn, ptr, size);
197 - buffer->posn += size;
198 - data_received++;
199 - return size;
200 -}
201 -
202 -static size_t fwrite_buffer_dynamic(const void *ptr, size_t eltsize,
203 - size_t nmemb, struct buffer *buffer)
204 -{
205 - size_t size = eltsize * nmemb;
206 - if (size > buffer->size - buffer->posn) {
207 - buffer->size = buffer->size * 3 / 2;
208 - if (buffer->size < buffer->posn + size)
209 - buffer->size = buffer->posn + size;
210 - buffer->buffer = xrealloc(buffer->buffer, buffer->size);
211 - }
212 - memcpy(buffer->buffer + buffer->posn, ptr, size);
213 - buffer->posn += size;
214 - data_received++;
215 - return size;
216 -}
61
62 static size_t fwrite_sha1_file(void *ptr, size_t eltsize, size_t nmemb,
63 void *data)
@@ -243,168 +87,9 @@ static size_t fwrite_sha1_file(void *ptr, size_t eltsize, size_t nmemb,
87 return size;
88 }
89
246 -#ifdef USE_CURL_MULTI
247 -static void process_curl_messages(void);
248 -static void process_request_queue(void);
249 -#endif
90 static void fetch_alternates(char *base);
91
252 -static CURL* get_curl_handle(void)
253 -{
254 - CURL* result = curl_easy_init();
255 -
256 - curl_easy_setopt(result, CURLOPT_SSL_VERIFYPEER, curl_ssl_verify);
257 -#if LIBCURL_VERSION_NUM >= 0x070907
258 - curl_easy_setopt(result, CURLOPT_NETRC, CURL_NETRC_OPTIONAL);
259 -#endif
260 -
261 - if (ssl_cert != NULL)
262 - curl_easy_setopt(result, CURLOPT_SSLCERT, ssl_cert);
263 -#if LIBCURL_VERSION_NUM >= 0x070902
264 - if (ssl_key != NULL)
265 - curl_easy_setopt(result, CURLOPT_SSLKEY, ssl_key);
266 -#endif
267 -#if LIBCURL_VERSION_NUM >= 0x070908
268 - if (ssl_capath != NULL)
269 - curl_easy_setopt(result, CURLOPT_CAPATH, ssl_capath);
270 -#endif
271 - if (ssl_cainfo != NULL)
272 - curl_easy_setopt(result, CURLOPT_CAINFO, ssl_cainfo);
273 - curl_easy_setopt(result, CURLOPT_FAILONERROR, 1);
274 -
275 - if (curl_low_speed_limit > 0 && curl_low_speed_time > 0) {
276 - curl_easy_setopt(result, CURLOPT_LOW_SPEED_LIMIT,
277 - curl_low_speed_limit);
278 - curl_easy_setopt(result, CURLOPT_LOW_SPEED_TIME,
279 - curl_low_speed_time);
280 - }
281 -
282 - curl_easy_setopt(result, CURLOPT_FOLLOWLOCATION, 1);
283 -
284 - return result;
285 -}
286 -
287 -static struct active_request_slot *get_active_slot(void)
288 -{
289 - struct active_request_slot *slot = active_queue_head;
290 - struct active_request_slot *newslot;
291 -
292 -#ifdef USE_CURL_MULTI
293 - int num_transfers;
294 -
295 - /* Wait for a slot to open up if the queue is full */
296 - while (active_requests >= max_requests) {
297 - curl_multi_perform(curlm, &num_transfers);
298 - if (num_transfers < active_requests) {
299 - process_curl_messages();
300 - }
301 - }
302 -#endif
303 -
304 - while (slot != NULL && slot->in_use) {
305 - slot = slot->next;
306 - }
307 - if (slot == NULL) {
308 - newslot = xmalloc(sizeof(*newslot));
309 - newslot->curl = NULL;
310 - newslot->in_use = 0;
311 - newslot->next = NULL;
312 -
313 - slot = active_queue_head;
314 - if (slot == NULL) {
315 - active_queue_head = newslot;
316 - } else {
317 - while (slot->next != NULL) {
318 - slot = slot->next;
319 - }
320 - slot->next = newslot;
321 - }
322 - slot = newslot;
323 - }
324 -
325 - if (slot->curl == NULL) {
326 -#ifdef NO_CURL_EASY_DUPHANDLE
327 - slot->curl = get_curl_handle();
328 -#else
329 - slot->curl = curl_easy_duphandle(curl_default);
330 -#endif
331 - }
332 -
333 - active_requests++;
334 - slot->in_use = 1;
335 - slot->done = 0;
336 - slot->local = NULL;
337 - slot->callback_data = NULL;
338 - slot->callback_func = NULL;
339 - curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, pragma_header);
340 - curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, no_range_header);
341 - curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, curl_errorstr);
342 -
343 - return slot;
344 -}
345 -
346 -static int start_active_slot(struct active_request_slot *slot)
347 -{
348 -#ifdef USE_CURL_MULTI
349 - CURLMcode curlm_result = curl_multi_add_handle(curlm, slot->curl);
350 -
351 - if (curlm_result != CURLM_OK &&
352 - curlm_result != CURLM_CALL_MULTI_PERFORM) {
353 - active_requests--;
354 - slot->in_use = 0;
355 - return 0;
356 - }
357 -#endif
358 - return 1;
359 -}
360 -
361 -static void run_active_slot(struct active_request_slot *slot)
362 -{
363 -#ifdef USE_CURL_MULTI
364 - int num_transfers;
365 - long last_pos = 0;
366 - long current_pos;
367 - fd_set readfds;
368 - fd_set writefds;
369 - fd_set excfds;
370 - int max_fd;
371 - struct timeval select_timeout;
372 - CURLMcode curlm_result;
373 -
374 - while (!slot->done) {
375 - data_received = 0;
376 - do {
377 - curlm_result = curl_multi_perform(curlm,
378 - &num_transfers);
379 - } while (curlm_result == CURLM_CALL_MULTI_PERFORM);
380 - if (num_transfers < active_requests) {
381 - process_curl_messages();
382 - process_request_queue();
383 - }
384 -
385 - if (!data_received && slot->local != NULL) {
386 - current_pos = ftell(slot->local);
387 - if (current_pos > last_pos)
388 - data_received++;
389 - last_pos = current_pos;
390 - }
391 -
392 - if (!slot->done && !data_received) {
393 - max_fd = 0;
394 - FD_ZERO(&readfds);
395 - FD_ZERO(&writefds);
396 - FD_ZERO(&excfds);
397 - select_timeout.tv_sec = 0;
398 - select_timeout.tv_usec = 50000;
399 - select(max_fd, &readfds, &writefds,
400 - &excfds, &select_timeout);
401 - }
402 - }
403 -#else
404 - slot->curl_result = curl_easy_perform(slot->curl);
405 - active_requests--;
406 -#endif
407 -}
92 +static void process_object_response(void *callback_data);
93
94 static void start_request(struct transfer_request *request)
95 {
@@ -503,6 +188,10 @@ static void start_request(struct transfer_request *request)
188 }
189
190 slot = get_active_slot();
191 + slot->callback_func = process_object_response;
192 + slot->callback_data = request;
193 + request->slot = slot;
194 +
195 curl_easy_setopt(slot->curl, CURLOPT_FILE, request);
196 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_sha1_file);
197 curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, request->errorstr);
@@ -523,15 +212,13 @@ static void start_request(struct transfer_request *request)
212 }
213
214 /* Try to get the request started, abort the request on error */
215 + request->state = ACTIVE;
216 if (!start_active_slot(slot)) {
217 request->state = ABORTED;
218 + request->slot = NULL;
219 close(request->local); request->local = -1;
220 free(request->url);
530 - return;
221 }
532 -
533 - request->slot = slot;
534 - request->state = ACTIVE;
222 }
223
224 static void finish_request(struct transfer_request *request)
@@ -567,6 +254,32 @@ static void finish_request(struct transfer_request *request)
254 pull_say("got %s\n", sha1_to_hex(request->sha1));
255 }
256
257 +static void process_object_response(void *callback_data)
258 +{
259 + struct transfer_request *request =
260 + (struct transfer_request *)callback_data;
261 +
262 + request->curl_result = request->slot->curl_result;
263 + request->http_code = request->slot->http_code;
264 + request->slot = NULL;
265 + request->state = COMPLETE;
266 +
267 + /* Use alternates if necessary */
268 + if (request->http_code == 404) {
269 + fetch_alternates(alt->base);
270 + if (request->repo->next != NULL) {
271 + request->repo =
272 + request->repo->next;
273 + close(request->local);
274 + request->local = -1;
275 + start_request(request);
276 + return;
277 + }
278 + }
279 +
280 + finish_request(request);
281 +}
282 +
283 static void release_request(struct transfer_request *request)
284 {
285 struct transfer_request *entry = request_queue_head;
@@ -587,73 +300,7 @@ static void release_request(struct transfer_request *request)
300 }
301
302 #ifdef USE_CURL_MULTI
590 -static void process_curl_messages(void)
591 -{
592 - int num_messages;
593 - struct active_request_slot *slot;
594 - struct transfer_request *request = NULL;
595 - CURLMsg *curl_message = curl_multi_info_read(curlm, &num_messages);
596 -
597 - while (curl_message != NULL) {
598 - if (curl_message->msg == CURLMSG_DONE) {
599 - int curl_result = curl_message->data.result;
600 - slot = active_queue_head;
601 - while (slot != NULL &&
602 - slot->curl != curl_message->easy_handle)
603 - slot = slot->next;
604 - if (slot != NULL) {
605 - curl_multi_remove_handle(curlm, slot->curl);
606 - active_requests--;
607 - slot->done = 1;
608 - slot->in_use = 0;
609 - slot->curl_result = curl_result;
610 - curl_easy_getinfo(slot->curl,
611 - CURLINFO_HTTP_CODE,
612 - &slot->http_code);
613 - request = request_queue_head;
614 - while (request != NULL &&
615 - request->slot != slot)
616 - request = request->next;
617 - } else {
618 - fprintf(stderr, "Received DONE message for unknown request!\n");
619 - }
620 -
621 - /* Process slot callback if appropriate */
622 - if (slot->callback_func != NULL) {
623 - slot->callback_func(slot->callback_data);
624 - }
625 -
626 - if (request != NULL) {
627 - request->curl_result = curl_result;
628 - request->http_code = slot->http_code;
629 - request->slot = NULL;
630 - request->state = COMPLETE;
631 -
632 - /* Use alternates if necessary */
633 - if (request->http_code == 404) {
634 - fetch_alternates(alt->base);
635 - if (request->repo->next != NULL) {
636 - request->repo =
637 - request->repo->next;
638 - close(request->local);
639 - request->local = -1;
640 - start_request(request);
641 - } else {
642 - finish_request(request);
643 - }
644 - } else {
645 - finish_request(request);
646 - }
647 - }
648 - } else {
649 - fprintf(stderr, "Unknown CURL message received: %d\n",
650 - (int)curl_message->msg);
651 - }
652 - curl_message = curl_multi_info_read(curlm, &num_messages);
653 - }
654 -}
655 -
656 -static void process_request_queue(void)
303 +void fill_active_slots(void)
304 {
305 struct transfer_request *request = request_queue_head;
306 struct active_request_slot *slot = active_queue_head;
@@ -706,9 +353,10 @@ void prefetch(unsigned char *sha1)
353 }
354 tail->next = newreq;
355 }
356 +
357 #ifdef USE_CURL_MULTI
710 - process_request_queue();
711 - process_curl_messages();
358 + fill_active_slots();
359 + step_active_slots();
360 #endif
361 }
362
@@ -815,12 +463,11 @@ static void process_alternates(void *callback_data)
463 alt_req->url);
464 active_requests++;
465 slot->in_use = 1;
818 - slot->done = 0;
466 if (start_active_slot(slot)) {
467 return;
468 } else {
469 got_alternates = -1;
823 - slot->done = 1;
470 + slot->in_use = 0;
471 return;
472 }
473 }
@@ -831,7 +478,7 @@ static void process_alternates(void *callback_data)
478 }
479 }
480
834 - fwrite_buffer_dynamic(&null_byte, 1, 1, alt_req->buffer);
481 + fwrite_buffer(&null_byte, 1, 1, alt_req->buffer);
482 alt_req->buffer->posn--;
483 data = alt_req->buffer->buffer;
484
@@ -902,16 +549,15 @@ static void fetch_alternates(char *base)
549 char *data;
550 struct active_request_slot *slot;
551 static struct alt_request alt_req;
905 - int num_transfers;
552
553 /* If another request has already started fetching alternates,
554 wait for them to arrive and return to processing this request's
555 curl message */
556 +#ifdef USE_CURL_MULTI
557 while (got_alternates == 0) {
911 - curl_multi_perform(curlm, &num_transfers);
912 - process_curl_messages();
913 - process_request_queue();
558 + step_active_slots();
559 }
560 +#endif
561
562 /* Nothing to do if they've already been fetched */
563 if (got_alternates == 1)
@@ -938,8 +584,7 @@ static void fetch_alternates(char *base)
584 slot->callback_data = &alt_req;
585
586 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
941 - curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION,
942 - fwrite_buffer_dynamic);
587 + curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
588 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
589
590 alt_req.base = base;
@@ -983,8 +628,7 @@ static int fetch_indices(struct alt_base *repo)
628
629 slot = get_active_slot();
630 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
986 - curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION,
987 - fwrite_buffer_dynamic);
631 + curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
632 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
633 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, NULL);
634 if (start_active_slot(slot)) {
@@ -1115,7 +759,7 @@ static int fetch_pack(struct alt_base *repo, unsigned char *sha1)
759 static int fetch_object(struct alt_base *repo, unsigned char *sha1)
760 {
761 char *hex = sha1_to_hex(sha1);
1118 - int ret;
762 + int ret = 0;
763 struct transfer_request *request = request_queue_head;
764
765 while (request != NULL && memcmp(request->sha1, sha1, 20))
@@ -1130,12 +774,7 @@ static int fetch_object(struct alt_base *repo, unsigned char *sha1)
774
775 #ifdef USE_CURL_MULTI
776 while (request->state == WAITING) {
1133 - int num_transfers;
1134 - curl_multi_perform(curlm, &num_transfers);
1135 - if (num_transfers < active_requests) {
1136 - process_curl_messages();
1137 - process_request_queue();
1138 - }
777 + step_active_slots();
778 }
779 #else
780 start_request(request);
@@ -1143,66 +782,33 @@ static int fetch_object(struct alt_base *repo, unsigned char *sha1)
782
783 while (request->state == ACTIVE) {
784 run_active_slot(request->slot);
1146 -#ifndef USE_CURL_MULTI
1147 - request->curl_result = request->slot->curl_result;
1148 - request->http_code = request->slot->http_code;
1149 - request->slot = NULL;
1150 -
1151 - /* Use alternates if necessary */
1152 - if (request->http_code == 404) {
1153 - fetch_alternates(alt->base);
1154 - if (request->repo->next != NULL) {
1155 - request->repo = request->repo->next;
1156 - close(request->local); request->local = -1;
1157 - start_request(request);
1158 - }
1159 - } else {
1160 - finish_request(request);
1161 - request->state = COMPLETE;
1162 - }
1163 -#endif
785 }
786 if (request->local != -1) {
787 close(request->local); request->local = -1;
788 }
789
790 if (request->state == ABORTED) {
1170 - release_request(request);
1171 - return error("Request for %s aborted", hex);
1172 - }
1173 -
1174 - if (request->curl_result != CURLE_OK && request->http_code != 416) {
791 + ret = error("Request for %s aborted", hex);
792 + } else if (request->curl_result != CURLE_OK &&
793 + request->http_code != 416) {
794 if (request->http_code == 404)
795 ret = -1; /* Be silent, it is probably in a pack. */
796 else
797 ret = error("%s (curl_result = %d, http_code = %ld, sha1 = %s)",
798 request->errorstr, request->curl_result,
799 request->http_code, hex);
1181 - release_request(request);
1182 - return ret;
1183 - }
1184 -
1185 - if (request->zret != Z_STREAM_END) {
800 + } else if (request->zret != Z_STREAM_END) {
801 ret = error("File %s (%s) corrupt\n", hex, request->url);
1187 - release_request(request);
1188 - return ret;
1189 - }
1190 -
1191 - if (memcmp(request->sha1, request->real_sha1, 20)) {
1192 - release_request(request);
1193 - return error("File %s has bad hash\n", hex);
1194 - }
1195 -
1196 - if (request->rename < 0) {
802 + } else if (memcmp(request->sha1, request->real_sha1, 20)) {
803 + ret = error("File %s has bad hash\n", hex);
804 + } else if (request->rename < 0) {
805 ret = error("unable to write sha1 filename %s: %s",
806 request->filename,
807 strerror(request->rename));
1200 - release_request(request);
1201 - return ret;
808 }
809
810 release_request(request);
1205 - return 0;
811 + return ret;
812 }
813
814 int fetch(unsigned char *sha1)
@@ -1303,10 +909,6 @@ int main(int argc, char **argv)
909 char *commit_id;
910 char *url;
911 int arg = 1;
1306 - struct active_request_slot *slot;
1307 - char *low_speed_limit;
1308 - char *low_speed_time;
1309 - char *wait_url;
912 int rc = 0;
913
914 while (arg < argc && argv[arg][0] == '-') {
@@ -1335,58 +937,9 @@ int main(int argc, char **argv)
937 commit_id = argv[arg];
938 url = argv[arg + 1];
939
1338 - curl_global_init(CURL_GLOBAL_ALL);
1339 -
1340 -#ifdef USE_CURL_MULTI
1341 - {
1342 - char *http_max_requests = getenv("GIT_HTTP_MAX_REQUESTS");
1343 - if (http_max_requests != NULL)
1344 - max_requests = atoi(http_max_requests);
1345 - }
1346 -
1347 - curlm = curl_multi_init();
1348 - if (curlm == NULL) {
1349 - fprintf(stderr, "Error creating curl multi handle.\n");
1350 - return 1;
1351 - }
1352 -#endif
1353 -
1354 - if (getenv("GIT_SSL_NO_VERIFY"))
1355 - curl_ssl_verify = 0;
1356 -
1357 - ssl_cert = getenv("GIT_SSL_CERT");
1358 -#if LIBCURL_VERSION_NUM >= 0x070902
1359 - ssl_key = getenv("GIT_SSL_KEY");
1360 -#endif
1361 -#if LIBCURL_VERSION_NUM >= 0x070908
1362 - ssl_capath = getenv("GIT_SSL_CAPATH");
1363 -#endif
1364 - ssl_cainfo = getenv("GIT_SSL_CAINFO");
1365 -
1366 - low_speed_limit = getenv("GIT_HTTP_LOW_SPEED_LIMIT");
1367 - if (low_speed_limit != NULL)
1368 - curl_low_speed_limit = strtol(low_speed_limit, NULL, 10);
1369 - low_speed_time = getenv("GIT_HTTP_LOW_SPEED_TIME");
1370 - if (low_speed_time != NULL)
1371 - curl_low_speed_time = strtol(low_speed_time, NULL, 10);
1372 -
1373 - git_config(http_options);
1374 -
1375 - if (curl_ssl_verify == -1)
1376 - curl_ssl_verify = 1;
1377 -
1378 -#ifdef USE_CURL_MULTI
1379 - if (max_requests < 1)
1380 - max_requests = DEFAULT_MAX_REQUESTS;
1381 -#endif
940 + http_init();
941
1383 - pragma_header = curl_slist_append(pragma_header, "Pragma: no-cache");
942 no_pragma_header = curl_slist_append(no_pragma_header, "Pragma:");
1385 - no_range_header = curl_slist_append(no_range_header, "Range:");
1386 -
1387 -#ifndef NO_CURL_EASY_DUPHANDLE
1388 - curl_default = get_curl_handle();
1389 -#endif
943
944 alt = xmalloc(sizeof(*alt));
945 alt->base = url;
@@ -1397,30 +950,9 @@ int main(int argc, char **argv)
950 if (pull(commit_id))
951 rc = 1;
952
1400 - curl_slist_free_all(pragma_header);
953 curl_slist_free_all(no_pragma_header);
1402 - curl_slist_free_all(no_range_header);
1403 -#ifndef NO_CURL_EASY_DUPHANDLE
1404 - curl_easy_cleanup(curl_default);
1405 -#endif
1406 - slot = active_queue_head;
1407 - while (slot != NULL) {
1408 - if (slot->in_use) {
1409 - if (get_verbosely) {
1410 - curl_easy_getinfo(slot->curl,
1411 - CURLINFO_EFFECTIVE_URL,
1412 - &wait_url);
1413 - fprintf(stderr, "Waiting for %s\n", wait_url);
1414 - }
1415 - run_active_slot(slot);
1416 - }
1417 - if (slot->curl != NULL)
1418 - curl_easy_cleanup(slot->curl);
1419 - slot = slot->next;
1420 - }
1421 -#ifdef USE_CURL_MULTI
1422 - curl_multi_cleanup(curlm);
1423 -#endif
1424 - curl_global_cleanup();
954 +
955 + http_cleanup();
956 +
957 return rc;
958 }
http-push.c
+36 -448
@@ -4,30 +4,15 @@
4 #include "fetch.h"
5 #include "tag.h"
6 #include "blob.h"
7 +#include "http.h"
8 +
9 +#ifdef USE_CURL_MULTI
10
8 -#include <curl/curl.h>
9 -#include <curl/easy.h>
11 #include <expat.h>
12
13 static const char http_push_usage[] =
14 "git-http-push [--complete] [--force] [--verbose] <url> <ref> [<ref>...]\n";
15
15 -#if LIBCURL_VERSION_NUM >= 0x070908
16 -#define USE_CURL_MULTI
17 -#define DEFAULT_MAX_REQUESTS 5
18 -#endif
19 -
20 -#if LIBCURL_VERSION_NUM < 0x070704
21 -#define curl_global_cleanup() do { /* nothing */ } while(0)
22 -#endif
23 -#if LIBCURL_VERSION_NUM < 0x070800
24 -#define curl_global_init(a) do { /* nothing */ } while(0)
25 -#endif
26 -
27 -#if LIBCURL_VERSION_NUM < 0x070c04
28 -#define NO_CURL_EASY_DUPHANDLE
29 -#endif
30 -
16 #ifndef XML_STATUS_OK
17 enum XML_Status {
18 XML_STATUS_OK = 1,
@@ -52,34 +37,17 @@ enum XML_Status {
37 #define LOCK_TIME 600
38 #define LOCK_REFRESH 30
39
55 -static int active_requests = 0;
56 -static int data_received;
40 static int pushing = 0;
41 static int aborted = 0;
42 static char remote_dir_exists[256];
43
61 -#ifdef USE_CURL_MULTI
62 -static int max_requests = -1;
63 -static CURLM *curlm;
64 -#endif
65 -#ifndef NO_CURL_EASY_DUPHANDLE
66 -static CURL *curl_default;
67 -#endif
44 static struct curl_slist *no_pragma_header;
45 static struct curl_slist *default_headers;
70 -static char curl_errorstr[CURL_ERROR_SIZE];
46
47 static int push_verbosely = 0;
48 static int push_all = 0;
49 static int force_all = 0;
50
76 -struct buffer
77 -{
78 - size_t posn;
79 - size_t size;
80 - void *buffer;
81 -};
82 -
51 struct repo
52 {
53 char *url;
@@ -122,31 +90,7 @@ struct transfer_request
90 struct transfer_request *next;
91 };
92
125 -struct active_request_slot
126 -{
127 - CURL *curl;
128 - FILE *local;
129 - int in_use;
130 - int done;
131 - CURLcode curl_result;
132 - long http_code;
133 - struct active_request_slot *next;
134 -};
135 -
93 static struct transfer_request *request_queue_head = NULL;
137 -static struct active_request_slot *active_queue_head = NULL;
138 -
139 -static int curl_ssl_verify = -1;
140 -static char *ssl_cert = NULL;
141 -#if LIBCURL_VERSION_NUM >= 0x070902
142 -static char *ssl_key = NULL;
143 -#endif
144 -#if LIBCURL_VERSION_NUM >= 0x070908
145 -static char *ssl_capath = NULL;
146 -#endif
147 -static char *ssl_cainfo = NULL;
148 -static long curl_low_speed_limit = -1;
149 -static long curl_low_speed_time = -1;
94
95 struct active_lock
96 {
@@ -174,260 +118,14 @@ struct lockprop
118 int lock_exclusive_write;
119 };
120
177 -static int http_options(const char *var, const char *value)
178 -{
179 - if (!strcmp("http.sslverify", var)) {
180 - if (curl_ssl_verify == -1) {
181 - curl_ssl_verify = git_config_bool(var, value);
182 - }
183 - return 0;
184 - }
121 +static void finish_request(struct transfer_request *request);
122
186 - if (!strcmp("http.sslcert", var)) {
187 - if (ssl_cert == NULL) {
188 - ssl_cert = xmalloc(strlen(value)+1);
189 - strcpy(ssl_cert, value);
190 - }
191 - return 0;
192 - }
193 -#if LIBCURL_VERSION_NUM >= 0x070902
194 - if (!strcmp("http.sslkey", var)) {
195 - if (ssl_key == NULL) {
196 - ssl_key = xmalloc(strlen(value)+1);
197 - strcpy(ssl_key, value);
198 - }
199 - return 0;
200 - }
201 -#endif
202 -#if LIBCURL_VERSION_NUM >= 0x070908
203 - if (!strcmp("http.sslcapath", var)) {
204 - if (ssl_capath == NULL) {
205 - ssl_capath = xmalloc(strlen(value)+1);
206 - strcpy(ssl_capath, value);
207 - }
208 - return 0;
209 - }
210 -#endif
211 - if (!strcmp("http.sslcainfo", var)) {
212 - if (ssl_cainfo == NULL) {
213 - ssl_cainfo = xmalloc(strlen(value)+1);
214 - strcpy(ssl_cainfo, value);
215 - }
216 - return 0;
217 - }
218 -
219 -#ifdef USE_CURL_MULTI
220 - if (!strcmp("http.maxrequests", var)) {
221 - if (max_requests == -1)
222 - max_requests = git_config_int(var, value);
223 - return 0;
224 - }
225 -#endif
226 -
227 - if (!strcmp("http.lowspeedlimit", var)) {
228 - if (curl_low_speed_limit == -1)
229 - curl_low_speed_limit = (long)git_config_int(var, value);
230 - return 0;
231 - }
232 - if (!strcmp("http.lowspeedtime", var)) {
233 - if (curl_low_speed_time == -1)
234 - curl_low_speed_time = (long)git_config_int(var, value);
235 - return 0;
236 - }
237 -
238 - /* Fall back on the default ones */
239 - return git_default_config(var, value);
240 -}
241 -
242 -static size_t fread_buffer(void *ptr, size_t eltsize, size_t nmemb,
243 - struct buffer *buffer)
244 -{
245 - size_t size = eltsize * nmemb;
246 - if (size > buffer->size - buffer->posn)
247 - size = buffer->size - buffer->posn;
248 - memcpy(ptr, buffer->buffer + buffer->posn, size);
249 - buffer->posn += size;
250 - return size;
251 -}
252 -
253 -static size_t fwrite_buffer_dynamic(const void *ptr, size_t eltsize,
254 - size_t nmemb, struct buffer *buffer)
255 -{
256 - size_t size = eltsize * nmemb;
257 - if (size > buffer->size - buffer->posn) {
258 - buffer->size = buffer->size * 3 / 2;
259 - if (buffer->size < buffer->posn + size)
260 - buffer->size = buffer->posn + size;
261 - buffer->buffer = xrealloc(buffer->buffer, buffer->size);
262 - }
263 - memcpy(buffer->buffer + buffer->posn, ptr, size);
264 - buffer->posn += size;
265 - data_received++;
266 - return size;
267 -}
268 -
269 -static size_t fwrite_null(const void *ptr, size_t eltsize,
270 - size_t nmemb, struct buffer *buffer)
271 -{
272 - data_received++;
273 - return eltsize * nmemb;
274 -}
275 -
276 -#ifdef USE_CURL_MULTI
277 -static void process_curl_messages(void);
278 -static void process_request_queue(void);
279 -#endif
280 -
281 -static CURL* get_curl_handle(void)
123 +static void process_response(void *callback_data)
124 {
283 - CURL* result = curl_easy_init();
284 -
285 - curl_easy_setopt(result, CURLOPT_SSL_VERIFYPEER, curl_ssl_verify);
286 -#if LIBCURL_VERSION_NUM >= 0x070907
287 - curl_easy_setopt(result, CURLOPT_NETRC, CURL_NETRC_OPTIONAL);
288 -#endif
125 + struct transfer_request *request =
126 + (struct transfer_request *)callback_data;
127
290 - if (ssl_cert != NULL)
291 - curl_easy_setopt(result, CURLOPT_SSLCERT, ssl_cert);
292 -#if LIBCURL_VERSION_NUM >= 0x070902
293 - if (ssl_key != NULL)
294 - curl_easy_setopt(result, CURLOPT_SSLKEY, ssl_key);
295 -#endif
296 -#if LIBCURL_VERSION_NUM >= 0x070908
297 - if (ssl_capath != NULL)
298 - curl_easy_setopt(result, CURLOPT_CAPATH, ssl_capath);
299 -#endif
300 - if (ssl_cainfo != NULL)
301 - curl_easy_setopt(result, CURLOPT_CAINFO, ssl_cainfo);
302 - curl_easy_setopt(result, CURLOPT_FAILONERROR, 1);
303 -
304 - if (curl_low_speed_limit > 0 && curl_low_speed_time > 0) {
305 - curl_easy_setopt(result, CURLOPT_LOW_SPEED_LIMIT,
306 - curl_low_speed_limit);
307 - curl_easy_setopt(result, CURLOPT_LOW_SPEED_TIME,
308 - curl_low_speed_time);
309 - }
310 -
311 - return result;
312 -}
313 -
314 -static struct active_request_slot *get_active_slot(void)
315 -{
316 - struct active_request_slot *slot = active_queue_head;
317 - struct active_request_slot *newslot;
318 -
319 -#ifdef USE_CURL_MULTI
320 - int num_transfers;
321 -
322 - /* Wait for a slot to open up if the queue is full */
323 - while (active_requests >= max_requests) {
324 - curl_multi_perform(curlm, &num_transfers);
325 - if (num_transfers < active_requests) {
326 - process_curl_messages();
327 - }
328 - }
329 -#endif
330 -
331 - while (slot != NULL && slot->in_use) {
332 - slot = slot->next;
333 - }
334 - if (slot == NULL) {
335 - newslot = xmalloc(sizeof(*newslot));
336 - newslot->curl = NULL;
337 - newslot->in_use = 0;
338 - newslot->next = NULL;
339 -
340 - slot = active_queue_head;
341 - if (slot == NULL) {
342 - active_queue_head = newslot;
343 - } else {
344 - while (slot->next != NULL) {
345 - slot = slot->next;
346 - }
347 - slot->next = newslot;
348 - }
349 - slot = newslot;
350 - }
351 -
352 - if (slot->curl == NULL) {
353 -#ifdef NO_CURL_EASY_DUPHANDLE
354 - slot->curl = get_curl_handle();
355 -#else
356 - slot->curl = curl_easy_duphandle(curl_default);
357 -#endif
358 - }
359 -
360 - active_requests++;
361 - slot->in_use = 1;
362 - slot->done = 0;
363 - slot->local = NULL;
364 - curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, default_headers);
365 - curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, curl_errorstr);
366 -
367 - return slot;
368 -}
369 -
370 -static int start_active_slot(struct active_request_slot *slot)
371 -{
372 -#ifdef USE_CURL_MULTI
373 - CURLMcode curlm_result = curl_multi_add_handle(curlm, slot->curl);
374 -
375 - if (curlm_result != CURLM_OK &&
376 - curlm_result != CURLM_CALL_MULTI_PERFORM) {
377 - active_requests--;
378 - slot->in_use = 0;
379 - return 0;
380 - }
381 -#endif
382 - return 1;
383 -}
384 -
385 -static void run_active_slot(struct active_request_slot *slot)
386 -{
387 -#ifdef USE_CURL_MULTI
388 - int num_transfers;
389 - long last_pos = 0;
390 - long current_pos;
391 - fd_set readfds;
392 - fd_set writefds;
393 - fd_set excfds;
394 - int max_fd;
395 - struct timeval select_timeout;
396 - CURLMcode curlm_result;
397 -
398 - while (!slot->done) {
399 - data_received = 0;
400 - do {
401 - curlm_result = curl_multi_perform(curlm,
402 - &num_transfers);
403 - } while (curlm_result == CURLM_CALL_MULTI_PERFORM);
404 - if (num_transfers < active_requests) {
405 - process_curl_messages();
406 - process_request_queue();
407 - }
408 -
409 - if (!data_received && slot->local != NULL) {
410 - current_pos = ftell(slot->local);
411 - if (current_pos > last_pos)
412 - data_received++;
413 - last_pos = current_pos;
414 - }
415 -
416 - if (!slot->done && !data_received) {
417 - max_fd = 0;
418 - FD_ZERO(&readfds);
419 - FD_ZERO(&writefds);
420 - FD_ZERO(&excfds);
421 - select_timeout.tv_sec = 0;
422 - select_timeout.tv_usec = 50000;
423 - select(max_fd, &readfds, &writefds,
424 - &excfds, &select_timeout);
425 - }
426 - }
427 -#else
428 - slot->curl_result = curl_easy_perform(slot->curl);
429 - active_requests--;
430 -#endif
128 + finish_request(request);
129 }
130
131 static void start_check(struct transfer_request *request)
@@ -447,6 +145,8 @@ static void start_check(struct transfer_request *request)
145 strcpy(posn, hex + 2);
146
147 slot = get_active_slot();
148 + slot->callback_func = process_response;
149 + slot->callback_data = request;
150 curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, request->errorstr);
151 curl_easy_setopt(slot->curl, CURLOPT_URL, request->url);
152 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 1);
@@ -476,6 +176,8 @@ static void start_mkcol(struct transfer_request *request)
176 strcpy(posn, "/");
177
178 slot = get_active_slot();
179 + slot->callback_func = process_response;
180 + slot->callback_data = request;
181 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1); /* undo PUT setup */
182 curl_easy_setopt(slot->curl, CURLOPT_URL, request->url);
183 curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, request->errorstr);
@@ -553,6 +255,8 @@ static void start_put(struct transfer_request *request)
255 strcpy(posn, request->lock->token);
256
257 slot = get_active_slot();
258 + slot->callback_func = process_response;
259 + slot->callback_data = request;
260 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &request->buffer);
261 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, request->buffer.size);
262 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
@@ -578,6 +282,8 @@ static void start_move(struct transfer_request *request)
282 struct curl_slist *dav_headers = NULL;
283
284 slot = get_active_slot();
285 + slot->callback_func = process_response;
286 + slot->callback_data = request;
287 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1); /* undo PUT setup */
288 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_MOVE);
289 dav_headers = curl_slist_append(dav_headers, request->dest);
@@ -725,48 +431,7 @@ static void release_request(struct transfer_request *request)
431 free(request);
432 }
433
728 -#ifdef USE_CURL_MULTI
729 -static void process_curl_messages(void)
730 -{
731 - int num_messages;
732 - struct active_request_slot *slot;
733 - struct transfer_request *request = NULL;
734 - CURLMsg *curl_message = curl_multi_info_read(curlm, &num_messages);
735 -
736 - while (curl_message != NULL) {
737 - if (curl_message->msg == CURLMSG_DONE) {
738 - slot = active_queue_head;
739 - while (slot != NULL &&
740 - slot->curl != curl_message->easy_handle)
741 - slot = slot->next;
742 - if (slot != NULL) {
743 - int curl_result = curl_message->data.result;
744 - curl_multi_remove_handle(curlm, slot->curl);
745 - active_requests--;
746 - slot->done = 1;
747 - slot->in_use = 0;
748 - slot->curl_result = curl_result;
749 - curl_easy_getinfo(slot->curl,
750 - CURLINFO_HTTP_CODE,
751 - &slot->http_code);
752 - request = request_queue_head;
753 - while (request != NULL &&
754 - request->slot != slot)
755 - request = request->next;
756 - if (request != NULL)
757 - finish_request(request);
758 - } else {
759 - fprintf(stderr, "Received DONE message for unknown request!\n");
760 - }
761 - } else {
762 - fprintf(stderr, "Unknown CURL message received: %d\n",
763 - (int)curl_message->msg);
764 - }
765 - curl_message = curl_multi_info_read(curlm, &num_messages);
766 - }
767 -}
768 -
769 -static void process_request_queue(void)
434 +void fill_active_slots(void)
435 {
436 struct transfer_request *request = request_queue_head;
437 struct active_request_slot *slot = active_queue_head;
@@ -797,20 +462,6 @@ static void process_request_queue(void)
462 slot = slot->next;
463 }
464 }
800 -#endif
801 -
802 -static void process_waiting_requests(void)
803 -{
804 - struct active_request_slot *slot = active_queue_head;
805 -
806 - while (slot != NULL)
807 - if (slot->in_use) {
808 - run_active_slot(slot);
809 - slot = active_queue_head;
810 - } else {
811 - slot = slot->next;
812 - }
813 -}
465
466 static void add_request(unsigned char *sha1, struct active_lock *lock)
467 {
@@ -834,10 +485,9 @@ static void add_request(unsigned char *sha1, struct active_lock *lock)
485 request->state = NEED_CHECK;
486 request->next = request_queue_head;
487 request_queue_head = request;
837 -#ifdef USE_CURL_MULTI
838 - process_request_queue();
839 - process_curl_messages();
840 -#endif
488 +
489 + fill_active_slots();
490 + step_active_slots();
491 }
492
493 static int fetch_index(unsigned char *sha1)
@@ -963,8 +613,7 @@ static int fetch_indices(void)
613
614 slot = get_active_slot();
615 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
966 - curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION,
967 - fwrite_buffer_dynamic);
616 + curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
617 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
618 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, NULL);
619 if (start_active_slot(slot)) {
@@ -1068,8 +717,7 @@ int fetch_ref(char *ref, unsigned char *sha1)
717 url = quote_ref_url(base, ref);
718 slot = get_active_slot();
719 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
1071 - curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION,
1072 - fwrite_buffer_dynamic);
720 + curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
721 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, NULL);
722 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
723 if (start_active_slot(slot)) {
@@ -1261,8 +909,7 @@ static struct active_lock *lock_remote(char *file, long timeout)
909 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.size);
910 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
911 curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
1264 - curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION,
1265 - fwrite_buffer_dynamic);
912 + curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
913 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
914 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
915 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_LOCK);
@@ -1390,8 +1037,7 @@ static int check_locking(void)
1037 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.size);
1038 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
1039 curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
1393 - curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION,
1394 - fwrite_buffer_dynamic);
1040 + curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
1041 curl_easy_setopt(slot->curl, CURLOPT_URL, remote->url);
1042 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
1043 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PROPFIND);
@@ -1560,8 +1206,6 @@ static int update_remote(unsigned char *sha1, struct active_lock *lock)
1206
1207 int main(int argc, char **argv)
1208 {
1563 - struct active_request_slot *slot;
1564 - struct active_request_slot *next_slot;
1209 struct transfer_request *request;
1210 struct transfer_request *next_request;
1211 int nr_refspec = 0;
@@ -1576,8 +1220,6 @@ int main(int argc, char **argv)
1220 unsigned char remote_sha1[20];
1221 struct active_lock *remote_lock;
1222 char *remote_path = NULL;
1579 - char *low_speed_limit;
1580 - char *low_speed_time;
1223 int rc = 0;
1224 int i;
1225
@@ -1617,50 +1259,7 @@ int main(int argc, char **argv)
1259
1260 memset(remote_dir_exists, 0, 256);
1261
1620 - curl_global_init(CURL_GLOBAL_ALL);
1621 -
1622 -#ifdef USE_CURL_MULTI
1623 - {
1624 - char *http_max_requests = getenv("GIT_HTTP_MAX_REQUESTS");
1625 - if (http_max_requests != NULL)
1626 - max_requests = atoi(http_max_requests);
1627 - }
1628 -
1629 - curlm = curl_multi_init();
1630 - if (curlm == NULL) {
1631 - fprintf(stderr, "Error creating curl multi handle.\n");
1632 - return 1;
1633 - }
1634 -#endif
1635 -
1636 - if (getenv("GIT_SSL_NO_VERIFY"))
1637 - curl_ssl_verify = 0;
1638 -
1639 - ssl_cert = getenv("GIT_SSL_CERT");
1640 -#if LIBCURL_VERSION_NUM >= 0x070902
1641 - ssl_key = getenv("GIT_SSL_KEY");
1642 -#endif
1643 -#if LIBCURL_VERSION_NUM >= 0x070908
1644 - ssl_capath = getenv("GIT_SSL_CAPATH");
1645 -#endif
1646 - ssl_cainfo = getenv("GIT_SSL_CAINFO");
1647 -
1648 - low_speed_limit = getenv("GIT_HTTP_LOW_SPEED_LIMIT");
1649 - if (low_speed_limit != NULL)
1650 - curl_low_speed_limit = strtol(low_speed_limit, NULL, 10);
1651 - low_speed_time = getenv("GIT_HTTP_LOW_SPEED_TIME");
1652 - if (low_speed_time != NULL)
1653 - curl_low_speed_time = strtol(low_speed_time, NULL, 10);
1654 -
1655 - git_config(http_options);
1656 -
1657 - if (curl_ssl_verify == -1)
1658 - curl_ssl_verify = 1;
1659 -
1660 -#ifdef USE_CURL_MULTI
1661 - if (max_requests < 1)
1662 - max_requests = DEFAULT_MAX_REQUESTS;
1663 -#endif
1262 + http_init();
1263
1264 no_pragma_header = curl_slist_append(no_pragma_header, "Pragma:");
1265 default_headers = curl_slist_append(default_headers, "Range:");
@@ -1669,10 +1268,6 @@ int main(int argc, char **argv)
1268 default_headers = curl_slist_append(default_headers,
1269 "Pragma: no-cache");
1270
1672 -#ifndef NO_CURL_EASY_DUPHANDLE
1673 - curl_default = get_curl_handle();
1674 -#endif
1675 -
1271 /* Verify DAV compliance/lock support */
1272 if (check_locking() != 0) {
1273 fprintf(stderr, "Error: no DAV locking support on remote repo %s\n", remote->url);
@@ -1766,13 +1361,13 @@ int main(int argc, char **argv)
1361 fetch_indices();
1362 get_delta(push_all ? NULL : remote_sha1,
1363 local_object, remote_lock);
1769 - process_waiting_requests();
1364 + finish_all_active_slots();
1365
1366 /* Push missing objects to remote, this would be a
1367 convenient time to pack them first if appropriate. */
1368 pushing = 1;
1774 - process_request_queue();
1775 - process_waiting_requests();
1369 + fill_active_slots();
1370 + finish_all_active_slots();
1371
1372 /* Update the remote branch if all went well */
1373 if (do_remote_update) {
@@ -1802,14 +1397,7 @@ int main(int argc, char **argv)
1397 curl_slist_free_all(no_pragma_header);
1398 curl_slist_free_all(default_headers);
1399
1805 - slot = active_queue_head;
1806 - while (slot != NULL) {
1807 - next_slot = slot->next;
1808 - if (slot->curl != NULL)
1809 - curl_easy_cleanup(slot->curl);
1810 - free(slot);
1811 - slot = next_slot;
1812 - }
1400 + http_cleanup();
1401
1402 request = request_queue_head;
1403 while (request != NULL) {
@@ -1818,12 +1406,12 @@ int main(int argc, char **argv)
1406 request = next_request;
1407 }
1408
1821 -#ifndef NO_CURL_EASY_DUPHANDLE
1822 - curl_easy_cleanup(curl_default);
1823 -#endif
1824 -#ifdef USE_CURL_MULTI
1825 - curl_multi_cleanup(curlm);
1826 -#endif
1827 - curl_global_cleanup();
1409 return rc;
1410 }
1411 +#else /* ifdef USE_CURL_MULTI */
1412 +int main(int argc, char **argv)
1413 +{
1414 + fprintf(stderr, "http-push requires curl 7.9.8 or higher.\n");
1415 + return 1;
1416 +}
1417 +#endif
http.c new
+443
@@ -0,0 +1,443 @@
1 +#include "http.h"
2 +
3 +int data_received;
4 +int active_requests = 0;
5 +
6 +#ifdef USE_CURL_MULTI
7 +int max_requests = -1;
8 +CURLM *curlm;
9 +#endif
10 +#ifndef NO_CURL_EASY_DUPHANDLE
11 +CURL *curl_default;
12 +#endif
13 +char curl_errorstr[CURL_ERROR_SIZE];
14 +
15 +int curl_ssl_verify = -1;
16 +char *ssl_cert = NULL;
17 +#if LIBCURL_VERSION_NUM >= 0x070902
18 +char *ssl_key = NULL;
19 +#endif
20 +#if LIBCURL_VERSION_NUM >= 0x070908
21 +char *ssl_capath = NULL;
22 +#endif
23 +char *ssl_cainfo = NULL;
24 +long curl_low_speed_limit = -1;
25 +long curl_low_speed_time = -1;
26 +
27 +struct curl_slist *pragma_header;
28 +struct curl_slist *no_range_header;
29 +
30 +struct active_request_slot *active_queue_head = NULL;
31 +
32 +size_t fread_buffer(void *ptr, size_t eltsize, size_t nmemb,
33 + struct buffer *buffer)
34 +{
35 + size_t size = eltsize * nmemb;
36 + if (size > buffer->size - buffer->posn)
37 + size = buffer->size - buffer->posn;
38 + memcpy(ptr, buffer->buffer + buffer->posn, size);
39 + buffer->posn += size;
40 + return size;
41 +}
42 +
43 +size_t fwrite_buffer(const void *ptr, size_t eltsize,
44 + size_t nmemb, struct buffer *buffer)
45 +{
46 + size_t size = eltsize * nmemb;
47 + if (size > buffer->size - buffer->posn) {
48 + buffer->size = buffer->size * 3 / 2;
49 + if (buffer->size < buffer->posn + size)
50 + buffer->size = buffer->posn + size;
51 + buffer->buffer = xrealloc(buffer->buffer, buffer->size);
52 + }
53 + memcpy(buffer->buffer + buffer->posn, ptr, size);
54 + buffer->posn += size;
55 + data_received++;
56 + return size;
57 +}
58 +
59 +size_t fwrite_null(const void *ptr, size_t eltsize,
60 + size_t nmemb, struct buffer *buffer)
61 +{
62 + data_received++;
63 + return eltsize * nmemb;
64 +}
65 +
66 +static void finish_active_slot(struct active_request_slot *slot);
67 +
68 +#ifdef USE_CURL_MULTI
69 +static void process_curl_messages(void)
70 +{
71 + int num_messages;
72 + struct active_request_slot *slot;
73 + CURLMsg *curl_message = curl_multi_info_read(curlm, &num_messages);
74 +
75 + while (curl_message != NULL) {
76 + if (curl_message->msg == CURLMSG_DONE) {
77 + int curl_result = curl_message->data.result;
78 + slot = active_queue_head;
79 + while (slot != NULL &&
80 + slot->curl != curl_message->easy_handle)
81 + slot = slot->next;
82 + if (slot != NULL) {
83 + curl_multi_remove_handle(curlm, slot->curl);
84 + slot->curl_result = curl_result;
85 + finish_active_slot(slot);
86 + } else {
87 + fprintf(stderr, "Received DONE message for unknown request!\n");
88 + }
89 + } else {
90 + fprintf(stderr, "Unknown CURL message received: %d\n",
91 + (int)curl_message->msg);
92 + }
93 + curl_message = curl_multi_info_read(curlm, &num_messages);
94 + }
95 +}
96 +#endif
97 +
98 +static int http_options(const char *var, const char *value)
99 +{
100 + if (!strcmp("http.sslverify", var)) {
101 + if (curl_ssl_verify == -1) {
102 + curl_ssl_verify = git_config_bool(var, value);
103 + }
104 + return 0;
105 + }
106 +
107 + if (!strcmp("http.sslcert", var)) {
108 + if (ssl_cert == NULL) {
109 + ssl_cert = xmalloc(strlen(value)+1);
110 + strcpy(ssl_cert, value);
111 + }
112 + return 0;
113 + }
114 +#if LIBCURL_VERSION_NUM >= 0x070902
115 + if (!strcmp("http.sslkey", var)) {
116 + if (ssl_key == NULL) {
117 + ssl_key = xmalloc(strlen(value)+1);
118 + strcpy(ssl_key, value);
119 + }
120 + return 0;
121 + }
122 +#endif
123 +#if LIBCURL_VERSION_NUM >= 0x070908
124 + if (!strcmp("http.sslcapath", var)) {
125 + if (ssl_capath == NULL) {
126 + ssl_capath = xmalloc(strlen(value)+1);
127 + strcpy(ssl_capath, value);
128 + }
129 + return 0;
130 + }
131 +#endif
132 + if (!strcmp("http.sslcainfo", var)) {
133 + if (ssl_cainfo == NULL) {
134 + ssl_cainfo = xmalloc(strlen(value)+1);
135 + strcpy(ssl_cainfo, value);
136 + }
137 + return 0;
138 + }
139 +
140 +#ifdef USE_CURL_MULTI
141 + if (!strcmp("http.maxrequests", var)) {
142 + if (max_requests == -1)
143 + max_requests = git_config_int(var, value);
144 + return 0;
145 + }
146 +#endif
147 +
148 + if (!strcmp("http.lowspeedlimit", var)) {
149 + if (curl_low_speed_limit == -1)
150 + curl_low_speed_limit = (long)git_config_int(var, value);
151 + return 0;
152 + }
153 + if (!strcmp("http.lowspeedtime", var)) {
154 + if (curl_low_speed_time == -1)
155 + curl_low_speed_time = (long)git_config_int(var, value);
156 + return 0;
157 + }
158 +
159 + /* Fall back on the default ones */
160 + return git_default_config(var, value);
161 +}
162 +
163 +void http_init(void)
164 +{
165 + char *low_speed_limit;
166 + char *low_speed_time;
167 +
168 + curl_global_init(CURL_GLOBAL_ALL);
169 +
170 + pragma_header = curl_slist_append(pragma_header, "Pragma: no-cache");
171 + no_range_header = curl_slist_append(no_range_header, "Range:");
172 +
173 +#ifdef USE_CURL_MULTI
174 + {
175 + char *http_max_requests = getenv("GIT_HTTP_MAX_REQUESTS");
176 + if (http_max_requests != NULL)
177 + max_requests = atoi(http_max_requests);
178 + }
179 +
180 + curlm = curl_multi_init();
181 + if (curlm == NULL) {
182 + fprintf(stderr, "Error creating curl multi handle.\n");
183 + exit(1);
184 + }
185 +#endif
186 +
187 + if (getenv("GIT_SSL_NO_VERIFY"))
188 + curl_ssl_verify = 0;
189 +
190 + ssl_cert = getenv("GIT_SSL_CERT");
191 +#if LIBCURL_VERSION_NUM >= 0x070902
192 + ssl_key = getenv("GIT_SSL_KEY");
193 +#endif
194 +#if LIBCURL_VERSION_NUM >= 0x070908
195 + ssl_capath = getenv("GIT_SSL_CAPATH");
196 +#endif
197 + ssl_cainfo = getenv("GIT_SSL_CAINFO");
198 +
199 + low_speed_limit = getenv("GIT_HTTP_LOW_SPEED_LIMIT");
200 + if (low_speed_limit != NULL)
201 + curl_low_speed_limit = strtol(low_speed_limit, NULL, 10);
202 + low_speed_time = getenv("GIT_HTTP_LOW_SPEED_TIME");
203 + if (low_speed_time != NULL)
204 + curl_low_speed_time = strtol(low_speed_time, NULL, 10);
205 +
206 + git_config(http_options);
207 +
208 + if (curl_ssl_verify == -1)
209 + curl_ssl_verify = 1;
210 +
211 +#ifdef USE_CURL_MULTI
212 + if (max_requests < 1)
213 + max_requests = DEFAULT_MAX_REQUESTS;
214 +#endif
215 +
216 +#ifndef NO_CURL_EASY_DUPHANDLE
217 + curl_default = get_curl_handle();
218 +#endif
219 +}
220 +
221 +void http_cleanup(void)
222 +{
223 + struct active_request_slot *slot = active_queue_head;
224 +#ifdef USE_CURL_MULTI
225 + char *wait_url;
226 + CURLMcode curlm_result;
227 +#endif
228 +
229 + while (slot != NULL) {
230 +#ifdef USE_CURL_MULTI
231 + if (slot->in_use) {
232 + curl_easy_getinfo(slot->curl,
233 + CURLINFO_EFFECTIVE_URL,
234 + &wait_url);
235 + fprintf(stderr, "Waiting for %s\n", wait_url);
236 + run_active_slot(slot);
237 + }
238 +#endif
239 + if (slot->curl != NULL)
240 + curl_easy_cleanup(slot->curl);
241 + slot = slot->next;
242 + }
243 +
244 +#ifndef NO_CURL_EASY_DUPHANDLE
245 + curl_easy_cleanup(curl_default);
246 +#endif
247 +
248 +#ifdef USE_CURL_MULTI
249 + curl_multi_cleanup(curlm);
250 +#endif
251 + curl_global_cleanup();
252 +
253 +}
254 +
255 +static CURL* get_curl_handle(void)
256 +{
257 + CURL* result = curl_easy_init();
258 +
259 + curl_easy_setopt(result, CURLOPT_SSL_VERIFYPEER, curl_ssl_verify);
260 +#if LIBCURL_VERSION_NUM >= 0x070907
261 + curl_easy_setopt(result, CURLOPT_NETRC, CURL_NETRC_OPTIONAL);
262 +#endif
263 +
264 + if (ssl_cert != NULL)
265 + curl_easy_setopt(result, CURLOPT_SSLCERT, ssl_cert);
266 +#if LIBCURL_VERSION_NUM >= 0x070902
267 + if (ssl_key != NULL)
268 + curl_easy_setopt(result, CURLOPT_SSLKEY, ssl_key);
269 +#endif
270 +#if LIBCURL_VERSION_NUM >= 0x070908
271 + if (ssl_capath != NULL)
272 + curl_easy_setopt(result, CURLOPT_CAPATH, ssl_capath);
273 +#endif
274 + if (ssl_cainfo != NULL)
275 + curl_easy_setopt(result, CURLOPT_CAINFO, ssl_cainfo);
276 + curl_easy_setopt(result, CURLOPT_FAILONERROR, 1);
277 +
278 + if (curl_low_speed_limit > 0 && curl_low_speed_time > 0) {
279 + curl_easy_setopt(result, CURLOPT_LOW_SPEED_LIMIT,
280 + curl_low_speed_limit);
281 + curl_easy_setopt(result, CURLOPT_LOW_SPEED_TIME,
282 + curl_low_speed_time);
283 + }
284 +
285 + curl_easy_setopt(result, CURLOPT_FOLLOWLOCATION, 1);
286 +
287 + return result;
288 +}
289 +
290 +struct active_request_slot *get_active_slot(void)
291 +{
292 + struct active_request_slot *slot = active_queue_head;
293 + struct active_request_slot *newslot;
294 +
295 +#ifdef USE_CURL_MULTI
296 + int num_transfers;
297 +
298 + /* Wait for a slot to open up if the queue is full */
299 + while (active_requests >= max_requests) {
300 + curl_multi_perform(curlm, &num_transfers);
301 + if (num_transfers < active_requests) {
302 + process_curl_messages();
303 + }
304 + }
305 +#endif
306 +
307 + while (slot != NULL && slot->in_use) {
308 + slot = slot->next;
309 + }
310 + if (slot == NULL) {
311 + newslot = xmalloc(sizeof(*newslot));
312 + newslot->curl = NULL;
313 + newslot->in_use = 0;
314 + newslot->next = NULL;
315 +
316 + slot = active_queue_head;
317 + if (slot == NULL) {
318 + active_queue_head = newslot;
319 + } else {
320 + while (slot->next != NULL) {
321 + slot = slot->next;
322 + }
323 + slot->next = newslot;
324 + }
325 + slot = newslot;
326 + }
327 +
328 + if (slot->curl == NULL) {
329 +#ifdef NO_CURL_EASY_DUPHANDLE
330 + slot->curl = get_curl_handle();
331 +#else
332 + slot->curl = curl_easy_duphandle(curl_default);
333 +#endif
334 + }
335 +
336 + active_requests++;
337 + slot->in_use = 1;
338 + slot->local = NULL;
339 + slot->callback_data = NULL;
340 + slot->callback_func = NULL;
341 + curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, pragma_header);
342 + curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, no_range_header);
343 + curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, curl_errorstr);
344 +
345 + return slot;
346 +}
347 +
348 +int start_active_slot(struct active_request_slot *slot)
349 +{
350 +#ifdef USE_CURL_MULTI
351 + CURLMcode curlm_result = curl_multi_add_handle(curlm, slot->curl);
352 +
353 + if (curlm_result != CURLM_OK &&
354 + curlm_result != CURLM_CALL_MULTI_PERFORM) {
355 + active_requests--;
356 + slot->in_use = 0;
357 + return 0;
358 + }
359 +#endif
360 + return 1;
361 +}
362 +
363 +#ifdef USE_CURL_MULTI
364 +void step_active_slots(void)
365 +{
366 + int num_transfers;
367 + CURLMcode curlm_result;
368 +
369 + do {
370 + curlm_result = curl_multi_perform(curlm, &num_transfers);
371 + } while (curlm_result == CURLM_CALL_MULTI_PERFORM);
372 + if (num_transfers < active_requests) {
373 + process_curl_messages();
374 + fill_active_slots();
375 + }
376 +}
377 +#endif
378 +
379 +void run_active_slot(struct active_request_slot *slot)
380 +{
381 +#ifdef USE_CURL_MULTI
382 + long last_pos = 0;
383 + long current_pos;
384 + fd_set readfds;
385 + fd_set writefds;
386 + fd_set excfds;
387 + int max_fd;
388 + struct timeval select_timeout;
389 +
390 + while (slot->in_use) {
391 + data_received = 0;
392 + step_active_slots();
393 +
394 + if (!data_received && slot->local != NULL) {
395 + current_pos = ftell(slot->local);
396 + if (current_pos > last_pos)
397 + data_received++;
398 + last_pos = current_pos;
399 + }
400 +
401 + if (slot->in_use && !data_received) {
402 + max_fd = 0;
403 + FD_ZERO(&readfds);
404 + FD_ZERO(&writefds);
405 + FD_ZERO(&excfds);
406 + select_timeout.tv_sec = 0;
407 + select_timeout.tv_usec = 50000;
408 + select(max_fd, &readfds, &writefds,
409 + &excfds, &select_timeout);
410 + }
411 + }
412 +#else
413 + while (slot->in_use) {
414 + slot->curl_result = curl_easy_perform(slot->curl);
415 + finish_active_slot(slot);
416 + }
417 +#endif
418 +}
419 +
420 +static void finish_active_slot(struct active_request_slot *slot)
421 +{
422 + active_requests--;
423 + slot->in_use = 0;
424 + curl_easy_getinfo(slot->curl, CURLINFO_HTTP_CODE, &slot->http_code);
425 +
426 + /* Run callback if appropriate */
427 + if (slot->callback_func != NULL) {
428 + slot->callback_func(slot->callback_data);
429 + }
430 +}
431 +
432 +void finish_all_active_slots(void)
433 +{
434 + struct active_request_slot *slot = active_queue_head;
435 +
436 + while (slot != NULL)
437 + if (slot->in_use) {
438 + run_active_slot(slot);
439 + slot = active_queue_head;
440 + } else {
441 + slot = slot->next;
442 + }
443 +}
http.h new
+95
@@ -0,0 +1,95 @@
1 +#ifndef HTTP_H
2 +#define HTTP_H
3 +
4 +#include "cache.h"
5 +
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)
16 +#endif
17 +#if LIBCURL_VERSION_NUM < 0x070800
18 +#define curl_global_init(a) do { /* nothing */ } while(0)
19 +#endif
20 +
21 +#if LIBCURL_VERSION_NUM < 0x070c04
22 +#define NO_CURL_EASY_DUPHANDLE
23 +#endif
24 +
25 +struct active_request_slot
26 +{
27 + CURL *curl;
28 + FILE *local;
29 + int in_use;
30 + CURLcode curl_result;
31 + long http_code;
32 + void *callback_data;
33 + void (*callback_func)(void *data);
34 + struct active_request_slot *next;
35 +};
36 +
37 +struct buffer
38 +{
39 + size_t posn;
40 + size_t size;
41 + void *buffer;
42 +};
43 +
44 +/* Curl request read/write callbacks */
45 +extern size_t fread_buffer(void *ptr, size_t eltsize, size_t nmemb,
46 + struct buffer *buffer);
47 +extern size_t fwrite_buffer(const void *ptr, size_t eltsize,
48 + size_t nmemb, struct buffer *buffer);
49 +extern size_t fwrite_null(const void *ptr, size_t eltsize,
50 + size_t nmemb, struct buffer *buffer);
51 +
52 +/* Slot lifecycle functions */
53 +extern struct active_request_slot *get_active_slot(void);
54 +extern int start_active_slot(struct active_request_slot *slot);
55 +extern void run_active_slot(struct active_request_slot *slot);
56 +extern void finish_all_active_slots(void);
57 +
58 +#ifdef USE_CURL_MULTI
59 +extern void fill_active_slots(void);
60 +extern void step_active_slots(void);
61 +#endif
62 +
63 +extern void http_init(void);
64 +extern void http_cleanup(void);
65 +
66 +extern int data_received;
67 +extern int active_requests;
68 +
69 +#ifdef USE_CURL_MULTI
70 +extern int max_requests;
71 +extern CURLM *curlm;
72 +#endif
73 +#ifndef NO_CURL_EASY_DUPHANDLE
74 +extern CURL *curl_default;
75 +#endif
76 +extern char curl_errorstr[CURL_ERROR_SIZE];
77 +
78 +extern int curl_ssl_verify;
79 +extern char *ssl_cert;
80 +#if LIBCURL_VERSION_NUM >= 0x070902
81 +extern char *ssl_key;
82 +#endif
83 +#if LIBCURL_VERSION_NUM >= 0x070908
84 +extern char *ssl_capath;
85 +#endif
86 +extern char *ssl_cainfo;
87 +extern long curl_low_speed_limit;
88 +extern long curl_low_speed_time;
89 +
90 +extern struct curl_slist *pragma_header;
91 +extern struct curl_slist *no_range_header;
92 +
93 +extern struct active_request_slot *active_queue_head;
94 +
95 +#endif /* HTTP_H */