]>
Commit | Line | Data |
---|---|---|
29508e1e NH |
1 | #include "http.h" |
2 | ||
3 | int data_received; | |
4251ccbd | 4 | int active_requests; |
29508e1e NH |
5 | |
6 | #ifdef USE_CURL_MULTI | |
cc3530e8 MH |
7 | static int max_requests = -1; |
8 | static CURLM *curlm; | |
29508e1e NH |
9 | #endif |
10 | #ifndef NO_CURL_EASY_DUPHANDLE | |
cc3530e8 | 11 | static CURL *curl_default; |
29508e1e NH |
12 | #endif |
13 | char curl_errorstr[CURL_ERROR_SIZE]; | |
14 | ||
cc3530e8 | 15 | static int curl_ssl_verify = -1; |
4251ccbd | 16 | static const char *ssl_cert; |
29508e1e | 17 | #if LIBCURL_VERSION_NUM >= 0x070902 |
4251ccbd | 18 | static const char *ssl_key; |
29508e1e NH |
19 | #endif |
20 | #if LIBCURL_VERSION_NUM >= 0x070908 | |
4251ccbd | 21 | static const char *ssl_capath; |
29508e1e | 22 | #endif |
4251ccbd | 23 | static const char *ssl_cainfo; |
cc3530e8 MH |
24 | static long curl_low_speed_limit = -1; |
25 | static long curl_low_speed_time = -1; | |
4251ccbd JH |
26 | static int curl_ftp_no_epsv; |
27 | static const char *curl_http_proxy; | |
c33976cb | 28 | static char *user_name, *user_pass; |
29508e1e | 29 | |
cc3530e8 | 30 | static struct curl_slist *pragma_header; |
29508e1e | 31 | |
4251ccbd | 32 | static struct active_request_slot *active_queue_head; |
29508e1e | 33 | |
f444e528 | 34 | size_t fread_buffer(void *ptr, size_t eltsize, size_t nmemb, void *buffer_) |
29508e1e NH |
35 | { |
36 | size_t size = eltsize * nmemb; | |
f444e528 JH |
37 | struct buffer *buffer = buffer_; |
38 | ||
028c2976 MH |
39 | if (size > buffer->buf.len - buffer->posn) |
40 | size = buffer->buf.len - buffer->posn; | |
41 | memcpy(ptr, buffer->buf.buf + buffer->posn, size); | |
29508e1e | 42 | buffer->posn += size; |
028c2976 | 43 | |
29508e1e NH |
44 | return size; |
45 | } | |
46 | ||
f444e528 | 47 | size_t fwrite_buffer(const void *ptr, size_t eltsize, size_t nmemb, void *buffer_) |
29508e1e NH |
48 | { |
49 | size_t size = eltsize * nmemb; | |
f444e528 JH |
50 | struct strbuf *buffer = buffer_; |
51 | ||
028c2976 | 52 | strbuf_add(buffer, ptr, size); |
29508e1e NH |
53 | data_received++; |
54 | return size; | |
55 | } | |
56 | ||
f444e528 | 57 | size_t fwrite_null(const void *ptr, size_t eltsize, size_t nmemb, void *strbuf) |
29508e1e NH |
58 | { |
59 | data_received++; | |
60 | return eltsize * nmemb; | |
61 | } | |
62 | ||
63 | static void finish_active_slot(struct active_request_slot *slot); | |
64 | ||
65 | #ifdef USE_CURL_MULTI | |
66 | static void process_curl_messages(void) | |
67 | { | |
68 | int num_messages; | |
69 | struct active_request_slot *slot; | |
70 | CURLMsg *curl_message = curl_multi_info_read(curlm, &num_messages); | |
71 | ||
72 | while (curl_message != NULL) { | |
73 | if (curl_message->msg == CURLMSG_DONE) { | |
74 | int curl_result = curl_message->data.result; | |
75 | slot = active_queue_head; | |
76 | while (slot != NULL && | |
77 | slot->curl != curl_message->easy_handle) | |
78 | slot = slot->next; | |
79 | if (slot != NULL) { | |
80 | curl_multi_remove_handle(curlm, slot->curl); | |
81 | slot->curl_result = curl_result; | |
82 | finish_active_slot(slot); | |
83 | } else { | |
84 | fprintf(stderr, "Received DONE message for unknown request!\n"); | |
85 | } | |
86 | } else { | |
87 | fprintf(stderr, "Unknown CURL message received: %d\n", | |
88 | (int)curl_message->msg); | |
89 | } | |
90 | curl_message = curl_multi_info_read(curlm, &num_messages); | |
91 | } | |
92 | } | |
93 | #endif | |
94 | ||
ef90d6d4 | 95 | static int http_options(const char *var, const char *value, void *cb) |
29508e1e NH |
96 | { |
97 | if (!strcmp("http.sslverify", var)) { | |
7059cd99 | 98 | curl_ssl_verify = git_config_bool(var, value); |
29508e1e NH |
99 | return 0; |
100 | } | |
7059cd99 JH |
101 | if (!strcmp("http.sslcert", var)) |
102 | return git_config_string(&ssl_cert, var, value); | |
29508e1e | 103 | #if LIBCURL_VERSION_NUM >= 0x070902 |
7059cd99 JH |
104 | if (!strcmp("http.sslkey", var)) |
105 | return git_config_string(&ssl_key, var, value); | |
29508e1e NH |
106 | #endif |
107 | #if LIBCURL_VERSION_NUM >= 0x070908 | |
7059cd99 JH |
108 | if (!strcmp("http.sslcapath", var)) |
109 | return git_config_string(&ssl_capath, var, value); | |
29508e1e | 110 | #endif |
7059cd99 JH |
111 | if (!strcmp("http.sslcainfo", var)) |
112 | return git_config_string(&ssl_cainfo, var, value); | |
a6080a0a | 113 | #ifdef USE_CURL_MULTI |
29508e1e | 114 | if (!strcmp("http.maxrequests", var)) { |
7059cd99 | 115 | max_requests = git_config_int(var, value); |
29508e1e NH |
116 | return 0; |
117 | } | |
118 | #endif | |
29508e1e | 119 | if (!strcmp("http.lowspeedlimit", var)) { |
7059cd99 | 120 | curl_low_speed_limit = (long)git_config_int(var, value); |
29508e1e NH |
121 | return 0; |
122 | } | |
123 | if (!strcmp("http.lowspeedtime", var)) { | |
7059cd99 | 124 | curl_low_speed_time = (long)git_config_int(var, value); |
29508e1e NH |
125 | return 0; |
126 | } | |
127 | ||
3ea099d4 SK |
128 | if (!strcmp("http.noepsv", var)) { |
129 | curl_ftp_no_epsv = git_config_bool(var, value); | |
130 | return 0; | |
131 | } | |
7059cd99 JH |
132 | if (!strcmp("http.proxy", var)) |
133 | return git_config_string(&curl_http_proxy, var, value); | |
3ea099d4 | 134 | |
29508e1e | 135 | /* Fall back on the default ones */ |
ef90d6d4 | 136 | return git_default_config(var, value, cb); |
29508e1e NH |
137 | } |
138 | ||
c33976cb JH |
139 | static void init_curl_http_auth(CURL *result) |
140 | { | |
750d9305 | 141 | if (user_name) { |
c33976cb JH |
142 | struct strbuf up = STRBUF_INIT; |
143 | if (!user_pass) | |
144 | user_pass = xstrdup(getpass("Password: ")); | |
145 | strbuf_addf(&up, "%s:%s", user_name, user_pass); | |
146 | curl_easy_setopt(result, CURLOPT_USERPWD, | |
147 | strbuf_detach(&up, NULL)); | |
148 | } | |
149 | } | |
150 | ||
4251ccbd | 151 | static CURL *get_curl_handle(void) |
11979b98 | 152 | { |
4251ccbd | 153 | CURL *result = curl_easy_init(); |
11979b98 | 154 | |
a5ccc597 JH |
155 | if (!curl_ssl_verify) { |
156 | curl_easy_setopt(result, CURLOPT_SSL_VERIFYPEER, 0); | |
157 | curl_easy_setopt(result, CURLOPT_SSL_VERIFYHOST, 0); | |
158 | } else { | |
159 | /* Verify authenticity of the peer's certificate */ | |
160 | curl_easy_setopt(result, CURLOPT_SSL_VERIFYPEER, 1); | |
161 | /* The name in the cert must match whom we tried to connect */ | |
162 | curl_easy_setopt(result, CURLOPT_SSL_VERIFYHOST, 2); | |
163 | } | |
164 | ||
11979b98 JH |
165 | #if LIBCURL_VERSION_NUM >= 0x070907 |
166 | curl_easy_setopt(result, CURLOPT_NETRC, CURL_NETRC_OPTIONAL); | |
167 | #endif | |
168 | ||
c33976cb JH |
169 | init_curl_http_auth(result); |
170 | ||
11979b98 JH |
171 | if (ssl_cert != NULL) |
172 | curl_easy_setopt(result, CURLOPT_SSLCERT, ssl_cert); | |
173 | #if LIBCURL_VERSION_NUM >= 0x070902 | |
174 | if (ssl_key != NULL) | |
175 | curl_easy_setopt(result, CURLOPT_SSLKEY, ssl_key); | |
176 | #endif | |
177 | #if LIBCURL_VERSION_NUM >= 0x070908 | |
178 | if (ssl_capath != NULL) | |
179 | curl_easy_setopt(result, CURLOPT_CAPATH, ssl_capath); | |
180 | #endif | |
181 | if (ssl_cainfo != NULL) | |
182 | curl_easy_setopt(result, CURLOPT_CAINFO, ssl_cainfo); | |
183 | curl_easy_setopt(result, CURLOPT_FAILONERROR, 1); | |
184 | ||
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); | |
190 | } | |
191 | ||
192 | curl_easy_setopt(result, CURLOPT_FOLLOWLOCATION, 1); | |
193 | ||
7982d74e MW |
194 | if (getenv("GIT_CURL_VERBOSE")) |
195 | curl_easy_setopt(result, CURLOPT_VERBOSE, 1); | |
196 | ||
20fc9bc5 NH |
197 | curl_easy_setopt(result, CURLOPT_USERAGENT, GIT_USER_AGENT); |
198 | ||
3ea099d4 SK |
199 | if (curl_ftp_no_epsv) |
200 | curl_easy_setopt(result, CURLOPT_FTP_USE_EPSV, 0); | |
201 | ||
9c5665aa SV |
202 | if (curl_http_proxy) |
203 | curl_easy_setopt(result, CURLOPT_PROXY, curl_http_proxy); | |
204 | ||
11979b98 JH |
205 | return result; |
206 | } | |
207 | ||
c33976cb JH |
208 | static void http_auth_init(const char *url) |
209 | { | |
210 | char *at, *colon, *cp, *slash; | |
211 | int len; | |
212 | ||
213 | cp = strstr(url, "://"); | |
214 | if (!cp) | |
215 | return; | |
216 | ||
217 | /* | |
218 | * Ok, the URL looks like "proto://something". Which one? | |
219 | * "proto://<user>:<pass>@<host>/...", | |
220 | * "proto://<user>@<host>/...", or just | |
221 | * "proto://<host>/..."? | |
222 | */ | |
223 | cp += 3; | |
224 | at = strchr(cp, '@'); | |
225 | colon = strchr(cp, ':'); | |
226 | slash = strchrnul(cp, '/'); | |
227 | if (!at || slash <= at) | |
228 | return; /* No credentials */ | |
229 | if (!colon || at <= colon) { | |
230 | /* Only username */ | |
231 | len = at - cp; | |
232 | user_name = xmalloc(len + 1); | |
233 | memcpy(user_name, cp, len); | |
234 | user_name[len] = '\0'; | |
235 | user_pass = NULL; | |
236 | } else { | |
237 | len = colon - cp; | |
238 | user_name = xmalloc(len + 1); | |
239 | memcpy(user_name, cp, len); | |
240 | user_name[len] = '\0'; | |
241 | len = at - (colon + 1); | |
242 | user_pass = xmalloc(len + 1); | |
243 | memcpy(user_pass, colon + 1, len); | |
244 | user_pass[len] = '\0'; | |
245 | } | |
246 | } | |
247 | ||
7059cd99 JH |
248 | static void set_from_env(const char **var, const char *envname) |
249 | { | |
250 | const char *val = getenv(envname); | |
251 | if (val) | |
252 | *var = val; | |
253 | } | |
254 | ||
9fc6440d | 255 | void http_init(struct remote *remote) |
29508e1e NH |
256 | { |
257 | char *low_speed_limit; | |
258 | char *low_speed_time; | |
259 | ||
7059cd99 JH |
260 | git_config(http_options, NULL); |
261 | ||
29508e1e NH |
262 | curl_global_init(CURL_GLOBAL_ALL); |
263 | ||
9fc6440d MH |
264 | if (remote && remote->http_proxy) |
265 | curl_http_proxy = xstrdup(remote->http_proxy); | |
266 | ||
29508e1e | 267 | pragma_header = curl_slist_append(pragma_header, "Pragma: no-cache"); |
29508e1e NH |
268 | |
269 | #ifdef USE_CURL_MULTI | |
270 | { | |
271 | char *http_max_requests = getenv("GIT_HTTP_MAX_REQUESTS"); | |
272 | if (http_max_requests != NULL) | |
273 | max_requests = atoi(http_max_requests); | |
274 | } | |
275 | ||
276 | curlm = curl_multi_init(); | |
277 | if (curlm == NULL) { | |
278 | fprintf(stderr, "Error creating curl multi handle.\n"); | |
279 | exit(1); | |
280 | } | |
281 | #endif | |
282 | ||
283 | if (getenv("GIT_SSL_NO_VERIFY")) | |
284 | curl_ssl_verify = 0; | |
285 | ||
7059cd99 | 286 | set_from_env(&ssl_cert, "GIT_SSL_CERT"); |
29508e1e | 287 | #if LIBCURL_VERSION_NUM >= 0x070902 |
7059cd99 | 288 | set_from_env(&ssl_key, "GIT_SSL_KEY"); |
29508e1e NH |
289 | #endif |
290 | #if LIBCURL_VERSION_NUM >= 0x070908 | |
7059cd99 | 291 | set_from_env(&ssl_capath, "GIT_SSL_CAPATH"); |
29508e1e | 292 | #endif |
7059cd99 | 293 | set_from_env(&ssl_cainfo, "GIT_SSL_CAINFO"); |
29508e1e NH |
294 | |
295 | low_speed_limit = getenv("GIT_HTTP_LOW_SPEED_LIMIT"); | |
296 | if (low_speed_limit != NULL) | |
297 | curl_low_speed_limit = strtol(low_speed_limit, NULL, 10); | |
298 | low_speed_time = getenv("GIT_HTTP_LOW_SPEED_TIME"); | |
299 | if (low_speed_time != NULL) | |
300 | curl_low_speed_time = strtol(low_speed_time, NULL, 10); | |
301 | ||
29508e1e NH |
302 | if (curl_ssl_verify == -1) |
303 | curl_ssl_verify = 1; | |
304 | ||
305 | #ifdef USE_CURL_MULTI | |
306 | if (max_requests < 1) | |
307 | max_requests = DEFAULT_MAX_REQUESTS; | |
308 | #endif | |
309 | ||
3ea099d4 SK |
310 | if (getenv("GIT_CURL_FTP_NO_EPSV")) |
311 | curl_ftp_no_epsv = 1; | |
312 | ||
c33976cb JH |
313 | if (remote && remote->url && remote->url[0]) |
314 | http_auth_init(remote->url[0]); | |
315 | ||
29508e1e NH |
316 | #ifndef NO_CURL_EASY_DUPHANDLE |
317 | curl_default = get_curl_handle(); | |
318 | #endif | |
319 | } | |
320 | ||
321 | void http_cleanup(void) | |
322 | { | |
323 | struct active_request_slot *slot = active_queue_head; | |
29508e1e NH |
324 | |
325 | while (slot != NULL) { | |
3278cd0a | 326 | struct active_request_slot *next = slot->next; |
f23d1f76 | 327 | if (slot->curl != NULL) { |
29508e1e | 328 | #ifdef USE_CURL_MULTI |
f23d1f76 | 329 | curl_multi_remove_handle(curlm, slot->curl); |
29508e1e | 330 | #endif |
29508e1e | 331 | curl_easy_cleanup(slot->curl); |
f23d1f76 | 332 | } |
3278cd0a SP |
333 | free(slot); |
334 | slot = next; | |
29508e1e | 335 | } |
3278cd0a | 336 | active_queue_head = NULL; |
29508e1e NH |
337 | |
338 | #ifndef NO_CURL_EASY_DUPHANDLE | |
339 | curl_easy_cleanup(curl_default); | |
340 | #endif | |
341 | ||
342 | #ifdef USE_CURL_MULTI | |
343 | curl_multi_cleanup(curlm); | |
344 | #endif | |
345 | curl_global_cleanup(); | |
b3ca4e4e NH |
346 | |
347 | curl_slist_free_all(pragma_header); | |
3278cd0a | 348 | pragma_header = NULL; |
9fc6440d MH |
349 | |
350 | if (curl_http_proxy) { | |
e4a80ecf | 351 | free((void *)curl_http_proxy); |
9fc6440d MH |
352 | curl_http_proxy = NULL; |
353 | } | |
29508e1e NH |
354 | } |
355 | ||
29508e1e NH |
356 | struct active_request_slot *get_active_slot(void) |
357 | { | |
358 | struct active_request_slot *slot = active_queue_head; | |
359 | struct active_request_slot *newslot; | |
360 | ||
361 | #ifdef USE_CURL_MULTI | |
362 | int num_transfers; | |
363 | ||
364 | /* Wait for a slot to open up if the queue is full */ | |
365 | while (active_requests >= max_requests) { | |
366 | curl_multi_perform(curlm, &num_transfers); | |
4251ccbd | 367 | if (num_transfers < active_requests) |
29508e1e | 368 | process_curl_messages(); |
29508e1e NH |
369 | } |
370 | #endif | |
371 | ||
4251ccbd | 372 | while (slot != NULL && slot->in_use) |
29508e1e | 373 | slot = slot->next; |
4251ccbd | 374 | |
29508e1e NH |
375 | if (slot == NULL) { |
376 | newslot = xmalloc(sizeof(*newslot)); | |
377 | newslot->curl = NULL; | |
378 | newslot->in_use = 0; | |
379 | newslot->next = NULL; | |
380 | ||
381 | slot = active_queue_head; | |
382 | if (slot == NULL) { | |
383 | active_queue_head = newslot; | |
384 | } else { | |
4251ccbd | 385 | while (slot->next != NULL) |
29508e1e | 386 | slot = slot->next; |
29508e1e NH |
387 | slot->next = newslot; |
388 | } | |
389 | slot = newslot; | |
390 | } | |
391 | ||
392 | if (slot->curl == NULL) { | |
393 | #ifdef NO_CURL_EASY_DUPHANDLE | |
394 | slot->curl = get_curl_handle(); | |
395 | #else | |
396 | slot->curl = curl_easy_duphandle(curl_default); | |
397 | #endif | |
398 | } | |
399 | ||
400 | active_requests++; | |
401 | slot->in_use = 1; | |
402 | slot->local = NULL; | |
c8568e13 | 403 | slot->results = NULL; |
baa7b67d | 404 | slot->finished = NULL; |
29508e1e NH |
405 | slot->callback_data = NULL; |
406 | slot->callback_func = NULL; | |
407 | curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, pragma_header); | |
29508e1e | 408 | curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, curl_errorstr); |
9094950d NH |
409 | curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, NULL); |
410 | curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, NULL); | |
411 | curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, NULL); | |
412 | curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 0); | |
413 | curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1); | |
29508e1e NH |
414 | |
415 | return slot; | |
416 | } | |
417 | ||
418 | int start_active_slot(struct active_request_slot *slot) | |
419 | { | |
420 | #ifdef USE_CURL_MULTI | |
421 | CURLMcode curlm_result = curl_multi_add_handle(curlm, slot->curl); | |
45c17412 | 422 | int num_transfers; |
29508e1e NH |
423 | |
424 | if (curlm_result != CURLM_OK && | |
425 | curlm_result != CURLM_CALL_MULTI_PERFORM) { | |
426 | active_requests--; | |
427 | slot->in_use = 0; | |
428 | return 0; | |
429 | } | |
45c17412 DB |
430 | |
431 | /* | |
432 | * We know there must be something to do, since we just added | |
433 | * something. | |
434 | */ | |
435 | curl_multi_perform(curlm, &num_transfers); | |
29508e1e NH |
436 | #endif |
437 | return 1; | |
438 | } | |
439 | ||
440 | #ifdef USE_CURL_MULTI | |
fc57b6aa DB |
441 | struct fill_chain { |
442 | void *data; | |
443 | int (*fill)(void *); | |
444 | struct fill_chain *next; | |
445 | }; | |
446 | ||
4251ccbd | 447 | static struct fill_chain *fill_cfg; |
fc57b6aa DB |
448 | |
449 | void add_fill_function(void *data, int (*fill)(void *)) | |
450 | { | |
e8eec71d | 451 | struct fill_chain *new = xmalloc(sizeof(*new)); |
fc57b6aa DB |
452 | struct fill_chain **linkp = &fill_cfg; |
453 | new->data = data; | |
454 | new->fill = fill; | |
455 | new->next = NULL; | |
456 | while (*linkp) | |
457 | linkp = &(*linkp)->next; | |
458 | *linkp = new; | |
459 | } | |
460 | ||
45c17412 DB |
461 | void fill_active_slots(void) |
462 | { | |
463 | struct active_request_slot *slot = active_queue_head; | |
464 | ||
fc57b6aa DB |
465 | while (active_requests < max_requests) { |
466 | struct fill_chain *fill; | |
467 | for (fill = fill_cfg; fill; fill = fill->next) | |
468 | if (fill->fill(fill->data)) | |
469 | break; | |
470 | ||
471 | if (!fill) | |
45c17412 | 472 | break; |
fc57b6aa | 473 | } |
45c17412 DB |
474 | |
475 | while (slot != NULL) { | |
476 | if (!slot->in_use && slot->curl != NULL) { | |
477 | curl_easy_cleanup(slot->curl); | |
478 | slot->curl = NULL; | |
479 | } | |
480 | slot = slot->next; | |
481 | } | |
482 | } | |
483 | ||
29508e1e NH |
484 | void step_active_slots(void) |
485 | { | |
486 | int num_transfers; | |
487 | CURLMcode curlm_result; | |
488 | ||
489 | do { | |
490 | curlm_result = curl_multi_perform(curlm, &num_transfers); | |
491 | } while (curlm_result == CURLM_CALL_MULTI_PERFORM); | |
492 | if (num_transfers < active_requests) { | |
493 | process_curl_messages(); | |
494 | fill_active_slots(); | |
495 | } | |
496 | } | |
497 | #endif | |
498 | ||
499 | void run_active_slot(struct active_request_slot *slot) | |
500 | { | |
501 | #ifdef USE_CURL_MULTI | |
502 | long last_pos = 0; | |
503 | long current_pos; | |
504 | fd_set readfds; | |
505 | fd_set writefds; | |
506 | fd_set excfds; | |
507 | int max_fd; | |
508 | struct timeval select_timeout; | |
baa7b67d | 509 | int finished = 0; |
29508e1e | 510 | |
baa7b67d NH |
511 | slot->finished = &finished; |
512 | while (!finished) { | |
29508e1e NH |
513 | data_received = 0; |
514 | step_active_slots(); | |
515 | ||
516 | if (!data_received && slot->local != NULL) { | |
517 | current_pos = ftell(slot->local); | |
518 | if (current_pos > last_pos) | |
519 | data_received++; | |
520 | last_pos = current_pos; | |
521 | } | |
522 | ||
523 | if (slot->in_use && !data_received) { | |
524 | max_fd = 0; | |
525 | FD_ZERO(&readfds); | |
526 | FD_ZERO(&writefds); | |
527 | FD_ZERO(&excfds); | |
528 | select_timeout.tv_sec = 0; | |
529 | select_timeout.tv_usec = 50000; | |
530 | select(max_fd, &readfds, &writefds, | |
531 | &excfds, &select_timeout); | |
532 | } | |
533 | } | |
534 | #else | |
535 | while (slot->in_use) { | |
536 | slot->curl_result = curl_easy_perform(slot->curl); | |
537 | finish_active_slot(slot); | |
538 | } | |
539 | #endif | |
540 | } | |
541 | ||
53f31389 | 542 | static void closedown_active_slot(struct active_request_slot *slot) |
29508e1e | 543 | { |
028c2976 MH |
544 | active_requests--; |
545 | slot->in_use = 0; | |
53f31389 MW |
546 | } |
547 | ||
548 | void release_active_slot(struct active_request_slot *slot) | |
549 | { | |
550 | closedown_active_slot(slot); | |
551 | if (slot->curl) { | |
b3ca4e4e | 552 | #ifdef USE_CURL_MULTI |
53f31389 | 553 | curl_multi_remove_handle(curlm, slot->curl); |
b3ca4e4e | 554 | #endif |
53f31389 MW |
555 | curl_easy_cleanup(slot->curl); |
556 | slot->curl = NULL; | |
557 | } | |
b3ca4e4e | 558 | #ifdef USE_CURL_MULTI |
53f31389 | 559 | fill_active_slots(); |
b3ca4e4e | 560 | #endif |
53f31389 MW |
561 | } |
562 | ||
563 | static void finish_active_slot(struct active_request_slot *slot) | |
564 | { | |
565 | closedown_active_slot(slot); | |
028c2976 | 566 | curl_easy_getinfo(slot->curl, CURLINFO_HTTP_CODE, &slot->http_code); |
c8568e13 | 567 | |
baa7b67d NH |
568 | if (slot->finished != NULL) |
569 | (*slot->finished) = 1; | |
570 | ||
c8568e13 NH |
571 | /* Store slot results so they can be read after the slot is reused */ |
572 | if (slot->results != NULL) { | |
573 | slot->results->curl_result = slot->curl_result; | |
574 | slot->results->http_code = slot->http_code; | |
575 | } | |
576 | ||
028c2976 | 577 | /* Run callback if appropriate */ |
4251ccbd | 578 | if (slot->callback_func != NULL) |
028c2976 | 579 | slot->callback_func(slot->callback_data); |
29508e1e NH |
580 | } |
581 | ||
582 | void finish_all_active_slots(void) | |
583 | { | |
584 | struct active_request_slot *slot = active_queue_head; | |
585 | ||
586 | while (slot != NULL) | |
587 | if (slot->in_use) { | |
588 | run_active_slot(slot); | |
589 | slot = active_queue_head; | |
590 | } else { | |
591 | slot = slot->next; | |
592 | } | |
593 | } | |
d7e92806 MH |
594 | |
595 | static inline int needs_quote(int ch) | |
596 | { | |
597 | if (((ch >= 'A') && (ch <= 'Z')) | |
598 | || ((ch >= 'a') && (ch <= 'z')) | |
599 | || ((ch >= '0') && (ch <= '9')) | |
600 | || (ch == '/') | |
601 | || (ch == '-') | |
602 | || (ch == '.')) | |
603 | return 0; | |
604 | return 1; | |
605 | } | |
606 | ||
607 | static inline int hex(int v) | |
608 | { | |
4251ccbd JH |
609 | if (v < 10) |
610 | return '0' + v; | |
611 | else | |
612 | return 'A' + v - 10; | |
d7e92806 MH |
613 | } |
614 | ||
615 | static char *quote_ref_url(const char *base, const char *ref) | |
616 | { | |
113106e0 | 617 | struct strbuf buf = STRBUF_INIT; |
d7e92806 | 618 | const char *cp; |
113106e0 | 619 | int ch; |
d7e92806 | 620 | |
113106e0 TRC |
621 | strbuf_addstr(&buf, base); |
622 | if (buf.len && buf.buf[buf.len - 1] != '/' && *ref != '/') | |
623 | strbuf_addstr(&buf, "/"); | |
624 | ||
625 | for (cp = ref; (ch = *cp) != 0; cp++) | |
d7e92806 | 626 | if (needs_quote(ch)) |
113106e0 | 627 | strbuf_addf(&buf, "%%%02x", ch); |
d7e92806 | 628 | else |
113106e0 | 629 | strbuf_addch(&buf, *cp); |
d7e92806 | 630 | |
113106e0 | 631 | return strbuf_detach(&buf, NULL); |
d7e92806 MH |
632 | } |
633 | ||
c13b2633 | 634 | int http_fetch_ref(const char *base, struct ref *ref) |
d7e92806 MH |
635 | { |
636 | char *url; | |
637 | struct strbuf buffer = STRBUF_INIT; | |
638 | struct active_request_slot *slot; | |
639 | struct slot_results results; | |
640 | int ret; | |
641 | ||
c13b2633 | 642 | url = quote_ref_url(base, ref->name); |
d7e92806 MH |
643 | slot = get_active_slot(); |
644 | slot->results = &results; | |
645 | curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer); | |
646 | curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer); | |
647 | curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, NULL); | |
648 | curl_easy_setopt(slot->curl, CURLOPT_URL, url); | |
649 | if (start_active_slot(slot)) { | |
650 | run_active_slot(slot); | |
651 | if (results.curl_result == CURLE_OK) { | |
652 | strbuf_rtrim(&buffer); | |
653 | if (buffer.len == 40) | |
c13b2633 | 654 | ret = get_sha1_hex(buffer.buf, ref->old_sha1); |
be885d96 DB |
655 | else if (!prefixcmp(buffer.buf, "ref: ")) { |
656 | ref->symref = xstrdup(buffer.buf + 5); | |
657 | ret = 0; | |
658 | } else | |
d7e92806 MH |
659 | ret = 1; |
660 | } else { | |
661 | ret = error("Couldn't get %s for %s\n%s", | |
c13b2633 | 662 | url, ref->name, curl_errorstr); |
d7e92806 MH |
663 | } |
664 | } else { | |
665 | ret = error("Unable to start request"); | |
666 | } | |
667 | ||
668 | strbuf_release(&buffer); | |
669 | free(url); | |
670 | return ret; | |
671 | } |