Fix username and password extraction from HTTP URLs
[git] / http.c
1 #include "http.h"
2 #include "pack.h"
3 #include "sideband.h"
4 #include "run-command.h"
5 #include "url.h"
6
7 int data_received;
8 int active_requests;
9 int http_is_verbose;
10 size_t http_post_buffer = 16 * LARGE_PACKET_MAX;
11
12 #if LIBCURL_VERSION_NUM >= 0x070a06
13 #define LIBCURL_CAN_HANDLE_AUTH_ANY
14 #endif
15
16 static int min_curl_sessions = 1;
17 static int curl_session_count;
18 #ifdef USE_CURL_MULTI
19 static int max_requests = -1;
20 static CURLM *curlm;
21 #endif
22 #ifndef NO_CURL_EASY_DUPHANDLE
23 static CURL *curl_default;
24 #endif
25
26 #define PREV_BUF_SIZE 4096
27 #define RANGE_HEADER_SIZE 30
28
29 char curl_errorstr[CURL_ERROR_SIZE];
30
31 static int curl_ssl_verify = -1;
32 static const char *ssl_cert;
33 #if LIBCURL_VERSION_NUM >= 0x070903
34 static const char *ssl_key;
35 #endif
36 #if LIBCURL_VERSION_NUM >= 0x070908
37 static const char *ssl_capath;
38 #endif
39 static const char *ssl_cainfo;
40 static long curl_low_speed_limit = -1;
41 static long curl_low_speed_time = -1;
42 static int curl_ftp_no_epsv;
43 static const char *curl_http_proxy;
44 static char *user_name, *user_pass;
45 static const char *user_agent;
46
47 #if LIBCURL_VERSION_NUM >= 0x071700
48 /* Use CURLOPT_KEYPASSWD as is */
49 #elif LIBCURL_VERSION_NUM >= 0x070903
50 #define CURLOPT_KEYPASSWD CURLOPT_SSLKEYPASSWD
51 #else
52 #define CURLOPT_KEYPASSWD CURLOPT_SSLCERTPASSWD
53 #endif
54
55 static char *ssl_cert_password;
56 static int ssl_cert_password_required;
57
58 static struct curl_slist *pragma_header;
59 static struct curl_slist *no_pragma_header;
60
61 static struct active_request_slot *active_queue_head;
62
63 size_t fread_buffer(void *ptr, size_t eltsize, size_t nmemb, void *buffer_)
64 {
65         size_t size = eltsize * nmemb;
66         struct buffer *buffer = buffer_;
67
68         if (size > buffer->buf.len - buffer->posn)
69                 size = buffer->buf.len - buffer->posn;
70         memcpy(ptr, buffer->buf.buf + buffer->posn, size);
71         buffer->posn += size;
72
73         return size;
74 }
75
76 #ifndef NO_CURL_IOCTL
77 curlioerr ioctl_buffer(CURL *handle, int cmd, void *clientp)
78 {
79         struct buffer *buffer = clientp;
80
81         switch (cmd) {
82         case CURLIOCMD_NOP:
83                 return CURLIOE_OK;
84
85         case CURLIOCMD_RESTARTREAD:
86                 buffer->posn = 0;
87                 return CURLIOE_OK;
88
89         default:
90                 return CURLIOE_UNKNOWNCMD;
91         }
92 }
93 #endif
94
95 size_t fwrite_buffer(const void *ptr, size_t eltsize, size_t nmemb, void *buffer_)
96 {
97         size_t size = eltsize * nmemb;
98         struct strbuf *buffer = buffer_;
99
100         strbuf_add(buffer, ptr, size);
101         data_received++;
102         return size;
103 }
104
105 size_t fwrite_null(const void *ptr, size_t eltsize, size_t nmemb, void *strbuf)
106 {
107         data_received++;
108         return eltsize * nmemb;
109 }
110
111 #ifdef USE_CURL_MULTI
112 static void process_curl_messages(void)
113 {
114         int num_messages;
115         struct active_request_slot *slot;
116         CURLMsg *curl_message = curl_multi_info_read(curlm, &num_messages);
117
118         while (curl_message != NULL) {
119                 if (curl_message->msg == CURLMSG_DONE) {
120                         int curl_result = curl_message->data.result;
121                         slot = active_queue_head;
122                         while (slot != NULL &&
123                                slot->curl != curl_message->easy_handle)
124                                 slot = slot->next;
125                         if (slot != NULL) {
126                                 curl_multi_remove_handle(curlm, slot->curl);
127                                 slot->curl_result = curl_result;
128                                 finish_active_slot(slot);
129                         } else {
130                                 fprintf(stderr, "Received DONE message for unknown request!\n");
131                         }
132                 } else {
133                         fprintf(stderr, "Unknown CURL message received: %d\n",
134                                 (int)curl_message->msg);
135                 }
136                 curl_message = curl_multi_info_read(curlm, &num_messages);
137         }
138 }
139 #endif
140
141 static int http_options(const char *var, const char *value, void *cb)
142 {
143         if (!strcmp("http.sslverify", var)) {
144                 curl_ssl_verify = git_config_bool(var, value);
145                 return 0;
146         }
147         if (!strcmp("http.sslcert", var))
148                 return git_config_string(&ssl_cert, var, value);
149 #if LIBCURL_VERSION_NUM >= 0x070903
150         if (!strcmp("http.sslkey", var))
151                 return git_config_string(&ssl_key, var, value);
152 #endif
153 #if LIBCURL_VERSION_NUM >= 0x070908
154         if (!strcmp("http.sslcapath", var))
155                 return git_config_string(&ssl_capath, var, value);
156 #endif
157         if (!strcmp("http.sslcainfo", var))
158                 return git_config_string(&ssl_cainfo, var, value);
159         if (!strcmp("http.sslcertpasswordprotected", var)) {
160                 if (git_config_bool(var, value))
161                         ssl_cert_password_required = 1;
162                 return 0;
163         }
164         if (!strcmp("http.minsessions", var)) {
165                 min_curl_sessions = git_config_int(var, value);
166 #ifndef USE_CURL_MULTI
167                 if (min_curl_sessions > 1)
168                         min_curl_sessions = 1;
169 #endif
170                 return 0;
171         }
172 #ifdef USE_CURL_MULTI
173         if (!strcmp("http.maxrequests", var)) {
174                 max_requests = git_config_int(var, value);
175                 return 0;
176         }
177 #endif
178         if (!strcmp("http.lowspeedlimit", var)) {
179                 curl_low_speed_limit = (long)git_config_int(var, value);
180                 return 0;
181         }
182         if (!strcmp("http.lowspeedtime", var)) {
183                 curl_low_speed_time = (long)git_config_int(var, value);
184                 return 0;
185         }
186
187         if (!strcmp("http.noepsv", var)) {
188                 curl_ftp_no_epsv = git_config_bool(var, value);
189                 return 0;
190         }
191         if (!strcmp("http.proxy", var))
192                 return git_config_string(&curl_http_proxy, var, value);
193
194         if (!strcmp("http.postbuffer", var)) {
195                 http_post_buffer = git_config_int(var, value);
196                 if (http_post_buffer < LARGE_PACKET_MAX)
197                         http_post_buffer = LARGE_PACKET_MAX;
198                 return 0;
199         }
200
201         if (!strcmp("http.useragent", var))
202                 return git_config_string(&user_agent, var, value);
203
204         /* Fall back on the default ones */
205         return git_default_config(var, value, cb);
206 }
207
208 static void init_curl_http_auth(CURL *result)
209 {
210         if (user_name) {
211                 struct strbuf up = STRBUF_INIT;
212                 if (!user_pass)
213                         user_pass = xstrdup(git_getpass("Password: "));
214                 strbuf_addf(&up, "%s:%s", user_name, user_pass);
215                 curl_easy_setopt(result, CURLOPT_USERPWD,
216                                  strbuf_detach(&up, NULL));
217         }
218 }
219
220 static int has_cert_password(void)
221 {
222         if (ssl_cert_password != NULL)
223                 return 1;
224         if (ssl_cert == NULL || ssl_cert_password_required != 1)
225                 return 0;
226         /* Only prompt the user once. */
227         ssl_cert_password_required = -1;
228         ssl_cert_password = git_getpass("Certificate Password: ");
229         if (ssl_cert_password != NULL) {
230                 ssl_cert_password = xstrdup(ssl_cert_password);
231                 return 1;
232         } else
233                 return 0;
234 }
235
236 static CURL *get_curl_handle(void)
237 {
238         CURL *result = curl_easy_init();
239
240         if (!curl_ssl_verify) {
241                 curl_easy_setopt(result, CURLOPT_SSL_VERIFYPEER, 0);
242                 curl_easy_setopt(result, CURLOPT_SSL_VERIFYHOST, 0);
243         } else {
244                 /* Verify authenticity of the peer's certificate */
245                 curl_easy_setopt(result, CURLOPT_SSL_VERIFYPEER, 1);
246                 /* The name in the cert must match whom we tried to connect */
247                 curl_easy_setopt(result, CURLOPT_SSL_VERIFYHOST, 2);
248         }
249
250 #if LIBCURL_VERSION_NUM >= 0x070907
251         curl_easy_setopt(result, CURLOPT_NETRC, CURL_NETRC_OPTIONAL);
252 #endif
253 #ifdef LIBCURL_CAN_HANDLE_AUTH_ANY
254         curl_easy_setopt(result, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
255 #endif
256
257         init_curl_http_auth(result);
258
259         if (ssl_cert != NULL)
260                 curl_easy_setopt(result, CURLOPT_SSLCERT, ssl_cert);
261         if (has_cert_password())
262                 curl_easy_setopt(result, CURLOPT_KEYPASSWD, ssl_cert_password);
263 #if LIBCURL_VERSION_NUM >= 0x070903
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         if (getenv("GIT_CURL_VERBOSE"))
285                 curl_easy_setopt(result, CURLOPT_VERBOSE, 1);
286
287         curl_easy_setopt(result, CURLOPT_USERAGENT,
288                 user_agent ? user_agent : GIT_HTTP_USER_AGENT);
289
290         if (curl_ftp_no_epsv)
291                 curl_easy_setopt(result, CURLOPT_FTP_USE_EPSV, 0);
292
293         if (curl_http_proxy)
294                 curl_easy_setopt(result, CURLOPT_PROXY, curl_http_proxy);
295
296         return result;
297 }
298
299 static void http_auth_init(const char *url)
300 {
301         char *at, *colon, *cp, *slash, *decoded;
302         int len;
303
304         cp = strstr(url, "://");
305         if (!cp)
306                 return;
307
308         /*
309          * Ok, the URL looks like "proto://something".  Which one?
310          * "proto://<user>:<pass>@<host>/...",
311          * "proto://<user>@<host>/...", or just
312          * "proto://<host>/..."?
313          */
314         cp += 3;
315         at = strchr(cp, '@');
316         colon = strchr(cp, ':');
317         slash = strchrnul(cp, '/');
318         if (!at || slash <= at)
319                 return; /* No credentials */
320         if (!colon || at <= colon) {
321                 /* Only username */
322                 len = at - cp;
323                 user_name = xmalloc(len + 1);
324                 memcpy(user_name, cp, len);
325                 user_name[len] = '\0';
326                 decoded = url_decode(user_name);
327                 free(user_name);
328                 user_name = decoded;
329                 user_pass = NULL;
330         } else {
331                 len = colon - cp;
332                 user_name = xmalloc(len + 1);
333                 memcpy(user_name, cp, len);
334                 user_name[len] = '\0';
335                 decoded = url_decode(user_name);
336                 free(user_name);
337                 user_name = decoded;
338                 len = at - (colon + 1);
339                 user_pass = xmalloc(len + 1);
340                 memcpy(user_pass, colon + 1, len);
341                 user_pass[len] = '\0';
342                 decoded = url_decode(user_pass);
343                 free(user_pass);
344                 user_pass = decoded;
345         }
346 }
347
348 static void set_from_env(const char **var, const char *envname)
349 {
350         const char *val = getenv(envname);
351         if (val)
352                 *var = val;
353 }
354
355 void http_init(struct remote *remote)
356 {
357         char *low_speed_limit;
358         char *low_speed_time;
359
360         http_is_verbose = 0;
361
362         git_config(http_options, NULL);
363
364         curl_global_init(CURL_GLOBAL_ALL);
365
366         if (remote && remote->http_proxy)
367                 curl_http_proxy = xstrdup(remote->http_proxy);
368
369         pragma_header = curl_slist_append(pragma_header, "Pragma: no-cache");
370         no_pragma_header = curl_slist_append(no_pragma_header, "Pragma:");
371
372 #ifdef USE_CURL_MULTI
373         {
374                 char *http_max_requests = getenv("GIT_HTTP_MAX_REQUESTS");
375                 if (http_max_requests != NULL)
376                         max_requests = atoi(http_max_requests);
377         }
378
379         curlm = curl_multi_init();
380         if (curlm == NULL) {
381                 fprintf(stderr, "Error creating curl multi handle.\n");
382                 exit(1);
383         }
384 #endif
385
386         if (getenv("GIT_SSL_NO_VERIFY"))
387                 curl_ssl_verify = 0;
388
389         set_from_env(&ssl_cert, "GIT_SSL_CERT");
390 #if LIBCURL_VERSION_NUM >= 0x070903
391         set_from_env(&ssl_key, "GIT_SSL_KEY");
392 #endif
393 #if LIBCURL_VERSION_NUM >= 0x070908
394         set_from_env(&ssl_capath, "GIT_SSL_CAPATH");
395 #endif
396         set_from_env(&ssl_cainfo, "GIT_SSL_CAINFO");
397
398         set_from_env(&user_agent, "GIT_HTTP_USER_AGENT");
399
400         low_speed_limit = getenv("GIT_HTTP_LOW_SPEED_LIMIT");
401         if (low_speed_limit != NULL)
402                 curl_low_speed_limit = strtol(low_speed_limit, NULL, 10);
403         low_speed_time = getenv("GIT_HTTP_LOW_SPEED_TIME");
404         if (low_speed_time != NULL)
405                 curl_low_speed_time = strtol(low_speed_time, NULL, 10);
406
407         if (curl_ssl_verify == -1)
408                 curl_ssl_verify = 1;
409
410         curl_session_count = 0;
411 #ifdef USE_CURL_MULTI
412         if (max_requests < 1)
413                 max_requests = DEFAULT_MAX_REQUESTS;
414 #endif
415
416         if (getenv("GIT_CURL_FTP_NO_EPSV"))
417                 curl_ftp_no_epsv = 1;
418
419         if (remote && remote->url && remote->url[0]) {
420                 http_auth_init(remote->url[0]);
421                 if (!ssl_cert_password_required &&
422                     getenv("GIT_SSL_CERT_PASSWORD_PROTECTED") &&
423                     !prefixcmp(remote->url[0], "https://"))
424                         ssl_cert_password_required = 1;
425         }
426
427 #ifndef NO_CURL_EASY_DUPHANDLE
428         curl_default = get_curl_handle();
429 #endif
430 }
431
432 void http_cleanup(void)
433 {
434         struct active_request_slot *slot = active_queue_head;
435
436         while (slot != NULL) {
437                 struct active_request_slot *next = slot->next;
438                 if (slot->curl != NULL) {
439 #ifdef USE_CURL_MULTI
440                         curl_multi_remove_handle(curlm, slot->curl);
441 #endif
442                         curl_easy_cleanup(slot->curl);
443                 }
444                 free(slot);
445                 slot = next;
446         }
447         active_queue_head = NULL;
448
449 #ifndef NO_CURL_EASY_DUPHANDLE
450         curl_easy_cleanup(curl_default);
451 #endif
452
453 #ifdef USE_CURL_MULTI
454         curl_multi_cleanup(curlm);
455 #endif
456         curl_global_cleanup();
457
458         curl_slist_free_all(pragma_header);
459         pragma_header = NULL;
460
461         curl_slist_free_all(no_pragma_header);
462         no_pragma_header = NULL;
463
464         if (curl_http_proxy) {
465                 free((void *)curl_http_proxy);
466                 curl_http_proxy = NULL;
467         }
468
469         if (ssl_cert_password != NULL) {
470                 memset(ssl_cert_password, 0, strlen(ssl_cert_password));
471                 free(ssl_cert_password);
472                 ssl_cert_password = NULL;
473         }
474         ssl_cert_password_required = 0;
475 }
476
477 struct active_request_slot *get_active_slot(void)
478 {
479         struct active_request_slot *slot = active_queue_head;
480         struct active_request_slot *newslot;
481
482 #ifdef USE_CURL_MULTI
483         int num_transfers;
484
485         /* Wait for a slot to open up if the queue is full */
486         while (active_requests >= max_requests) {
487                 curl_multi_perform(curlm, &num_transfers);
488                 if (num_transfers < active_requests)
489                         process_curl_messages();
490         }
491 #endif
492
493         while (slot != NULL && slot->in_use)
494                 slot = slot->next;
495
496         if (slot == NULL) {
497                 newslot = xmalloc(sizeof(*newslot));
498                 newslot->curl = NULL;
499                 newslot->in_use = 0;
500                 newslot->next = NULL;
501
502                 slot = active_queue_head;
503                 if (slot == NULL) {
504                         active_queue_head = newslot;
505                 } else {
506                         while (slot->next != NULL)
507                                 slot = slot->next;
508                         slot->next = newslot;
509                 }
510                 slot = newslot;
511         }
512
513         if (slot->curl == NULL) {
514 #ifdef NO_CURL_EASY_DUPHANDLE
515                 slot->curl = get_curl_handle();
516 #else
517                 slot->curl = curl_easy_duphandle(curl_default);
518 #endif
519                 curl_session_count++;
520         }
521
522         active_requests++;
523         slot->in_use = 1;
524         slot->local = NULL;
525         slot->results = NULL;
526         slot->finished = NULL;
527         slot->callback_data = NULL;
528         slot->callback_func = NULL;
529         curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, pragma_header);
530         curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, curl_errorstr);
531         curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, NULL);
532         curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, NULL);
533         curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, NULL);
534         curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 0);
535         curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
536
537         return slot;
538 }
539
540 int start_active_slot(struct active_request_slot *slot)
541 {
542 #ifdef USE_CURL_MULTI
543         CURLMcode curlm_result = curl_multi_add_handle(curlm, slot->curl);
544         int num_transfers;
545
546         if (curlm_result != CURLM_OK &&
547             curlm_result != CURLM_CALL_MULTI_PERFORM) {
548                 active_requests--;
549                 slot->in_use = 0;
550                 return 0;
551         }
552
553         /*
554          * We know there must be something to do, since we just added
555          * something.
556          */
557         curl_multi_perform(curlm, &num_transfers);
558 #endif
559         return 1;
560 }
561
562 #ifdef USE_CURL_MULTI
563 struct fill_chain {
564         void *data;
565         int (*fill)(void *);
566         struct fill_chain *next;
567 };
568
569 static struct fill_chain *fill_cfg;
570
571 void add_fill_function(void *data, int (*fill)(void *))
572 {
573         struct fill_chain *new = xmalloc(sizeof(*new));
574         struct fill_chain **linkp = &fill_cfg;
575         new->data = data;
576         new->fill = fill;
577         new->next = NULL;
578         while (*linkp)
579                 linkp = &(*linkp)->next;
580         *linkp = new;
581 }
582
583 void fill_active_slots(void)
584 {
585         struct active_request_slot *slot = active_queue_head;
586
587         while (active_requests < max_requests) {
588                 struct fill_chain *fill;
589                 for (fill = fill_cfg; fill; fill = fill->next)
590                         if (fill->fill(fill->data))
591                                 break;
592
593                 if (!fill)
594                         break;
595         }
596
597         while (slot != NULL) {
598                 if (!slot->in_use && slot->curl != NULL
599                         && curl_session_count > min_curl_sessions) {
600                         curl_easy_cleanup(slot->curl);
601                         slot->curl = NULL;
602                         curl_session_count--;
603                 }
604                 slot = slot->next;
605         }
606 }
607
608 void step_active_slots(void)
609 {
610         int num_transfers;
611         CURLMcode curlm_result;
612
613         do {
614                 curlm_result = curl_multi_perform(curlm, &num_transfers);
615         } while (curlm_result == CURLM_CALL_MULTI_PERFORM);
616         if (num_transfers < active_requests) {
617                 process_curl_messages();
618                 fill_active_slots();
619         }
620 }
621 #endif
622
623 void run_active_slot(struct active_request_slot *slot)
624 {
625 #ifdef USE_CURL_MULTI
626         long last_pos = 0;
627         long current_pos;
628         fd_set readfds;
629         fd_set writefds;
630         fd_set excfds;
631         int max_fd;
632         struct timeval select_timeout;
633         int finished = 0;
634
635         slot->finished = &finished;
636         while (!finished) {
637                 data_received = 0;
638                 step_active_slots();
639
640                 if (!data_received && slot->local != NULL) {
641                         current_pos = ftell(slot->local);
642                         if (current_pos > last_pos)
643                                 data_received++;
644                         last_pos = current_pos;
645                 }
646
647                 if (slot->in_use && !data_received) {
648                         max_fd = 0;
649                         FD_ZERO(&readfds);
650                         FD_ZERO(&writefds);
651                         FD_ZERO(&excfds);
652                         select_timeout.tv_sec = 0;
653                         select_timeout.tv_usec = 50000;
654                         select(max_fd, &readfds, &writefds,
655                                &excfds, &select_timeout);
656                 }
657         }
658 #else
659         while (slot->in_use) {
660                 slot->curl_result = curl_easy_perform(slot->curl);
661                 finish_active_slot(slot);
662         }
663 #endif
664 }
665
666 static void closedown_active_slot(struct active_request_slot *slot)
667 {
668         active_requests--;
669         slot->in_use = 0;
670 }
671
672 static void release_active_slot(struct active_request_slot *slot)
673 {
674         closedown_active_slot(slot);
675         if (slot->curl && curl_session_count > min_curl_sessions) {
676 #ifdef USE_CURL_MULTI
677                 curl_multi_remove_handle(curlm, slot->curl);
678 #endif
679                 curl_easy_cleanup(slot->curl);
680                 slot->curl = NULL;
681                 curl_session_count--;
682         }
683 #ifdef USE_CURL_MULTI
684         fill_active_slots();
685 #endif
686 }
687
688 void finish_active_slot(struct active_request_slot *slot)
689 {
690         closedown_active_slot(slot);
691         curl_easy_getinfo(slot->curl, CURLINFO_HTTP_CODE, &slot->http_code);
692
693         if (slot->finished != NULL)
694                 (*slot->finished) = 1;
695
696         /* Store slot results so they can be read after the slot is reused */
697         if (slot->results != NULL) {
698                 slot->results->curl_result = slot->curl_result;
699                 slot->results->http_code = slot->http_code;
700         }
701
702         /* Run callback if appropriate */
703         if (slot->callback_func != NULL)
704                 slot->callback_func(slot->callback_data);
705 }
706
707 void finish_all_active_slots(void)
708 {
709         struct active_request_slot *slot = active_queue_head;
710
711         while (slot != NULL)
712                 if (slot->in_use) {
713                         run_active_slot(slot);
714                         slot = active_queue_head;
715                 } else {
716                         slot = slot->next;
717                 }
718 }
719
720 /* Helpers for modifying and creating URLs */
721 static inline int needs_quote(int ch)
722 {
723         if (((ch >= 'A') && (ch <= 'Z'))
724                         || ((ch >= 'a') && (ch <= 'z'))
725                         || ((ch >= '0') && (ch <= '9'))
726                         || (ch == '/')
727                         || (ch == '-')
728                         || (ch == '.'))
729                 return 0;
730         return 1;
731 }
732
733 static inline int hex(int v)
734 {
735         if (v < 10)
736                 return '0' + v;
737         else
738                 return 'A' + v - 10;
739 }
740
741 void end_url_with_slash(struct strbuf *buf, const char *url)
742 {
743         strbuf_addstr(buf, url);
744         if (buf->len && buf->buf[buf->len - 1] != '/')
745                 strbuf_addstr(buf, "/");
746 }
747
748 static char *quote_ref_url(const char *base, const char *ref)
749 {
750         struct strbuf buf = STRBUF_INIT;
751         const char *cp;
752         int ch;
753
754         end_url_with_slash(&buf, base);
755
756         for (cp = ref; (ch = *cp) != 0; cp++)
757                 if (needs_quote(ch))
758                         strbuf_addf(&buf, "%%%02x", ch);
759                 else
760                         strbuf_addch(&buf, *cp);
761
762         return strbuf_detach(&buf, NULL);
763 }
764
765 void append_remote_object_url(struct strbuf *buf, const char *url,
766                               const char *hex,
767                               int only_two_digit_prefix)
768 {
769         end_url_with_slash(buf, url);
770
771         strbuf_addf(buf, "objects/%.*s/", 2, hex);
772         if (!only_two_digit_prefix)
773                 strbuf_addf(buf, "%s", hex+2);
774 }
775
776 char *get_remote_object_url(const char *url, const char *hex,
777                             int only_two_digit_prefix)
778 {
779         struct strbuf buf = STRBUF_INIT;
780         append_remote_object_url(&buf, url, hex, only_two_digit_prefix);
781         return strbuf_detach(&buf, NULL);
782 }
783
784 /* http_request() targets */
785 #define HTTP_REQUEST_STRBUF     0
786 #define HTTP_REQUEST_FILE       1
787
788 static int http_request(const char *url, void *result, int target, int options)
789 {
790         struct active_request_slot *slot;
791         struct slot_results results;
792         struct curl_slist *headers = NULL;
793         struct strbuf buf = STRBUF_INIT;
794         int ret;
795
796         slot = get_active_slot();
797         slot->results = &results;
798         curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
799
800         if (result == NULL) {
801                 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 1);
802         } else {
803                 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
804                 curl_easy_setopt(slot->curl, CURLOPT_FILE, result);
805
806                 if (target == HTTP_REQUEST_FILE) {
807                         long posn = ftell(result);
808                         curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION,
809                                          fwrite);
810                         if (posn > 0) {
811                                 strbuf_addf(&buf, "Range: bytes=%ld-", posn);
812                                 headers = curl_slist_append(headers, buf.buf);
813                                 strbuf_reset(&buf);
814                         }
815                         slot->local = result;
816                 } else
817                         curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION,
818                                          fwrite_buffer);
819         }
820
821         strbuf_addstr(&buf, "Pragma:");
822         if (options & HTTP_NO_CACHE)
823                 strbuf_addstr(&buf, " no-cache");
824
825         headers = curl_slist_append(headers, buf.buf);
826
827         curl_easy_setopt(slot->curl, CURLOPT_URL, url);
828         curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
829
830         if (start_active_slot(slot)) {
831                 run_active_slot(slot);
832                 if (results.curl_result == CURLE_OK)
833                         ret = HTTP_OK;
834                 else if (missing_target(&results))
835                         ret = HTTP_MISSING_TARGET;
836                 else if (results.http_code == 401) {
837                         if (user_name) {
838                                 ret = HTTP_NOAUTH;
839                         } else {
840                                 /*
841                                  * git_getpass is needed here because its very likely stdin/stdout are
842                                  * pipes to our parent process.  So we instead need to use /dev/tty,
843                                  * but that is non-portable.  Using git_getpass() can at least be stubbed
844                                  * on other platforms with a different implementation if/when necessary.
845                                  */
846                                 user_name = xstrdup(git_getpass("Username: "));
847                                 init_curl_http_auth(slot->curl);
848                                 ret = HTTP_REAUTH;
849                         }
850                 } else
851                         ret = HTTP_ERROR;
852         } else {
853                 error("Unable to start HTTP request for %s", url);
854                 ret = HTTP_START_FAILED;
855         }
856
857         slot->local = NULL;
858         curl_slist_free_all(headers);
859         strbuf_release(&buf);
860
861         return ret;
862 }
863
864 int http_get_strbuf(const char *url, struct strbuf *result, int options)
865 {
866         int http_ret = http_request(url, result, HTTP_REQUEST_STRBUF, options);
867         if (http_ret == HTTP_REAUTH) {
868                 http_ret = http_request(url, result, HTTP_REQUEST_STRBUF, options);
869         }
870         return http_ret;
871 }
872
873 /*
874  * Downloads an url and stores the result in the given file.
875  *
876  * If a previous interrupted download is detected (i.e. a previous temporary
877  * file is still around) the download is resumed.
878  */
879 static int http_get_file(const char *url, const char *filename, int options)
880 {
881         int ret;
882         struct strbuf tmpfile = STRBUF_INIT;
883         FILE *result;
884
885         strbuf_addf(&tmpfile, "%s.temp", filename);
886         result = fopen(tmpfile.buf, "a");
887         if (! result) {
888                 error("Unable to open local file %s", tmpfile.buf);
889                 ret = HTTP_ERROR;
890                 goto cleanup;
891         }
892
893         ret = http_request(url, result, HTTP_REQUEST_FILE, options);
894         fclose(result);
895
896         if ((ret == HTTP_OK) && move_temp_to_file(tmpfile.buf, filename))
897                 ret = HTTP_ERROR;
898 cleanup:
899         strbuf_release(&tmpfile);
900         return ret;
901 }
902
903 int http_error(const char *url, int ret)
904 {
905         /* http_request has already handled HTTP_START_FAILED. */
906         if (ret != HTTP_START_FAILED)
907                 error("%s while accessing %s\n", curl_errorstr, url);
908
909         return ret;
910 }
911
912 int http_fetch_ref(const char *base, struct ref *ref)
913 {
914         char *url;
915         struct strbuf buffer = STRBUF_INIT;
916         int ret = -1;
917
918         url = quote_ref_url(base, ref->name);
919         if (http_get_strbuf(url, &buffer, HTTP_NO_CACHE) == HTTP_OK) {
920                 strbuf_rtrim(&buffer);
921                 if (buffer.len == 40)
922                         ret = get_sha1_hex(buffer.buf, ref->old_sha1);
923                 else if (!prefixcmp(buffer.buf, "ref: ")) {
924                         ref->symref = xstrdup(buffer.buf + 5);
925                         ret = 0;
926                 }
927         }
928
929         strbuf_release(&buffer);
930         free(url);
931         return ret;
932 }
933
934 /* Helpers for fetching packs */
935 static char *fetch_pack_index(unsigned char *sha1, const char *base_url)
936 {
937         char *url, *tmp;
938         struct strbuf buf = STRBUF_INIT;
939
940         if (http_is_verbose)
941                 fprintf(stderr, "Getting index for pack %s\n", sha1_to_hex(sha1));
942
943         end_url_with_slash(&buf, base_url);
944         strbuf_addf(&buf, "objects/pack/pack-%s.idx", sha1_to_hex(sha1));
945         url = strbuf_detach(&buf, NULL);
946
947         strbuf_addf(&buf, "%s.temp", sha1_pack_index_name(sha1));
948         tmp = strbuf_detach(&buf, NULL);
949
950         if (http_get_file(url, tmp, 0) != HTTP_OK) {
951                 error("Unable to get pack index %s\n", url);
952                 free(tmp);
953                 tmp = NULL;
954         }
955
956         free(url);
957         return tmp;
958 }
959
960 static int fetch_and_setup_pack_index(struct packed_git **packs_head,
961         unsigned char *sha1, const char *base_url)
962 {
963         struct packed_git *new_pack;
964         char *tmp_idx = NULL;
965         int ret;
966
967         if (has_pack_index(sha1)) {
968                 new_pack = parse_pack_index(sha1, NULL);
969                 if (!new_pack)
970                         return -1; /* parse_pack_index() already issued error message */
971                 goto add_pack;
972         }
973
974         tmp_idx = fetch_pack_index(sha1, base_url);
975         if (!tmp_idx)
976                 return -1;
977
978         new_pack = parse_pack_index(sha1, tmp_idx);
979         if (!new_pack) {
980                 unlink(tmp_idx);
981                 free(tmp_idx);
982
983                 return -1; /* parse_pack_index() already issued error message */
984         }
985
986         ret = verify_pack_index(new_pack);
987         if (!ret) {
988                 close_pack_index(new_pack);
989                 ret = move_temp_to_file(tmp_idx, sha1_pack_index_name(sha1));
990         }
991         free(tmp_idx);
992         if (ret)
993                 return -1;
994
995 add_pack:
996         new_pack->next = *packs_head;
997         *packs_head = new_pack;
998         return 0;
999 }
1000
1001 int http_get_info_packs(const char *base_url, struct packed_git **packs_head)
1002 {
1003         int ret = 0, i = 0;
1004         char *url, *data;
1005         struct strbuf buf = STRBUF_INIT;
1006         unsigned char sha1[20];
1007
1008         end_url_with_slash(&buf, base_url);
1009         strbuf_addstr(&buf, "objects/info/packs");
1010         url = strbuf_detach(&buf, NULL);
1011
1012         ret = http_get_strbuf(url, &buf, HTTP_NO_CACHE);
1013         if (ret != HTTP_OK)
1014                 goto cleanup;
1015
1016         data = buf.buf;
1017         while (i < buf.len) {
1018                 switch (data[i]) {
1019                 case 'P':
1020                         i++;
1021                         if (i + 52 <= buf.len &&
1022                             !prefixcmp(data + i, " pack-") &&
1023                             !prefixcmp(data + i + 46, ".pack\n")) {
1024                                 get_sha1_hex(data + i + 6, sha1);
1025                                 fetch_and_setup_pack_index(packs_head, sha1,
1026                                                       base_url);
1027                                 i += 51;
1028                                 break;
1029                         }
1030                 default:
1031                         while (i < buf.len && data[i] != '\n')
1032                                 i++;
1033                 }
1034                 i++;
1035         }
1036
1037 cleanup:
1038         free(url);
1039         return ret;
1040 }
1041
1042 void release_http_pack_request(struct http_pack_request *preq)
1043 {
1044         if (preq->packfile != NULL) {
1045                 fclose(preq->packfile);
1046                 preq->packfile = NULL;
1047                 preq->slot->local = NULL;
1048         }
1049         if (preq->range_header != NULL) {
1050                 curl_slist_free_all(preq->range_header);
1051                 preq->range_header = NULL;
1052         }
1053         preq->slot = NULL;
1054         free(preq->url);
1055 }
1056
1057 int finish_http_pack_request(struct http_pack_request *preq)
1058 {
1059         struct packed_git **lst;
1060         struct packed_git *p = preq->target;
1061         char *tmp_idx;
1062         struct child_process ip;
1063         const char *ip_argv[8];
1064
1065         close_pack_index(p);
1066
1067         fclose(preq->packfile);
1068         preq->packfile = NULL;
1069         preq->slot->local = NULL;
1070
1071         lst = preq->lst;
1072         while (*lst != p)
1073                 lst = &((*lst)->next);
1074         *lst = (*lst)->next;
1075
1076         tmp_idx = xstrdup(preq->tmpfile);
1077         strcpy(tmp_idx + strlen(tmp_idx) - strlen(".pack.temp"),
1078                ".idx.temp");
1079
1080         ip_argv[0] = "index-pack";
1081         ip_argv[1] = "-o";
1082         ip_argv[2] = tmp_idx;
1083         ip_argv[3] = preq->tmpfile;
1084         ip_argv[4] = NULL;
1085
1086         memset(&ip, 0, sizeof(ip));
1087         ip.argv = ip_argv;
1088         ip.git_cmd = 1;
1089         ip.no_stdin = 1;
1090         ip.no_stdout = 1;
1091
1092         if (run_command(&ip)) {
1093                 unlink(preq->tmpfile);
1094                 unlink(tmp_idx);
1095                 free(tmp_idx);
1096                 return -1;
1097         }
1098
1099         unlink(sha1_pack_index_name(p->sha1));
1100
1101         if (move_temp_to_file(preq->tmpfile, sha1_pack_name(p->sha1))
1102          || move_temp_to_file(tmp_idx, sha1_pack_index_name(p->sha1))) {
1103                 free(tmp_idx);
1104                 return -1;
1105         }
1106
1107         install_packed_git(p);
1108         free(tmp_idx);
1109         return 0;
1110 }
1111
1112 struct http_pack_request *new_http_pack_request(
1113         struct packed_git *target, const char *base_url)
1114 {
1115         long prev_posn = 0;
1116         char range[RANGE_HEADER_SIZE];
1117         struct strbuf buf = STRBUF_INIT;
1118         struct http_pack_request *preq;
1119
1120         preq = xmalloc(sizeof(*preq));
1121         preq->target = target;
1122         preq->range_header = NULL;
1123
1124         end_url_with_slash(&buf, base_url);
1125         strbuf_addf(&buf, "objects/pack/pack-%s.pack",
1126                 sha1_to_hex(target->sha1));
1127         preq->url = strbuf_detach(&buf, NULL);
1128
1129         snprintf(preq->tmpfile, sizeof(preq->tmpfile), "%s.temp",
1130                 sha1_pack_name(target->sha1));
1131         preq->packfile = fopen(preq->tmpfile, "a");
1132         if (!preq->packfile) {
1133                 error("Unable to open local file %s for pack",
1134                       preq->tmpfile);
1135                 goto abort;
1136         }
1137
1138         preq->slot = get_active_slot();
1139         preq->slot->local = preq->packfile;
1140         curl_easy_setopt(preq->slot->curl, CURLOPT_FILE, preq->packfile);
1141         curl_easy_setopt(preq->slot->curl, CURLOPT_WRITEFUNCTION, fwrite);
1142         curl_easy_setopt(preq->slot->curl, CURLOPT_URL, preq->url);
1143         curl_easy_setopt(preq->slot->curl, CURLOPT_HTTPHEADER,
1144                 no_pragma_header);
1145
1146         /*
1147          * If there is data present from a previous transfer attempt,
1148          * resume where it left off
1149          */
1150         prev_posn = ftell(preq->packfile);
1151         if (prev_posn>0) {
1152                 if (http_is_verbose)
1153                         fprintf(stderr,
1154                                 "Resuming fetch of pack %s at byte %ld\n",
1155                                 sha1_to_hex(target->sha1), prev_posn);
1156                 sprintf(range, "Range: bytes=%ld-", prev_posn);
1157                 preq->range_header = curl_slist_append(NULL, range);
1158                 curl_easy_setopt(preq->slot->curl, CURLOPT_HTTPHEADER,
1159                         preq->range_header);
1160         }
1161
1162         return preq;
1163
1164 abort:
1165         free(preq->url);
1166         free(preq);
1167         return NULL;
1168 }
1169
1170 /* Helpers for fetching objects (loose) */
1171 static size_t fwrite_sha1_file(void *ptr, size_t eltsize, size_t nmemb,
1172                                void *data)
1173 {
1174         unsigned char expn[4096];
1175         size_t size = eltsize * nmemb;
1176         int posn = 0;
1177         struct http_object_request *freq =
1178                 (struct http_object_request *)data;
1179         do {
1180                 ssize_t retval = xwrite(freq->localfile,
1181                                         (char *) ptr + posn, size - posn);
1182                 if (retval < 0)
1183                         return posn;
1184                 posn += retval;
1185         } while (posn < size);
1186
1187         freq->stream.avail_in = size;
1188         freq->stream.next_in = ptr;
1189         do {
1190                 freq->stream.next_out = expn;
1191                 freq->stream.avail_out = sizeof(expn);
1192                 freq->zret = git_inflate(&freq->stream, Z_SYNC_FLUSH);
1193                 git_SHA1_Update(&freq->c, expn,
1194                                 sizeof(expn) - freq->stream.avail_out);
1195         } while (freq->stream.avail_in && freq->zret == Z_OK);
1196         data_received++;
1197         return size;
1198 }
1199
1200 struct http_object_request *new_http_object_request(const char *base_url,
1201         unsigned char *sha1)
1202 {
1203         char *hex = sha1_to_hex(sha1);
1204         char *filename;
1205         char prevfile[PATH_MAX];
1206         int prevlocal;
1207         unsigned char prev_buf[PREV_BUF_SIZE];
1208         ssize_t prev_read = 0;
1209         long prev_posn = 0;
1210         char range[RANGE_HEADER_SIZE];
1211         struct curl_slist *range_header = NULL;
1212         struct http_object_request *freq;
1213
1214         freq = xmalloc(sizeof(*freq));
1215         hashcpy(freq->sha1, sha1);
1216         freq->localfile = -1;
1217
1218         filename = sha1_file_name(sha1);
1219         snprintf(freq->tmpfile, sizeof(freq->tmpfile),
1220                  "%s.temp", filename);
1221
1222         snprintf(prevfile, sizeof(prevfile), "%s.prev", filename);
1223         unlink_or_warn(prevfile);
1224         rename(freq->tmpfile, prevfile);
1225         unlink_or_warn(freq->tmpfile);
1226
1227         if (freq->localfile != -1)
1228                 error("fd leakage in start: %d", freq->localfile);
1229         freq->localfile = open(freq->tmpfile,
1230                                O_WRONLY | O_CREAT | O_EXCL, 0666);
1231         /*
1232          * This could have failed due to the "lazy directory creation";
1233          * try to mkdir the last path component.
1234          */
1235         if (freq->localfile < 0 && errno == ENOENT) {
1236                 char *dir = strrchr(freq->tmpfile, '/');
1237                 if (dir) {
1238                         *dir = 0;
1239                         mkdir(freq->tmpfile, 0777);
1240                         *dir = '/';
1241                 }
1242                 freq->localfile = open(freq->tmpfile,
1243                                        O_WRONLY | O_CREAT | O_EXCL, 0666);
1244         }
1245
1246         if (freq->localfile < 0) {
1247                 error("Couldn't create temporary file %s: %s",
1248                       freq->tmpfile, strerror(errno));
1249                 goto abort;
1250         }
1251
1252         memset(&freq->stream, 0, sizeof(freq->stream));
1253
1254         git_inflate_init(&freq->stream);
1255
1256         git_SHA1_Init(&freq->c);
1257
1258         freq->url = get_remote_object_url(base_url, hex, 0);
1259
1260         /*
1261          * If a previous temp file is present, process what was already
1262          * fetched.
1263          */
1264         prevlocal = open(prevfile, O_RDONLY);
1265         if (prevlocal != -1) {
1266                 do {
1267                         prev_read = xread(prevlocal, prev_buf, PREV_BUF_SIZE);
1268                         if (prev_read>0) {
1269                                 if (fwrite_sha1_file(prev_buf,
1270                                                      1,
1271                                                      prev_read,
1272                                                      freq) == prev_read) {
1273                                         prev_posn += prev_read;
1274                                 } else {
1275                                         prev_read = -1;
1276                                 }
1277                         }
1278                 } while (prev_read > 0);
1279                 close(prevlocal);
1280         }
1281         unlink_or_warn(prevfile);
1282
1283         /*
1284          * Reset inflate/SHA1 if there was an error reading the previous temp
1285          * file; also rewind to the beginning of the local file.
1286          */
1287         if (prev_read == -1) {
1288                 memset(&freq->stream, 0, sizeof(freq->stream));
1289                 git_inflate_init(&freq->stream);
1290                 git_SHA1_Init(&freq->c);
1291                 if (prev_posn>0) {
1292                         prev_posn = 0;
1293                         lseek(freq->localfile, 0, SEEK_SET);
1294                         if (ftruncate(freq->localfile, 0) < 0) {
1295                                 error("Couldn't truncate temporary file %s: %s",
1296                                           freq->tmpfile, strerror(errno));
1297                                 goto abort;
1298                         }
1299                 }
1300         }
1301
1302         freq->slot = get_active_slot();
1303
1304         curl_easy_setopt(freq->slot->curl, CURLOPT_FILE, freq);
1305         curl_easy_setopt(freq->slot->curl, CURLOPT_WRITEFUNCTION, fwrite_sha1_file);
1306         curl_easy_setopt(freq->slot->curl, CURLOPT_ERRORBUFFER, freq->errorstr);
1307         curl_easy_setopt(freq->slot->curl, CURLOPT_URL, freq->url);
1308         curl_easy_setopt(freq->slot->curl, CURLOPT_HTTPHEADER, no_pragma_header);
1309
1310         /*
1311          * If we have successfully processed data from a previous fetch
1312          * attempt, only fetch the data we don't already have.
1313          */
1314         if (prev_posn>0) {
1315                 if (http_is_verbose)
1316                         fprintf(stderr,
1317                                 "Resuming fetch of object %s at byte %ld\n",
1318                                 hex, prev_posn);
1319                 sprintf(range, "Range: bytes=%ld-", prev_posn);
1320                 range_header = curl_slist_append(range_header, range);
1321                 curl_easy_setopt(freq->slot->curl,
1322                                  CURLOPT_HTTPHEADER, range_header);
1323         }
1324
1325         return freq;
1326
1327 abort:
1328         free(filename);
1329         free(freq->url);
1330         free(freq);
1331         return NULL;
1332 }
1333
1334 void process_http_object_request(struct http_object_request *freq)
1335 {
1336         if (freq->slot == NULL)
1337                 return;
1338         freq->curl_result = freq->slot->curl_result;
1339         freq->http_code = freq->slot->http_code;
1340         freq->slot = NULL;
1341 }
1342
1343 int finish_http_object_request(struct http_object_request *freq)
1344 {
1345         struct stat st;
1346
1347         close(freq->localfile);
1348         freq->localfile = -1;
1349
1350         process_http_object_request(freq);
1351
1352         if (freq->http_code == 416) {
1353                 warning("requested range invalid; we may already have all the data.");
1354         } else if (freq->curl_result != CURLE_OK) {
1355                 if (stat(freq->tmpfile, &st) == 0)
1356                         if (st.st_size == 0)
1357                                 unlink_or_warn(freq->tmpfile);
1358                 return -1;
1359         }
1360
1361         git_inflate_end(&freq->stream);
1362         git_SHA1_Final(freq->real_sha1, &freq->c);
1363         if (freq->zret != Z_STREAM_END) {
1364                 unlink_or_warn(freq->tmpfile);
1365                 return -1;
1366         }
1367         if (hashcmp(freq->sha1, freq->real_sha1)) {
1368                 unlink_or_warn(freq->tmpfile);
1369                 return -1;
1370         }
1371         freq->rename =
1372                 move_temp_to_file(freq->tmpfile, sha1_file_name(freq->sha1));
1373
1374         return freq->rename;
1375 }
1376
1377 void abort_http_object_request(struct http_object_request *freq)
1378 {
1379         unlink_or_warn(freq->tmpfile);
1380
1381         release_http_object_request(freq);
1382 }
1383
1384 void release_http_object_request(struct http_object_request *freq)
1385 {
1386         if (freq->localfile != -1) {
1387                 close(freq->localfile);
1388                 freq->localfile = -1;
1389         }
1390         if (freq->url != NULL) {
1391                 free(freq->url);
1392                 freq->url = NULL;
1393         }
1394         if (freq->slot != NULL) {
1395                 freq->slot->callback_func = NULL;
1396                 freq->slot->callback_data = NULL;
1397                 release_active_slot(freq->slot);
1398                 freq->slot = NULL;
1399         }
1400 }