]> git.ipfire.org Git - thirdparty/git.git/blob - http.c
a64005ceb80bfca356845fa228881eebb21c09ac
[thirdparty/git.git] / http.c
1 #include "git-compat-util.h"
2 #include "git-curl-compat.h"
3 #include "hex.h"
4 #include "http.h"
5 #include "config.h"
6 #include "pack.h"
7 #include "sideband.h"
8 #include "run-command.h"
9 #include "url.h"
10 #include "urlmatch.h"
11 #include "credential.h"
12 #include "version.h"
13 #include "pkt-line.h"
14 #include "gettext.h"
15 #include "trace.h"
16 #include "transport.h"
17 #include "packfile.h"
18 #include "string-list.h"
19 #include "object-file.h"
20 #include "object-store-ll.h"
21
22 static struct trace_key trace_curl = TRACE_KEY_INIT(CURL);
23 static int trace_curl_data = 1;
24 static int trace_curl_redact = 1;
25 long int git_curl_ipresolve = CURL_IPRESOLVE_WHATEVER;
26 int active_requests;
27 int http_is_verbose;
28 ssize_t http_post_buffer = 16 * LARGE_PACKET_MAX;
29
30 static int min_curl_sessions = 1;
31 static int curl_session_count;
32 static int max_requests = -1;
33 static CURLM *curlm;
34 static CURL *curl_default;
35
36 #define PREV_BUF_SIZE 4096
37
38 char curl_errorstr[CURL_ERROR_SIZE];
39
40 static int curl_ssl_verify = -1;
41 static int curl_ssl_try;
42 static const char *curl_http_version = NULL;
43 static const char *ssl_cert;
44 static const char *ssl_cert_type;
45 static const char *ssl_cipherlist;
46 static const char *ssl_version;
47 static struct {
48 const char *name;
49 long ssl_version;
50 } sslversions[] = {
51 { "sslv2", CURL_SSLVERSION_SSLv2 },
52 { "sslv3", CURL_SSLVERSION_SSLv3 },
53 { "tlsv1", CURL_SSLVERSION_TLSv1 },
54 #ifdef GIT_CURL_HAVE_CURL_SSLVERSION_TLSv1_0
55 { "tlsv1.0", CURL_SSLVERSION_TLSv1_0 },
56 { "tlsv1.1", CURL_SSLVERSION_TLSv1_1 },
57 { "tlsv1.2", CURL_SSLVERSION_TLSv1_2 },
58 #endif
59 #ifdef GIT_CURL_HAVE_CURL_SSLVERSION_TLSv1_3
60 { "tlsv1.3", CURL_SSLVERSION_TLSv1_3 },
61 #endif
62 };
63 static const char *ssl_key;
64 static const char *ssl_key_type;
65 static const char *ssl_capath;
66 static const char *curl_no_proxy;
67 #ifdef GIT_CURL_HAVE_CURLOPT_PINNEDPUBLICKEY
68 static const char *ssl_pinnedkey;
69 #endif
70 static const char *ssl_cainfo;
71 static long curl_low_speed_limit = -1;
72 static long curl_low_speed_time = -1;
73 static int curl_ftp_no_epsv;
74 static const char *curl_http_proxy;
75 static const char *http_proxy_authmethod;
76
77 static const char *http_proxy_ssl_cert;
78 static const char *http_proxy_ssl_key;
79 static const char *http_proxy_ssl_ca_info;
80 static struct credential proxy_cert_auth = CREDENTIAL_INIT;
81 static int proxy_ssl_cert_password_required;
82
83 static struct {
84 const char *name;
85 long curlauth_param;
86 } proxy_authmethods[] = {
87 { "basic", CURLAUTH_BASIC },
88 { "digest", CURLAUTH_DIGEST },
89 { "negotiate", CURLAUTH_GSSNEGOTIATE },
90 { "ntlm", CURLAUTH_NTLM },
91 { "anyauth", CURLAUTH_ANY },
92 /*
93 * CURLAUTH_DIGEST_IE has no corresponding command-line option in
94 * curl(1) and is not included in CURLAUTH_ANY, so we leave it out
95 * here, too
96 */
97 };
98 #ifdef CURLGSSAPI_DELEGATION_FLAG
99 static const char *curl_deleg;
100 static struct {
101 const char *name;
102 long curl_deleg_param;
103 } curl_deleg_levels[] = {
104 { "none", CURLGSSAPI_DELEGATION_NONE },
105 { "policy", CURLGSSAPI_DELEGATION_POLICY_FLAG },
106 { "always", CURLGSSAPI_DELEGATION_FLAG },
107 };
108 #endif
109
110 static struct credential proxy_auth = CREDENTIAL_INIT;
111 static const char *curl_proxyuserpwd;
112 static const char *curl_cookie_file;
113 static int curl_save_cookies;
114 struct credential http_auth = CREDENTIAL_INIT;
115 static int http_proactive_auth;
116 static const char *user_agent;
117 static int curl_empty_auth = -1;
118
119 enum http_follow_config http_follow_config = HTTP_FOLLOW_INITIAL;
120
121 static struct credential cert_auth = CREDENTIAL_INIT;
122 static int ssl_cert_password_required;
123 static unsigned long http_auth_methods = CURLAUTH_ANY;
124 static int http_auth_methods_restricted;
125 /* Modes for which empty_auth cannot actually help us. */
126 static unsigned long empty_auth_useless =
127 CURLAUTH_BASIC
128 | CURLAUTH_DIGEST_IE
129 | CURLAUTH_DIGEST;
130
131 static struct curl_slist *pragma_header;
132 static struct curl_slist *no_pragma_header;
133 static struct string_list extra_http_headers = STRING_LIST_INIT_DUP;
134
135 static struct curl_slist *host_resolutions;
136
137 static struct active_request_slot *active_queue_head;
138
139 static char *cached_accept_language;
140
141 static char *http_ssl_backend;
142
143 static int http_schannel_check_revoke = 1;
144 /*
145 * With the backend being set to `schannel`, setting sslCAinfo would override
146 * the Certificate Store in cURL v7.60.0 and later, which is not what we want
147 * by default.
148 */
149 static int http_schannel_use_ssl_cainfo;
150
151 size_t fread_buffer(char *ptr, size_t eltsize, size_t nmemb, void *buffer_)
152 {
153 size_t size = eltsize * nmemb;
154 struct buffer *buffer = buffer_;
155
156 if (size > buffer->buf.len - buffer->posn)
157 size = buffer->buf.len - buffer->posn;
158 memcpy(ptr, buffer->buf.buf + buffer->posn, size);
159 buffer->posn += size;
160
161 return size / eltsize;
162 }
163
164 int seek_buffer(void *clientp, curl_off_t offset, int origin)
165 {
166 struct buffer *buffer = clientp;
167
168 if (origin != SEEK_SET)
169 BUG("seek_buffer only handles SEEK_SET");
170 if (offset < 0 || offset >= buffer->buf.len) {
171 error("curl seek would be outside of buffer");
172 return CURL_SEEKFUNC_FAIL;
173 }
174
175 buffer->posn = offset;
176 return CURL_SEEKFUNC_OK;
177 }
178
179 size_t fwrite_buffer(char *ptr, size_t eltsize, size_t nmemb, void *buffer_)
180 {
181 size_t size = eltsize * nmemb;
182 struct strbuf *buffer = buffer_;
183
184 strbuf_add(buffer, ptr, size);
185 return nmemb;
186 }
187
188 /*
189 * A folded header continuation line starts with any number of spaces or
190 * horizontal tab characters (SP or HTAB) as per RFC 7230 section 3.2.
191 * It is not a continuation line if the line starts with any other character.
192 */
193 static inline int is_hdr_continuation(const char *ptr, const size_t size)
194 {
195 return size && (*ptr == ' ' || *ptr == '\t');
196 }
197
198 static size_t fwrite_wwwauth(char *ptr, size_t eltsize, size_t nmemb, void *p UNUSED)
199 {
200 size_t size = eltsize * nmemb;
201 struct strvec *values = &http_auth.wwwauth_headers;
202 struct strbuf buf = STRBUF_INIT;
203 const char *val;
204 size_t val_len;
205
206 /*
207 * Header lines may not come NULL-terminated from libcurl so we must
208 * limit all scans to the maximum length of the header line, or leverage
209 * strbufs for all operations.
210 *
211 * In addition, it is possible that header values can be split over
212 * multiple lines as per RFC 7230. 'Line folding' has been deprecated
213 * but older servers may still emit them. A continuation header field
214 * value is identified as starting with a space or horizontal tab.
215 *
216 * The formal definition of a header field as given in RFC 7230 is:
217 *
218 * header-field = field-name ":" OWS field-value OWS
219 *
220 * field-name = token
221 * field-value = *( field-content / obs-fold )
222 * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
223 * field-vchar = VCHAR / obs-text
224 *
225 * obs-fold = CRLF 1*( SP / HTAB )
226 * ; obsolete line folding
227 * ; see Section 3.2.4
228 */
229
230 /* Start of a new WWW-Authenticate header */
231 if (skip_iprefix_mem(ptr, size, "www-authenticate:", &val, &val_len)) {
232 strbuf_add(&buf, val, val_len);
233
234 /*
235 * Strip the CRLF that should be present at the end of each
236 * field as well as any trailing or leading whitespace from the
237 * value.
238 */
239 strbuf_trim(&buf);
240
241 strvec_push(values, buf.buf);
242 http_auth.header_is_last_match = 1;
243 goto exit;
244 }
245
246 /*
247 * This line could be a continuation of the previously matched header
248 * field. If this is the case then we should append this value to the
249 * end of the previously consumed value.
250 */
251 if (http_auth.header_is_last_match && is_hdr_continuation(ptr, size)) {
252 /*
253 * Trim the CRLF and any leading or trailing from this line.
254 */
255 strbuf_add(&buf, ptr, size);
256 strbuf_trim(&buf);
257
258 /*
259 * At this point we should always have at least one existing
260 * value, even if it is empty. Do not bother appending the new
261 * value if this continuation header is itself empty.
262 */
263 if (!values->nr) {
264 BUG("should have at least one existing header value");
265 } else if (buf.len) {
266 char *prev = xstrdup(values->v[values->nr - 1]);
267
268 /* Join two non-empty values with a single space. */
269 const char *const sp = *prev ? " " : "";
270
271 strvec_pop(values);
272 strvec_pushf(values, "%s%s%s", prev, sp, buf.buf);
273 free(prev);
274 }
275
276 goto exit;
277 }
278
279 /* Not a continuation of a previously matched auth header line. */
280 http_auth.header_is_last_match = 0;
281
282 /*
283 * If this is a HTTP status line and not a header field, this signals
284 * a different HTTP response. libcurl writes all the output of all
285 * response headers of all responses, including redirects.
286 * We only care about the last HTTP request response's headers so clear
287 * the existing array.
288 */
289 if (skip_iprefix_mem(ptr, size, "http/", &val, &val_len))
290 strvec_clear(values);
291
292 exit:
293 strbuf_release(&buf);
294 return size;
295 }
296
297 size_t fwrite_null(char *ptr UNUSED, size_t eltsize UNUSED, size_t nmemb,
298 void *data UNUSED)
299 {
300 return nmemb;
301 }
302
303 static void closedown_active_slot(struct active_request_slot *slot)
304 {
305 active_requests--;
306 slot->in_use = 0;
307 }
308
309 static void finish_active_slot(struct active_request_slot *slot)
310 {
311 closedown_active_slot(slot);
312 curl_easy_getinfo(slot->curl, CURLINFO_HTTP_CODE, &slot->http_code);
313
314 if (slot->finished)
315 (*slot->finished) = 1;
316
317 /* Store slot results so they can be read after the slot is reused */
318 if (slot->results) {
319 slot->results->curl_result = slot->curl_result;
320 slot->results->http_code = slot->http_code;
321 curl_easy_getinfo(slot->curl, CURLINFO_HTTPAUTH_AVAIL,
322 &slot->results->auth_avail);
323
324 curl_easy_getinfo(slot->curl, CURLINFO_HTTP_CONNECTCODE,
325 &slot->results->http_connectcode);
326 }
327
328 /* Run callback if appropriate */
329 if (slot->callback_func)
330 slot->callback_func(slot->callback_data);
331 }
332
333 static void xmulti_remove_handle(struct active_request_slot *slot)
334 {
335 curl_multi_remove_handle(curlm, slot->curl);
336 }
337
338 static void process_curl_messages(void)
339 {
340 int num_messages;
341 struct active_request_slot *slot;
342 CURLMsg *curl_message = curl_multi_info_read(curlm, &num_messages);
343
344 while (curl_message != NULL) {
345 if (curl_message->msg == CURLMSG_DONE) {
346 int curl_result = curl_message->data.result;
347 slot = active_queue_head;
348 while (slot != NULL &&
349 slot->curl != curl_message->easy_handle)
350 slot = slot->next;
351 if (slot) {
352 xmulti_remove_handle(slot);
353 slot->curl_result = curl_result;
354 finish_active_slot(slot);
355 } else {
356 fprintf(stderr, "Received DONE message for unknown request!\n");
357 }
358 } else {
359 fprintf(stderr, "Unknown CURL message received: %d\n",
360 (int)curl_message->msg);
361 }
362 curl_message = curl_multi_info_read(curlm, &num_messages);
363 }
364 }
365
366 static int http_options(const char *var, const char *value,
367 const struct config_context *ctx, void *data)
368 {
369 if (!strcmp("http.version", var)) {
370 return git_config_string(&curl_http_version, var, value);
371 }
372 if (!strcmp("http.sslverify", var)) {
373 curl_ssl_verify = git_config_bool(var, value);
374 return 0;
375 }
376 if (!strcmp("http.sslcipherlist", var))
377 return git_config_string(&ssl_cipherlist, var, value);
378 if (!strcmp("http.sslversion", var))
379 return git_config_string(&ssl_version, var, value);
380 if (!strcmp("http.sslcert", var))
381 return git_config_pathname(&ssl_cert, var, value);
382 if (!strcmp("http.sslcerttype", var))
383 return git_config_string(&ssl_cert_type, var, value);
384 if (!strcmp("http.sslkey", var))
385 return git_config_pathname(&ssl_key, var, value);
386 if (!strcmp("http.sslkeytype", var))
387 return git_config_string(&ssl_key_type, var, value);
388 if (!strcmp("http.sslcapath", var))
389 return git_config_pathname(&ssl_capath, var, value);
390 if (!strcmp("http.sslcainfo", var))
391 return git_config_pathname(&ssl_cainfo, var, value);
392 if (!strcmp("http.sslcertpasswordprotected", var)) {
393 ssl_cert_password_required = git_config_bool(var, value);
394 return 0;
395 }
396 if (!strcmp("http.ssltry", var)) {
397 curl_ssl_try = git_config_bool(var, value);
398 return 0;
399 }
400 if (!strcmp("http.sslbackend", var)) {
401 free(http_ssl_backend);
402 http_ssl_backend = xstrdup_or_null(value);
403 return 0;
404 }
405
406 if (!strcmp("http.schannelcheckrevoke", var)) {
407 http_schannel_check_revoke = git_config_bool(var, value);
408 return 0;
409 }
410
411 if (!strcmp("http.schannelusesslcainfo", var)) {
412 http_schannel_use_ssl_cainfo = git_config_bool(var, value);
413 return 0;
414 }
415
416 if (!strcmp("http.minsessions", var)) {
417 min_curl_sessions = git_config_int(var, value, ctx->kvi);
418 if (min_curl_sessions > 1)
419 min_curl_sessions = 1;
420 return 0;
421 }
422 if (!strcmp("http.maxrequests", var)) {
423 max_requests = git_config_int(var, value, ctx->kvi);
424 return 0;
425 }
426 if (!strcmp("http.lowspeedlimit", var)) {
427 curl_low_speed_limit = (long)git_config_int(var, value, ctx->kvi);
428 return 0;
429 }
430 if (!strcmp("http.lowspeedtime", var)) {
431 curl_low_speed_time = (long)git_config_int(var, value, ctx->kvi);
432 return 0;
433 }
434
435 if (!strcmp("http.noepsv", var)) {
436 curl_ftp_no_epsv = git_config_bool(var, value);
437 return 0;
438 }
439 if (!strcmp("http.proxy", var))
440 return git_config_string(&curl_http_proxy, var, value);
441
442 if (!strcmp("http.proxyauthmethod", var))
443 return git_config_string(&http_proxy_authmethod, var, value);
444
445 if (!strcmp("http.proxysslcert", var))
446 return git_config_string(&http_proxy_ssl_cert, var, value);
447
448 if (!strcmp("http.proxysslkey", var))
449 return git_config_string(&http_proxy_ssl_key, var, value);
450
451 if (!strcmp("http.proxysslcainfo", var))
452 return git_config_string(&http_proxy_ssl_ca_info, var, value);
453
454 if (!strcmp("http.proxysslcertpasswordprotected", var)) {
455 proxy_ssl_cert_password_required = git_config_bool(var, value);
456 return 0;
457 }
458
459 if (!strcmp("http.cookiefile", var))
460 return git_config_pathname(&curl_cookie_file, var, value);
461 if (!strcmp("http.savecookies", var)) {
462 curl_save_cookies = git_config_bool(var, value);
463 return 0;
464 }
465
466 if (!strcmp("http.postbuffer", var)) {
467 http_post_buffer = git_config_ssize_t(var, value, ctx->kvi);
468 if (http_post_buffer < 0)
469 warning(_("negative value for http.postBuffer; defaulting to %d"), LARGE_PACKET_MAX);
470 if (http_post_buffer < LARGE_PACKET_MAX)
471 http_post_buffer = LARGE_PACKET_MAX;
472 return 0;
473 }
474
475 if (!strcmp("http.useragent", var))
476 return git_config_string(&user_agent, var, value);
477
478 if (!strcmp("http.emptyauth", var)) {
479 if (value && !strcmp("auto", value))
480 curl_empty_auth = -1;
481 else
482 curl_empty_auth = git_config_bool(var, value);
483 return 0;
484 }
485
486 if (!strcmp("http.delegation", var)) {
487 #ifdef CURLGSSAPI_DELEGATION_FLAG
488 return git_config_string(&curl_deleg, var, value);
489 #else
490 warning(_("Delegation control is not supported with cURL < 7.22.0"));
491 return 0;
492 #endif
493 }
494
495 if (!strcmp("http.pinnedpubkey", var)) {
496 #ifdef GIT_CURL_HAVE_CURLOPT_PINNEDPUBLICKEY
497 return git_config_pathname(&ssl_pinnedkey, var, value);
498 #else
499 warning(_("Public key pinning not supported with cURL < 7.39.0"));
500 return 0;
501 #endif
502 }
503
504 if (!strcmp("http.extraheader", var)) {
505 if (!value) {
506 return config_error_nonbool(var);
507 } else if (!*value) {
508 string_list_clear(&extra_http_headers, 0);
509 } else {
510 string_list_append(&extra_http_headers, value);
511 }
512 return 0;
513 }
514
515 if (!strcmp("http.curloptresolve", var)) {
516 if (!value) {
517 return config_error_nonbool(var);
518 } else if (!*value) {
519 curl_slist_free_all(host_resolutions);
520 host_resolutions = NULL;
521 } else {
522 host_resolutions = curl_slist_append(host_resolutions, value);
523 }
524 return 0;
525 }
526
527 if (!strcmp("http.followredirects", var)) {
528 if (value && !strcmp(value, "initial"))
529 http_follow_config = HTTP_FOLLOW_INITIAL;
530 else if (git_config_bool(var, value))
531 http_follow_config = HTTP_FOLLOW_ALWAYS;
532 else
533 http_follow_config = HTTP_FOLLOW_NONE;
534 return 0;
535 }
536
537 /* Fall back on the default ones */
538 return git_default_config(var, value, ctx, data);
539 }
540
541 static int curl_empty_auth_enabled(void)
542 {
543 if (curl_empty_auth >= 0)
544 return curl_empty_auth;
545
546 /*
547 * In the automatic case, kick in the empty-auth
548 * hack as long as we would potentially try some
549 * method more exotic than "Basic" or "Digest".
550 *
551 * But only do this when this is our second or
552 * subsequent request, as by then we know what
553 * methods are available.
554 */
555 if (http_auth_methods_restricted &&
556 (http_auth_methods & ~empty_auth_useless))
557 return 1;
558 return 0;
559 }
560
561 static void init_curl_http_auth(CURL *result)
562 {
563 if (!http_auth.username || !*http_auth.username) {
564 if (curl_empty_auth_enabled())
565 curl_easy_setopt(result, CURLOPT_USERPWD, ":");
566 return;
567 }
568
569 credential_fill(&http_auth);
570
571 curl_easy_setopt(result, CURLOPT_USERNAME, http_auth.username);
572 curl_easy_setopt(result, CURLOPT_PASSWORD, http_auth.password);
573 }
574
575 /* *var must be free-able */
576 static void var_override(const char **var, char *value)
577 {
578 if (value) {
579 free((void *)*var);
580 *var = xstrdup(value);
581 }
582 }
583
584 static void set_proxyauth_name_password(CURL *result)
585 {
586 curl_easy_setopt(result, CURLOPT_PROXYUSERNAME,
587 proxy_auth.username);
588 curl_easy_setopt(result, CURLOPT_PROXYPASSWORD,
589 proxy_auth.password);
590 }
591
592 static void init_curl_proxy_auth(CURL *result)
593 {
594 if (proxy_auth.username) {
595 if (!proxy_auth.password)
596 credential_fill(&proxy_auth);
597 set_proxyauth_name_password(result);
598 }
599
600 var_override(&http_proxy_authmethod, getenv("GIT_HTTP_PROXY_AUTHMETHOD"));
601
602 if (http_proxy_authmethod) {
603 int i;
604 for (i = 0; i < ARRAY_SIZE(proxy_authmethods); i++) {
605 if (!strcmp(http_proxy_authmethod, proxy_authmethods[i].name)) {
606 curl_easy_setopt(result, CURLOPT_PROXYAUTH,
607 proxy_authmethods[i].curlauth_param);
608 break;
609 }
610 }
611 if (i == ARRAY_SIZE(proxy_authmethods)) {
612 warning("unsupported proxy authentication method %s: using anyauth",
613 http_proxy_authmethod);
614 curl_easy_setopt(result, CURLOPT_PROXYAUTH, CURLAUTH_ANY);
615 }
616 }
617 else
618 curl_easy_setopt(result, CURLOPT_PROXYAUTH, CURLAUTH_ANY);
619 }
620
621 static int has_cert_password(void)
622 {
623 if (ssl_cert == NULL || ssl_cert_password_required != 1)
624 return 0;
625 if (!cert_auth.password) {
626 cert_auth.protocol = xstrdup("cert");
627 cert_auth.host = xstrdup("");
628 cert_auth.username = xstrdup("");
629 cert_auth.path = xstrdup(ssl_cert);
630 credential_fill(&cert_auth);
631 }
632 return 1;
633 }
634
635 #ifdef GIT_CURL_HAVE_CURLOPT_PROXY_KEYPASSWD
636 static int has_proxy_cert_password(void)
637 {
638 if (http_proxy_ssl_cert == NULL || proxy_ssl_cert_password_required != 1)
639 return 0;
640 if (!proxy_cert_auth.password) {
641 proxy_cert_auth.protocol = xstrdup("cert");
642 proxy_cert_auth.host = xstrdup("");
643 proxy_cert_auth.username = xstrdup("");
644 proxy_cert_auth.path = xstrdup(http_proxy_ssl_cert);
645 credential_fill(&proxy_cert_auth);
646 }
647 return 1;
648 }
649 #endif
650
651 #ifdef GITCURL_HAVE_CURLOPT_TCP_KEEPALIVE
652 static void set_curl_keepalive(CURL *c)
653 {
654 curl_easy_setopt(c, CURLOPT_TCP_KEEPALIVE, 1);
655 }
656
657 #else
658 static int sockopt_callback(void *client, curl_socket_t fd, curlsocktype type)
659 {
660 int ka = 1;
661 int rc;
662 socklen_t len = (socklen_t)sizeof(ka);
663
664 if (type != CURLSOCKTYPE_IPCXN)
665 return 0;
666
667 rc = setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (void *)&ka, len);
668 if (rc < 0)
669 warning_errno("unable to set SO_KEEPALIVE on socket");
670
671 return CURL_SOCKOPT_OK;
672 }
673
674 static void set_curl_keepalive(CURL *c)
675 {
676 curl_easy_setopt(c, CURLOPT_SOCKOPTFUNCTION, sockopt_callback);
677 }
678 #endif
679
680 /* Return 1 if redactions have been made, 0 otherwise. */
681 static int redact_sensitive_header(struct strbuf *header, size_t offset)
682 {
683 int ret = 0;
684 const char *sensitive_header;
685
686 if (trace_curl_redact &&
687 (skip_iprefix(header->buf + offset, "Authorization:", &sensitive_header) ||
688 skip_iprefix(header->buf + offset, "Proxy-Authorization:", &sensitive_header))) {
689 /* The first token is the type, which is OK to log */
690 while (isspace(*sensitive_header))
691 sensitive_header++;
692 while (*sensitive_header && !isspace(*sensitive_header))
693 sensitive_header++;
694 /* Everything else is opaque and possibly sensitive */
695 strbuf_setlen(header, sensitive_header - header->buf);
696 strbuf_addstr(header, " <redacted>");
697 ret = 1;
698 } else if (trace_curl_redact &&
699 skip_iprefix(header->buf + offset, "Cookie:", &sensitive_header)) {
700 struct strbuf redacted_header = STRBUF_INIT;
701 const char *cookie;
702
703 while (isspace(*sensitive_header))
704 sensitive_header++;
705
706 cookie = sensitive_header;
707
708 while (cookie) {
709 char *equals;
710 char *semicolon = strstr(cookie, "; ");
711 if (semicolon)
712 *semicolon = 0;
713 equals = strchrnul(cookie, '=');
714 if (!equals) {
715 /* invalid cookie, just append and continue */
716 strbuf_addstr(&redacted_header, cookie);
717 continue;
718 }
719 strbuf_add(&redacted_header, cookie, equals - cookie);
720 strbuf_addstr(&redacted_header, "=<redacted>");
721 if (semicolon) {
722 /*
723 * There are more cookies. (Or, for some
724 * reason, the input string ends in "; ".)
725 */
726 strbuf_addstr(&redacted_header, "; ");
727 cookie = semicolon + strlen("; ");
728 } else {
729 cookie = NULL;
730 }
731 }
732
733 strbuf_setlen(header, sensitive_header - header->buf);
734 strbuf_addbuf(header, &redacted_header);
735 ret = 1;
736 }
737 return ret;
738 }
739
740 static int match_curl_h2_trace(const char *line, const char **out)
741 {
742 const char *p;
743
744 /*
745 * curl prior to 8.1.0 gives us:
746 *
747 * h2h3 [<header-name>: <header-val>]
748 *
749 * Starting in 8.1.0, the first token became just "h2".
750 */
751 if (skip_iprefix(line, "h2h3 [", out) ||
752 skip_iprefix(line, "h2 [", out))
753 return 1;
754
755 /*
756 * curl 8.3.0 uses:
757 * [HTTP/2] [<stream-id>] [<header-name>: <header-val>]
758 * where <stream-id> is numeric.
759 */
760 if (skip_iprefix(line, "[HTTP/2] [", &p)) {
761 while (isdigit(*p))
762 p++;
763 if (skip_prefix(p, "] [", out))
764 return 1;
765 }
766
767 return 0;
768 }
769
770 /* Redact headers in info */
771 static void redact_sensitive_info_header(struct strbuf *header)
772 {
773 const char *sensitive_header;
774
775 if (trace_curl_redact &&
776 match_curl_h2_trace(header->buf, &sensitive_header)) {
777 if (redact_sensitive_header(header, sensitive_header - header->buf)) {
778 /* redaction ate our closing bracket */
779 strbuf_addch(header, ']');
780 }
781 }
782 }
783
784 static void curl_dump_header(const char *text, unsigned char *ptr, size_t size, int hide_sensitive_header)
785 {
786 struct strbuf out = STRBUF_INIT;
787 struct strbuf **headers, **header;
788
789 strbuf_addf(&out, "%s, %10.10ld bytes (0x%8.8lx)\n",
790 text, (long)size, (long)size);
791 trace_strbuf(&trace_curl, &out);
792 strbuf_reset(&out);
793 strbuf_add(&out, ptr, size);
794 headers = strbuf_split_max(&out, '\n', 0);
795
796 for (header = headers; *header; header++) {
797 if (hide_sensitive_header)
798 redact_sensitive_header(*header, 0);
799 strbuf_insertstr((*header), 0, text);
800 strbuf_insertstr((*header), strlen(text), ": ");
801 strbuf_rtrim((*header));
802 strbuf_addch((*header), '\n');
803 trace_strbuf(&trace_curl, (*header));
804 }
805 strbuf_list_free(headers);
806 strbuf_release(&out);
807 }
808
809 static void curl_dump_data(const char *text, unsigned char *ptr, size_t size)
810 {
811 size_t i;
812 struct strbuf out = STRBUF_INIT;
813 unsigned int width = 60;
814
815 strbuf_addf(&out, "%s, %10.10ld bytes (0x%8.8lx)\n",
816 text, (long)size, (long)size);
817 trace_strbuf(&trace_curl, &out);
818
819 for (i = 0; i < size; i += width) {
820 size_t w;
821
822 strbuf_reset(&out);
823 strbuf_addf(&out, "%s: ", text);
824 for (w = 0; (w < width) && (i + w < size); w++) {
825 unsigned char ch = ptr[i + w];
826
827 strbuf_addch(&out,
828 (ch >= 0x20) && (ch < 0x80)
829 ? ch : '.');
830 }
831 strbuf_addch(&out, '\n');
832 trace_strbuf(&trace_curl, &out);
833 }
834 strbuf_release(&out);
835 }
836
837 static void curl_dump_info(char *data, size_t size)
838 {
839 struct strbuf buf = STRBUF_INIT;
840
841 strbuf_add(&buf, data, size);
842
843 redact_sensitive_info_header(&buf);
844 trace_printf_key(&trace_curl, "== Info: %s", buf.buf);
845
846 strbuf_release(&buf);
847 }
848
849 static int curl_trace(CURL *handle UNUSED, curl_infotype type,
850 char *data, size_t size,
851 void *userp UNUSED)
852 {
853 const char *text;
854 enum { NO_FILTER = 0, DO_FILTER = 1 };
855
856 switch (type) {
857 case CURLINFO_TEXT:
858 curl_dump_info(data, size);
859 break;
860 case CURLINFO_HEADER_OUT:
861 text = "=> Send header";
862 curl_dump_header(text, (unsigned char *)data, size, DO_FILTER);
863 break;
864 case CURLINFO_DATA_OUT:
865 if (trace_curl_data) {
866 text = "=> Send data";
867 curl_dump_data(text, (unsigned char *)data, size);
868 }
869 break;
870 case CURLINFO_SSL_DATA_OUT:
871 if (trace_curl_data) {
872 text = "=> Send SSL data";
873 curl_dump_data(text, (unsigned char *)data, size);
874 }
875 break;
876 case CURLINFO_HEADER_IN:
877 text = "<= Recv header";
878 curl_dump_header(text, (unsigned char *)data, size, NO_FILTER);
879 break;
880 case CURLINFO_DATA_IN:
881 if (trace_curl_data) {
882 text = "<= Recv data";
883 curl_dump_data(text, (unsigned char *)data, size);
884 }
885 break;
886 case CURLINFO_SSL_DATA_IN:
887 if (trace_curl_data) {
888 text = "<= Recv SSL data";
889 curl_dump_data(text, (unsigned char *)data, size);
890 }
891 break;
892
893 default: /* we ignore unknown types by default */
894 return 0;
895 }
896 return 0;
897 }
898
899 void http_trace_curl_no_data(void)
900 {
901 trace_override_envvar(&trace_curl, "1");
902 trace_curl_data = 0;
903 }
904
905 void setup_curl_trace(CURL *handle)
906 {
907 if (!trace_want(&trace_curl))
908 return;
909 curl_easy_setopt(handle, CURLOPT_VERBOSE, 1L);
910 curl_easy_setopt(handle, CURLOPT_DEBUGFUNCTION, curl_trace);
911 curl_easy_setopt(handle, CURLOPT_DEBUGDATA, NULL);
912 }
913
914 static void proto_list_append(struct strbuf *list, const char *proto)
915 {
916 if (!list)
917 return;
918 if (list->len)
919 strbuf_addch(list, ',');
920 strbuf_addstr(list, proto);
921 }
922
923 static long get_curl_allowed_protocols(int from_user, struct strbuf *list)
924 {
925 long bits = 0;
926
927 if (is_transport_allowed("http", from_user)) {
928 bits |= CURLPROTO_HTTP;
929 proto_list_append(list, "http");
930 }
931 if (is_transport_allowed("https", from_user)) {
932 bits |= CURLPROTO_HTTPS;
933 proto_list_append(list, "https");
934 }
935 if (is_transport_allowed("ftp", from_user)) {
936 bits |= CURLPROTO_FTP;
937 proto_list_append(list, "ftp");
938 }
939 if (is_transport_allowed("ftps", from_user)) {
940 bits |= CURLPROTO_FTPS;
941 proto_list_append(list, "ftps");
942 }
943
944 return bits;
945 }
946
947 #ifdef GIT_CURL_HAVE_CURL_HTTP_VERSION_2
948 static int get_curl_http_version_opt(const char *version_string, long *opt)
949 {
950 int i;
951 static struct {
952 const char *name;
953 long opt_token;
954 } choice[] = {
955 { "HTTP/1.1", CURL_HTTP_VERSION_1_1 },
956 { "HTTP/2", CURL_HTTP_VERSION_2 }
957 };
958
959 for (i = 0; i < ARRAY_SIZE(choice); i++) {
960 if (!strcmp(version_string, choice[i].name)) {
961 *opt = choice[i].opt_token;
962 return 0;
963 }
964 }
965
966 warning("unknown value given to http.version: '%s'", version_string);
967 return -1; /* not found */
968 }
969
970 #endif
971
972 static CURL *get_curl_handle(void)
973 {
974 CURL *result = curl_easy_init();
975
976 if (!result)
977 die("curl_easy_init failed");
978
979 if (!curl_ssl_verify) {
980 curl_easy_setopt(result, CURLOPT_SSL_VERIFYPEER, 0);
981 curl_easy_setopt(result, CURLOPT_SSL_VERIFYHOST, 0);
982 } else {
983 /* Verify authenticity of the peer's certificate */
984 curl_easy_setopt(result, CURLOPT_SSL_VERIFYPEER, 1);
985 /* The name in the cert must match whom we tried to connect */
986 curl_easy_setopt(result, CURLOPT_SSL_VERIFYHOST, 2);
987 }
988
989 #ifdef GIT_CURL_HAVE_CURL_HTTP_VERSION_2
990 if (curl_http_version) {
991 long opt;
992 if (!get_curl_http_version_opt(curl_http_version, &opt)) {
993 /* Set request use http version */
994 curl_easy_setopt(result, CURLOPT_HTTP_VERSION, opt);
995 }
996 }
997 #endif
998
999 curl_easy_setopt(result, CURLOPT_NETRC, CURL_NETRC_OPTIONAL);
1000 curl_easy_setopt(result, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
1001
1002 #ifdef CURLGSSAPI_DELEGATION_FLAG
1003 if (curl_deleg) {
1004 int i;
1005 for (i = 0; i < ARRAY_SIZE(curl_deleg_levels); i++) {
1006 if (!strcmp(curl_deleg, curl_deleg_levels[i].name)) {
1007 curl_easy_setopt(result, CURLOPT_GSSAPI_DELEGATION,
1008 curl_deleg_levels[i].curl_deleg_param);
1009 break;
1010 }
1011 }
1012 if (i == ARRAY_SIZE(curl_deleg_levels))
1013 warning("Unknown delegation method '%s': using default",
1014 curl_deleg);
1015 }
1016 #endif
1017
1018 if (http_ssl_backend && !strcmp("schannel", http_ssl_backend) &&
1019 !http_schannel_check_revoke) {
1020 #ifdef GIT_CURL_HAVE_CURLSSLOPT_NO_REVOKE
1021 curl_easy_setopt(result, CURLOPT_SSL_OPTIONS, CURLSSLOPT_NO_REVOKE);
1022 #else
1023 warning(_("CURLSSLOPT_NO_REVOKE not supported with cURL < 7.44.0"));
1024 #endif
1025 }
1026
1027 if (http_proactive_auth)
1028 init_curl_http_auth(result);
1029
1030 if (getenv("GIT_SSL_VERSION"))
1031 ssl_version = getenv("GIT_SSL_VERSION");
1032 if (ssl_version && *ssl_version) {
1033 int i;
1034 for (i = 0; i < ARRAY_SIZE(sslversions); i++) {
1035 if (!strcmp(ssl_version, sslversions[i].name)) {
1036 curl_easy_setopt(result, CURLOPT_SSLVERSION,
1037 sslversions[i].ssl_version);
1038 break;
1039 }
1040 }
1041 if (i == ARRAY_SIZE(sslversions))
1042 warning("unsupported ssl version %s: using default",
1043 ssl_version);
1044 }
1045
1046 if (getenv("GIT_SSL_CIPHER_LIST"))
1047 ssl_cipherlist = getenv("GIT_SSL_CIPHER_LIST");
1048 if (ssl_cipherlist != NULL && *ssl_cipherlist)
1049 curl_easy_setopt(result, CURLOPT_SSL_CIPHER_LIST,
1050 ssl_cipherlist);
1051
1052 if (ssl_cert)
1053 curl_easy_setopt(result, CURLOPT_SSLCERT, ssl_cert);
1054 if (ssl_cert_type)
1055 curl_easy_setopt(result, CURLOPT_SSLCERTTYPE, ssl_cert_type);
1056 if (has_cert_password())
1057 curl_easy_setopt(result, CURLOPT_KEYPASSWD, cert_auth.password);
1058 if (ssl_key)
1059 curl_easy_setopt(result, CURLOPT_SSLKEY, ssl_key);
1060 if (ssl_key_type)
1061 curl_easy_setopt(result, CURLOPT_SSLKEYTYPE, ssl_key_type);
1062 if (ssl_capath)
1063 curl_easy_setopt(result, CURLOPT_CAPATH, ssl_capath);
1064 #ifdef GIT_CURL_HAVE_CURLOPT_PINNEDPUBLICKEY
1065 if (ssl_pinnedkey)
1066 curl_easy_setopt(result, CURLOPT_PINNEDPUBLICKEY, ssl_pinnedkey);
1067 #endif
1068 if (http_ssl_backend && !strcmp("schannel", http_ssl_backend) &&
1069 !http_schannel_use_ssl_cainfo) {
1070 curl_easy_setopt(result, CURLOPT_CAINFO, NULL);
1071 #ifdef GIT_CURL_HAVE_CURLOPT_PROXY_CAINFO
1072 curl_easy_setopt(result, CURLOPT_PROXY_CAINFO, NULL);
1073 #endif
1074 } else if (ssl_cainfo != NULL || http_proxy_ssl_ca_info != NULL) {
1075 if (ssl_cainfo)
1076 curl_easy_setopt(result, CURLOPT_CAINFO, ssl_cainfo);
1077 #ifdef GIT_CURL_HAVE_CURLOPT_PROXY_CAINFO
1078 if (http_proxy_ssl_ca_info)
1079 curl_easy_setopt(result, CURLOPT_PROXY_CAINFO, http_proxy_ssl_ca_info);
1080 #endif
1081 }
1082
1083 if (curl_low_speed_limit > 0 && curl_low_speed_time > 0) {
1084 curl_easy_setopt(result, CURLOPT_LOW_SPEED_LIMIT,
1085 curl_low_speed_limit);
1086 curl_easy_setopt(result, CURLOPT_LOW_SPEED_TIME,
1087 curl_low_speed_time);
1088 }
1089
1090 curl_easy_setopt(result, CURLOPT_MAXREDIRS, 20);
1091 curl_easy_setopt(result, CURLOPT_POSTREDIR, CURL_REDIR_POST_ALL);
1092
1093 #ifdef GIT_CURL_HAVE_CURLOPT_PROTOCOLS_STR
1094 {
1095 struct strbuf buf = STRBUF_INIT;
1096
1097 get_curl_allowed_protocols(0, &buf);
1098 curl_easy_setopt(result, CURLOPT_REDIR_PROTOCOLS_STR, buf.buf);
1099 strbuf_reset(&buf);
1100
1101 get_curl_allowed_protocols(-1, &buf);
1102 curl_easy_setopt(result, CURLOPT_PROTOCOLS_STR, buf.buf);
1103 strbuf_release(&buf);
1104 }
1105 #else
1106 curl_easy_setopt(result, CURLOPT_REDIR_PROTOCOLS,
1107 get_curl_allowed_protocols(0, NULL));
1108 curl_easy_setopt(result, CURLOPT_PROTOCOLS,
1109 get_curl_allowed_protocols(-1, NULL));
1110 #endif
1111
1112 if (getenv("GIT_CURL_VERBOSE"))
1113 http_trace_curl_no_data();
1114 setup_curl_trace(result);
1115 if (getenv("GIT_TRACE_CURL_NO_DATA"))
1116 trace_curl_data = 0;
1117 if (!git_env_bool("GIT_TRACE_REDACT", 1))
1118 trace_curl_redact = 0;
1119
1120 curl_easy_setopt(result, CURLOPT_USERAGENT,
1121 user_agent ? user_agent : git_user_agent());
1122
1123 if (curl_ftp_no_epsv)
1124 curl_easy_setopt(result, CURLOPT_FTP_USE_EPSV, 0);
1125
1126 if (curl_ssl_try)
1127 curl_easy_setopt(result, CURLOPT_USE_SSL, CURLUSESSL_TRY);
1128
1129 /*
1130 * CURL also examines these variables as a fallback; but we need to query
1131 * them here in order to decide whether to prompt for missing password (cf.
1132 * init_curl_proxy_auth()).
1133 *
1134 * Unlike many other common environment variables, these are historically
1135 * lowercase only. It appears that CURL did not know this and implemented
1136 * only uppercase variants, which was later corrected to take both - with
1137 * the exception of http_proxy, which is lowercase only also in CURL. As
1138 * the lowercase versions are the historical quasi-standard, they take
1139 * precedence here, as in CURL.
1140 */
1141 if (!curl_http_proxy) {
1142 if (http_auth.protocol && !strcmp(http_auth.protocol, "https")) {
1143 var_override(&curl_http_proxy, getenv("HTTPS_PROXY"));
1144 var_override(&curl_http_proxy, getenv("https_proxy"));
1145 } else {
1146 var_override(&curl_http_proxy, getenv("http_proxy"));
1147 }
1148 if (!curl_http_proxy) {
1149 var_override(&curl_http_proxy, getenv("ALL_PROXY"));
1150 var_override(&curl_http_proxy, getenv("all_proxy"));
1151 }
1152 }
1153
1154 if (curl_http_proxy && curl_http_proxy[0] == '\0') {
1155 /*
1156 * Handle case with the empty http.proxy value here to keep
1157 * common code clean.
1158 * NB: empty option disables proxying at all.
1159 */
1160 curl_easy_setopt(result, CURLOPT_PROXY, "");
1161 } else if (curl_http_proxy) {
1162 if (starts_with(curl_http_proxy, "socks5h"))
1163 curl_easy_setopt(result,
1164 CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5_HOSTNAME);
1165 else if (starts_with(curl_http_proxy, "socks5"))
1166 curl_easy_setopt(result,
1167 CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
1168 else if (starts_with(curl_http_proxy, "socks4a"))
1169 curl_easy_setopt(result,
1170 CURLOPT_PROXYTYPE, CURLPROXY_SOCKS4A);
1171 else if (starts_with(curl_http_proxy, "socks"))
1172 curl_easy_setopt(result,
1173 CURLOPT_PROXYTYPE, CURLPROXY_SOCKS4);
1174 #ifdef GIT_CURL_HAVE_CURLOPT_PROXY_KEYPASSWD
1175 else if (starts_with(curl_http_proxy, "https")) {
1176 curl_easy_setopt(result, CURLOPT_PROXYTYPE, CURLPROXY_HTTPS);
1177
1178 if (http_proxy_ssl_cert)
1179 curl_easy_setopt(result, CURLOPT_PROXY_SSLCERT, http_proxy_ssl_cert);
1180
1181 if (http_proxy_ssl_key)
1182 curl_easy_setopt(result, CURLOPT_PROXY_SSLKEY, http_proxy_ssl_key);
1183
1184 if (has_proxy_cert_password())
1185 curl_easy_setopt(result, CURLOPT_PROXY_KEYPASSWD, proxy_cert_auth.password);
1186 }
1187 #endif
1188 if (strstr(curl_http_proxy, "://"))
1189 credential_from_url(&proxy_auth, curl_http_proxy);
1190 else {
1191 struct strbuf url = STRBUF_INIT;
1192 strbuf_addf(&url, "http://%s", curl_http_proxy);
1193 credential_from_url(&proxy_auth, url.buf);
1194 strbuf_release(&url);
1195 }
1196
1197 if (!proxy_auth.host)
1198 die("Invalid proxy URL '%s'", curl_http_proxy);
1199
1200 curl_easy_setopt(result, CURLOPT_PROXY, proxy_auth.host);
1201 var_override(&curl_no_proxy, getenv("NO_PROXY"));
1202 var_override(&curl_no_proxy, getenv("no_proxy"));
1203 curl_easy_setopt(result, CURLOPT_NOPROXY, curl_no_proxy);
1204 }
1205 init_curl_proxy_auth(result);
1206
1207 set_curl_keepalive(result);
1208
1209 return result;
1210 }
1211
1212 static void set_from_env(const char **var, const char *envname)
1213 {
1214 const char *val = getenv(envname);
1215 if (val)
1216 *var = val;
1217 }
1218
1219 void http_init(struct remote *remote, const char *url, int proactive_auth)
1220 {
1221 char *low_speed_limit;
1222 char *low_speed_time;
1223 char *normalized_url;
1224 struct urlmatch_config config = URLMATCH_CONFIG_INIT;
1225
1226 config.section = "http";
1227 config.key = NULL;
1228 config.collect_fn = http_options;
1229 config.cascade_fn = git_default_config;
1230 config.cb = NULL;
1231
1232 http_is_verbose = 0;
1233 normalized_url = url_normalize(url, &config.url);
1234
1235 git_config(urlmatch_config_entry, &config);
1236 free(normalized_url);
1237 string_list_clear(&config.vars, 1);
1238
1239 #ifdef GIT_CURL_HAVE_CURLSSLSET_NO_BACKENDS
1240 if (http_ssl_backend) {
1241 const curl_ssl_backend **backends;
1242 struct strbuf buf = STRBUF_INIT;
1243 int i;
1244
1245 switch (curl_global_sslset(-1, http_ssl_backend, &backends)) {
1246 case CURLSSLSET_UNKNOWN_BACKEND:
1247 strbuf_addf(&buf, _("Unsupported SSL backend '%s'. "
1248 "Supported SSL backends:"),
1249 http_ssl_backend);
1250 for (i = 0; backends[i]; i++)
1251 strbuf_addf(&buf, "\n\t%s", backends[i]->name);
1252 die("%s", buf.buf);
1253 case CURLSSLSET_NO_BACKENDS:
1254 die(_("Could not set SSL backend to '%s': "
1255 "cURL was built without SSL backends"),
1256 http_ssl_backend);
1257 case CURLSSLSET_TOO_LATE:
1258 die(_("Could not set SSL backend to '%s': already set"),
1259 http_ssl_backend);
1260 case CURLSSLSET_OK:
1261 break; /* Okay! */
1262 }
1263 }
1264 #endif
1265
1266 if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK)
1267 die("curl_global_init failed");
1268
1269 http_proactive_auth = proactive_auth;
1270
1271 if (remote && remote->http_proxy)
1272 curl_http_proxy = xstrdup(remote->http_proxy);
1273
1274 if (remote)
1275 var_override(&http_proxy_authmethod, remote->http_proxy_authmethod);
1276
1277 pragma_header = curl_slist_append(http_copy_default_headers(),
1278 "Pragma: no-cache");
1279 no_pragma_header = curl_slist_append(http_copy_default_headers(),
1280 "Pragma:");
1281
1282 {
1283 char *http_max_requests = getenv("GIT_HTTP_MAX_REQUESTS");
1284 if (http_max_requests)
1285 max_requests = atoi(http_max_requests);
1286 }
1287
1288 curlm = curl_multi_init();
1289 if (!curlm)
1290 die("curl_multi_init failed");
1291
1292 if (getenv("GIT_SSL_NO_VERIFY"))
1293 curl_ssl_verify = 0;
1294
1295 set_from_env(&ssl_cert, "GIT_SSL_CERT");
1296 set_from_env(&ssl_cert_type, "GIT_SSL_CERT_TYPE");
1297 set_from_env(&ssl_key, "GIT_SSL_KEY");
1298 set_from_env(&ssl_key_type, "GIT_SSL_KEY_TYPE");
1299 set_from_env(&ssl_capath, "GIT_SSL_CAPATH");
1300 set_from_env(&ssl_cainfo, "GIT_SSL_CAINFO");
1301
1302 set_from_env(&user_agent, "GIT_HTTP_USER_AGENT");
1303
1304 low_speed_limit = getenv("GIT_HTTP_LOW_SPEED_LIMIT");
1305 if (low_speed_limit)
1306 curl_low_speed_limit = strtol(low_speed_limit, NULL, 10);
1307 low_speed_time = getenv("GIT_HTTP_LOW_SPEED_TIME");
1308 if (low_speed_time)
1309 curl_low_speed_time = strtol(low_speed_time, NULL, 10);
1310
1311 if (curl_ssl_verify == -1)
1312 curl_ssl_verify = 1;
1313
1314 curl_session_count = 0;
1315 if (max_requests < 1)
1316 max_requests = DEFAULT_MAX_REQUESTS;
1317
1318 set_from_env(&http_proxy_ssl_cert, "GIT_PROXY_SSL_CERT");
1319 set_from_env(&http_proxy_ssl_key, "GIT_PROXY_SSL_KEY");
1320 set_from_env(&http_proxy_ssl_ca_info, "GIT_PROXY_SSL_CAINFO");
1321
1322 if (getenv("GIT_PROXY_SSL_CERT_PASSWORD_PROTECTED"))
1323 proxy_ssl_cert_password_required = 1;
1324
1325 if (getenv("GIT_CURL_FTP_NO_EPSV"))
1326 curl_ftp_no_epsv = 1;
1327
1328 if (url) {
1329 credential_from_url(&http_auth, url);
1330 if (!ssl_cert_password_required &&
1331 getenv("GIT_SSL_CERT_PASSWORD_PROTECTED") &&
1332 starts_with(url, "https://"))
1333 ssl_cert_password_required = 1;
1334 }
1335
1336 curl_default = get_curl_handle();
1337 }
1338
1339 void http_cleanup(void)
1340 {
1341 struct active_request_slot *slot = active_queue_head;
1342
1343 while (slot != NULL) {
1344 struct active_request_slot *next = slot->next;
1345 if (slot->curl) {
1346 xmulti_remove_handle(slot);
1347 curl_easy_cleanup(slot->curl);
1348 }
1349 free(slot);
1350 slot = next;
1351 }
1352 active_queue_head = NULL;
1353
1354 curl_easy_cleanup(curl_default);
1355
1356 curl_multi_cleanup(curlm);
1357 curl_global_cleanup();
1358
1359 string_list_clear(&extra_http_headers, 0);
1360
1361 curl_slist_free_all(pragma_header);
1362 pragma_header = NULL;
1363
1364 curl_slist_free_all(no_pragma_header);
1365 no_pragma_header = NULL;
1366
1367 curl_slist_free_all(host_resolutions);
1368 host_resolutions = NULL;
1369
1370 if (curl_http_proxy) {
1371 free((void *)curl_http_proxy);
1372 curl_http_proxy = NULL;
1373 }
1374
1375 if (proxy_auth.password) {
1376 memset(proxy_auth.password, 0, strlen(proxy_auth.password));
1377 FREE_AND_NULL(proxy_auth.password);
1378 }
1379
1380 free((void *)curl_proxyuserpwd);
1381 curl_proxyuserpwd = NULL;
1382
1383 free((void *)http_proxy_authmethod);
1384 http_proxy_authmethod = NULL;
1385
1386 if (cert_auth.password) {
1387 memset(cert_auth.password, 0, strlen(cert_auth.password));
1388 FREE_AND_NULL(cert_auth.password);
1389 }
1390 ssl_cert_password_required = 0;
1391
1392 if (proxy_cert_auth.password) {
1393 memset(proxy_cert_auth.password, 0, strlen(proxy_cert_auth.password));
1394 FREE_AND_NULL(proxy_cert_auth.password);
1395 }
1396 proxy_ssl_cert_password_required = 0;
1397
1398 FREE_AND_NULL(cached_accept_language);
1399 }
1400
1401 struct active_request_slot *get_active_slot(void)
1402 {
1403 struct active_request_slot *slot = active_queue_head;
1404 struct active_request_slot *newslot;
1405
1406 int num_transfers;
1407
1408 /* Wait for a slot to open up if the queue is full */
1409 while (active_requests >= max_requests) {
1410 curl_multi_perform(curlm, &num_transfers);
1411 if (num_transfers < active_requests)
1412 process_curl_messages();
1413 }
1414
1415 while (slot != NULL && slot->in_use)
1416 slot = slot->next;
1417
1418 if (!slot) {
1419 newslot = xmalloc(sizeof(*newslot));
1420 newslot->curl = NULL;
1421 newslot->in_use = 0;
1422 newslot->next = NULL;
1423
1424 slot = active_queue_head;
1425 if (!slot) {
1426 active_queue_head = newslot;
1427 } else {
1428 while (slot->next != NULL)
1429 slot = slot->next;
1430 slot->next = newslot;
1431 }
1432 slot = newslot;
1433 }
1434
1435 if (!slot->curl) {
1436 slot->curl = curl_easy_duphandle(curl_default);
1437 curl_session_count++;
1438 }
1439
1440 active_requests++;
1441 slot->in_use = 1;
1442 slot->results = NULL;
1443 slot->finished = NULL;
1444 slot->callback_data = NULL;
1445 slot->callback_func = NULL;
1446 curl_easy_setopt(slot->curl, CURLOPT_COOKIEFILE, curl_cookie_file);
1447 if (curl_save_cookies)
1448 curl_easy_setopt(slot->curl, CURLOPT_COOKIEJAR, curl_cookie_file);
1449 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, pragma_header);
1450 curl_easy_setopt(slot->curl, CURLOPT_RESOLVE, host_resolutions);
1451 curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, curl_errorstr);
1452 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, NULL);
1453 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, NULL);
1454 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, NULL);
1455 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, NULL);
1456 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 0);
1457 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
1458 curl_easy_setopt(slot->curl, CURLOPT_FAILONERROR, 1);
1459 curl_easy_setopt(slot->curl, CURLOPT_RANGE, NULL);
1460
1461 /*
1462 * Default following to off unless "ALWAYS" is configured; this gives
1463 * callers a sane starting point, and they can tweak for individual
1464 * HTTP_FOLLOW_* cases themselves.
1465 */
1466 if (http_follow_config == HTTP_FOLLOW_ALWAYS)
1467 curl_easy_setopt(slot->curl, CURLOPT_FOLLOWLOCATION, 1);
1468 else
1469 curl_easy_setopt(slot->curl, CURLOPT_FOLLOWLOCATION, 0);
1470
1471 curl_easy_setopt(slot->curl, CURLOPT_IPRESOLVE, git_curl_ipresolve);
1472 curl_easy_setopt(slot->curl, CURLOPT_HTTPAUTH, http_auth_methods);
1473 if (http_auth.password || curl_empty_auth_enabled())
1474 init_curl_http_auth(slot->curl);
1475
1476 return slot;
1477 }
1478
1479 int start_active_slot(struct active_request_slot *slot)
1480 {
1481 CURLMcode curlm_result = curl_multi_add_handle(curlm, slot->curl);
1482 int num_transfers;
1483
1484 if (curlm_result != CURLM_OK &&
1485 curlm_result != CURLM_CALL_MULTI_PERFORM) {
1486 warning("curl_multi_add_handle failed: %s",
1487 curl_multi_strerror(curlm_result));
1488 active_requests--;
1489 slot->in_use = 0;
1490 return 0;
1491 }
1492
1493 /*
1494 * We know there must be something to do, since we just added
1495 * something.
1496 */
1497 curl_multi_perform(curlm, &num_transfers);
1498 return 1;
1499 }
1500
1501 struct fill_chain {
1502 void *data;
1503 int (*fill)(void *);
1504 struct fill_chain *next;
1505 };
1506
1507 static struct fill_chain *fill_cfg;
1508
1509 void add_fill_function(void *data, int (*fill)(void *))
1510 {
1511 struct fill_chain *new_fill = xmalloc(sizeof(*new_fill));
1512 struct fill_chain **linkp = &fill_cfg;
1513 new_fill->data = data;
1514 new_fill->fill = fill;
1515 new_fill->next = NULL;
1516 while (*linkp)
1517 linkp = &(*linkp)->next;
1518 *linkp = new_fill;
1519 }
1520
1521 void fill_active_slots(void)
1522 {
1523 struct active_request_slot *slot = active_queue_head;
1524
1525 while (active_requests < max_requests) {
1526 struct fill_chain *fill;
1527 for (fill = fill_cfg; fill; fill = fill->next)
1528 if (fill->fill(fill->data))
1529 break;
1530
1531 if (!fill)
1532 break;
1533 }
1534
1535 while (slot != NULL) {
1536 if (!slot->in_use && slot->curl != NULL
1537 && curl_session_count > min_curl_sessions) {
1538 curl_easy_cleanup(slot->curl);
1539 slot->curl = NULL;
1540 curl_session_count--;
1541 }
1542 slot = slot->next;
1543 }
1544 }
1545
1546 void step_active_slots(void)
1547 {
1548 int num_transfers;
1549 CURLMcode curlm_result;
1550
1551 do {
1552 curlm_result = curl_multi_perform(curlm, &num_transfers);
1553 } while (curlm_result == CURLM_CALL_MULTI_PERFORM);
1554 if (num_transfers < active_requests) {
1555 process_curl_messages();
1556 fill_active_slots();
1557 }
1558 }
1559
1560 void run_active_slot(struct active_request_slot *slot)
1561 {
1562 fd_set readfds;
1563 fd_set writefds;
1564 fd_set excfds;
1565 int max_fd;
1566 struct timeval select_timeout;
1567 int finished = 0;
1568
1569 slot->finished = &finished;
1570 while (!finished) {
1571 step_active_slots();
1572
1573 if (slot->in_use) {
1574 long curl_timeout;
1575 curl_multi_timeout(curlm, &curl_timeout);
1576 if (curl_timeout == 0) {
1577 continue;
1578 } else if (curl_timeout == -1) {
1579 select_timeout.tv_sec = 0;
1580 select_timeout.tv_usec = 50000;
1581 } else {
1582 select_timeout.tv_sec = curl_timeout / 1000;
1583 select_timeout.tv_usec = (curl_timeout % 1000) * 1000;
1584 }
1585
1586 max_fd = -1;
1587 FD_ZERO(&readfds);
1588 FD_ZERO(&writefds);
1589 FD_ZERO(&excfds);
1590 curl_multi_fdset(curlm, &readfds, &writefds, &excfds, &max_fd);
1591
1592 /*
1593 * It can happen that curl_multi_timeout returns a pathologically
1594 * long timeout when curl_multi_fdset returns no file descriptors
1595 * to read. See commit message for more details.
1596 */
1597 if (max_fd < 0 &&
1598 (select_timeout.tv_sec > 0 ||
1599 select_timeout.tv_usec > 50000)) {
1600 select_timeout.tv_sec = 0;
1601 select_timeout.tv_usec = 50000;
1602 }
1603
1604 select(max_fd+1, &readfds, &writefds, &excfds, &select_timeout);
1605 }
1606 }
1607
1608 /*
1609 * The value of slot->finished we set before the loop was used
1610 * to set our "finished" variable when our request completed.
1611 *
1612 * 1. The slot may not have been reused for another requst
1613 * yet, in which case it still has &finished.
1614 *
1615 * 2. The slot may already be in-use to serve another request,
1616 * which can further be divided into two cases:
1617 *
1618 * (a) If call run_active_slot() hasn't been called for that
1619 * other request, slot->finished would have been cleared
1620 * by get_active_slot() and has NULL.
1621 *
1622 * (b) If the request did call run_active_slot(), then the
1623 * call would have updated slot->finished at the beginning
1624 * of this function, and with the clearing of the member
1625 * below, we would find that slot->finished is now NULL.
1626 *
1627 * In all cases, slot->finished has no useful information to
1628 * anybody at this point. Some compilers warn us for
1629 * attempting to smuggle a pointer that is about to become
1630 * invalid, i.e. &finished. We clear it here to assure them.
1631 */
1632 slot->finished = NULL;
1633 }
1634
1635 static void release_active_slot(struct active_request_slot *slot)
1636 {
1637 closedown_active_slot(slot);
1638 if (slot->curl) {
1639 xmulti_remove_handle(slot);
1640 if (curl_session_count > min_curl_sessions) {
1641 curl_easy_cleanup(slot->curl);
1642 slot->curl = NULL;
1643 curl_session_count--;
1644 }
1645 }
1646 fill_active_slots();
1647 }
1648
1649 void finish_all_active_slots(void)
1650 {
1651 struct active_request_slot *slot = active_queue_head;
1652
1653 while (slot != NULL)
1654 if (slot->in_use) {
1655 run_active_slot(slot);
1656 slot = active_queue_head;
1657 } else {
1658 slot = slot->next;
1659 }
1660 }
1661
1662 /* Helpers for modifying and creating URLs */
1663 static inline int needs_quote(int ch)
1664 {
1665 if (((ch >= 'A') && (ch <= 'Z'))
1666 || ((ch >= 'a') && (ch <= 'z'))
1667 || ((ch >= '0') && (ch <= '9'))
1668 || (ch == '/')
1669 || (ch == '-')
1670 || (ch == '.'))
1671 return 0;
1672 return 1;
1673 }
1674
1675 static char *quote_ref_url(const char *base, const char *ref)
1676 {
1677 struct strbuf buf = STRBUF_INIT;
1678 const char *cp;
1679 int ch;
1680
1681 end_url_with_slash(&buf, base);
1682
1683 for (cp = ref; (ch = *cp) != 0; cp++)
1684 if (needs_quote(ch))
1685 strbuf_addf(&buf, "%%%02x", ch);
1686 else
1687 strbuf_addch(&buf, *cp);
1688
1689 return strbuf_detach(&buf, NULL);
1690 }
1691
1692 void append_remote_object_url(struct strbuf *buf, const char *url,
1693 const char *hex,
1694 int only_two_digit_prefix)
1695 {
1696 end_url_with_slash(buf, url);
1697
1698 strbuf_addf(buf, "objects/%.*s/", 2, hex);
1699 if (!only_two_digit_prefix)
1700 strbuf_addstr(buf, hex + 2);
1701 }
1702
1703 char *get_remote_object_url(const char *url, const char *hex,
1704 int only_two_digit_prefix)
1705 {
1706 struct strbuf buf = STRBUF_INIT;
1707 append_remote_object_url(&buf, url, hex, only_two_digit_prefix);
1708 return strbuf_detach(&buf, NULL);
1709 }
1710
1711 void normalize_curl_result(CURLcode *result, long http_code,
1712 char *errorstr, size_t errorlen)
1713 {
1714 /*
1715 * If we see a failing http code with CURLE_OK, we have turned off
1716 * FAILONERROR (to keep the server's custom error response), and should
1717 * translate the code into failure here.
1718 *
1719 * Likewise, if we see a redirect (30x code), that means we turned off
1720 * redirect-following, and we should treat the result as an error.
1721 */
1722 if (*result == CURLE_OK && http_code >= 300) {
1723 *result = CURLE_HTTP_RETURNED_ERROR;
1724 /*
1725 * Normally curl will already have put the "reason phrase"
1726 * from the server into curl_errorstr; unfortunately without
1727 * FAILONERROR it is lost, so we can give only the numeric
1728 * status code.
1729 */
1730 xsnprintf(errorstr, errorlen,
1731 "The requested URL returned error: %ld",
1732 http_code);
1733 }
1734 }
1735
1736 static int handle_curl_result(struct slot_results *results)
1737 {
1738 normalize_curl_result(&results->curl_result, results->http_code,
1739 curl_errorstr, sizeof(curl_errorstr));
1740
1741 if (results->curl_result == CURLE_OK) {
1742 credential_approve(&http_auth);
1743 credential_approve(&proxy_auth);
1744 credential_approve(&cert_auth);
1745 return HTTP_OK;
1746 } else if (results->curl_result == CURLE_SSL_CERTPROBLEM) {
1747 /*
1748 * We can't tell from here whether it's a bad path, bad
1749 * certificate, bad password, or something else wrong
1750 * with the certificate. So we reject the credential to
1751 * avoid caching or saving a bad password.
1752 */
1753 credential_reject(&cert_auth);
1754 return HTTP_NOAUTH;
1755 #ifdef GIT_CURL_HAVE_CURLE_SSL_PINNEDPUBKEYNOTMATCH
1756 } else if (results->curl_result == CURLE_SSL_PINNEDPUBKEYNOTMATCH) {
1757 return HTTP_NOMATCHPUBLICKEY;
1758 #endif
1759 } else if (missing_target(results))
1760 return HTTP_MISSING_TARGET;
1761 else if (results->http_code == 401) {
1762 if (http_auth.username && http_auth.password) {
1763 credential_reject(&http_auth);
1764 return HTTP_NOAUTH;
1765 } else {
1766 http_auth_methods &= ~CURLAUTH_GSSNEGOTIATE;
1767 if (results->auth_avail) {
1768 http_auth_methods &= results->auth_avail;
1769 http_auth_methods_restricted = 1;
1770 }
1771 return HTTP_REAUTH;
1772 }
1773 } else {
1774 if (results->http_connectcode == 407)
1775 credential_reject(&proxy_auth);
1776 if (!curl_errorstr[0])
1777 strlcpy(curl_errorstr,
1778 curl_easy_strerror(results->curl_result),
1779 sizeof(curl_errorstr));
1780 return HTTP_ERROR;
1781 }
1782 }
1783
1784 int run_one_slot(struct active_request_slot *slot,
1785 struct slot_results *results)
1786 {
1787 slot->results = results;
1788 if (!start_active_slot(slot)) {
1789 xsnprintf(curl_errorstr, sizeof(curl_errorstr),
1790 "failed to start HTTP request");
1791 return HTTP_START_FAILED;
1792 }
1793
1794 run_active_slot(slot);
1795 return handle_curl_result(results);
1796 }
1797
1798 struct curl_slist *http_copy_default_headers(void)
1799 {
1800 struct curl_slist *headers = NULL;
1801 const struct string_list_item *item;
1802
1803 for_each_string_list_item(item, &extra_http_headers)
1804 headers = curl_slist_append(headers, item->string);
1805
1806 return headers;
1807 }
1808
1809 static CURLcode curlinfo_strbuf(CURL *curl, CURLINFO info, struct strbuf *buf)
1810 {
1811 char *ptr;
1812 CURLcode ret;
1813
1814 strbuf_reset(buf);
1815 ret = curl_easy_getinfo(curl, info, &ptr);
1816 if (!ret && ptr)
1817 strbuf_addstr(buf, ptr);
1818 return ret;
1819 }
1820
1821 /*
1822 * Check for and extract a content-type parameter. "raw"
1823 * should be positioned at the start of the potential
1824 * parameter, with any whitespace already removed.
1825 *
1826 * "name" is the name of the parameter. The value is appended
1827 * to "out".
1828 */
1829 static int extract_param(const char *raw, const char *name,
1830 struct strbuf *out)
1831 {
1832 size_t len = strlen(name);
1833
1834 if (strncasecmp(raw, name, len))
1835 return -1;
1836 raw += len;
1837
1838 if (*raw != '=')
1839 return -1;
1840 raw++;
1841
1842 while (*raw && !isspace(*raw) && *raw != ';')
1843 strbuf_addch(out, *raw++);
1844 return 0;
1845 }
1846
1847 /*
1848 * Extract a normalized version of the content type, with any
1849 * spaces suppressed, all letters lowercased, and no trailing ";"
1850 * or parameters.
1851 *
1852 * Note that we will silently remove even invalid whitespace. For
1853 * example, "text / plain" is specifically forbidden by RFC 2616,
1854 * but "text/plain" is the only reasonable output, and this keeps
1855 * our code simple.
1856 *
1857 * If the "charset" argument is not NULL, store the value of any
1858 * charset parameter there.
1859 *
1860 * Example:
1861 * "TEXT/PLAIN; charset=utf-8" -> "text/plain", "utf-8"
1862 * "text / plain" -> "text/plain"
1863 */
1864 static void extract_content_type(struct strbuf *raw, struct strbuf *type,
1865 struct strbuf *charset)
1866 {
1867 const char *p;
1868
1869 strbuf_reset(type);
1870 strbuf_grow(type, raw->len);
1871 for (p = raw->buf; *p; p++) {
1872 if (isspace(*p))
1873 continue;
1874 if (*p == ';') {
1875 p++;
1876 break;
1877 }
1878 strbuf_addch(type, tolower(*p));
1879 }
1880
1881 if (!charset)
1882 return;
1883
1884 strbuf_reset(charset);
1885 while (*p) {
1886 while (isspace(*p) || *p == ';')
1887 p++;
1888 if (!extract_param(p, "charset", charset))
1889 return;
1890 while (*p && !isspace(*p))
1891 p++;
1892 }
1893
1894 if (!charset->len && starts_with(type->buf, "text/"))
1895 strbuf_addstr(charset, "ISO-8859-1");
1896 }
1897
1898 static void write_accept_language(struct strbuf *buf)
1899 {
1900 /*
1901 * MAX_DECIMAL_PLACES must not be larger than 3. If it is larger than
1902 * that, q-value will be smaller than 0.001, the minimum q-value the
1903 * HTTP specification allows. See
1904 * http://tools.ietf.org/html/rfc7231#section-5.3.1 for q-value.
1905 */
1906 const int MAX_DECIMAL_PLACES = 3;
1907 const int MAX_LANGUAGE_TAGS = 1000;
1908 const int MAX_ACCEPT_LANGUAGE_HEADER_SIZE = 4000;
1909 char **language_tags = NULL;
1910 int num_langs = 0;
1911 const char *s = get_preferred_languages();
1912 int i;
1913 struct strbuf tag = STRBUF_INIT;
1914
1915 /* Don't add Accept-Language header if no language is preferred. */
1916 if (!s)
1917 return;
1918
1919 /*
1920 * Split the colon-separated string of preferred languages into
1921 * language_tags array.
1922 */
1923 do {
1924 /* collect language tag */
1925 for (; *s && (isalnum(*s) || *s == '_'); s++)
1926 strbuf_addch(&tag, *s == '_' ? '-' : *s);
1927
1928 /* skip .codeset, @modifier and any other unnecessary parts */
1929 while (*s && *s != ':')
1930 s++;
1931
1932 if (tag.len) {
1933 num_langs++;
1934 REALLOC_ARRAY(language_tags, num_langs);
1935 language_tags[num_langs - 1] = strbuf_detach(&tag, NULL);
1936 if (num_langs >= MAX_LANGUAGE_TAGS - 1) /* -1 for '*' */
1937 break;
1938 }
1939 } while (*s++);
1940
1941 /* write Accept-Language header into buf */
1942 if (num_langs) {
1943 int last_buf_len = 0;
1944 int max_q;
1945 int decimal_places;
1946 char q_format[32];
1947
1948 /* add '*' */
1949 REALLOC_ARRAY(language_tags, num_langs + 1);
1950 language_tags[num_langs++] = "*"; /* it's OK; this won't be freed */
1951
1952 /* compute decimal_places */
1953 for (max_q = 1, decimal_places = 0;
1954 max_q < num_langs && decimal_places <= MAX_DECIMAL_PLACES;
1955 decimal_places++, max_q *= 10)
1956 ;
1957
1958 xsnprintf(q_format, sizeof(q_format), ";q=0.%%0%dd", decimal_places);
1959
1960 strbuf_addstr(buf, "Accept-Language: ");
1961
1962 for (i = 0; i < num_langs; i++) {
1963 if (i > 0)
1964 strbuf_addstr(buf, ", ");
1965
1966 strbuf_addstr(buf, language_tags[i]);
1967
1968 if (i > 0)
1969 strbuf_addf(buf, q_format, max_q - i);
1970
1971 if (buf->len > MAX_ACCEPT_LANGUAGE_HEADER_SIZE) {
1972 strbuf_remove(buf, last_buf_len, buf->len - last_buf_len);
1973 break;
1974 }
1975
1976 last_buf_len = buf->len;
1977 }
1978 }
1979
1980 /* free language tags -- last one is a static '*' */
1981 for (i = 0; i < num_langs - 1; i++)
1982 free(language_tags[i]);
1983 free(language_tags);
1984 }
1985
1986 /*
1987 * Get an Accept-Language header which indicates user's preferred languages.
1988 *
1989 * Examples:
1990 * LANGUAGE= -> ""
1991 * LANGUAGE=ko:en -> "Accept-Language: ko, en; q=0.9, *; q=0.1"
1992 * LANGUAGE=ko_KR.UTF-8:sr@latin -> "Accept-Language: ko-KR, sr; q=0.9, *; q=0.1"
1993 * LANGUAGE=ko LANG=en_US.UTF-8 -> "Accept-Language: ko, *; q=0.1"
1994 * LANGUAGE= LANG=en_US.UTF-8 -> "Accept-Language: en-US, *; q=0.1"
1995 * LANGUAGE= LANG=C -> ""
1996 */
1997 const char *http_get_accept_language_header(void)
1998 {
1999 if (!cached_accept_language) {
2000 struct strbuf buf = STRBUF_INIT;
2001 write_accept_language(&buf);
2002 if (buf.len > 0)
2003 cached_accept_language = strbuf_detach(&buf, NULL);
2004 }
2005
2006 return cached_accept_language;
2007 }
2008
2009 static void http_opt_request_remainder(CURL *curl, off_t pos)
2010 {
2011 char buf[128];
2012 xsnprintf(buf, sizeof(buf), "%"PRIuMAX"-", (uintmax_t)pos);
2013 curl_easy_setopt(curl, CURLOPT_RANGE, buf);
2014 }
2015
2016 /* http_request() targets */
2017 #define HTTP_REQUEST_STRBUF 0
2018 #define HTTP_REQUEST_FILE 1
2019
2020 static int http_request(const char *url,
2021 void *result, int target,
2022 const struct http_get_options *options)
2023 {
2024 struct active_request_slot *slot;
2025 struct slot_results results;
2026 struct curl_slist *headers = http_copy_default_headers();
2027 struct strbuf buf = STRBUF_INIT;
2028 const char *accept_language;
2029 int ret;
2030
2031 slot = get_active_slot();
2032 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
2033
2034 if (!result) {
2035 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 1);
2036 } else {
2037 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
2038 curl_easy_setopt(slot->curl, CURLOPT_WRITEDATA, result);
2039
2040 if (target == HTTP_REQUEST_FILE) {
2041 off_t posn = ftello(result);
2042 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION,
2043 fwrite);
2044 if (posn > 0)
2045 http_opt_request_remainder(slot->curl, posn);
2046 } else
2047 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION,
2048 fwrite_buffer);
2049 }
2050
2051 curl_easy_setopt(slot->curl, CURLOPT_HEADERFUNCTION, fwrite_wwwauth);
2052
2053 accept_language = http_get_accept_language_header();
2054
2055 if (accept_language)
2056 headers = curl_slist_append(headers, accept_language);
2057
2058 strbuf_addstr(&buf, "Pragma:");
2059 if (options && options->no_cache)
2060 strbuf_addstr(&buf, " no-cache");
2061 if (options && options->initial_request &&
2062 http_follow_config == HTTP_FOLLOW_INITIAL)
2063 curl_easy_setopt(slot->curl, CURLOPT_FOLLOWLOCATION, 1);
2064
2065 headers = curl_slist_append(headers, buf.buf);
2066
2067 /* Add additional headers here */
2068 if (options && options->extra_headers) {
2069 const struct string_list_item *item;
2070 for_each_string_list_item(item, options->extra_headers) {
2071 headers = curl_slist_append(headers, item->string);
2072 }
2073 }
2074
2075 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
2076 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
2077 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "");
2078 curl_easy_setopt(slot->curl, CURLOPT_FAILONERROR, 0);
2079
2080 ret = run_one_slot(slot, &results);
2081
2082 if (options && options->content_type) {
2083 struct strbuf raw = STRBUF_INIT;
2084 curlinfo_strbuf(slot->curl, CURLINFO_CONTENT_TYPE, &raw);
2085 extract_content_type(&raw, options->content_type,
2086 options->charset);
2087 strbuf_release(&raw);
2088 }
2089
2090 if (options && options->effective_url)
2091 curlinfo_strbuf(slot->curl, CURLINFO_EFFECTIVE_URL,
2092 options->effective_url);
2093
2094 curl_slist_free_all(headers);
2095 strbuf_release(&buf);
2096
2097 return ret;
2098 }
2099
2100 /*
2101 * Update the "base" url to a more appropriate value, as deduced by
2102 * redirects seen when requesting a URL starting with "url".
2103 *
2104 * The "asked" parameter is a URL that we asked curl to access, and must begin
2105 * with "base".
2106 *
2107 * The "got" parameter is the URL that curl reported to us as where we ended
2108 * up.
2109 *
2110 * Returns 1 if we updated the base url, 0 otherwise.
2111 *
2112 * Our basic strategy is to compare "base" and "asked" to find the bits
2113 * specific to our request. We then strip those bits off of "got" to yield the
2114 * new base. So for example, if our base is "http://example.com/foo.git",
2115 * and we ask for "http://example.com/foo.git/info/refs", we might end up
2116 * with "https://other.example.com/foo.git/info/refs". We would want the
2117 * new URL to become "https://other.example.com/foo.git".
2118 *
2119 * Note that this assumes a sane redirect scheme. It's entirely possible
2120 * in the example above to end up at a URL that does not even end in
2121 * "info/refs". In such a case we die. There's not much we can do, such a
2122 * scheme is unlikely to represent a real git repository, and failing to
2123 * rewrite the base opens options for malicious redirects to do funny things.
2124 */
2125 static int update_url_from_redirect(struct strbuf *base,
2126 const char *asked,
2127 const struct strbuf *got)
2128 {
2129 const char *tail;
2130 size_t new_len;
2131
2132 if (!strcmp(asked, got->buf))
2133 return 0;
2134
2135 if (!skip_prefix(asked, base->buf, &tail))
2136 BUG("update_url_from_redirect: %s is not a superset of %s",
2137 asked, base->buf);
2138
2139 new_len = got->len;
2140 if (!strip_suffix_mem(got->buf, &new_len, tail))
2141 die(_("unable to update url base from redirection:\n"
2142 " asked for: %s\n"
2143 " redirect: %s"),
2144 asked, got->buf);
2145
2146 strbuf_reset(base);
2147 strbuf_add(base, got->buf, new_len);
2148
2149 return 1;
2150 }
2151
2152 static int http_request_reauth(const char *url,
2153 void *result, int target,
2154 struct http_get_options *options)
2155 {
2156 int ret = http_request(url, result, target, options);
2157
2158 if (ret != HTTP_OK && ret != HTTP_REAUTH)
2159 return ret;
2160
2161 if (options && options->effective_url && options->base_url) {
2162 if (update_url_from_redirect(options->base_url,
2163 url, options->effective_url)) {
2164 credential_from_url(&http_auth, options->base_url->buf);
2165 url = options->effective_url->buf;
2166 }
2167 }
2168
2169 if (ret != HTTP_REAUTH)
2170 return ret;
2171
2172 /*
2173 * The previous request may have put cruft into our output stream; we
2174 * should clear it out before making our next request.
2175 */
2176 switch (target) {
2177 case HTTP_REQUEST_STRBUF:
2178 strbuf_reset(result);
2179 break;
2180 case HTTP_REQUEST_FILE:
2181 if (fflush(result)) {
2182 error_errno("unable to flush a file");
2183 return HTTP_START_FAILED;
2184 }
2185 rewind(result);
2186 if (ftruncate(fileno(result), 0) < 0) {
2187 error_errno("unable to truncate a file");
2188 return HTTP_START_FAILED;
2189 }
2190 break;
2191 default:
2192 BUG("Unknown http_request target");
2193 }
2194
2195 credential_fill(&http_auth);
2196
2197 return http_request(url, result, target, options);
2198 }
2199
2200 int http_get_strbuf(const char *url,
2201 struct strbuf *result,
2202 struct http_get_options *options)
2203 {
2204 return http_request_reauth(url, result, HTTP_REQUEST_STRBUF, options);
2205 }
2206
2207 /*
2208 * Downloads a URL and stores the result in the given file.
2209 *
2210 * If a previous interrupted download is detected (i.e. a previous temporary
2211 * file is still around) the download is resumed.
2212 */
2213 int http_get_file(const char *url, const char *filename,
2214 struct http_get_options *options)
2215 {
2216 int ret;
2217 struct strbuf tmpfile = STRBUF_INIT;
2218 FILE *result;
2219
2220 strbuf_addf(&tmpfile, "%s.temp", filename);
2221 result = fopen(tmpfile.buf, "a");
2222 if (!result) {
2223 error("Unable to open local file %s", tmpfile.buf);
2224 ret = HTTP_ERROR;
2225 goto cleanup;
2226 }
2227
2228 ret = http_request_reauth(url, result, HTTP_REQUEST_FILE, options);
2229 fclose(result);
2230
2231 if (ret == HTTP_OK && finalize_object_file(tmpfile.buf, filename))
2232 ret = HTTP_ERROR;
2233 cleanup:
2234 strbuf_release(&tmpfile);
2235 return ret;
2236 }
2237
2238 int http_fetch_ref(const char *base, struct ref *ref)
2239 {
2240 struct http_get_options options = {0};
2241 char *url;
2242 struct strbuf buffer = STRBUF_INIT;
2243 int ret = -1;
2244
2245 options.no_cache = 1;
2246
2247 url = quote_ref_url(base, ref->name);
2248 if (http_get_strbuf(url, &buffer, &options) == HTTP_OK) {
2249 strbuf_rtrim(&buffer);
2250 if (buffer.len == the_hash_algo->hexsz)
2251 ret = get_oid_hex(buffer.buf, &ref->old_oid);
2252 else if (starts_with(buffer.buf, "ref: ")) {
2253 ref->symref = xstrdup(buffer.buf + 5);
2254 ret = 0;
2255 }
2256 }
2257
2258 strbuf_release(&buffer);
2259 free(url);
2260 return ret;
2261 }
2262
2263 /* Helpers for fetching packs */
2264 static char *fetch_pack_index(unsigned char *hash, const char *base_url)
2265 {
2266 char *url, *tmp;
2267 struct strbuf buf = STRBUF_INIT;
2268
2269 if (http_is_verbose)
2270 fprintf(stderr, "Getting index for pack %s\n", hash_to_hex(hash));
2271
2272 end_url_with_slash(&buf, base_url);
2273 strbuf_addf(&buf, "objects/pack/pack-%s.idx", hash_to_hex(hash));
2274 url = strbuf_detach(&buf, NULL);
2275
2276 strbuf_addf(&buf, "%s.temp", sha1_pack_index_name(hash));
2277 tmp = strbuf_detach(&buf, NULL);
2278
2279 if (http_get_file(url, tmp, NULL) != HTTP_OK) {
2280 error("Unable to get pack index %s", url);
2281 FREE_AND_NULL(tmp);
2282 }
2283
2284 free(url);
2285 return tmp;
2286 }
2287
2288 static int fetch_and_setup_pack_index(struct packed_git **packs_head,
2289 unsigned char *sha1, const char *base_url)
2290 {
2291 struct packed_git *new_pack;
2292 char *tmp_idx = NULL;
2293 int ret;
2294
2295 if (has_pack_index(sha1)) {
2296 new_pack = parse_pack_index(sha1, sha1_pack_index_name(sha1));
2297 if (!new_pack)
2298 return -1; /* parse_pack_index() already issued error message */
2299 goto add_pack;
2300 }
2301
2302 tmp_idx = fetch_pack_index(sha1, base_url);
2303 if (!tmp_idx)
2304 return -1;
2305
2306 new_pack = parse_pack_index(sha1, tmp_idx);
2307 if (!new_pack) {
2308 unlink(tmp_idx);
2309 free(tmp_idx);
2310
2311 return -1; /* parse_pack_index() already issued error message */
2312 }
2313
2314 ret = verify_pack_index(new_pack);
2315 if (!ret) {
2316 close_pack_index(new_pack);
2317 ret = finalize_object_file(tmp_idx, sha1_pack_index_name(sha1));
2318 }
2319 free(tmp_idx);
2320 if (ret)
2321 return -1;
2322
2323 add_pack:
2324 new_pack->next = *packs_head;
2325 *packs_head = new_pack;
2326 return 0;
2327 }
2328
2329 int http_get_info_packs(const char *base_url, struct packed_git **packs_head)
2330 {
2331 struct http_get_options options = {0};
2332 int ret = 0;
2333 char *url;
2334 const char *data;
2335 struct strbuf buf = STRBUF_INIT;
2336 struct object_id oid;
2337
2338 end_url_with_slash(&buf, base_url);
2339 strbuf_addstr(&buf, "objects/info/packs");
2340 url = strbuf_detach(&buf, NULL);
2341
2342 options.no_cache = 1;
2343 ret = http_get_strbuf(url, &buf, &options);
2344 if (ret != HTTP_OK)
2345 goto cleanup;
2346
2347 data = buf.buf;
2348 while (*data) {
2349 if (skip_prefix(data, "P pack-", &data) &&
2350 !parse_oid_hex(data, &oid, &data) &&
2351 skip_prefix(data, ".pack", &data) &&
2352 (*data == '\n' || *data == '\0')) {
2353 fetch_and_setup_pack_index(packs_head, oid.hash, base_url);
2354 } else {
2355 data = strchrnul(data, '\n');
2356 }
2357 if (*data)
2358 data++; /* skip past newline */
2359 }
2360
2361 cleanup:
2362 free(url);
2363 return ret;
2364 }
2365
2366 void release_http_pack_request(struct http_pack_request *preq)
2367 {
2368 if (preq->packfile) {
2369 fclose(preq->packfile);
2370 preq->packfile = NULL;
2371 }
2372 preq->slot = NULL;
2373 strbuf_release(&preq->tmpfile);
2374 free(preq->url);
2375 free(preq);
2376 }
2377
2378 static const char *default_index_pack_args[] =
2379 {"index-pack", "--stdin", NULL};
2380
2381 int finish_http_pack_request(struct http_pack_request *preq)
2382 {
2383 struct child_process ip = CHILD_PROCESS_INIT;
2384 int tmpfile_fd;
2385 int ret = 0;
2386
2387 fclose(preq->packfile);
2388 preq->packfile = NULL;
2389
2390 tmpfile_fd = xopen(preq->tmpfile.buf, O_RDONLY);
2391
2392 ip.git_cmd = 1;
2393 ip.in = tmpfile_fd;
2394 strvec_pushv(&ip.args, preq->index_pack_args ?
2395 preq->index_pack_args :
2396 default_index_pack_args);
2397
2398 if (preq->preserve_index_pack_stdout)
2399 ip.out = 0;
2400 else
2401 ip.no_stdout = 1;
2402
2403 if (run_command(&ip)) {
2404 ret = -1;
2405 goto cleanup;
2406 }
2407
2408 cleanup:
2409 close(tmpfile_fd);
2410 unlink(preq->tmpfile.buf);
2411 return ret;
2412 }
2413
2414 void http_install_packfile(struct packed_git *p,
2415 struct packed_git **list_to_remove_from)
2416 {
2417 struct packed_git **lst = list_to_remove_from;
2418
2419 while (*lst != p)
2420 lst = &((*lst)->next);
2421 *lst = (*lst)->next;
2422
2423 install_packed_git(the_repository, p);
2424 }
2425
2426 struct http_pack_request *new_http_pack_request(
2427 const unsigned char *packed_git_hash, const char *base_url) {
2428
2429 struct strbuf buf = STRBUF_INIT;
2430
2431 end_url_with_slash(&buf, base_url);
2432 strbuf_addf(&buf, "objects/pack/pack-%s.pack",
2433 hash_to_hex(packed_git_hash));
2434 return new_direct_http_pack_request(packed_git_hash,
2435 strbuf_detach(&buf, NULL));
2436 }
2437
2438 struct http_pack_request *new_direct_http_pack_request(
2439 const unsigned char *packed_git_hash, char *url)
2440 {
2441 off_t prev_posn = 0;
2442 struct http_pack_request *preq;
2443
2444 CALLOC_ARRAY(preq, 1);
2445 strbuf_init(&preq->tmpfile, 0);
2446
2447 preq->url = url;
2448
2449 strbuf_addf(&preq->tmpfile, "%s.temp", sha1_pack_name(packed_git_hash));
2450 preq->packfile = fopen(preq->tmpfile.buf, "a");
2451 if (!preq->packfile) {
2452 error("Unable to open local file %s for pack",
2453 preq->tmpfile.buf);
2454 goto abort;
2455 }
2456
2457 preq->slot = get_active_slot();
2458 curl_easy_setopt(preq->slot->curl, CURLOPT_WRITEDATA, preq->packfile);
2459 curl_easy_setopt(preq->slot->curl, CURLOPT_WRITEFUNCTION, fwrite);
2460 curl_easy_setopt(preq->slot->curl, CURLOPT_URL, preq->url);
2461 curl_easy_setopt(preq->slot->curl, CURLOPT_HTTPHEADER,
2462 no_pragma_header);
2463
2464 /*
2465 * If there is data present from a previous transfer attempt,
2466 * resume where it left off
2467 */
2468 prev_posn = ftello(preq->packfile);
2469 if (prev_posn>0) {
2470 if (http_is_verbose)
2471 fprintf(stderr,
2472 "Resuming fetch of pack %s at byte %"PRIuMAX"\n",
2473 hash_to_hex(packed_git_hash),
2474 (uintmax_t)prev_posn);
2475 http_opt_request_remainder(preq->slot->curl, prev_posn);
2476 }
2477
2478 return preq;
2479
2480 abort:
2481 strbuf_release(&preq->tmpfile);
2482 free(preq->url);
2483 free(preq);
2484 return NULL;
2485 }
2486
2487 /* Helpers for fetching objects (loose) */
2488 static size_t fwrite_sha1_file(char *ptr, size_t eltsize, size_t nmemb,
2489 void *data)
2490 {
2491 unsigned char expn[4096];
2492 size_t size = eltsize * nmemb;
2493 int posn = 0;
2494 struct http_object_request *freq = data;
2495 struct active_request_slot *slot = freq->slot;
2496
2497 if (slot) {
2498 CURLcode c = curl_easy_getinfo(slot->curl, CURLINFO_HTTP_CODE,
2499 &slot->http_code);
2500 if (c != CURLE_OK)
2501 BUG("curl_easy_getinfo for HTTP code failed: %s",
2502 curl_easy_strerror(c));
2503 if (slot->http_code >= 300)
2504 return nmemb;
2505 }
2506
2507 do {
2508 ssize_t retval = xwrite(freq->localfile,
2509 (char *) ptr + posn, size - posn);
2510 if (retval < 0)
2511 return posn / eltsize;
2512 posn += retval;
2513 } while (posn < size);
2514
2515 freq->stream.avail_in = size;
2516 freq->stream.next_in = (void *)ptr;
2517 do {
2518 freq->stream.next_out = expn;
2519 freq->stream.avail_out = sizeof(expn);
2520 freq->zret = git_inflate(&freq->stream, Z_SYNC_FLUSH);
2521 the_hash_algo->update_fn(&freq->c, expn,
2522 sizeof(expn) - freq->stream.avail_out);
2523 } while (freq->stream.avail_in && freq->zret == Z_OK);
2524 return nmemb;
2525 }
2526
2527 struct http_object_request *new_http_object_request(const char *base_url,
2528 const struct object_id *oid)
2529 {
2530 char *hex = oid_to_hex(oid);
2531 struct strbuf filename = STRBUF_INIT;
2532 struct strbuf prevfile = STRBUF_INIT;
2533 int prevlocal;
2534 char prev_buf[PREV_BUF_SIZE];
2535 ssize_t prev_read = 0;
2536 off_t prev_posn = 0;
2537 struct http_object_request *freq;
2538
2539 CALLOC_ARRAY(freq, 1);
2540 strbuf_init(&freq->tmpfile, 0);
2541 oidcpy(&freq->oid, oid);
2542 freq->localfile = -1;
2543
2544 loose_object_path(the_repository, &filename, oid);
2545 strbuf_addf(&freq->tmpfile, "%s.temp", filename.buf);
2546
2547 strbuf_addf(&prevfile, "%s.prev", filename.buf);
2548 unlink_or_warn(prevfile.buf);
2549 rename(freq->tmpfile.buf, prevfile.buf);
2550 unlink_or_warn(freq->tmpfile.buf);
2551 strbuf_release(&filename);
2552
2553 if (freq->localfile != -1)
2554 error("fd leakage in start: %d", freq->localfile);
2555 freq->localfile = open(freq->tmpfile.buf,
2556 O_WRONLY | O_CREAT | O_EXCL, 0666);
2557 /*
2558 * This could have failed due to the "lazy directory creation";
2559 * try to mkdir the last path component.
2560 */
2561 if (freq->localfile < 0 && errno == ENOENT) {
2562 char *dir = strrchr(freq->tmpfile.buf, '/');
2563 if (dir) {
2564 *dir = 0;
2565 mkdir(freq->tmpfile.buf, 0777);
2566 *dir = '/';
2567 }
2568 freq->localfile = open(freq->tmpfile.buf,
2569 O_WRONLY | O_CREAT | O_EXCL, 0666);
2570 }
2571
2572 if (freq->localfile < 0) {
2573 error_errno("Couldn't create temporary file %s",
2574 freq->tmpfile.buf);
2575 goto abort;
2576 }
2577
2578 git_inflate_init(&freq->stream);
2579
2580 the_hash_algo->init_fn(&freq->c);
2581
2582 freq->url = get_remote_object_url(base_url, hex, 0);
2583
2584 /*
2585 * If a previous temp file is present, process what was already
2586 * fetched.
2587 */
2588 prevlocal = open(prevfile.buf, O_RDONLY);
2589 if (prevlocal != -1) {
2590 do {
2591 prev_read = xread(prevlocal, prev_buf, PREV_BUF_SIZE);
2592 if (prev_read>0) {
2593 if (fwrite_sha1_file(prev_buf,
2594 1,
2595 prev_read,
2596 freq) == prev_read) {
2597 prev_posn += prev_read;
2598 } else {
2599 prev_read = -1;
2600 }
2601 }
2602 } while (prev_read > 0);
2603 close(prevlocal);
2604 }
2605 unlink_or_warn(prevfile.buf);
2606 strbuf_release(&prevfile);
2607
2608 /*
2609 * Reset inflate/SHA1 if there was an error reading the previous temp
2610 * file; also rewind to the beginning of the local file.
2611 */
2612 if (prev_read == -1) {
2613 memset(&freq->stream, 0, sizeof(freq->stream));
2614 git_inflate_init(&freq->stream);
2615 the_hash_algo->init_fn(&freq->c);
2616 if (prev_posn>0) {
2617 prev_posn = 0;
2618 lseek(freq->localfile, 0, SEEK_SET);
2619 if (ftruncate(freq->localfile, 0) < 0) {
2620 error_errno("Couldn't truncate temporary file %s",
2621 freq->tmpfile.buf);
2622 goto abort;
2623 }
2624 }
2625 }
2626
2627 freq->slot = get_active_slot();
2628
2629 curl_easy_setopt(freq->slot->curl, CURLOPT_WRITEDATA, freq);
2630 curl_easy_setopt(freq->slot->curl, CURLOPT_FAILONERROR, 0);
2631 curl_easy_setopt(freq->slot->curl, CURLOPT_WRITEFUNCTION, fwrite_sha1_file);
2632 curl_easy_setopt(freq->slot->curl, CURLOPT_ERRORBUFFER, freq->errorstr);
2633 curl_easy_setopt(freq->slot->curl, CURLOPT_URL, freq->url);
2634 curl_easy_setopt(freq->slot->curl, CURLOPT_HTTPHEADER, no_pragma_header);
2635
2636 /*
2637 * If we have successfully processed data from a previous fetch
2638 * attempt, only fetch the data we don't already have.
2639 */
2640 if (prev_posn>0) {
2641 if (http_is_verbose)
2642 fprintf(stderr,
2643 "Resuming fetch of object %s at byte %"PRIuMAX"\n",
2644 hex, (uintmax_t)prev_posn);
2645 http_opt_request_remainder(freq->slot->curl, prev_posn);
2646 }
2647
2648 return freq;
2649
2650 abort:
2651 strbuf_release(&prevfile);
2652 free(freq->url);
2653 free(freq);
2654 return NULL;
2655 }
2656
2657 void process_http_object_request(struct http_object_request *freq)
2658 {
2659 if (!freq->slot)
2660 return;
2661 freq->curl_result = freq->slot->curl_result;
2662 freq->http_code = freq->slot->http_code;
2663 freq->slot = NULL;
2664 }
2665
2666 int finish_http_object_request(struct http_object_request *freq)
2667 {
2668 struct stat st;
2669 struct strbuf filename = STRBUF_INIT;
2670
2671 close(freq->localfile);
2672 freq->localfile = -1;
2673
2674 process_http_object_request(freq);
2675
2676 if (freq->http_code == 416) {
2677 warning("requested range invalid; we may already have all the data.");
2678 } else if (freq->curl_result != CURLE_OK) {
2679 if (stat(freq->tmpfile.buf, &st) == 0)
2680 if (st.st_size == 0)
2681 unlink_or_warn(freq->tmpfile.buf);
2682 return -1;
2683 }
2684
2685 git_inflate_end(&freq->stream);
2686 the_hash_algo->final_oid_fn(&freq->real_oid, &freq->c);
2687 if (freq->zret != Z_STREAM_END) {
2688 unlink_or_warn(freq->tmpfile.buf);
2689 return -1;
2690 }
2691 if (!oideq(&freq->oid, &freq->real_oid)) {
2692 unlink_or_warn(freq->tmpfile.buf);
2693 return -1;
2694 }
2695 loose_object_path(the_repository, &filename, &freq->oid);
2696 freq->rename = finalize_object_file(freq->tmpfile.buf, filename.buf);
2697 strbuf_release(&filename);
2698
2699 return freq->rename;
2700 }
2701
2702 void abort_http_object_request(struct http_object_request *freq)
2703 {
2704 unlink_or_warn(freq->tmpfile.buf);
2705
2706 release_http_object_request(freq);
2707 }
2708
2709 void release_http_object_request(struct http_object_request *freq)
2710 {
2711 if (freq->localfile != -1) {
2712 close(freq->localfile);
2713 freq->localfile = -1;
2714 }
2715 FREE_AND_NULL(freq->url);
2716 if (freq->slot) {
2717 freq->slot->callback_func = NULL;
2718 freq->slot->callback_data = NULL;
2719 release_active_slot(freq->slot);
2720 freq->slot = NULL;
2721 }
2722 strbuf_release(&freq->tmpfile);
2723 }