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