Revert "MinGW: Add missing file mode bit defines"
[git/dscho.git] / http.c
bloba588895fcb3091a9c2cc5c4b024063b660506438
1 #include "http.h"
2 #include "pack.h"
3 #include "sideband.h"
4 #include "run-command.h"
5 #include "url.h"
6 #include "exec_cmd.h"
8 int data_received;
9 int active_requests;
10 int http_is_verbose;
11 size_t http_post_buffer = 16 * LARGE_PACKET_MAX;
13 #if LIBCURL_VERSION_NUM >= 0x070a06
14 #define LIBCURL_CAN_HANDLE_AUTH_ANY
15 #endif
17 static int min_curl_sessions = 1;
18 static int curl_session_count;
19 #ifdef USE_CURL_MULTI
20 static int max_requests = -1;
21 static CURLM *curlm;
22 #endif
23 #ifndef NO_CURL_EASY_DUPHANDLE
24 static CURL *curl_default;
25 #endif
27 #define PREV_BUF_SIZE 4096
28 #define RANGE_HEADER_SIZE 30
30 char curl_errorstr[CURL_ERROR_SIZE];
32 static int curl_ssl_verify = -1;
33 static const char *ssl_cert;
34 #if LIBCURL_VERSION_NUM >= 0x070903
35 static const char *ssl_key;
36 #endif
37 #if LIBCURL_VERSION_NUM >= 0x070908
38 static const char *ssl_capath;
39 #endif
40 static const char *ssl_cainfo;
41 static long curl_low_speed_limit = -1;
42 static long curl_low_speed_time = -1;
43 static int curl_ftp_no_epsv;
44 static const char *curl_http_proxy;
45 static const char *curl_cookie_file;
46 static char *user_name, *user_pass, *description;
47 static const char *user_agent;
49 #if LIBCURL_VERSION_NUM >= 0x071700
50 /* Use CURLOPT_KEYPASSWD as is */
51 #elif LIBCURL_VERSION_NUM >= 0x070903
52 #define CURLOPT_KEYPASSWD CURLOPT_SSLKEYPASSWD
53 #else
54 #define CURLOPT_KEYPASSWD CURLOPT_SSLCERTPASSWD
55 #endif
57 static char *ssl_cert_password;
58 static int ssl_cert_password_required;
60 static struct curl_slist *pragma_header;
61 static struct curl_slist *no_pragma_header;
63 static struct active_request_slot *active_queue_head;
65 size_t fread_buffer(char *ptr, size_t eltsize, size_t nmemb, void *buffer_)
67 size_t size = eltsize * nmemb;
68 struct buffer *buffer = buffer_;
70 if (size > buffer->buf.len - buffer->posn)
71 size = buffer->buf.len - buffer->posn;
72 memcpy(ptr, buffer->buf.buf + buffer->posn, size);
73 buffer->posn += size;
75 return size;
78 #ifndef NO_CURL_IOCTL
79 curlioerr ioctl_buffer(CURL *handle, int cmd, void *clientp)
81 struct buffer *buffer = clientp;
83 switch (cmd) {
84 case CURLIOCMD_NOP:
85 return CURLIOE_OK;
87 case CURLIOCMD_RESTARTREAD:
88 buffer->posn = 0;
89 return CURLIOE_OK;
91 default:
92 return CURLIOE_UNKNOWNCMD;
95 #endif
97 size_t fwrite_buffer(char *ptr, size_t eltsize, size_t nmemb, void *buffer_)
99 size_t size = eltsize * nmemb;
100 struct strbuf *buffer = buffer_;
102 strbuf_add(buffer, ptr, size);
103 data_received++;
104 return size;
107 size_t fwrite_null(char *ptr, size_t eltsize, size_t nmemb, void *strbuf)
109 data_received++;
110 return eltsize * nmemb;
113 #ifdef USE_CURL_MULTI
114 static void process_curl_messages(void)
116 int num_messages;
117 struct active_request_slot *slot;
118 CURLMsg *curl_message = curl_multi_info_read(curlm, &num_messages);
120 while (curl_message != NULL) {
121 if (curl_message->msg == CURLMSG_DONE) {
122 int curl_result = curl_message->data.result;
123 slot = active_queue_head;
124 while (slot != NULL &&
125 slot->curl != curl_message->easy_handle)
126 slot = slot->next;
127 if (slot != NULL) {
128 curl_multi_remove_handle(curlm, slot->curl);
129 slot->curl_result = curl_result;
130 finish_active_slot(slot);
131 } else {
132 fprintf(stderr, "Received DONE message for unknown request!\n");
134 } else {
135 fprintf(stderr, "Unknown CURL message received: %d\n",
136 (int)curl_message->msg);
138 curl_message = curl_multi_info_read(curlm, &num_messages);
141 #endif
143 static char *git_getpass_with_description(const char *what, const char *desc)
145 struct strbuf prompt = STRBUF_INIT;
146 char *r;
148 if (desc)
149 strbuf_addf(&prompt, "%s for '%s': ", what, desc);
150 else
151 strbuf_addf(&prompt, "%s: ", what);
153 * NEEDSWORK: for usernames, we should do something less magical that
154 * actually echoes the characters. However, we need to read from
155 * /dev/tty and not stdio, which is not portable (but getpass will do
156 * it for us). http.c uses the same workaround.
158 r = git_getpass(prompt.buf);
160 strbuf_release(&prompt);
161 return xstrdup(r);
164 static int git_config_path(const char **result,
165 const char *var, const char *value)
167 if (git_config_string(result, var, value))
168 return 1;
169 #ifdef __MINGW32__
170 if (**result == '/')
171 *result = system_path((*result) + 1);
172 #endif
173 return 0;
176 static int http_options(const char *var, const char *value, void *cb)
178 if (!strcmp("http.sslverify", var)) {
179 curl_ssl_verify = git_config_bool(var, value);
180 return 0;
182 if (!strcmp("http.sslcert", var))
183 return git_config_path(&ssl_cert, var, value);
184 #if LIBCURL_VERSION_NUM >= 0x070903
185 if (!strcmp("http.sslkey", var))
186 return git_config_path(&ssl_key, var, value);
187 #endif
188 #if LIBCURL_VERSION_NUM >= 0x070908
189 if (!strcmp("http.sslcapath", var))
190 return git_config_path(&ssl_capath, var, value);
191 #endif
192 if (!strcmp("http.sslcainfo", var))
193 return git_config_path(&ssl_cainfo, var, value);
194 if (!strcmp("http.sslcertpasswordprotected", var)) {
195 if (git_config_bool(var, value))
196 ssl_cert_password_required = 1;
197 return 0;
199 if (!strcmp("http.minsessions", var)) {
200 min_curl_sessions = git_config_int(var, value);
201 #ifndef USE_CURL_MULTI
202 if (min_curl_sessions > 1)
203 min_curl_sessions = 1;
204 #endif
205 return 0;
207 #ifdef USE_CURL_MULTI
208 if (!strcmp("http.maxrequests", var)) {
209 max_requests = git_config_int(var, value);
210 return 0;
212 #endif
213 if (!strcmp("http.lowspeedlimit", var)) {
214 curl_low_speed_limit = (long)git_config_int(var, value);
215 return 0;
217 if (!strcmp("http.lowspeedtime", var)) {
218 curl_low_speed_time = (long)git_config_int(var, value);
219 return 0;
222 if (!strcmp("http.noepsv", var)) {
223 curl_ftp_no_epsv = git_config_bool(var, value);
224 return 0;
226 if (!strcmp("http.proxy", var))
227 return git_config_string(&curl_http_proxy, var, value);
229 if (!strcmp("http.cookiefile", var))
230 return git_config_string(&curl_cookie_file, var, value);
232 if (!strcmp("http.postbuffer", var)) {
233 http_post_buffer = git_config_int(var, value);
234 if (http_post_buffer < LARGE_PACKET_MAX)
235 http_post_buffer = LARGE_PACKET_MAX;
236 return 0;
239 if (!strcmp("http.useragent", var))
240 return git_config_string(&user_agent, var, value);
242 /* Fall back on the default ones */
243 return git_default_config(var, value, cb);
246 static void init_curl_http_auth(CURL *result)
248 if (user_name) {
249 struct strbuf up = STRBUF_INIT;
250 if (!user_pass)
251 user_pass = xstrdup(git_getpass_with_description("Password", description));
252 strbuf_addf(&up, "%s:%s", user_name, user_pass);
253 curl_easy_setopt(result, CURLOPT_USERPWD,
254 strbuf_detach(&up, NULL));
258 static int has_cert_password(void)
260 if (ssl_cert_password != NULL)
261 return 1;
262 if (ssl_cert == NULL || ssl_cert_password_required != 1)
263 return 0;
264 /* Only prompt the user once. */
265 ssl_cert_password_required = -1;
266 ssl_cert_password = git_getpass_with_description("Certificate Password", description);
267 if (ssl_cert_password != NULL) {
268 ssl_cert_password = xstrdup(ssl_cert_password);
269 return 1;
270 } else
271 return 0;
274 static CURL *get_curl_handle(void)
276 CURL *result = curl_easy_init();
278 if (!curl_ssl_verify) {
279 curl_easy_setopt(result, CURLOPT_SSL_VERIFYPEER, 0);
280 curl_easy_setopt(result, CURLOPT_SSL_VERIFYHOST, 0);
281 } else {
282 /* Verify authenticity of the peer's certificate */
283 curl_easy_setopt(result, CURLOPT_SSL_VERIFYPEER, 1);
284 /* The name in the cert must match whom we tried to connect */
285 curl_easy_setopt(result, CURLOPT_SSL_VERIFYHOST, 2);
288 #if LIBCURL_VERSION_NUM >= 0x070907
289 curl_easy_setopt(result, CURLOPT_NETRC, CURL_NETRC_OPTIONAL);
290 #endif
291 #ifdef LIBCURL_CAN_HANDLE_AUTH_ANY
292 curl_easy_setopt(result, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
293 #endif
295 init_curl_http_auth(result);
297 if (ssl_cert != NULL)
298 curl_easy_setopt(result, CURLOPT_SSLCERT, ssl_cert);
299 if (has_cert_password())
300 curl_easy_setopt(result, CURLOPT_KEYPASSWD, ssl_cert_password);
301 #if LIBCURL_VERSION_NUM >= 0x070903
302 if (ssl_key != NULL)
303 curl_easy_setopt(result, CURLOPT_SSLKEY, ssl_key);
304 #endif
305 #if LIBCURL_VERSION_NUM >= 0x070908
306 if (ssl_capath != NULL)
307 curl_easy_setopt(result, CURLOPT_CAPATH, ssl_capath);
308 #endif
309 if (ssl_cainfo != NULL)
310 curl_easy_setopt(result, CURLOPT_CAINFO, ssl_cainfo);
311 curl_easy_setopt(result, CURLOPT_FAILONERROR, 1);
313 if (curl_low_speed_limit > 0 && curl_low_speed_time > 0) {
314 curl_easy_setopt(result, CURLOPT_LOW_SPEED_LIMIT,
315 curl_low_speed_limit);
316 curl_easy_setopt(result, CURLOPT_LOW_SPEED_TIME,
317 curl_low_speed_time);
320 curl_easy_setopt(result, CURLOPT_FOLLOWLOCATION, 1);
321 #if LIBCURL_VERSION_NUM >= 0x071301
322 curl_easy_setopt(result, CURLOPT_POSTREDIR, CURL_REDIR_POST_ALL);
323 #elif LIBCURL_VERSION_NUM >= 0x071101
324 curl_easy_setopt(result, CURLOPT_POST301, 1);
325 #endif
327 if (getenv("GIT_CURL_VERBOSE"))
328 curl_easy_setopt(result, CURLOPT_VERBOSE, 1);
330 curl_easy_setopt(result, CURLOPT_USERAGENT,
331 user_agent ? user_agent : GIT_HTTP_USER_AGENT);
333 if (curl_ftp_no_epsv)
334 curl_easy_setopt(result, CURLOPT_FTP_USE_EPSV, 0);
336 if (curl_http_proxy)
337 curl_easy_setopt(result, CURLOPT_PROXY, curl_http_proxy);
339 return result;
342 static void http_auth_init(const char *url)
344 const char *at, *colon, *cp, *slash, *host;
346 cp = strstr(url, "://");
347 if (!cp)
348 return;
351 * Ok, the URL looks like "proto://something". Which one?
352 * "proto://<user>:<pass>@<host>/...",
353 * "proto://<user>@<host>/...", or just
354 * "proto://<host>/..."?
356 cp += 3;
357 at = strchr(cp, '@');
358 colon = strchr(cp, ':');
359 slash = strchrnul(cp, '/');
360 if (!at || slash <= at) {
361 /* No credentials, but we may have to ask for some later */
362 host = cp;
364 else if (!colon || at <= colon) {
365 /* Only username */
366 user_name = url_decode_mem(cp, at - cp);
367 user_pass = NULL;
368 host = at + 1;
369 } else {
370 user_name = url_decode_mem(cp, colon - cp);
371 user_pass = url_decode_mem(colon + 1, at - (colon + 1));
372 host = at + 1;
375 description = url_decode_mem(host, slash - host);
378 static void set_from_env(const char **var, const char *envname)
380 const char *val = getenv(envname);
381 if (val)
382 *var = val;
385 void http_init(struct remote *remote, const char *url)
387 char *low_speed_limit;
388 char *low_speed_time;
390 http_is_verbose = 0;
392 git_config(http_options, NULL);
394 curl_global_init(CURL_GLOBAL_ALL);
396 if (remote && remote->http_proxy)
397 curl_http_proxy = xstrdup(remote->http_proxy);
399 pragma_header = curl_slist_append(pragma_header, "Pragma: no-cache");
400 no_pragma_header = curl_slist_append(no_pragma_header, "Pragma:");
402 #ifdef USE_CURL_MULTI
404 char *http_max_requests = getenv("GIT_HTTP_MAX_REQUESTS");
405 if (http_max_requests != NULL)
406 max_requests = atoi(http_max_requests);
409 curlm = curl_multi_init();
410 if (curlm == NULL) {
411 fprintf(stderr, "Error creating curl multi handle.\n");
412 exit(1);
414 #endif
416 if (getenv("GIT_SSL_NO_VERIFY"))
417 curl_ssl_verify = 0;
419 set_from_env(&ssl_cert, "GIT_SSL_CERT");
420 #if LIBCURL_VERSION_NUM >= 0x070903
421 set_from_env(&ssl_key, "GIT_SSL_KEY");
422 #endif
423 #if LIBCURL_VERSION_NUM >= 0x070908
424 set_from_env(&ssl_capath, "GIT_SSL_CAPATH");
425 #endif
426 set_from_env(&ssl_cainfo, "GIT_SSL_CAINFO");
428 set_from_env(&user_agent, "GIT_HTTP_USER_AGENT");
430 low_speed_limit = getenv("GIT_HTTP_LOW_SPEED_LIMIT");
431 if (low_speed_limit != NULL)
432 curl_low_speed_limit = strtol(low_speed_limit, NULL, 10);
433 low_speed_time = getenv("GIT_HTTP_LOW_SPEED_TIME");
434 if (low_speed_time != NULL)
435 curl_low_speed_time = strtol(low_speed_time, NULL, 10);
437 if (curl_ssl_verify == -1)
438 curl_ssl_verify = 1;
440 curl_session_count = 0;
441 #ifdef USE_CURL_MULTI
442 if (max_requests < 1)
443 max_requests = DEFAULT_MAX_REQUESTS;
444 #endif
446 if (getenv("GIT_CURL_FTP_NO_EPSV"))
447 curl_ftp_no_epsv = 1;
449 if (url) {
450 http_auth_init(url);
451 if (!ssl_cert_password_required &&
452 getenv("GIT_SSL_CERT_PASSWORD_PROTECTED") &&
453 !prefixcmp(url, "https://"))
454 ssl_cert_password_required = 1;
457 #ifndef NO_CURL_EASY_DUPHANDLE
458 curl_default = get_curl_handle();
459 #endif
462 void http_cleanup(void)
464 struct active_request_slot *slot = active_queue_head;
466 while (slot != NULL) {
467 struct active_request_slot *next = slot->next;
468 if (slot->curl != NULL) {
469 #ifdef USE_CURL_MULTI
470 curl_multi_remove_handle(curlm, slot->curl);
471 #endif
472 curl_easy_cleanup(slot->curl);
474 free(slot);
475 slot = next;
477 active_queue_head = NULL;
479 #ifndef NO_CURL_EASY_DUPHANDLE
480 curl_easy_cleanup(curl_default);
481 #endif
483 #ifdef USE_CURL_MULTI
484 curl_multi_cleanup(curlm);
485 #endif
486 curl_global_cleanup();
488 curl_slist_free_all(pragma_header);
489 pragma_header = NULL;
491 curl_slist_free_all(no_pragma_header);
492 no_pragma_header = NULL;
494 if (curl_http_proxy) {
495 free((void *)curl_http_proxy);
496 curl_http_proxy = NULL;
499 if (ssl_cert_password != NULL) {
500 memset(ssl_cert_password, 0, strlen(ssl_cert_password));
501 free(ssl_cert_password);
502 ssl_cert_password = NULL;
504 ssl_cert_password_required = 0;
507 struct active_request_slot *get_active_slot(void)
509 struct active_request_slot *slot = active_queue_head;
510 struct active_request_slot *newslot;
512 #ifdef USE_CURL_MULTI
513 int num_transfers;
515 /* Wait for a slot to open up if the queue is full */
516 while (active_requests >= max_requests) {
517 curl_multi_perform(curlm, &num_transfers);
518 if (num_transfers < active_requests)
519 process_curl_messages();
521 #endif
523 while (slot != NULL && slot->in_use)
524 slot = slot->next;
526 if (slot == NULL) {
527 newslot = xmalloc(sizeof(*newslot));
528 newslot->curl = NULL;
529 newslot->in_use = 0;
530 newslot->next = NULL;
532 slot = active_queue_head;
533 if (slot == NULL) {
534 active_queue_head = newslot;
535 } else {
536 while (slot->next != NULL)
537 slot = slot->next;
538 slot->next = newslot;
540 slot = newslot;
543 if (slot->curl == NULL) {
544 #ifdef NO_CURL_EASY_DUPHANDLE
545 slot->curl = get_curl_handle();
546 #else
547 slot->curl = curl_easy_duphandle(curl_default);
548 #endif
549 curl_session_count++;
552 active_requests++;
553 slot->in_use = 1;
554 slot->local = NULL;
555 slot->results = NULL;
556 slot->finished = NULL;
557 slot->callback_data = NULL;
558 slot->callback_func = NULL;
559 curl_easy_setopt(slot->curl, CURLOPT_COOKIEFILE, curl_cookie_file);
560 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, pragma_header);
561 curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, curl_errorstr);
562 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, NULL);
563 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, NULL);
564 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, NULL);
565 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, NULL);
566 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 0);
567 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
569 return slot;
572 int start_active_slot(struct active_request_slot *slot)
574 #ifdef USE_CURL_MULTI
575 CURLMcode curlm_result = curl_multi_add_handle(curlm, slot->curl);
576 int num_transfers;
578 if (curlm_result != CURLM_OK &&
579 curlm_result != CURLM_CALL_MULTI_PERFORM) {
580 active_requests--;
581 slot->in_use = 0;
582 return 0;
586 * We know there must be something to do, since we just added
587 * something.
589 curl_multi_perform(curlm, &num_transfers);
590 #endif
591 return 1;
594 #ifdef USE_CURL_MULTI
595 struct fill_chain {
596 void *data;
597 int (*fill)(void *);
598 struct fill_chain *next;
601 static struct fill_chain *fill_cfg;
603 void add_fill_function(void *data, int (*fill)(void *))
605 struct fill_chain *new = xmalloc(sizeof(*new));
606 struct fill_chain **linkp = &fill_cfg;
607 new->data = data;
608 new->fill = fill;
609 new->next = NULL;
610 while (*linkp)
611 linkp = &(*linkp)->next;
612 *linkp = new;
615 void fill_active_slots(void)
617 struct active_request_slot *slot = active_queue_head;
619 while (active_requests < max_requests) {
620 struct fill_chain *fill;
621 for (fill = fill_cfg; fill; fill = fill->next)
622 if (fill->fill(fill->data))
623 break;
625 if (!fill)
626 break;
629 while (slot != NULL) {
630 if (!slot->in_use && slot->curl != NULL
631 && curl_session_count > min_curl_sessions) {
632 curl_easy_cleanup(slot->curl);
633 slot->curl = NULL;
634 curl_session_count--;
636 slot = slot->next;
640 void step_active_slots(void)
642 int num_transfers;
643 CURLMcode curlm_result;
645 do {
646 curlm_result = curl_multi_perform(curlm, &num_transfers);
647 } while (curlm_result == CURLM_CALL_MULTI_PERFORM);
648 if (num_transfers < active_requests) {
649 process_curl_messages();
650 fill_active_slots();
653 #endif
655 void run_active_slot(struct active_request_slot *slot)
657 #ifdef USE_CURL_MULTI
658 long last_pos = 0;
659 long current_pos;
660 fd_set readfds;
661 fd_set writefds;
662 fd_set excfds;
663 int max_fd;
664 struct timeval select_timeout;
665 int finished = 0;
667 slot->finished = &finished;
668 while (!finished) {
669 data_received = 0;
670 step_active_slots();
672 if (!data_received && slot->local != NULL) {
673 current_pos = ftell(slot->local);
674 if (current_pos > last_pos)
675 data_received++;
676 last_pos = current_pos;
679 if (slot->in_use && !data_received) {
680 max_fd = 0;
681 FD_ZERO(&readfds);
682 FD_ZERO(&writefds);
683 FD_ZERO(&excfds);
684 select_timeout.tv_sec = 0;
685 select_timeout.tv_usec = 50000;
686 select(max_fd, &readfds, &writefds,
687 &excfds, &select_timeout);
690 #else
691 while (slot->in_use) {
692 slot->curl_result = curl_easy_perform(slot->curl);
693 finish_active_slot(slot);
695 #endif
698 static void closedown_active_slot(struct active_request_slot *slot)
700 active_requests--;
701 slot->in_use = 0;
704 static void release_active_slot(struct active_request_slot *slot)
706 closedown_active_slot(slot);
707 if (slot->curl && curl_session_count > min_curl_sessions) {
708 #ifdef USE_CURL_MULTI
709 curl_multi_remove_handle(curlm, slot->curl);
710 #endif
711 curl_easy_cleanup(slot->curl);
712 slot->curl = NULL;
713 curl_session_count--;
715 #ifdef USE_CURL_MULTI
716 fill_active_slots();
717 #endif
720 void finish_active_slot(struct active_request_slot *slot)
722 closedown_active_slot(slot);
723 curl_easy_getinfo(slot->curl, CURLINFO_HTTP_CODE, &slot->http_code);
725 if (slot->finished != NULL)
726 (*slot->finished) = 1;
728 /* Store slot results so they can be read after the slot is reused */
729 if (slot->results != NULL) {
730 slot->results->curl_result = slot->curl_result;
731 slot->results->http_code = slot->http_code;
734 /* Run callback if appropriate */
735 if (slot->callback_func != NULL)
736 slot->callback_func(slot->callback_data);
739 void finish_all_active_slots(void)
741 struct active_request_slot *slot = active_queue_head;
743 while (slot != NULL)
744 if (slot->in_use) {
745 run_active_slot(slot);
746 slot = active_queue_head;
747 } else {
748 slot = slot->next;
752 /* Helpers for modifying and creating URLs */
753 static inline int needs_quote(int ch)
755 if (((ch >= 'A') && (ch <= 'Z'))
756 || ((ch >= 'a') && (ch <= 'z'))
757 || ((ch >= '0') && (ch <= '9'))
758 || (ch == '/')
759 || (ch == '-')
760 || (ch == '.'))
761 return 0;
762 return 1;
765 static inline int hex(int v)
767 if (v < 10)
768 return '0' + v;
769 else
770 return 'A' + v - 10;
773 static char *quote_ref_url(const char *base, const char *ref)
775 struct strbuf buf = STRBUF_INIT;
776 const char *cp;
777 int ch;
779 end_url_with_slash(&buf, base);
781 for (cp = ref; (ch = *cp) != 0; cp++)
782 if (needs_quote(ch))
783 strbuf_addf(&buf, "%%%02x", ch);
784 else
785 strbuf_addch(&buf, *cp);
787 return strbuf_detach(&buf, NULL);
790 void append_remote_object_url(struct strbuf *buf, const char *url,
791 const char *hex,
792 int only_two_digit_prefix)
794 end_url_with_slash(buf, url);
796 strbuf_addf(buf, "objects/%.*s/", 2, hex);
797 if (!only_two_digit_prefix)
798 strbuf_addf(buf, "%s", hex+2);
801 char *get_remote_object_url(const char *url, const char *hex,
802 int only_two_digit_prefix)
804 struct strbuf buf = STRBUF_INIT;
805 append_remote_object_url(&buf, url, hex, only_two_digit_prefix);
806 return strbuf_detach(&buf, NULL);
809 /* http_request() targets */
810 #define HTTP_REQUEST_STRBUF 0
811 #define HTTP_REQUEST_FILE 1
813 static int http_request(const char *url, void *result, int target, int options)
815 struct active_request_slot *slot;
816 struct slot_results results;
817 struct curl_slist *headers = NULL;
818 struct strbuf buf = STRBUF_INIT;
819 int ret;
821 slot = get_active_slot();
822 slot->results = &results;
823 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
825 if (result == NULL) {
826 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 1);
827 } else {
828 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
829 curl_easy_setopt(slot->curl, CURLOPT_FILE, result);
831 if (target == HTTP_REQUEST_FILE) {
832 long posn = ftell(result);
833 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION,
834 fwrite);
835 if (posn > 0) {
836 strbuf_addf(&buf, "Range: bytes=%ld-", posn);
837 headers = curl_slist_append(headers, buf.buf);
838 strbuf_reset(&buf);
840 slot->local = result;
841 } else
842 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION,
843 fwrite_buffer);
846 strbuf_addstr(&buf, "Pragma:");
847 if (options & HTTP_NO_CACHE)
848 strbuf_addstr(&buf, " no-cache");
850 headers = curl_slist_append(headers, buf.buf);
852 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
853 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
855 if (start_active_slot(slot)) {
856 run_active_slot(slot);
857 if (results.curl_result == CURLE_OK)
858 ret = HTTP_OK;
859 else if (missing_target(&results))
860 ret = HTTP_MISSING_TARGET;
861 else if (results.http_code == 401) {
862 if (user_name) {
863 ret = HTTP_NOAUTH;
864 } else {
866 * git_getpass is needed here because its very likely stdin/stdout are
867 * pipes to our parent process. So we instead need to use /dev/tty,
868 * but that is non-portable. Using git_getpass() can at least be stubbed
869 * on other platforms with a different implementation if/when necessary.
871 user_name = xstrdup(git_getpass_with_description("Username", description));
872 init_curl_http_auth(slot->curl);
873 ret = HTTP_REAUTH;
875 } else {
876 if (!curl_errorstr[0])
877 strlcpy(curl_errorstr,
878 curl_easy_strerror(results.curl_result),
879 sizeof(curl_errorstr));
880 ret = HTTP_ERROR;
882 } else {
883 error("Unable to start HTTP request for %s", url);
884 ret = HTTP_START_FAILED;
887 slot->local = NULL;
888 curl_slist_free_all(headers);
889 strbuf_release(&buf);
891 return ret;
894 static int http_request_reauth(const char *url, void *result, int target,
895 int options)
897 int ret = http_request(url, result, target, options);
898 if (ret != HTTP_REAUTH)
899 return ret;
900 return http_request(url, result, target, options);
903 int http_get_strbuf(const char *url, struct strbuf *result, int options)
905 return http_request_reauth(url, result, HTTP_REQUEST_STRBUF, options);
909 * Downloads an url and stores the result in the given file.
911 * If a previous interrupted download is detected (i.e. a previous temporary
912 * file is still around) the download is resumed.
914 static int http_get_file(const char *url, const char *filename, int options)
916 int ret;
917 struct strbuf tmpfile = STRBUF_INIT;
918 FILE *result;
920 strbuf_addf(&tmpfile, "%s.temp", filename);
921 result = fopen(tmpfile.buf, "a");
922 if (! result) {
923 error("Unable to open local file %s", tmpfile.buf);
924 ret = HTTP_ERROR;
925 goto cleanup;
928 ret = http_request_reauth(url, result, HTTP_REQUEST_FILE, options);
929 fclose(result);
931 if ((ret == HTTP_OK) && move_temp_to_file(tmpfile.buf, filename))
932 ret = HTTP_ERROR;
933 cleanup:
934 strbuf_release(&tmpfile);
935 return ret;
938 int http_error(const char *url, int ret)
940 /* http_request has already handled HTTP_START_FAILED. */
941 if (ret != HTTP_START_FAILED)
942 error("%s while accessing %s", curl_errorstr, url);
944 return ret;
947 int http_fetch_ref(const char *base, struct ref *ref)
949 char *url;
950 struct strbuf buffer = STRBUF_INIT;
951 int ret = -1;
953 url = quote_ref_url(base, ref->name);
954 if (http_get_strbuf(url, &buffer, HTTP_NO_CACHE) == HTTP_OK) {
955 strbuf_rtrim(&buffer);
956 if (buffer.len == 40)
957 ret = get_sha1_hex(buffer.buf, ref->old_sha1);
958 else if (!prefixcmp(buffer.buf, "ref: ")) {
959 ref->symref = xstrdup(buffer.buf + 5);
960 ret = 0;
964 strbuf_release(&buffer);
965 free(url);
966 return ret;
969 /* Helpers for fetching packs */
970 static char *fetch_pack_index(unsigned char *sha1, const char *base_url)
972 char *url, *tmp;
973 struct strbuf buf = STRBUF_INIT;
975 if (http_is_verbose)
976 fprintf(stderr, "Getting index for pack %s\n", sha1_to_hex(sha1));
978 end_url_with_slash(&buf, base_url);
979 strbuf_addf(&buf, "objects/pack/pack-%s.idx", sha1_to_hex(sha1));
980 url = strbuf_detach(&buf, NULL);
982 strbuf_addf(&buf, "%s.temp", sha1_pack_index_name(sha1));
983 tmp = strbuf_detach(&buf, NULL);
985 if (http_get_file(url, tmp, 0) != HTTP_OK) {
986 error("Unable to get pack index %s\n", url);
987 free(tmp);
988 tmp = NULL;
991 free(url);
992 return tmp;
995 static int fetch_and_setup_pack_index(struct packed_git **packs_head,
996 unsigned char *sha1, const char *base_url)
998 struct packed_git *new_pack;
999 char *tmp_idx = NULL;
1000 int ret;
1002 if (has_pack_index(sha1)) {
1003 new_pack = parse_pack_index(sha1, NULL);
1004 if (!new_pack)
1005 return -1; /* parse_pack_index() already issued error message */
1006 goto add_pack;
1009 tmp_idx = fetch_pack_index(sha1, base_url);
1010 if (!tmp_idx)
1011 return -1;
1013 new_pack = parse_pack_index(sha1, tmp_idx);
1014 if (!new_pack) {
1015 unlink(tmp_idx);
1016 free(tmp_idx);
1018 return -1; /* parse_pack_index() already issued error message */
1021 ret = verify_pack_index(new_pack);
1022 if (!ret) {
1023 close_pack_index(new_pack);
1024 ret = move_temp_to_file(tmp_idx, sha1_pack_index_name(sha1));
1026 free(tmp_idx);
1027 if (ret)
1028 return -1;
1030 add_pack:
1031 new_pack->next = *packs_head;
1032 *packs_head = new_pack;
1033 return 0;
1036 int http_get_info_packs(const char *base_url, struct packed_git **packs_head)
1038 int ret = 0, i = 0;
1039 char *url, *data;
1040 struct strbuf buf = STRBUF_INIT;
1041 unsigned char sha1[20];
1043 end_url_with_slash(&buf, base_url);
1044 strbuf_addstr(&buf, "objects/info/packs");
1045 url = strbuf_detach(&buf, NULL);
1047 ret = http_get_strbuf(url, &buf, HTTP_NO_CACHE);
1048 if (ret != HTTP_OK)
1049 goto cleanup;
1051 data = buf.buf;
1052 while (i < buf.len) {
1053 switch (data[i]) {
1054 case 'P':
1055 i++;
1056 if (i + 52 <= buf.len &&
1057 !prefixcmp(data + i, " pack-") &&
1058 !prefixcmp(data + i + 46, ".pack\n")) {
1059 get_sha1_hex(data + i + 6, sha1);
1060 fetch_and_setup_pack_index(packs_head, sha1,
1061 base_url);
1062 i += 51;
1063 break;
1065 default:
1066 while (i < buf.len && data[i] != '\n')
1067 i++;
1069 i++;
1072 cleanup:
1073 free(url);
1074 return ret;
1077 void release_http_pack_request(struct http_pack_request *preq)
1079 if (preq->packfile != NULL) {
1080 fclose(preq->packfile);
1081 preq->packfile = NULL;
1082 preq->slot->local = NULL;
1084 if (preq->range_header != NULL) {
1085 curl_slist_free_all(preq->range_header);
1086 preq->range_header = NULL;
1088 preq->slot = NULL;
1089 free(preq->url);
1092 int finish_http_pack_request(struct http_pack_request *preq)
1094 struct packed_git **lst;
1095 struct packed_git *p = preq->target;
1096 char *tmp_idx;
1097 struct child_process ip;
1098 const char *ip_argv[8];
1100 close_pack_index(p);
1102 fclose(preq->packfile);
1103 preq->packfile = NULL;
1104 preq->slot->local = NULL;
1106 lst = preq->lst;
1107 while (*lst != p)
1108 lst = &((*lst)->next);
1109 *lst = (*lst)->next;
1111 tmp_idx = xstrdup(preq->tmpfile);
1112 strcpy(tmp_idx + strlen(tmp_idx) - strlen(".pack.temp"),
1113 ".idx.temp");
1115 ip_argv[0] = "index-pack";
1116 ip_argv[1] = "-o";
1117 ip_argv[2] = tmp_idx;
1118 ip_argv[3] = preq->tmpfile;
1119 ip_argv[4] = NULL;
1121 memset(&ip, 0, sizeof(ip));
1122 ip.argv = ip_argv;
1123 ip.git_cmd = 1;
1124 ip.no_stdin = 1;
1125 ip.no_stdout = 1;
1127 if (run_command(&ip)) {
1128 unlink(preq->tmpfile);
1129 unlink(tmp_idx);
1130 free(tmp_idx);
1131 return -1;
1134 unlink(sha1_pack_index_name(p->sha1));
1136 if (move_temp_to_file(preq->tmpfile, sha1_pack_name(p->sha1))
1137 || move_temp_to_file(tmp_idx, sha1_pack_index_name(p->sha1))) {
1138 free(tmp_idx);
1139 return -1;
1142 install_packed_git(p);
1143 free(tmp_idx);
1144 return 0;
1147 struct http_pack_request *new_http_pack_request(
1148 struct packed_git *target, const char *base_url)
1150 long prev_posn = 0;
1151 char range[RANGE_HEADER_SIZE];
1152 struct strbuf buf = STRBUF_INIT;
1153 struct http_pack_request *preq;
1155 preq = xcalloc(1, sizeof(*preq));
1156 preq->target = target;
1158 end_url_with_slash(&buf, base_url);
1159 strbuf_addf(&buf, "objects/pack/pack-%s.pack",
1160 sha1_to_hex(target->sha1));
1161 preq->url = strbuf_detach(&buf, NULL);
1163 snprintf(preq->tmpfile, sizeof(preq->tmpfile), "%s.temp",
1164 sha1_pack_name(target->sha1));
1165 preq->packfile = fopen(preq->tmpfile, "a");
1166 if (!preq->packfile) {
1167 error("Unable to open local file %s for pack",
1168 preq->tmpfile);
1169 goto abort;
1172 preq->slot = get_active_slot();
1173 preq->slot->local = preq->packfile;
1174 curl_easy_setopt(preq->slot->curl, CURLOPT_FILE, preq->packfile);
1175 curl_easy_setopt(preq->slot->curl, CURLOPT_WRITEFUNCTION, fwrite);
1176 curl_easy_setopt(preq->slot->curl, CURLOPT_URL, preq->url);
1177 curl_easy_setopt(preq->slot->curl, CURLOPT_HTTPHEADER,
1178 no_pragma_header);
1181 * If there is data present from a previous transfer attempt,
1182 * resume where it left off
1184 prev_posn = ftell(preq->packfile);
1185 if (prev_posn>0) {
1186 if (http_is_verbose)
1187 fprintf(stderr,
1188 "Resuming fetch of pack %s at byte %ld\n",
1189 sha1_to_hex(target->sha1), prev_posn);
1190 sprintf(range, "Range: bytes=%ld-", prev_posn);
1191 preq->range_header = curl_slist_append(NULL, range);
1192 curl_easy_setopt(preq->slot->curl, CURLOPT_HTTPHEADER,
1193 preq->range_header);
1196 return preq;
1198 abort:
1199 free(preq->url);
1200 free(preq);
1201 return NULL;
1204 /* Helpers for fetching objects (loose) */
1205 static size_t fwrite_sha1_file(char *ptr, size_t eltsize, size_t nmemb,
1206 void *data)
1208 unsigned char expn[4096];
1209 size_t size = eltsize * nmemb;
1210 int posn = 0;
1211 struct http_object_request *freq =
1212 (struct http_object_request *)data;
1213 do {
1214 ssize_t retval = xwrite(freq->localfile,
1215 (char *) ptr + posn, size - posn);
1216 if (retval < 0)
1217 return posn;
1218 posn += retval;
1219 } while (posn < size);
1221 freq->stream.avail_in = size;
1222 freq->stream.next_in = (void *)ptr;
1223 do {
1224 freq->stream.next_out = expn;
1225 freq->stream.avail_out = sizeof(expn);
1226 freq->zret = git_inflate(&freq->stream, Z_SYNC_FLUSH);
1227 git_SHA1_Update(&freq->c, expn,
1228 sizeof(expn) - freq->stream.avail_out);
1229 } while (freq->stream.avail_in && freq->zret == Z_OK);
1230 data_received++;
1231 return size;
1234 struct http_object_request *new_http_object_request(const char *base_url,
1235 unsigned char *sha1)
1237 char *hex = sha1_to_hex(sha1);
1238 char *filename;
1239 char prevfile[PATH_MAX];
1240 int prevlocal;
1241 char prev_buf[PREV_BUF_SIZE];
1242 ssize_t prev_read = 0;
1243 long prev_posn = 0;
1244 char range[RANGE_HEADER_SIZE];
1245 struct curl_slist *range_header = NULL;
1246 struct http_object_request *freq;
1248 freq = xcalloc(1, sizeof(*freq));
1249 hashcpy(freq->sha1, sha1);
1250 freq->localfile = -1;
1252 filename = sha1_file_name(sha1);
1253 snprintf(freq->tmpfile, sizeof(freq->tmpfile),
1254 "%s.temp", filename);
1256 snprintf(prevfile, sizeof(prevfile), "%s.prev", filename);
1257 unlink_or_warn(prevfile);
1258 rename(freq->tmpfile, prevfile);
1259 unlink_or_warn(freq->tmpfile);
1261 if (freq->localfile != -1)
1262 error("fd leakage in start: %d", freq->localfile);
1263 freq->localfile = open(freq->tmpfile,
1264 O_WRONLY | O_CREAT | O_EXCL, 0666);
1266 * This could have failed due to the "lazy directory creation";
1267 * try to mkdir the last path component.
1269 if (freq->localfile < 0 && errno == ENOENT) {
1270 char *dir = strrchr(freq->tmpfile, '/');
1271 if (dir) {
1272 *dir = 0;
1273 mkdir(freq->tmpfile, 0777);
1274 *dir = '/';
1276 freq->localfile = open(freq->tmpfile,
1277 O_WRONLY | O_CREAT | O_EXCL, 0666);
1280 if (freq->localfile < 0) {
1281 error("Couldn't create temporary file %s: %s",
1282 freq->tmpfile, strerror(errno));
1283 goto abort;
1286 git_inflate_init(&freq->stream);
1288 git_SHA1_Init(&freq->c);
1290 freq->url = get_remote_object_url(base_url, hex, 0);
1293 * If a previous temp file is present, process what was already
1294 * fetched.
1296 prevlocal = open(prevfile, O_RDONLY);
1297 if (prevlocal != -1) {
1298 do {
1299 prev_read = xread(prevlocal, prev_buf, PREV_BUF_SIZE);
1300 if (prev_read>0) {
1301 if (fwrite_sha1_file(prev_buf,
1303 prev_read,
1304 freq) == prev_read) {
1305 prev_posn += prev_read;
1306 } else {
1307 prev_read = -1;
1310 } while (prev_read > 0);
1311 close(prevlocal);
1313 unlink_or_warn(prevfile);
1316 * Reset inflate/SHA1 if there was an error reading the previous temp
1317 * file; also rewind to the beginning of the local file.
1319 if (prev_read == -1) {
1320 memset(&freq->stream, 0, sizeof(freq->stream));
1321 git_inflate_init(&freq->stream);
1322 git_SHA1_Init(&freq->c);
1323 if (prev_posn>0) {
1324 prev_posn = 0;
1325 lseek(freq->localfile, 0, SEEK_SET);
1326 if (ftruncate(freq->localfile, 0) < 0) {
1327 error("Couldn't truncate temporary file %s: %s",
1328 freq->tmpfile, strerror(errno));
1329 goto abort;
1334 freq->slot = get_active_slot();
1336 curl_easy_setopt(freq->slot->curl, CURLOPT_FILE, freq);
1337 curl_easy_setopt(freq->slot->curl, CURLOPT_WRITEFUNCTION, fwrite_sha1_file);
1338 curl_easy_setopt(freq->slot->curl, CURLOPT_ERRORBUFFER, freq->errorstr);
1339 curl_easy_setopt(freq->slot->curl, CURLOPT_URL, freq->url);
1340 curl_easy_setopt(freq->slot->curl, CURLOPT_HTTPHEADER, no_pragma_header);
1343 * If we have successfully processed data from a previous fetch
1344 * attempt, only fetch the data we don't already have.
1346 if (prev_posn>0) {
1347 if (http_is_verbose)
1348 fprintf(stderr,
1349 "Resuming fetch of object %s at byte %ld\n",
1350 hex, prev_posn);
1351 sprintf(range, "Range: bytes=%ld-", prev_posn);
1352 range_header = curl_slist_append(range_header, range);
1353 curl_easy_setopt(freq->slot->curl,
1354 CURLOPT_HTTPHEADER, range_header);
1357 return freq;
1359 abort:
1360 free(freq->url);
1361 free(freq);
1362 return NULL;
1365 void process_http_object_request(struct http_object_request *freq)
1367 if (freq->slot == NULL)
1368 return;
1369 freq->curl_result = freq->slot->curl_result;
1370 freq->http_code = freq->slot->http_code;
1371 freq->slot = NULL;
1374 int finish_http_object_request(struct http_object_request *freq)
1376 struct stat st;
1378 close(freq->localfile);
1379 freq->localfile = -1;
1381 process_http_object_request(freq);
1383 if (freq->http_code == 416) {
1384 warning("requested range invalid; we may already have all the data.");
1385 } else if (freq->curl_result != CURLE_OK) {
1386 if (stat(freq->tmpfile, &st) == 0)
1387 if (st.st_size == 0)
1388 unlink_or_warn(freq->tmpfile);
1389 return -1;
1392 git_inflate_end(&freq->stream);
1393 git_SHA1_Final(freq->real_sha1, &freq->c);
1394 if (freq->zret != Z_STREAM_END) {
1395 unlink_or_warn(freq->tmpfile);
1396 return -1;
1398 if (hashcmp(freq->sha1, freq->real_sha1)) {
1399 unlink_or_warn(freq->tmpfile);
1400 return -1;
1402 freq->rename =
1403 move_temp_to_file(freq->tmpfile, sha1_file_name(freq->sha1));
1405 return freq->rename;
1408 void abort_http_object_request(struct http_object_request *freq)
1410 unlink_or_warn(freq->tmpfile);
1412 release_http_object_request(freq);
1415 void release_http_object_request(struct http_object_request *freq)
1417 if (freq->localfile != -1) {
1418 close(freq->localfile);
1419 freq->localfile = -1;
1421 if (freq->url != NULL) {
1422 free(freq->url);
1423 freq->url = NULL;
1425 if (freq->slot != NULL) {
1426 freq->slot->callback_func = NULL;
1427 freq->slot->callback_data = NULL;
1428 release_active_slot(freq->slot);
1429 freq->slot = NULL;