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