Merge branch 'sp/remote-curl-ssl-strerror'
[git] / http.c
1 #include "git-compat-util.h"
2 #include "http.h"
3 #include "pack.h"
4 #include "sideband.h"
5 #include "run-command.h"
6 #include "url.h"
7 #include "urlmatch.h"
8 #include "credential.h"
9 #include "version.h"
10 #include "pkt-line.h"
11 #include "gettext.h"
12 #include "transport.h"
13
14 #if LIBCURL_VERSION_NUM >= 0x070a08
15 long int git_curl_ipresolve = CURL_IPRESOLVE_WHATEVER;
16 #else
17 long int git_curl_ipresolve;
18 #endif
19 int active_requests;
20 int http_is_verbose;
21 size_t http_post_buffer = 16 * LARGE_PACKET_MAX;
22
23 #if LIBCURL_VERSION_NUM >= 0x070a06
24 #define LIBCURL_CAN_HANDLE_AUTH_ANY
25 #endif
26
27 static int min_curl_sessions = 1;
28 static int curl_session_count;
29 #ifdef USE_CURL_MULTI
30 static int max_requests = -1;
31 static CURLM *curlm;
32 #endif
33 #ifndef NO_CURL_EASY_DUPHANDLE
34 static CURL *curl_default;
35 #endif
36
37 #define PREV_BUF_SIZE 4096
38
39 char curl_errorstr[CURL_ERROR_SIZE];
40
41 static int curl_ssl_verify = -1;
42 static int curl_ssl_try;
43 static const char *ssl_cert;
44 static const char *ssl_cipherlist;
45 static const char *ssl_version;
46 static struct {
47         const char *name;
48         long ssl_version;
49 } sslversions[] = {
50         { "sslv2", CURL_SSLVERSION_SSLv2 },
51         { "sslv3", CURL_SSLVERSION_SSLv3 },
52         { "tlsv1", CURL_SSLVERSION_TLSv1 },
53 #if LIBCURL_VERSION_NUM >= 0x072200
54         { "tlsv1.0", CURL_SSLVERSION_TLSv1_0 },
55         { "tlsv1.1", CURL_SSLVERSION_TLSv1_1 },
56         { "tlsv1.2", CURL_SSLVERSION_TLSv1_2 },
57 #endif
58 };
59 #if LIBCURL_VERSION_NUM >= 0x070903
60 static const char *ssl_key;
61 #endif
62 #if LIBCURL_VERSION_NUM >= 0x070908
63 static const char *ssl_capath;
64 #endif
65 static const char *ssl_cainfo;
66 static long curl_low_speed_limit = -1;
67 static long curl_low_speed_time = -1;
68 static int curl_ftp_no_epsv;
69 static const char *curl_http_proxy;
70 static const char *http_proxy_authmethod;
71 static struct {
72         const char *name;
73         long curlauth_param;
74 } proxy_authmethods[] = {
75         { "basic", CURLAUTH_BASIC },
76         { "digest", CURLAUTH_DIGEST },
77         { "negotiate", CURLAUTH_GSSNEGOTIATE },
78         { "ntlm", CURLAUTH_NTLM },
79 #ifdef LIBCURL_CAN_HANDLE_AUTH_ANY
80         { "anyauth", CURLAUTH_ANY },
81 #endif
82         /*
83          * CURLAUTH_DIGEST_IE has no corresponding command-line option in
84          * curl(1) and is not included in CURLAUTH_ANY, so we leave it out
85          * here, too
86          */
87 };
88 static struct credential proxy_auth = CREDENTIAL_INIT;
89 static const char *curl_proxyuserpwd;
90 static const char *curl_cookie_file;
91 static int curl_save_cookies;
92 struct credential http_auth = CREDENTIAL_INIT;
93 static int http_proactive_auth;
94 static const char *user_agent;
95
96 #if LIBCURL_VERSION_NUM >= 0x071700
97 /* Use CURLOPT_KEYPASSWD as is */
98 #elif LIBCURL_VERSION_NUM >= 0x070903
99 #define CURLOPT_KEYPASSWD CURLOPT_SSLKEYPASSWD
100 #else
101 #define CURLOPT_KEYPASSWD CURLOPT_SSLCERTPASSWD
102 #endif
103
104 static struct credential cert_auth = CREDENTIAL_INIT;
105 static int ssl_cert_password_required;
106 #ifdef LIBCURL_CAN_HANDLE_AUTH_ANY
107 static unsigned long http_auth_methods = CURLAUTH_ANY;
108 #endif
109
110 static struct curl_slist *pragma_header;
111 static struct curl_slist *no_pragma_header;
112
113 static struct active_request_slot *active_queue_head;
114
115 static char *cached_accept_language;
116
117 size_t fread_buffer(char *ptr, size_t eltsize, size_t nmemb, void *buffer_)
118 {
119         size_t size = eltsize * nmemb;
120         struct buffer *buffer = buffer_;
121
122         if (size > buffer->buf.len - buffer->posn)
123                 size = buffer->buf.len - buffer->posn;
124         memcpy(ptr, buffer->buf.buf + buffer->posn, size);
125         buffer->posn += size;
126
127         return size;
128 }
129
130 #ifndef NO_CURL_IOCTL
131 curlioerr ioctl_buffer(CURL *handle, int cmd, void *clientp)
132 {
133         struct buffer *buffer = clientp;
134
135         switch (cmd) {
136         case CURLIOCMD_NOP:
137                 return CURLIOE_OK;
138
139         case CURLIOCMD_RESTARTREAD:
140                 buffer->posn = 0;
141                 return CURLIOE_OK;
142
143         default:
144                 return CURLIOE_UNKNOWNCMD;
145         }
146 }
147 #endif
148
149 size_t fwrite_buffer(char *ptr, size_t eltsize, size_t nmemb, void *buffer_)
150 {
151         size_t size = eltsize * nmemb;
152         struct strbuf *buffer = buffer_;
153
154         strbuf_add(buffer, ptr, size);
155         return size;
156 }
157
158 size_t fwrite_null(char *ptr, size_t eltsize, size_t nmemb, void *strbuf)
159 {
160         return eltsize * nmemb;
161 }
162
163 static void closedown_active_slot(struct active_request_slot *slot)
164 {
165         active_requests--;
166         slot->in_use = 0;
167 }
168
169 static void finish_active_slot(struct active_request_slot *slot)
170 {
171         closedown_active_slot(slot);
172         curl_easy_getinfo(slot->curl, CURLINFO_HTTP_CODE, &slot->http_code);
173
174         if (slot->finished != NULL)
175                 (*slot->finished) = 1;
176
177         /* Store slot results so they can be read after the slot is reused */
178         if (slot->results != NULL) {
179                 slot->results->curl_result = slot->curl_result;
180                 slot->results->http_code = slot->http_code;
181 #if LIBCURL_VERSION_NUM >= 0x070a08
182                 curl_easy_getinfo(slot->curl, CURLINFO_HTTPAUTH_AVAIL,
183                                   &slot->results->auth_avail);
184 #else
185                 slot->results->auth_avail = 0;
186 #endif
187
188                 curl_easy_getinfo(slot->curl, CURLINFO_HTTP_CONNECTCODE,
189                         &slot->results->http_connectcode);
190         }
191
192         /* Run callback if appropriate */
193         if (slot->callback_func != NULL)
194                 slot->callback_func(slot->callback_data);
195 }
196
197 #ifdef USE_CURL_MULTI
198 static void process_curl_messages(void)
199 {
200         int num_messages;
201         struct active_request_slot *slot;
202         CURLMsg *curl_message = curl_multi_info_read(curlm, &num_messages);
203
204         while (curl_message != NULL) {
205                 if (curl_message->msg == CURLMSG_DONE) {
206                         int curl_result = curl_message->data.result;
207                         slot = active_queue_head;
208                         while (slot != NULL &&
209                                slot->curl != curl_message->easy_handle)
210                                 slot = slot->next;
211                         if (slot != NULL) {
212                                 curl_multi_remove_handle(curlm, slot->curl);
213                                 slot->curl_result = curl_result;
214                                 finish_active_slot(slot);
215                         } else {
216                                 fprintf(stderr, "Received DONE message for unknown request!\n");
217                         }
218                 } else {
219                         fprintf(stderr, "Unknown CURL message received: %d\n",
220                                 (int)curl_message->msg);
221                 }
222                 curl_message = curl_multi_info_read(curlm, &num_messages);
223         }
224 }
225 #endif
226
227 static int http_options(const char *var, const char *value, void *cb)
228 {
229         if (!strcmp("http.sslverify", var)) {
230                 curl_ssl_verify = git_config_bool(var, value);
231                 return 0;
232         }
233         if (!strcmp("http.sslcipherlist", var))
234                 return git_config_string(&ssl_cipherlist, var, value);
235         if (!strcmp("http.sslversion", var))
236                 return git_config_string(&ssl_version, var, value);
237         if (!strcmp("http.sslcert", var))
238                 return git_config_string(&ssl_cert, var, value);
239 #if LIBCURL_VERSION_NUM >= 0x070903
240         if (!strcmp("http.sslkey", var))
241                 return git_config_string(&ssl_key, var, value);
242 #endif
243 #if LIBCURL_VERSION_NUM >= 0x070908
244         if (!strcmp("http.sslcapath", var))
245                 return git_config_pathname(&ssl_capath, var, value);
246 #endif
247         if (!strcmp("http.sslcainfo", var))
248                 return git_config_pathname(&ssl_cainfo, var, value);
249         if (!strcmp("http.sslcertpasswordprotected", var)) {
250                 ssl_cert_password_required = git_config_bool(var, value);
251                 return 0;
252         }
253         if (!strcmp("http.ssltry", var)) {
254                 curl_ssl_try = git_config_bool(var, value);
255                 return 0;
256         }
257         if (!strcmp("http.minsessions", var)) {
258                 min_curl_sessions = git_config_int(var, value);
259 #ifndef USE_CURL_MULTI
260                 if (min_curl_sessions > 1)
261                         min_curl_sessions = 1;
262 #endif
263                 return 0;
264         }
265 #ifdef USE_CURL_MULTI
266         if (!strcmp("http.maxrequests", var)) {
267                 max_requests = git_config_int(var, value);
268                 return 0;
269         }
270 #endif
271         if (!strcmp("http.lowspeedlimit", var)) {
272                 curl_low_speed_limit = (long)git_config_int(var, value);
273                 return 0;
274         }
275         if (!strcmp("http.lowspeedtime", var)) {
276                 curl_low_speed_time = (long)git_config_int(var, value);
277                 return 0;
278         }
279
280         if (!strcmp("http.noepsv", var)) {
281                 curl_ftp_no_epsv = git_config_bool(var, value);
282                 return 0;
283         }
284         if (!strcmp("http.proxy", var))
285                 return git_config_string(&curl_http_proxy, var, value);
286
287         if (!strcmp("http.proxyauthmethod", var))
288                 return git_config_string(&http_proxy_authmethod, var, value);
289
290         if (!strcmp("http.cookiefile", var))
291                 return git_config_string(&curl_cookie_file, var, value);
292         if (!strcmp("http.savecookies", var)) {
293                 curl_save_cookies = git_config_bool(var, value);
294                 return 0;
295         }
296
297         if (!strcmp("http.postbuffer", var)) {
298                 http_post_buffer = git_config_int(var, value);
299                 if (http_post_buffer < LARGE_PACKET_MAX)
300                         http_post_buffer = LARGE_PACKET_MAX;
301                 return 0;
302         }
303
304         if (!strcmp("http.useragent", var))
305                 return git_config_string(&user_agent, var, value);
306
307         /* Fall back on the default ones */
308         return git_default_config(var, value, cb);
309 }
310
311 static void init_curl_http_auth(CURL *result)
312 {
313         if (!http_auth.username)
314                 return;
315
316         credential_fill(&http_auth);
317
318 #if LIBCURL_VERSION_NUM >= 0x071301
319         curl_easy_setopt(result, CURLOPT_USERNAME, http_auth.username);
320         curl_easy_setopt(result, CURLOPT_PASSWORD, http_auth.password);
321 #else
322         {
323                 static struct strbuf up = STRBUF_INIT;
324                 /*
325                  * Note that we assume we only ever have a single set of
326                  * credentials in a given program run, so we do not have
327                  * to worry about updating this buffer, only setting its
328                  * initial value.
329                  */
330                 if (!up.len)
331                         strbuf_addf(&up, "%s:%s",
332                                 http_auth.username, http_auth.password);
333                 curl_easy_setopt(result, CURLOPT_USERPWD, up.buf);
334         }
335 #endif
336 }
337
338 /* *var must be free-able */
339 static void var_override(const char **var, char *value)
340 {
341         if (value) {
342                 free((void *)*var);
343                 *var = xstrdup(value);
344         }
345 }
346
347 static void set_proxyauth_name_password(CURL *result)
348 {
349 #if LIBCURL_VERSION_NUM >= 0x071301
350                 curl_easy_setopt(result, CURLOPT_PROXYUSERNAME,
351                         proxy_auth.username);
352                 curl_easy_setopt(result, CURLOPT_PROXYPASSWORD,
353                         proxy_auth.password);
354 #else
355                 struct strbuf s = STRBUF_INIT;
356
357                 strbuf_addstr_urlencode(&s, proxy_auth.username, 1);
358                 strbuf_addch(&s, ':');
359                 strbuf_addstr_urlencode(&s, proxy_auth.password, 1);
360                 curl_proxyuserpwd = strbuf_detach(&s, NULL);
361                 curl_easy_setopt(result, CURLOPT_PROXYUSERPWD, curl_proxyuserpwd);
362 #endif
363 }
364
365 static void init_curl_proxy_auth(CURL *result)
366 {
367         if (proxy_auth.username) {
368                 if (!proxy_auth.password)
369                         credential_fill(&proxy_auth);
370                 set_proxyauth_name_password(result);
371         }
372
373         var_override(&http_proxy_authmethod, getenv("GIT_HTTP_PROXY_AUTHMETHOD"));
374
375 #if LIBCURL_VERSION_NUM >= 0x070a07 /* CURLOPT_PROXYAUTH and CURLAUTH_ANY */
376         if (http_proxy_authmethod) {
377                 int i;
378                 for (i = 0; i < ARRAY_SIZE(proxy_authmethods); i++) {
379                         if (!strcmp(http_proxy_authmethod, proxy_authmethods[i].name)) {
380                                 curl_easy_setopt(result, CURLOPT_PROXYAUTH,
381                                                 proxy_authmethods[i].curlauth_param);
382                                 break;
383                         }
384                 }
385                 if (i == ARRAY_SIZE(proxy_authmethods)) {
386                         warning("unsupported proxy authentication method %s: using anyauth",
387                                         http_proxy_authmethod);
388                         curl_easy_setopt(result, CURLOPT_PROXYAUTH, CURLAUTH_ANY);
389                 }
390         }
391         else
392                 curl_easy_setopt(result, CURLOPT_PROXYAUTH, CURLAUTH_ANY);
393 #endif
394 }
395
396 static int has_cert_password(void)
397 {
398         if (ssl_cert == NULL || ssl_cert_password_required != 1)
399                 return 0;
400         if (!cert_auth.password) {
401                 cert_auth.protocol = xstrdup("cert");
402                 cert_auth.username = xstrdup("");
403                 cert_auth.path = xstrdup(ssl_cert);
404                 credential_fill(&cert_auth);
405         }
406         return 1;
407 }
408
409 #if LIBCURL_VERSION_NUM >= 0x071900
410 static void set_curl_keepalive(CURL *c)
411 {
412         curl_easy_setopt(c, CURLOPT_TCP_KEEPALIVE, 1);
413 }
414
415 #elif LIBCURL_VERSION_NUM >= 0x071000
416 static int sockopt_callback(void *client, curl_socket_t fd, curlsocktype type)
417 {
418         int ka = 1;
419         int rc;
420         socklen_t len = (socklen_t)sizeof(ka);
421
422         if (type != CURLSOCKTYPE_IPCXN)
423                 return 0;
424
425         rc = setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (void *)&ka, len);
426         if (rc < 0)
427                 warning("unable to set SO_KEEPALIVE on socket %s",
428                         strerror(errno));
429
430         return 0; /* CURL_SOCKOPT_OK only exists since curl 7.21.5 */
431 }
432
433 static void set_curl_keepalive(CURL *c)
434 {
435         curl_easy_setopt(c, CURLOPT_SOCKOPTFUNCTION, sockopt_callback);
436 }
437
438 #else
439 static void set_curl_keepalive(CURL *c)
440 {
441         /* not supported on older curl versions */
442 }
443 #endif
444
445 static CURL *get_curl_handle(void)
446 {
447         CURL *result = curl_easy_init();
448         long allowed_protocols = 0;
449
450         if (!result)
451                 die("curl_easy_init failed");
452
453         if (!curl_ssl_verify) {
454                 curl_easy_setopt(result, CURLOPT_SSL_VERIFYPEER, 0);
455                 curl_easy_setopt(result, CURLOPT_SSL_VERIFYHOST, 0);
456         } else {
457                 /* Verify authenticity of the peer's certificate */
458                 curl_easy_setopt(result, CURLOPT_SSL_VERIFYPEER, 1);
459                 /* The name in the cert must match whom we tried to connect */
460                 curl_easy_setopt(result, CURLOPT_SSL_VERIFYHOST, 2);
461         }
462
463 #if LIBCURL_VERSION_NUM >= 0x070907
464         curl_easy_setopt(result, CURLOPT_NETRC, CURL_NETRC_OPTIONAL);
465 #endif
466 #ifdef LIBCURL_CAN_HANDLE_AUTH_ANY
467         curl_easy_setopt(result, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
468 #endif
469
470         if (http_proactive_auth)
471                 init_curl_http_auth(result);
472
473         if (getenv("GIT_SSL_VERSION"))
474                 ssl_version = getenv("GIT_SSL_VERSION");
475         if (ssl_version && *ssl_version) {
476                 int i;
477                 for (i = 0; i < ARRAY_SIZE(sslversions); i++) {
478                         if (!strcmp(ssl_version, sslversions[i].name)) {
479                                 curl_easy_setopt(result, CURLOPT_SSLVERSION,
480                                                  sslversions[i].ssl_version);
481                                 break;
482                         }
483                 }
484                 if (i == ARRAY_SIZE(sslversions))
485                         warning("unsupported ssl version %s: using default",
486                                 ssl_version);
487         }
488
489         if (getenv("GIT_SSL_CIPHER_LIST"))
490                 ssl_cipherlist = getenv("GIT_SSL_CIPHER_LIST");
491         if (ssl_cipherlist != NULL && *ssl_cipherlist)
492                 curl_easy_setopt(result, CURLOPT_SSL_CIPHER_LIST,
493                                 ssl_cipherlist);
494
495         if (ssl_cert != NULL)
496                 curl_easy_setopt(result, CURLOPT_SSLCERT, ssl_cert);
497         if (has_cert_password())
498                 curl_easy_setopt(result, CURLOPT_KEYPASSWD, cert_auth.password);
499 #if LIBCURL_VERSION_NUM >= 0x070903
500         if (ssl_key != NULL)
501                 curl_easy_setopt(result, CURLOPT_SSLKEY, ssl_key);
502 #endif
503 #if LIBCURL_VERSION_NUM >= 0x070908
504         if (ssl_capath != NULL)
505                 curl_easy_setopt(result, CURLOPT_CAPATH, ssl_capath);
506 #endif
507         if (ssl_cainfo != NULL)
508                 curl_easy_setopt(result, CURLOPT_CAINFO, ssl_cainfo);
509
510         if (curl_low_speed_limit > 0 && curl_low_speed_time > 0) {
511                 curl_easy_setopt(result, CURLOPT_LOW_SPEED_LIMIT,
512                                  curl_low_speed_limit);
513                 curl_easy_setopt(result, CURLOPT_LOW_SPEED_TIME,
514                                  curl_low_speed_time);
515         }
516
517         curl_easy_setopt(result, CURLOPT_FOLLOWLOCATION, 1);
518         curl_easy_setopt(result, CURLOPT_MAXREDIRS, 20);
519 #if LIBCURL_VERSION_NUM >= 0x071301
520         curl_easy_setopt(result, CURLOPT_POSTREDIR, CURL_REDIR_POST_ALL);
521 #elif LIBCURL_VERSION_NUM >= 0x071101
522         curl_easy_setopt(result, CURLOPT_POST301, 1);
523 #endif
524 #if LIBCURL_VERSION_NUM >= 0x071304
525         if (is_transport_allowed("http"))
526                 allowed_protocols |= CURLPROTO_HTTP;
527         if (is_transport_allowed("https"))
528                 allowed_protocols |= CURLPROTO_HTTPS;
529         if (is_transport_allowed("ftp"))
530                 allowed_protocols |= CURLPROTO_FTP;
531         if (is_transport_allowed("ftps"))
532                 allowed_protocols |= CURLPROTO_FTPS;
533         curl_easy_setopt(result, CURLOPT_REDIR_PROTOCOLS, allowed_protocols);
534 #else
535         if (transport_restrict_protocols())
536                 warning("protocol restrictions not applied to curl redirects because\n"
537                         "your curl version is too old (>= 7.19.4)");
538 #endif
539
540         if (getenv("GIT_CURL_VERBOSE"))
541                 curl_easy_setopt(result, CURLOPT_VERBOSE, 1);
542
543         curl_easy_setopt(result, CURLOPT_USERAGENT,
544                 user_agent ? user_agent : git_user_agent());
545
546         if (curl_ftp_no_epsv)
547                 curl_easy_setopt(result, CURLOPT_FTP_USE_EPSV, 0);
548
549 #ifdef CURLOPT_USE_SSL
550         if (curl_ssl_try)
551                 curl_easy_setopt(result, CURLOPT_USE_SSL, CURLUSESSL_TRY);
552 #endif
553
554         /*
555          * CURL also examines these variables as a fallback; but we need to query
556          * them here in order to decide whether to prompt for missing password (cf.
557          * init_curl_proxy_auth()).
558          *
559          * Unlike many other common environment variables, these are historically
560          * lowercase only. It appears that CURL did not know this and implemented
561          * only uppercase variants, which was later corrected to take both - with
562          * the exception of http_proxy, which is lowercase only also in CURL. As
563          * the lowercase versions are the historical quasi-standard, they take
564          * precedence here, as in CURL.
565          */
566         if (!curl_http_proxy) {
567                 if (!strcmp(http_auth.protocol, "https")) {
568                         var_override(&curl_http_proxy, getenv("HTTPS_PROXY"));
569                         var_override(&curl_http_proxy, getenv("https_proxy"));
570                 } else {
571                         var_override(&curl_http_proxy, getenv("http_proxy"));
572                 }
573                 if (!curl_http_proxy) {
574                         var_override(&curl_http_proxy, getenv("ALL_PROXY"));
575                         var_override(&curl_http_proxy, getenv("all_proxy"));
576                 }
577         }
578
579         if (curl_http_proxy) {
580                 curl_easy_setopt(result, CURLOPT_PROXY, curl_http_proxy);
581 #if LIBCURL_VERSION_NUM >= 0x071800
582                 if (starts_with(curl_http_proxy, "socks5"))
583                         curl_easy_setopt(result,
584                                 CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
585                 else if (starts_with(curl_http_proxy, "socks4a"))
586                         curl_easy_setopt(result,
587                                 CURLOPT_PROXYTYPE, CURLPROXY_SOCKS4A);
588                 else if (starts_with(curl_http_proxy, "socks"))
589                         curl_easy_setopt(result,
590                                 CURLOPT_PROXYTYPE, CURLPROXY_SOCKS4);
591 #endif
592                 if (strstr(curl_http_proxy, "://"))
593                         credential_from_url(&proxy_auth, curl_http_proxy);
594                 else {
595                         struct strbuf url = STRBUF_INIT;
596                         strbuf_addf(&url, "http://%s", curl_http_proxy);
597                         credential_from_url(&proxy_auth, url.buf);
598                         strbuf_release(&url);
599                 }
600
601                 curl_easy_setopt(result, CURLOPT_PROXY, proxy_auth.host);
602         }
603         init_curl_proxy_auth(result);
604
605         set_curl_keepalive(result);
606
607         return result;
608 }
609
610 static void set_from_env(const char **var, const char *envname)
611 {
612         const char *val = getenv(envname);
613         if (val)
614                 *var = val;
615 }
616
617 void http_init(struct remote *remote, const char *url, int proactive_auth)
618 {
619         char *low_speed_limit;
620         char *low_speed_time;
621         char *normalized_url;
622         struct urlmatch_config config = { STRING_LIST_INIT_DUP };
623
624         config.section = "http";
625         config.key = NULL;
626         config.collect_fn = http_options;
627         config.cascade_fn = git_default_config;
628         config.cb = NULL;
629
630         http_is_verbose = 0;
631         normalized_url = url_normalize(url, &config.url);
632
633         git_config(urlmatch_config_entry, &config);
634         free(normalized_url);
635
636         if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK)
637                 die("curl_global_init failed");
638
639         http_proactive_auth = proactive_auth;
640
641         if (remote && remote->http_proxy)
642                 curl_http_proxy = xstrdup(remote->http_proxy);
643
644         if (remote)
645                 var_override(&http_proxy_authmethod, remote->http_proxy_authmethod);
646
647         pragma_header = curl_slist_append(pragma_header, "Pragma: no-cache");
648         no_pragma_header = curl_slist_append(no_pragma_header, "Pragma:");
649
650 #ifdef USE_CURL_MULTI
651         {
652                 char *http_max_requests = getenv("GIT_HTTP_MAX_REQUESTS");
653                 if (http_max_requests != NULL)
654                         max_requests = atoi(http_max_requests);
655         }
656
657         curlm = curl_multi_init();
658         if (!curlm)
659                 die("curl_multi_init failed");
660 #endif
661
662         if (getenv("GIT_SSL_NO_VERIFY"))
663                 curl_ssl_verify = 0;
664
665         set_from_env(&ssl_cert, "GIT_SSL_CERT");
666 #if LIBCURL_VERSION_NUM >= 0x070903
667         set_from_env(&ssl_key, "GIT_SSL_KEY");
668 #endif
669 #if LIBCURL_VERSION_NUM >= 0x070908
670         set_from_env(&ssl_capath, "GIT_SSL_CAPATH");
671 #endif
672         set_from_env(&ssl_cainfo, "GIT_SSL_CAINFO");
673
674         set_from_env(&user_agent, "GIT_HTTP_USER_AGENT");
675
676         low_speed_limit = getenv("GIT_HTTP_LOW_SPEED_LIMIT");
677         if (low_speed_limit != NULL)
678                 curl_low_speed_limit = strtol(low_speed_limit, NULL, 10);
679         low_speed_time = getenv("GIT_HTTP_LOW_SPEED_TIME");
680         if (low_speed_time != NULL)
681                 curl_low_speed_time = strtol(low_speed_time, NULL, 10);
682
683         if (curl_ssl_verify == -1)
684                 curl_ssl_verify = 1;
685
686         curl_session_count = 0;
687 #ifdef USE_CURL_MULTI
688         if (max_requests < 1)
689                 max_requests = DEFAULT_MAX_REQUESTS;
690 #endif
691
692         if (getenv("GIT_CURL_FTP_NO_EPSV"))
693                 curl_ftp_no_epsv = 1;
694
695         if (url) {
696                 credential_from_url(&http_auth, url);
697                 if (!ssl_cert_password_required &&
698                     getenv("GIT_SSL_CERT_PASSWORD_PROTECTED") &&
699                     starts_with(url, "https://"))
700                         ssl_cert_password_required = 1;
701         }
702
703 #ifndef NO_CURL_EASY_DUPHANDLE
704         curl_default = get_curl_handle();
705 #endif
706 }
707
708 void http_cleanup(void)
709 {
710         struct active_request_slot *slot = active_queue_head;
711
712         while (slot != NULL) {
713                 struct active_request_slot *next = slot->next;
714                 if (slot->curl != NULL) {
715 #ifdef USE_CURL_MULTI
716                         curl_multi_remove_handle(curlm, slot->curl);
717 #endif
718                         curl_easy_cleanup(slot->curl);
719                 }
720                 free(slot);
721                 slot = next;
722         }
723         active_queue_head = NULL;
724
725 #ifndef NO_CURL_EASY_DUPHANDLE
726         curl_easy_cleanup(curl_default);
727 #endif
728
729 #ifdef USE_CURL_MULTI
730         curl_multi_cleanup(curlm);
731 #endif
732         curl_global_cleanup();
733
734         curl_slist_free_all(pragma_header);
735         pragma_header = NULL;
736
737         curl_slist_free_all(no_pragma_header);
738         no_pragma_header = NULL;
739
740         if (curl_http_proxy) {
741                 free((void *)curl_http_proxy);
742                 curl_http_proxy = NULL;
743         }
744
745         if (proxy_auth.password) {
746                 memset(proxy_auth.password, 0, strlen(proxy_auth.password));
747                 free(proxy_auth.password);
748                 proxy_auth.password = NULL;
749         }
750
751         free((void *)curl_proxyuserpwd);
752         curl_proxyuserpwd = NULL;
753
754         free((void *)http_proxy_authmethod);
755         http_proxy_authmethod = NULL;
756
757         if (cert_auth.password != NULL) {
758                 memset(cert_auth.password, 0, strlen(cert_auth.password));
759                 free(cert_auth.password);
760                 cert_auth.password = NULL;
761         }
762         ssl_cert_password_required = 0;
763
764         free(cached_accept_language);
765         cached_accept_language = NULL;
766 }
767
768 struct active_request_slot *get_active_slot(void)
769 {
770         struct active_request_slot *slot = active_queue_head;
771         struct active_request_slot *newslot;
772
773 #ifdef USE_CURL_MULTI
774         int num_transfers;
775
776         /* Wait for a slot to open up if the queue is full */
777         while (active_requests >= max_requests) {
778                 curl_multi_perform(curlm, &num_transfers);
779                 if (num_transfers < active_requests)
780                         process_curl_messages();
781         }
782 #endif
783
784         while (slot != NULL && slot->in_use)
785                 slot = slot->next;
786
787         if (slot == NULL) {
788                 newslot = xmalloc(sizeof(*newslot));
789                 newslot->curl = NULL;
790                 newslot->in_use = 0;
791                 newslot->next = NULL;
792
793                 slot = active_queue_head;
794                 if (slot == NULL) {
795                         active_queue_head = newslot;
796                 } else {
797                         while (slot->next != NULL)
798                                 slot = slot->next;
799                         slot->next = newslot;
800                 }
801                 slot = newslot;
802         }
803
804         if (slot->curl == NULL) {
805 #ifdef NO_CURL_EASY_DUPHANDLE
806                 slot->curl = get_curl_handle();
807 #else
808                 slot->curl = curl_easy_duphandle(curl_default);
809 #endif
810                 curl_session_count++;
811         }
812
813         active_requests++;
814         slot->in_use = 1;
815         slot->results = NULL;
816         slot->finished = NULL;
817         slot->callback_data = NULL;
818         slot->callback_func = NULL;
819         curl_easy_setopt(slot->curl, CURLOPT_COOKIEFILE, curl_cookie_file);
820         if (curl_save_cookies)
821                 curl_easy_setopt(slot->curl, CURLOPT_COOKIEJAR, curl_cookie_file);
822         curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, pragma_header);
823         curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, curl_errorstr);
824         curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, NULL);
825         curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, NULL);
826         curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, NULL);
827         curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, NULL);
828         curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 0);
829         curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
830         curl_easy_setopt(slot->curl, CURLOPT_FAILONERROR, 1);
831         curl_easy_setopt(slot->curl, CURLOPT_RANGE, NULL);
832
833 #if LIBCURL_VERSION_NUM >= 0x070a08
834         curl_easy_setopt(slot->curl, CURLOPT_IPRESOLVE, git_curl_ipresolve);
835 #endif
836 #ifdef LIBCURL_CAN_HANDLE_AUTH_ANY
837         curl_easy_setopt(slot->curl, CURLOPT_HTTPAUTH, http_auth_methods);
838 #endif
839         if (http_auth.password)
840                 init_curl_http_auth(slot->curl);
841
842         return slot;
843 }
844
845 int start_active_slot(struct active_request_slot *slot)
846 {
847 #ifdef USE_CURL_MULTI
848         CURLMcode curlm_result = curl_multi_add_handle(curlm, slot->curl);
849         int num_transfers;
850
851         if (curlm_result != CURLM_OK &&
852             curlm_result != CURLM_CALL_MULTI_PERFORM) {
853                 active_requests--;
854                 slot->in_use = 0;
855                 return 0;
856         }
857
858         /*
859          * We know there must be something to do, since we just added
860          * something.
861          */
862         curl_multi_perform(curlm, &num_transfers);
863 #endif
864         return 1;
865 }
866
867 #ifdef USE_CURL_MULTI
868 struct fill_chain {
869         void *data;
870         int (*fill)(void *);
871         struct fill_chain *next;
872 };
873
874 static struct fill_chain *fill_cfg;
875
876 void add_fill_function(void *data, int (*fill)(void *))
877 {
878         struct fill_chain *new = xmalloc(sizeof(*new));
879         struct fill_chain **linkp = &fill_cfg;
880         new->data = data;
881         new->fill = fill;
882         new->next = NULL;
883         while (*linkp)
884                 linkp = &(*linkp)->next;
885         *linkp = new;
886 }
887
888 void fill_active_slots(void)
889 {
890         struct active_request_slot *slot = active_queue_head;
891
892         while (active_requests < max_requests) {
893                 struct fill_chain *fill;
894                 for (fill = fill_cfg; fill; fill = fill->next)
895                         if (fill->fill(fill->data))
896                                 break;
897
898                 if (!fill)
899                         break;
900         }
901
902         while (slot != NULL) {
903                 if (!slot->in_use && slot->curl != NULL
904                         && curl_session_count > min_curl_sessions) {
905                         curl_easy_cleanup(slot->curl);
906                         slot->curl = NULL;
907                         curl_session_count--;
908                 }
909                 slot = slot->next;
910         }
911 }
912
913 void step_active_slots(void)
914 {
915         int num_transfers;
916         CURLMcode curlm_result;
917
918         do {
919                 curlm_result = curl_multi_perform(curlm, &num_transfers);
920         } while (curlm_result == CURLM_CALL_MULTI_PERFORM);
921         if (num_transfers < active_requests) {
922                 process_curl_messages();
923                 fill_active_slots();
924         }
925 }
926 #endif
927
928 void run_active_slot(struct active_request_slot *slot)
929 {
930 #ifdef USE_CURL_MULTI
931         fd_set readfds;
932         fd_set writefds;
933         fd_set excfds;
934         int max_fd;
935         struct timeval select_timeout;
936         int finished = 0;
937
938         slot->finished = &finished;
939         while (!finished) {
940                 step_active_slots();
941
942                 if (slot->in_use) {
943 #if LIBCURL_VERSION_NUM >= 0x070f04
944                         long curl_timeout;
945                         curl_multi_timeout(curlm, &curl_timeout);
946                         if (curl_timeout == 0) {
947                                 continue;
948                         } else if (curl_timeout == -1) {
949                                 select_timeout.tv_sec  = 0;
950                                 select_timeout.tv_usec = 50000;
951                         } else {
952                                 select_timeout.tv_sec  =  curl_timeout / 1000;
953                                 select_timeout.tv_usec = (curl_timeout % 1000) * 1000;
954                         }
955 #else
956                         select_timeout.tv_sec  = 0;
957                         select_timeout.tv_usec = 50000;
958 #endif
959
960                         max_fd = -1;
961                         FD_ZERO(&readfds);
962                         FD_ZERO(&writefds);
963                         FD_ZERO(&excfds);
964                         curl_multi_fdset(curlm, &readfds, &writefds, &excfds, &max_fd);
965
966                         /*
967                          * It can happen that curl_multi_timeout returns a pathologically
968                          * long timeout when curl_multi_fdset returns no file descriptors
969                          * to read.  See commit message for more details.
970                          */
971                         if (max_fd < 0 &&
972                             (select_timeout.tv_sec > 0 ||
973                              select_timeout.tv_usec > 50000)) {
974                                 select_timeout.tv_sec  = 0;
975                                 select_timeout.tv_usec = 50000;
976                         }
977
978                         select(max_fd+1, &readfds, &writefds, &excfds, &select_timeout);
979                 }
980         }
981 #else
982         while (slot->in_use) {
983                 slot->curl_result = curl_easy_perform(slot->curl);
984                 finish_active_slot(slot);
985         }
986 #endif
987 }
988
989 static void release_active_slot(struct active_request_slot *slot)
990 {
991         closedown_active_slot(slot);
992         if (slot->curl && curl_session_count > min_curl_sessions) {
993 #ifdef USE_CURL_MULTI
994                 curl_multi_remove_handle(curlm, slot->curl);
995 #endif
996                 curl_easy_cleanup(slot->curl);
997                 slot->curl = NULL;
998                 curl_session_count--;
999         }
1000 #ifdef USE_CURL_MULTI
1001         fill_active_slots();
1002 #endif
1003 }
1004
1005 void finish_all_active_slots(void)
1006 {
1007         struct active_request_slot *slot = active_queue_head;
1008
1009         while (slot != NULL)
1010                 if (slot->in_use) {
1011                         run_active_slot(slot);
1012                         slot = active_queue_head;
1013                 } else {
1014                         slot = slot->next;
1015                 }
1016 }
1017
1018 /* Helpers for modifying and creating URLs */
1019 static inline int needs_quote(int ch)
1020 {
1021         if (((ch >= 'A') && (ch <= 'Z'))
1022                         || ((ch >= 'a') && (ch <= 'z'))
1023                         || ((ch >= '0') && (ch <= '9'))
1024                         || (ch == '/')
1025                         || (ch == '-')
1026                         || (ch == '.'))
1027                 return 0;
1028         return 1;
1029 }
1030
1031 static char *quote_ref_url(const char *base, const char *ref)
1032 {
1033         struct strbuf buf = STRBUF_INIT;
1034         const char *cp;
1035         int ch;
1036
1037         end_url_with_slash(&buf, base);
1038
1039         for (cp = ref; (ch = *cp) != 0; cp++)
1040                 if (needs_quote(ch))
1041                         strbuf_addf(&buf, "%%%02x", ch);
1042                 else
1043                         strbuf_addch(&buf, *cp);
1044
1045         return strbuf_detach(&buf, NULL);
1046 }
1047
1048 void append_remote_object_url(struct strbuf *buf, const char *url,
1049                               const char *hex,
1050                               int only_two_digit_prefix)
1051 {
1052         end_url_with_slash(buf, url);
1053
1054         strbuf_addf(buf, "objects/%.*s/", 2, hex);
1055         if (!only_two_digit_prefix)
1056                 strbuf_addf(buf, "%s", hex+2);
1057 }
1058
1059 char *get_remote_object_url(const char *url, const char *hex,
1060                             int only_two_digit_prefix)
1061 {
1062         struct strbuf buf = STRBUF_INIT;
1063         append_remote_object_url(&buf, url, hex, only_two_digit_prefix);
1064         return strbuf_detach(&buf, NULL);
1065 }
1066
1067 static int handle_curl_result(struct slot_results *results)
1068 {
1069         /*
1070          * If we see a failing http code with CURLE_OK, we have turned off
1071          * FAILONERROR (to keep the server's custom error response), and should
1072          * translate the code into failure here.
1073          */
1074         if (results->curl_result == CURLE_OK &&
1075             results->http_code >= 400) {
1076                 results->curl_result = CURLE_HTTP_RETURNED_ERROR;
1077                 /*
1078                  * Normally curl will already have put the "reason phrase"
1079                  * from the server into curl_errorstr; unfortunately without
1080                  * FAILONERROR it is lost, so we can give only the numeric
1081                  * status code.
1082                  */
1083                 snprintf(curl_errorstr, sizeof(curl_errorstr),
1084                          "The requested URL returned error: %ld",
1085                          results->http_code);
1086         }
1087
1088         if (results->curl_result == CURLE_OK) {
1089                 credential_approve(&http_auth);
1090                 if (proxy_auth.password)
1091                         credential_approve(&proxy_auth);
1092                 return HTTP_OK;
1093         } else if (missing_target(results))
1094                 return HTTP_MISSING_TARGET;
1095         else if (results->http_code == 401) {
1096                 if (http_auth.username && http_auth.password) {
1097                         credential_reject(&http_auth);
1098                         return HTTP_NOAUTH;
1099                 } else {
1100 #ifdef LIBCURL_CAN_HANDLE_AUTH_ANY
1101                         http_auth_methods &= ~CURLAUTH_GSSNEGOTIATE;
1102 #endif
1103                         return HTTP_REAUTH;
1104                 }
1105         } else {
1106                 if (results->http_connectcode == 407)
1107                         credential_reject(&proxy_auth);
1108 #if LIBCURL_VERSION_NUM >= 0x070c00
1109                 if (!curl_errorstr[0])
1110                         strlcpy(curl_errorstr,
1111                                 curl_easy_strerror(results->curl_result),
1112                                 sizeof(curl_errorstr));
1113 #endif
1114                 return HTTP_ERROR;
1115         }
1116 }
1117
1118 int run_one_slot(struct active_request_slot *slot,
1119                  struct slot_results *results)
1120 {
1121         slot->results = results;
1122         if (!start_active_slot(slot)) {
1123                 snprintf(curl_errorstr, sizeof(curl_errorstr),
1124                          "failed to start HTTP request");
1125                 return HTTP_START_FAILED;
1126         }
1127
1128         run_active_slot(slot);
1129         return handle_curl_result(results);
1130 }
1131
1132 static CURLcode curlinfo_strbuf(CURL *curl, CURLINFO info, struct strbuf *buf)
1133 {
1134         char *ptr;
1135         CURLcode ret;
1136
1137         strbuf_reset(buf);
1138         ret = curl_easy_getinfo(curl, info, &ptr);
1139         if (!ret && ptr)
1140                 strbuf_addstr(buf, ptr);
1141         return ret;
1142 }
1143
1144 /*
1145  * Check for and extract a content-type parameter. "raw"
1146  * should be positioned at the start of the potential
1147  * parameter, with any whitespace already removed.
1148  *
1149  * "name" is the name of the parameter. The value is appended
1150  * to "out".
1151  */
1152 static int extract_param(const char *raw, const char *name,
1153                          struct strbuf *out)
1154 {
1155         size_t len = strlen(name);
1156
1157         if (strncasecmp(raw, name, len))
1158                 return -1;
1159         raw += len;
1160
1161         if (*raw != '=')
1162                 return -1;
1163         raw++;
1164
1165         while (*raw && !isspace(*raw) && *raw != ';')
1166                 strbuf_addch(out, *raw++);
1167         return 0;
1168 }
1169
1170 /*
1171  * Extract a normalized version of the content type, with any
1172  * spaces suppressed, all letters lowercased, and no trailing ";"
1173  * or parameters.
1174  *
1175  * Note that we will silently remove even invalid whitespace. For
1176  * example, "text / plain" is specifically forbidden by RFC 2616,
1177  * but "text/plain" is the only reasonable output, and this keeps
1178  * our code simple.
1179  *
1180  * If the "charset" argument is not NULL, store the value of any
1181  * charset parameter there.
1182  *
1183  * Example:
1184  *   "TEXT/PLAIN; charset=utf-8" -> "text/plain", "utf-8"
1185  *   "text / plain" -> "text/plain"
1186  */
1187 static void extract_content_type(struct strbuf *raw, struct strbuf *type,
1188                                  struct strbuf *charset)
1189 {
1190         const char *p;
1191
1192         strbuf_reset(type);
1193         strbuf_grow(type, raw->len);
1194         for (p = raw->buf; *p; p++) {
1195                 if (isspace(*p))
1196                         continue;
1197                 if (*p == ';') {
1198                         p++;
1199                         break;
1200                 }
1201                 strbuf_addch(type, tolower(*p));
1202         }
1203
1204         if (!charset)
1205                 return;
1206
1207         strbuf_reset(charset);
1208         while (*p) {
1209                 while (isspace(*p) || *p == ';')
1210                         p++;
1211                 if (!extract_param(p, "charset", charset))
1212                         return;
1213                 while (*p && !isspace(*p))
1214                         p++;
1215         }
1216
1217         if (!charset->len && starts_with(type->buf, "text/"))
1218                 strbuf_addstr(charset, "ISO-8859-1");
1219 }
1220
1221 static void write_accept_language(struct strbuf *buf)
1222 {
1223         /*
1224          * MAX_DECIMAL_PLACES must not be larger than 3. If it is larger than
1225          * that, q-value will be smaller than 0.001, the minimum q-value the
1226          * HTTP specification allows. See
1227          * http://tools.ietf.org/html/rfc7231#section-5.3.1 for q-value.
1228          */
1229         const int MAX_DECIMAL_PLACES = 3;
1230         const int MAX_LANGUAGE_TAGS = 1000;
1231         const int MAX_ACCEPT_LANGUAGE_HEADER_SIZE = 4000;
1232         char **language_tags = NULL;
1233         int num_langs = 0;
1234         const char *s = get_preferred_languages();
1235         int i;
1236         struct strbuf tag = STRBUF_INIT;
1237
1238         /* Don't add Accept-Language header if no language is preferred. */
1239         if (!s)
1240                 return;
1241
1242         /*
1243          * Split the colon-separated string of preferred languages into
1244          * language_tags array.
1245          */
1246         do {
1247                 /* collect language tag */
1248                 for (; *s && (isalnum(*s) || *s == '_'); s++)
1249                         strbuf_addch(&tag, *s == '_' ? '-' : *s);
1250
1251                 /* skip .codeset, @modifier and any other unnecessary parts */
1252                 while (*s && *s != ':')
1253                         s++;
1254
1255                 if (tag.len) {
1256                         num_langs++;
1257                         REALLOC_ARRAY(language_tags, num_langs);
1258                         language_tags[num_langs - 1] = strbuf_detach(&tag, NULL);
1259                         if (num_langs >= MAX_LANGUAGE_TAGS - 1) /* -1 for '*' */
1260                                 break;
1261                 }
1262         } while (*s++);
1263
1264         /* write Accept-Language header into buf */
1265         if (num_langs) {
1266                 int last_buf_len = 0;
1267                 int max_q;
1268                 int decimal_places;
1269                 char q_format[32];
1270
1271                 /* add '*' */
1272                 REALLOC_ARRAY(language_tags, num_langs + 1);
1273                 language_tags[num_langs++] = "*"; /* it's OK; this won't be freed */
1274
1275                 /* compute decimal_places */
1276                 for (max_q = 1, decimal_places = 0;
1277                      max_q < num_langs && decimal_places <= MAX_DECIMAL_PLACES;
1278                      decimal_places++, max_q *= 10)
1279                         ;
1280
1281                 xsnprintf(q_format, sizeof(q_format), ";q=0.%%0%dd", decimal_places);
1282
1283                 strbuf_addstr(buf, "Accept-Language: ");
1284
1285                 for (i = 0; i < num_langs; i++) {
1286                         if (i > 0)
1287                                 strbuf_addstr(buf, ", ");
1288
1289                         strbuf_addstr(buf, language_tags[i]);
1290
1291                         if (i > 0)
1292                                 strbuf_addf(buf, q_format, max_q - i);
1293
1294                         if (buf->len > MAX_ACCEPT_LANGUAGE_HEADER_SIZE) {
1295                                 strbuf_remove(buf, last_buf_len, buf->len - last_buf_len);
1296                                 break;
1297                         }
1298
1299                         last_buf_len = buf->len;
1300                 }
1301         }
1302
1303         /* free language tags -- last one is a static '*' */
1304         for (i = 0; i < num_langs - 1; i++)
1305                 free(language_tags[i]);
1306         free(language_tags);
1307 }
1308
1309 /*
1310  * Get an Accept-Language header which indicates user's preferred languages.
1311  *
1312  * Examples:
1313  *   LANGUAGE= -> ""
1314  *   LANGUAGE=ko:en -> "Accept-Language: ko, en; q=0.9, *; q=0.1"
1315  *   LANGUAGE=ko_KR.UTF-8:sr@latin -> "Accept-Language: ko-KR, sr; q=0.9, *; q=0.1"
1316  *   LANGUAGE=ko LANG=en_US.UTF-8 -> "Accept-Language: ko, *; q=0.1"
1317  *   LANGUAGE= LANG=en_US.UTF-8 -> "Accept-Language: en-US, *; q=0.1"
1318  *   LANGUAGE= LANG=C -> ""
1319  */
1320 static const char *get_accept_language(void)
1321 {
1322         if (!cached_accept_language) {
1323                 struct strbuf buf = STRBUF_INIT;
1324                 write_accept_language(&buf);
1325                 if (buf.len > 0)
1326                         cached_accept_language = strbuf_detach(&buf, NULL);
1327         }
1328
1329         return cached_accept_language;
1330 }
1331
1332 static void http_opt_request_remainder(CURL *curl, off_t pos)
1333 {
1334         char buf[128];
1335         xsnprintf(buf, sizeof(buf), "%"PRIuMAX"-", (uintmax_t)pos);
1336         curl_easy_setopt(curl, CURLOPT_RANGE, buf);
1337 }
1338
1339 /* http_request() targets */
1340 #define HTTP_REQUEST_STRBUF     0
1341 #define HTTP_REQUEST_FILE       1
1342
1343 static int http_request(const char *url,
1344                         void *result, int target,
1345                         const struct http_get_options *options)
1346 {
1347         struct active_request_slot *slot;
1348         struct slot_results results;
1349         struct curl_slist *headers = NULL;
1350         struct strbuf buf = STRBUF_INIT;
1351         const char *accept_language;
1352         int ret;
1353
1354         slot = get_active_slot();
1355         curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
1356
1357         if (result == NULL) {
1358                 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 1);
1359         } else {
1360                 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
1361                 curl_easy_setopt(slot->curl, CURLOPT_FILE, result);
1362
1363                 if (target == HTTP_REQUEST_FILE) {
1364                         off_t posn = ftello(result);
1365                         curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION,
1366                                          fwrite);
1367                         if (posn > 0)
1368                                 http_opt_request_remainder(slot->curl, posn);
1369                 } else
1370                         curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION,
1371                                          fwrite_buffer);
1372         }
1373
1374         accept_language = get_accept_language();
1375
1376         if (accept_language)
1377                 headers = curl_slist_append(headers, accept_language);
1378
1379         strbuf_addstr(&buf, "Pragma:");
1380         if (options && options->no_cache)
1381                 strbuf_addstr(&buf, " no-cache");
1382         if (options && options->keep_error)
1383                 curl_easy_setopt(slot->curl, CURLOPT_FAILONERROR, 0);
1384
1385         headers = curl_slist_append(headers, buf.buf);
1386
1387         curl_easy_setopt(slot->curl, CURLOPT_URL, url);
1388         curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
1389         curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "gzip");
1390
1391         ret = run_one_slot(slot, &results);
1392
1393         if (options && options->content_type) {
1394                 struct strbuf raw = STRBUF_INIT;
1395                 curlinfo_strbuf(slot->curl, CURLINFO_CONTENT_TYPE, &raw);
1396                 extract_content_type(&raw, options->content_type,
1397                                      options->charset);
1398                 strbuf_release(&raw);
1399         }
1400
1401         if (options && options->effective_url)
1402                 curlinfo_strbuf(slot->curl, CURLINFO_EFFECTIVE_URL,
1403                                 options->effective_url);
1404
1405         curl_slist_free_all(headers);
1406         strbuf_release(&buf);
1407
1408         return ret;
1409 }
1410
1411 /*
1412  * Update the "base" url to a more appropriate value, as deduced by
1413  * redirects seen when requesting a URL starting with "url".
1414  *
1415  * The "asked" parameter is a URL that we asked curl to access, and must begin
1416  * with "base".
1417  *
1418  * The "got" parameter is the URL that curl reported to us as where we ended
1419  * up.
1420  *
1421  * Returns 1 if we updated the base url, 0 otherwise.
1422  *
1423  * Our basic strategy is to compare "base" and "asked" to find the bits
1424  * specific to our request. We then strip those bits off of "got" to yield the
1425  * new base. So for example, if our base is "http://example.com/foo.git",
1426  * and we ask for "http://example.com/foo.git/info/refs", we might end up
1427  * with "https://other.example.com/foo.git/info/refs". We would want the
1428  * new URL to become "https://other.example.com/foo.git".
1429  *
1430  * Note that this assumes a sane redirect scheme. It's entirely possible
1431  * in the example above to end up at a URL that does not even end in
1432  * "info/refs".  In such a case we simply punt, as there is not much we can
1433  * do (and such a scheme is unlikely to represent a real git repository,
1434  * which means we are likely about to abort anyway).
1435  */
1436 static int update_url_from_redirect(struct strbuf *base,
1437                                     const char *asked,
1438                                     const struct strbuf *got)
1439 {
1440         const char *tail;
1441         size_t tail_len;
1442
1443         if (!strcmp(asked, got->buf))
1444                 return 0;
1445
1446         if (!skip_prefix(asked, base->buf, &tail))
1447                 die("BUG: update_url_from_redirect: %s is not a superset of %s",
1448                     asked, base->buf);
1449
1450         tail_len = strlen(tail);
1451
1452         if (got->len < tail_len ||
1453             strcmp(tail, got->buf + got->len - tail_len))
1454                 return 0; /* insane redirect scheme */
1455
1456         strbuf_reset(base);
1457         strbuf_add(base, got->buf, got->len - tail_len);
1458         return 1;
1459 }
1460
1461 static int http_request_reauth(const char *url,
1462                                void *result, int target,
1463                                struct http_get_options *options)
1464 {
1465         int ret = http_request(url, result, target, options);
1466
1467         if (options && options->effective_url && options->base_url) {
1468                 if (update_url_from_redirect(options->base_url,
1469                                              url, options->effective_url)) {
1470                         credential_from_url(&http_auth, options->base_url->buf);
1471                         url = options->effective_url->buf;
1472                 }
1473         }
1474
1475         if (ret != HTTP_REAUTH)
1476                 return ret;
1477
1478         /*
1479          * If we are using KEEP_ERROR, the previous request may have
1480          * put cruft into our output stream; we should clear it out before
1481          * making our next request. We only know how to do this for
1482          * the strbuf case, but that is enough to satisfy current callers.
1483          */
1484         if (options && options->keep_error) {
1485                 switch (target) {
1486                 case HTTP_REQUEST_STRBUF:
1487                         strbuf_reset(result);
1488                         break;
1489                 default:
1490                         die("BUG: HTTP_KEEP_ERROR is only supported with strbufs");
1491                 }
1492         }
1493
1494         credential_fill(&http_auth);
1495
1496         return http_request(url, result, target, options);
1497 }
1498
1499 int http_get_strbuf(const char *url,
1500                     struct strbuf *result,
1501                     struct http_get_options *options)
1502 {
1503         return http_request_reauth(url, result, HTTP_REQUEST_STRBUF, options);
1504 }
1505
1506 /*
1507  * Downloads a URL and stores the result in the given file.
1508  *
1509  * If a previous interrupted download is detected (i.e. a previous temporary
1510  * file is still around) the download is resumed.
1511  */
1512 static int http_get_file(const char *url, const char *filename,
1513                          struct http_get_options *options)
1514 {
1515         int ret;
1516         struct strbuf tmpfile = STRBUF_INIT;
1517         FILE *result;
1518
1519         strbuf_addf(&tmpfile, "%s.temp", filename);
1520         result = fopen(tmpfile.buf, "a");
1521         if (!result) {
1522                 error("Unable to open local file %s", tmpfile.buf);
1523                 ret = HTTP_ERROR;
1524                 goto cleanup;
1525         }
1526
1527         ret = http_request_reauth(url, result, HTTP_REQUEST_FILE, options);
1528         fclose(result);
1529
1530         if (ret == HTTP_OK && finalize_object_file(tmpfile.buf, filename))
1531                 ret = HTTP_ERROR;
1532 cleanup:
1533         strbuf_release(&tmpfile);
1534         return ret;
1535 }
1536
1537 int http_fetch_ref(const char *base, struct ref *ref)
1538 {
1539         struct http_get_options options = {0};
1540         char *url;
1541         struct strbuf buffer = STRBUF_INIT;
1542         int ret = -1;
1543
1544         options.no_cache = 1;
1545
1546         url = quote_ref_url(base, ref->name);
1547         if (http_get_strbuf(url, &buffer, &options) == HTTP_OK) {
1548                 strbuf_rtrim(&buffer);
1549                 if (buffer.len == 40)
1550                         ret = get_oid_hex(buffer.buf, &ref->old_oid);
1551                 else if (starts_with(buffer.buf, "ref: ")) {
1552                         ref->symref = xstrdup(buffer.buf + 5);
1553                         ret = 0;
1554                 }
1555         }
1556
1557         strbuf_release(&buffer);
1558         free(url);
1559         return ret;
1560 }
1561
1562 /* Helpers for fetching packs */
1563 static char *fetch_pack_index(unsigned char *sha1, const char *base_url)
1564 {
1565         char *url, *tmp;
1566         struct strbuf buf = STRBUF_INIT;
1567
1568         if (http_is_verbose)
1569                 fprintf(stderr, "Getting index for pack %s\n", sha1_to_hex(sha1));
1570
1571         end_url_with_slash(&buf, base_url);
1572         strbuf_addf(&buf, "objects/pack/pack-%s.idx", sha1_to_hex(sha1));
1573         url = strbuf_detach(&buf, NULL);
1574
1575         strbuf_addf(&buf, "%s.temp", sha1_pack_index_name(sha1));
1576         tmp = strbuf_detach(&buf, NULL);
1577
1578         if (http_get_file(url, tmp, NULL) != HTTP_OK) {
1579                 error("Unable to get pack index %s", url);
1580                 free(tmp);
1581                 tmp = NULL;
1582         }
1583
1584         free(url);
1585         return tmp;
1586 }
1587
1588 static int fetch_and_setup_pack_index(struct packed_git **packs_head,
1589         unsigned char *sha1, const char *base_url)
1590 {
1591         struct packed_git *new_pack;
1592         char *tmp_idx = NULL;
1593         int ret;
1594
1595         if (has_pack_index(sha1)) {
1596                 new_pack = parse_pack_index(sha1, sha1_pack_index_name(sha1));
1597                 if (!new_pack)
1598                         return -1; /* parse_pack_index() already issued error message */
1599                 goto add_pack;
1600         }
1601
1602         tmp_idx = fetch_pack_index(sha1, base_url);
1603         if (!tmp_idx)
1604                 return -1;
1605
1606         new_pack = parse_pack_index(sha1, tmp_idx);
1607         if (!new_pack) {
1608                 unlink(tmp_idx);
1609                 free(tmp_idx);
1610
1611                 return -1; /* parse_pack_index() already issued error message */
1612         }
1613
1614         ret = verify_pack_index(new_pack);
1615         if (!ret) {
1616                 close_pack_index(new_pack);
1617                 ret = finalize_object_file(tmp_idx, sha1_pack_index_name(sha1));
1618         }
1619         free(tmp_idx);
1620         if (ret)
1621                 return -1;
1622
1623 add_pack:
1624         new_pack->next = *packs_head;
1625         *packs_head = new_pack;
1626         return 0;
1627 }
1628
1629 int http_get_info_packs(const char *base_url, struct packed_git **packs_head)
1630 {
1631         struct http_get_options options = {0};
1632         int ret = 0, i = 0;
1633         char *url, *data;
1634         struct strbuf buf = STRBUF_INIT;
1635         unsigned char sha1[20];
1636
1637         end_url_with_slash(&buf, base_url);
1638         strbuf_addstr(&buf, "objects/info/packs");
1639         url = strbuf_detach(&buf, NULL);
1640
1641         options.no_cache = 1;
1642         ret = http_get_strbuf(url, &buf, &options);
1643         if (ret != HTTP_OK)
1644                 goto cleanup;
1645
1646         data = buf.buf;
1647         while (i < buf.len) {
1648                 switch (data[i]) {
1649                 case 'P':
1650                         i++;
1651                         if (i + 52 <= buf.len &&
1652                             starts_with(data + i, " pack-") &&
1653                             starts_with(data + i + 46, ".pack\n")) {
1654                                 get_sha1_hex(data + i + 6, sha1);
1655                                 fetch_and_setup_pack_index(packs_head, sha1,
1656                                                       base_url);
1657                                 i += 51;
1658                                 break;
1659                         }
1660                 default:
1661                         while (i < buf.len && data[i] != '\n')
1662                                 i++;
1663                 }
1664                 i++;
1665         }
1666
1667 cleanup:
1668         free(url);
1669         return ret;
1670 }
1671
1672 void release_http_pack_request(struct http_pack_request *preq)
1673 {
1674         if (preq->packfile != NULL) {
1675                 fclose(preq->packfile);
1676                 preq->packfile = NULL;
1677         }
1678         preq->slot = NULL;
1679         free(preq->url);
1680         free(preq);
1681 }
1682
1683 int finish_http_pack_request(struct http_pack_request *preq)
1684 {
1685         struct packed_git **lst;
1686         struct packed_git *p = preq->target;
1687         char *tmp_idx;
1688         size_t len;
1689         struct child_process ip = CHILD_PROCESS_INIT;
1690         const char *ip_argv[8];
1691
1692         close_pack_index(p);
1693
1694         fclose(preq->packfile);
1695         preq->packfile = NULL;
1696
1697         lst = preq->lst;
1698         while (*lst != p)
1699                 lst = &((*lst)->next);
1700         *lst = (*lst)->next;
1701
1702         if (!strip_suffix(preq->tmpfile, ".pack.temp", &len))
1703                 die("BUG: pack tmpfile does not end in .pack.temp?");
1704         tmp_idx = xstrfmt("%.*s.idx.temp", (int)len, preq->tmpfile);
1705
1706         ip_argv[0] = "index-pack";
1707         ip_argv[1] = "-o";
1708         ip_argv[2] = tmp_idx;
1709         ip_argv[3] = preq->tmpfile;
1710         ip_argv[4] = NULL;
1711
1712         ip.argv = ip_argv;
1713         ip.git_cmd = 1;
1714         ip.no_stdin = 1;
1715         ip.no_stdout = 1;
1716
1717         if (run_command(&ip)) {
1718                 unlink(preq->tmpfile);
1719                 unlink(tmp_idx);
1720                 free(tmp_idx);
1721                 return -1;
1722         }
1723
1724         unlink(sha1_pack_index_name(p->sha1));
1725
1726         if (finalize_object_file(preq->tmpfile, sha1_pack_name(p->sha1))
1727          || finalize_object_file(tmp_idx, sha1_pack_index_name(p->sha1))) {
1728                 free(tmp_idx);
1729                 return -1;
1730         }
1731
1732         install_packed_git(p);
1733         free(tmp_idx);
1734         return 0;
1735 }
1736
1737 struct http_pack_request *new_http_pack_request(
1738         struct packed_git *target, const char *base_url)
1739 {
1740         off_t prev_posn = 0;
1741         struct strbuf buf = STRBUF_INIT;
1742         struct http_pack_request *preq;
1743
1744         preq = xcalloc(1, sizeof(*preq));
1745         preq->target = target;
1746
1747         end_url_with_slash(&buf, base_url);
1748         strbuf_addf(&buf, "objects/pack/pack-%s.pack",
1749                 sha1_to_hex(target->sha1));
1750         preq->url = strbuf_detach(&buf, NULL);
1751
1752         snprintf(preq->tmpfile, sizeof(preq->tmpfile), "%s.temp",
1753                 sha1_pack_name(target->sha1));
1754         preq->packfile = fopen(preq->tmpfile, "a");
1755         if (!preq->packfile) {
1756                 error("Unable to open local file %s for pack",
1757                       preq->tmpfile);
1758                 goto abort;
1759         }
1760
1761         preq->slot = get_active_slot();
1762         curl_easy_setopt(preq->slot->curl, CURLOPT_FILE, preq->packfile);
1763         curl_easy_setopt(preq->slot->curl, CURLOPT_WRITEFUNCTION, fwrite);
1764         curl_easy_setopt(preq->slot->curl, CURLOPT_URL, preq->url);
1765         curl_easy_setopt(preq->slot->curl, CURLOPT_HTTPHEADER,
1766                 no_pragma_header);
1767
1768         /*
1769          * If there is data present from a previous transfer attempt,
1770          * resume where it left off
1771          */
1772         prev_posn = ftello(preq->packfile);
1773         if (prev_posn>0) {
1774                 if (http_is_verbose)
1775                         fprintf(stderr,
1776                                 "Resuming fetch of pack %s at byte %"PRIuMAX"\n",
1777                                 sha1_to_hex(target->sha1), (uintmax_t)prev_posn);
1778                 http_opt_request_remainder(preq->slot->curl, prev_posn);
1779         }
1780
1781         return preq;
1782
1783 abort:
1784         free(preq->url);
1785         free(preq);
1786         return NULL;
1787 }
1788
1789 /* Helpers for fetching objects (loose) */
1790 static size_t fwrite_sha1_file(char *ptr, size_t eltsize, size_t nmemb,
1791                                void *data)
1792 {
1793         unsigned char expn[4096];
1794         size_t size = eltsize * nmemb;
1795         int posn = 0;
1796         struct http_object_request *freq =
1797                 (struct http_object_request *)data;
1798         do {
1799                 ssize_t retval = xwrite(freq->localfile,
1800                                         (char *) ptr + posn, size - posn);
1801                 if (retval < 0)
1802                         return posn;
1803                 posn += retval;
1804         } while (posn < size);
1805
1806         freq->stream.avail_in = size;
1807         freq->stream.next_in = (void *)ptr;
1808         do {
1809                 freq->stream.next_out = expn;
1810                 freq->stream.avail_out = sizeof(expn);
1811                 freq->zret = git_inflate(&freq->stream, Z_SYNC_FLUSH);
1812                 git_SHA1_Update(&freq->c, expn,
1813                                 sizeof(expn) - freq->stream.avail_out);
1814         } while (freq->stream.avail_in && freq->zret == Z_OK);
1815         return size;
1816 }
1817
1818 struct http_object_request *new_http_object_request(const char *base_url,
1819         unsigned char *sha1)
1820 {
1821         char *hex = sha1_to_hex(sha1);
1822         const char *filename;
1823         char prevfile[PATH_MAX];
1824         int prevlocal;
1825         char prev_buf[PREV_BUF_SIZE];
1826         ssize_t prev_read = 0;
1827         off_t prev_posn = 0;
1828         struct http_object_request *freq;
1829
1830         freq = xcalloc(1, sizeof(*freq));
1831         hashcpy(freq->sha1, sha1);
1832         freq->localfile = -1;
1833
1834         filename = sha1_file_name(sha1);
1835         snprintf(freq->tmpfile, sizeof(freq->tmpfile),
1836                  "%s.temp", filename);
1837
1838         snprintf(prevfile, sizeof(prevfile), "%s.prev", filename);
1839         unlink_or_warn(prevfile);
1840         rename(freq->tmpfile, prevfile);
1841         unlink_or_warn(freq->tmpfile);
1842
1843         if (freq->localfile != -1)
1844                 error("fd leakage in start: %d", freq->localfile);
1845         freq->localfile = open(freq->tmpfile,
1846                                O_WRONLY | O_CREAT | O_EXCL, 0666);
1847         /*
1848          * This could have failed due to the "lazy directory creation";
1849          * try to mkdir the last path component.
1850          */
1851         if (freq->localfile < 0 && errno == ENOENT) {
1852                 char *dir = strrchr(freq->tmpfile, '/');
1853                 if (dir) {
1854                         *dir = 0;
1855                         mkdir(freq->tmpfile, 0777);
1856                         *dir = '/';
1857                 }
1858                 freq->localfile = open(freq->tmpfile,
1859                                        O_WRONLY | O_CREAT | O_EXCL, 0666);
1860         }
1861
1862         if (freq->localfile < 0) {
1863                 error("Couldn't create temporary file %s: %s",
1864                       freq->tmpfile, strerror(errno));
1865                 goto abort;
1866         }
1867
1868         git_inflate_init(&freq->stream);
1869
1870         git_SHA1_Init(&freq->c);
1871
1872         freq->url = get_remote_object_url(base_url, hex, 0);
1873
1874         /*
1875          * If a previous temp file is present, process what was already
1876          * fetched.
1877          */
1878         prevlocal = open(prevfile, O_RDONLY);
1879         if (prevlocal != -1) {
1880                 do {
1881                         prev_read = xread(prevlocal, prev_buf, PREV_BUF_SIZE);
1882                         if (prev_read>0) {
1883                                 if (fwrite_sha1_file(prev_buf,
1884                                                      1,
1885                                                      prev_read,
1886                                                      freq) == prev_read) {
1887                                         prev_posn += prev_read;
1888                                 } else {
1889                                         prev_read = -1;
1890                                 }
1891                         }
1892                 } while (prev_read > 0);
1893                 close(prevlocal);
1894         }
1895         unlink_or_warn(prevfile);
1896
1897         /*
1898          * Reset inflate/SHA1 if there was an error reading the previous temp
1899          * file; also rewind to the beginning of the local file.
1900          */
1901         if (prev_read == -1) {
1902                 memset(&freq->stream, 0, sizeof(freq->stream));
1903                 git_inflate_init(&freq->stream);
1904                 git_SHA1_Init(&freq->c);
1905                 if (prev_posn>0) {
1906                         prev_posn = 0;
1907                         lseek(freq->localfile, 0, SEEK_SET);
1908                         if (ftruncate(freq->localfile, 0) < 0) {
1909                                 error("Couldn't truncate temporary file %s: %s",
1910                                           freq->tmpfile, strerror(errno));
1911                                 goto abort;
1912                         }
1913                 }
1914         }
1915
1916         freq->slot = get_active_slot();
1917
1918         curl_easy_setopt(freq->slot->curl, CURLOPT_FILE, freq);
1919         curl_easy_setopt(freq->slot->curl, CURLOPT_WRITEFUNCTION, fwrite_sha1_file);
1920         curl_easy_setopt(freq->slot->curl, CURLOPT_ERRORBUFFER, freq->errorstr);
1921         curl_easy_setopt(freq->slot->curl, CURLOPT_URL, freq->url);
1922         curl_easy_setopt(freq->slot->curl, CURLOPT_HTTPHEADER, no_pragma_header);
1923
1924         /*
1925          * If we have successfully processed data from a previous fetch
1926          * attempt, only fetch the data we don't already have.
1927          */
1928         if (prev_posn>0) {
1929                 if (http_is_verbose)
1930                         fprintf(stderr,
1931                                 "Resuming fetch of object %s at byte %"PRIuMAX"\n",
1932                                 hex, (uintmax_t)prev_posn);
1933                 http_opt_request_remainder(freq->slot->curl, prev_posn);
1934         }
1935
1936         return freq;
1937
1938 abort:
1939         free(freq->url);
1940         free(freq);
1941         return NULL;
1942 }
1943
1944 void process_http_object_request(struct http_object_request *freq)
1945 {
1946         if (freq->slot == NULL)
1947                 return;
1948         freq->curl_result = freq->slot->curl_result;
1949         freq->http_code = freq->slot->http_code;
1950         freq->slot = NULL;
1951 }
1952
1953 int finish_http_object_request(struct http_object_request *freq)
1954 {
1955         struct stat st;
1956
1957         close(freq->localfile);
1958         freq->localfile = -1;
1959
1960         process_http_object_request(freq);
1961
1962         if (freq->http_code == 416) {
1963                 warning("requested range invalid; we may already have all the data.");
1964         } else if (freq->curl_result != CURLE_OK) {
1965                 if (stat(freq->tmpfile, &st) == 0)
1966                         if (st.st_size == 0)
1967                                 unlink_or_warn(freq->tmpfile);
1968                 return -1;
1969         }
1970
1971         git_inflate_end(&freq->stream);
1972         git_SHA1_Final(freq->real_sha1, &freq->c);
1973         if (freq->zret != Z_STREAM_END) {
1974                 unlink_or_warn(freq->tmpfile);
1975                 return -1;
1976         }
1977         if (hashcmp(freq->sha1, freq->real_sha1)) {
1978                 unlink_or_warn(freq->tmpfile);
1979                 return -1;
1980         }
1981         freq->rename =
1982                 finalize_object_file(freq->tmpfile, sha1_file_name(freq->sha1));
1983
1984         return freq->rename;
1985 }
1986
1987 void abort_http_object_request(struct http_object_request *freq)
1988 {
1989         unlink_or_warn(freq->tmpfile);
1990
1991         release_http_object_request(freq);
1992 }
1993
1994 void release_http_object_request(struct http_object_request *freq)
1995 {
1996         if (freq->localfile != -1) {
1997                 close(freq->localfile);
1998                 freq->localfile = -1;
1999         }
2000         if (freq->url != NULL) {
2001                 free(freq->url);
2002                 freq->url = NULL;
2003         }
2004         if (freq->slot != NULL) {
2005                 freq->slot->callback_func = NULL;
2006                 freq->slot->callback_data = NULL;
2007                 release_active_slot(freq->slot);
2008                 freq->slot = NULL;
2009         }
2010 }