blob: a0f169d2fe535643617aa10fa2e92a65d6b04ab0 [file] [log] [blame]
David Aguilar1c4b6602014-09-14 00:40:45 -07001#include "git-compat-util.h"
Nick Hengeveld29508e12005-11-18 11:02:58 -08002#include "http.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07003#include "config.h"
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +08004#include "pack.h"
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -07005#include "sideband.h"
Shawn O. Pearcefe72d422010-04-19 07:23:09 -07006#include "run-command.h"
Gabriel Coronaf39f72d2010-11-14 02:51:15 +01007#include "url.h"
Kyle J. McKay6a569932013-08-05 13:20:36 -07008#include "urlmatch.h"
Jeff King148bb6a2011-12-10 05:31:21 -05009#include "credential.h"
Jeff King745c7c82012-06-02 15:03:08 -040010#include "version.h"
Jeff King047ec602013-02-20 15:02:45 -050011#include "pkt-line.h"
Jeff King93f7d912015-02-25 22:04:16 -050012#include "gettext.h"
Blake Burkhartf4113ca2015-09-22 18:06:04 -040013#include "transport.h"
Jonathan Tan4f39cd82017-08-18 15:20:16 -070014#include "packfile.h"
Brandon Williams19113a22017-10-16 10:55:29 -070015#include "protocol.h"
Jonathan Tan83411782018-01-18 16:28:01 -080016#include "string-list.h"
Stefan Bellera80d72d2018-03-23 18:20:59 +010017#include "object-store.h"
Nick Hengeveld29508e12005-11-18 11:02:58 -080018
Elia Pinto74c682d2016-05-23 13:44:02 +000019static struct trace_key trace_curl = TRACE_KEY_INIT(CURL);
Jonathan Tan8ba18e62018-01-18 16:28:02 -080020static int trace_curl_data = 1;
Jonathan Tan827e7d42020-06-05 14:21:36 -070021static int trace_curl_redact = 1;
Eric Wongc915f112016-02-03 04:09:14 +000022long int git_curl_ipresolve = CURL_IPRESOLVE_WHATEVER;
Junio C Hamano4251ccb2009-03-09 18:47:29 -070023int active_requests;
Tay Ray Chuane9176742009-06-06 16:43:41 +080024int http_is_verbose;
David Turner37ee6802017-04-11 14:13:57 -040025ssize_t http_post_buffer = 16 * LARGE_PACKET_MAX;
Nick Hengeveld29508e12005-11-18 11:02:58 -080026
Tay Ray Chuanad75ebe2009-11-27 23:42:26 +080027static int min_curl_sessions = 1;
28static int curl_session_count;
Mike Hommeycc3530e2007-12-09 18:04:57 +010029static int max_requests = -1;
30static CURLM *curlm;
Mike Hommeycc3530e2007-12-09 18:04:57 +010031static CURL *curl_default;
Tay Ray Chuan5424bc52009-06-06 16:44:02 +080032
33#define PREV_BUF_SIZE 4096
Tay Ray Chuan5424bc52009-06-06 16:44:02 +080034
Nick Hengeveld29508e12005-11-18 11:02:58 -080035char curl_errorstr[CURL_ERROR_SIZE];
36
Mike Hommeycc3530e2007-12-09 18:04:57 +010037static int curl_ssl_verify = -1;
Modestas Vainius4bc444e2013-04-07 22:10:39 +030038static int curl_ssl_try;
Force Charlied73019f2018-11-08 19:44:14 -080039static const char *curl_http_version = NULL;
Junio C Hamano4251ccb2009-03-09 18:47:29 -070040static const char *ssl_cert;
Lars Kellogg-Stedmanf6f2a9e2015-05-08 09:22:15 -040041static const char *ssl_cipherlist;
Elia Pinto01861cb2015-08-14 21:37:43 +020042static const char *ssl_version;
43static struct {
44 const char *name;
45 long ssl_version;
46} sslversions[] = {
47 { "sslv2", CURL_SSLVERSION_SSLv2 },
48 { "sslv3", CURL_SSLVERSION_SSLv3 },
49 { "tlsv1", CURL_SSLVERSION_TLSv1 },
50#if LIBCURL_VERSION_NUM >= 0x072200
51 { "tlsv1.0", CURL_SSLVERSION_TLSv1_0 },
52 { "tlsv1.1", CURL_SSLVERSION_TLSv1_1 },
53 { "tlsv1.2", CURL_SSLVERSION_TLSv1_2 },
54#endif
Loganaden Velvindrond81b6512018-03-29 14:14:18 +040055#if LIBCURL_VERSION_NUM >= 0x073400
56 { "tlsv1.3", CURL_SSLVERSION_TLSv1_3 },
57#endif
Elia Pinto01861cb2015-08-14 21:37:43 +020058};
Junio C Hamano4251ccb2009-03-09 18:47:29 -070059static const char *ssl_key;
Junio C Hamano4251ccb2009-03-09 18:47:29 -070060static const char *ssl_capath;
Ramsay Jonesb8fd6002018-03-14 21:56:06 +000061static const char *curl_no_proxy;
Christoph Eggeraeff8a62016-02-15 15:04:22 +010062#if LIBCURL_VERSION_NUM >= 0x072c00
63static const char *ssl_pinnedkey;
64#endif
Junio C Hamano4251ccb2009-03-09 18:47:29 -070065static const char *ssl_cainfo;
Mike Hommeycc3530e2007-12-09 18:04:57 +010066static long curl_low_speed_limit = -1;
67static long curl_low_speed_time = -1;
Junio C Hamano4251ccb2009-03-09 18:47:29 -070068static int curl_ftp_no_epsv;
69static const char *curl_http_proxy;
Knut Frankeef976392016-01-26 13:02:47 +000070static const char *http_proxy_authmethod;
Jorge Lopez Silva88238e02020-03-04 18:40:05 +000071
72static const char *http_proxy_ssl_cert;
73static const char *http_proxy_ssl_key;
74static const char *http_proxy_ssl_ca_info;
75static struct credential proxy_cert_auth = CREDENTIAL_INIT;
76static int proxy_ssl_cert_password_required;
77
Knut Frankeef976392016-01-26 13:02:47 +000078static struct {
79 const char *name;
80 long curlauth_param;
81} proxy_authmethods[] = {
82 { "basic", CURLAUTH_BASIC },
83 { "digest", CURLAUTH_DIGEST },
84 { "negotiate", CURLAUTH_GSSNEGOTIATE },
85 { "ntlm", CURLAUTH_NTLM },
Knut Frankeef976392016-01-26 13:02:47 +000086 { "anyauth", CURLAUTH_ANY },
Knut Frankeef976392016-01-26 13:02:47 +000087 /*
88 * CURLAUTH_DIGEST_IE has no corresponding command-line option in
89 * curl(1) and is not included in CURLAUTH_ANY, so we leave it out
90 * here, too
91 */
92};
Tom G. Christensendd5df532017-08-11 18:37:34 +020093#ifdef CURLGSSAPI_DELEGATION_FLAG
Petr Stodulka26a7b232016-09-28 20:01:34 +020094static const char *curl_deleg;
95static struct {
96 const char *name;
97 long curl_deleg_param;
98} curl_deleg_levels[] = {
99 { "none", CURLGSSAPI_DELEGATION_NONE },
100 { "policy", CURLGSSAPI_DELEGATION_POLICY_FLAG },
101 { "always", CURLGSSAPI_DELEGATION_FLAG },
102};
103#endif
104
Knut Franke372370f2016-01-26 13:02:48 +0000105static struct credential proxy_auth = CREDENTIAL_INIT;
106static const char *curl_proxyuserpwd;
Duncan Brownbcfb95d2011-06-02 16:31:25 -0400107static const char *curl_cookie_file;
Dave Borowitz912b2ac2013-07-23 15:40:17 -0700108static int curl_save_cookies;
Jeff King2501aff2013-09-28 04:31:45 -0400109struct credential http_auth = CREDENTIAL_INIT;
Jeff Kinga4ddbc32011-12-13 19:11:56 -0500110static int http_proactive_auth;
Spencer E. Olsonb1d10582010-08-11 14:40:38 -0600111static const char *user_agent;
Jeff King40a18fc2017-02-25 14:18:31 -0500112static int curl_empty_auth = -1;
Nick Hengeveld29508e12005-11-18 11:02:58 -0800113
Jeff King50d34132016-12-06 13:24:41 -0500114enum http_follow_config http_follow_config = HTTP_FOLLOW_INITIAL;
115
Jeff King148bb6a2011-12-10 05:31:21 -0500116static struct credential cert_auth = CREDENTIAL_INIT;
Mark Lodato30dd9162009-05-27 23:16:02 -0400117static int ssl_cert_password_required;
brian m. carlson4dbe6642015-01-08 00:29:20 +0000118static unsigned long http_auth_methods = CURLAUTH_ANY;
Jeff King40a18fc2017-02-25 14:18:31 -0500119static int http_auth_methods_restricted;
120/* Modes for which empty_auth cannot actually help us. */
121static unsigned long empty_auth_useless =
122 CURLAUTH_BASIC
Jeff King40a18fc2017-02-25 14:18:31 -0500123 | CURLAUTH_DIGEST_IE
Jeff King40a18fc2017-02-25 14:18:31 -0500124 | CURLAUTH_DIGEST;
Mark Lodato30dd9162009-05-27 23:16:02 -0400125
Mike Hommeycc3530e2007-12-09 18:04:57 +0100126static struct curl_slist *pragma_header;
Tay Ray Chuan5424bc52009-06-06 16:44:02 +0800127static struct curl_slist *no_pragma_header;
Johannes Schindelin4d17fd22019-11-06 10:04:55 +0000128static struct string_list extra_http_headers = STRING_LIST_INIT_DUP;
Tay Ray Chuane9176742009-06-06 16:43:41 +0800129
Junio C Hamano4251ccb2009-03-09 18:47:29 -0700130static struct active_request_slot *active_queue_head;
Nick Hengeveld29508e12005-11-18 11:02:58 -0800131
Yi EungJunf18604b2015-01-28 21:04:37 +0900132static char *cached_accept_language;
133
Johannes Schindelin21084e82018-10-15 03:14:43 -0700134static char *http_ssl_backend;
135
Brendan Forster93aef7c2018-10-25 11:53:55 -0700136static int http_schannel_check_revoke = 1;
Johannes Schindelinb67d40a2018-10-25 11:53:56 -0700137/*
138 * With the backend being set to `schannel`, setting sslCAinfo would override
139 * the Certificate Store in cURL v7.60.0 and later, which is not what we want
140 * by default.
141 */
142static int http_schannel_use_ssl_cainfo;
Brendan Forster93aef7c2018-10-25 11:53:55 -0700143
Dan McGeea04ff3e2011-05-03 23:47:27 +0800144size_t fread_buffer(char *ptr, size_t eltsize, size_t nmemb, void *buffer_)
Nick Hengeveld29508e12005-11-18 11:02:58 -0800145{
146 size_t size = eltsize * nmemb;
Junio C Hamanof444e522008-07-04 00:37:40 -0700147 struct buffer *buffer = buffer_;
148
Mike Hommey028c2972007-12-09 20:30:59 +0100149 if (size > buffer->buf.len - buffer->posn)
150 size = buffer->buf.len - buffer->posn;
151 memcpy(ptr, buffer->buf.buf + buffer->posn, size);
Nick Hengeveld29508e12005-11-18 11:02:58 -0800152 buffer->posn += size;
Mike Hommey028c2972007-12-09 20:30:59 +0100153
Mike Hommey5c3d5a32019-05-08 08:03:54 +0900154 return size / eltsize;
Nick Hengeveld29508e12005-11-18 11:02:58 -0800155}
156
Martin Storsjö3944ba02009-04-01 19:48:24 +0300157curlioerr ioctl_buffer(CURL *handle, int cmd, void *clientp)
158{
159 struct buffer *buffer = clientp;
160
161 switch (cmd) {
162 case CURLIOCMD_NOP:
163 return CURLIOE_OK;
164
165 case CURLIOCMD_RESTARTREAD:
166 buffer->posn = 0;
167 return CURLIOE_OK;
168
169 default:
170 return CURLIOE_UNKNOWNCMD;
171 }
172}
Martin Storsjö3944ba02009-04-01 19:48:24 +0300173
Dan McGeea04ff3e2011-05-03 23:47:27 +0800174size_t fwrite_buffer(char *ptr, size_t eltsize, size_t nmemb, void *buffer_)
Nick Hengeveld29508e12005-11-18 11:02:58 -0800175{
176 size_t size = eltsize * nmemb;
Junio C Hamanof444e522008-07-04 00:37:40 -0700177 struct strbuf *buffer = buffer_;
178
Mike Hommey028c2972007-12-09 20:30:59 +0100179 strbuf_add(buffer, ptr, size);
Mike Hommey5c3d5a32019-05-08 08:03:54 +0900180 return nmemb;
Nick Hengeveld29508e12005-11-18 11:02:58 -0800181}
182
Dan McGeea04ff3e2011-05-03 23:47:27 +0800183size_t fwrite_null(char *ptr, size_t eltsize, size_t nmemb, void *strbuf)
Nick Hengeveld29508e12005-11-18 11:02:58 -0800184{
Mike Hommey5c3d5a32019-05-08 08:03:54 +0900185 return nmemb;
Nick Hengeveld29508e12005-11-18 11:02:58 -0800186}
187
Junio C Hamanob90a3d72015-01-14 15:40:46 -0800188static void closedown_active_slot(struct active_request_slot *slot)
189{
190 active_requests--;
191 slot->in_use = 0;
192}
193
194static void finish_active_slot(struct active_request_slot *slot)
195{
196 closedown_active_slot(slot);
197 curl_easy_getinfo(slot->curl, CURLINFO_HTTP_CODE, &slot->http_code);
198
199 if (slot->finished != NULL)
200 (*slot->finished) = 1;
201
202 /* Store slot results so they can be read after the slot is reused */
203 if (slot->results != NULL) {
204 slot->results->curl_result = slot->curl_result;
205 slot->results->http_code = slot->http_code;
Junio C Hamanob90a3d72015-01-14 15:40:46 -0800206 curl_easy_getinfo(slot->curl, CURLINFO_HTTPAUTH_AVAIL,
207 &slot->results->auth_avail);
Knut Franke372370f2016-01-26 13:02:48 +0000208
209 curl_easy_getinfo(slot->curl, CURLINFO_HTTP_CONNECTCODE,
210 &slot->results->http_connectcode);
Junio C Hamanob90a3d72015-01-14 15:40:46 -0800211 }
212
213 /* Run callback if appropriate */
214 if (slot->callback_func != NULL)
215 slot->callback_func(slot->callback_data);
216}
217
Eric Wongd8b6b842016-09-13 00:25:56 +0000218static void xmulti_remove_handle(struct active_request_slot *slot)
219{
Eric Wongd8b6b842016-09-13 00:25:56 +0000220 curl_multi_remove_handle(curlm, slot->curl);
Eric Wongd8b6b842016-09-13 00:25:56 +0000221}
222
Nick Hengeveld29508e12005-11-18 11:02:58 -0800223static void process_curl_messages(void)
224{
225 int num_messages;
226 struct active_request_slot *slot;
227 CURLMsg *curl_message = curl_multi_info_read(curlm, &num_messages);
228
229 while (curl_message != NULL) {
230 if (curl_message->msg == CURLMSG_DONE) {
231 int curl_result = curl_message->data.result;
232 slot = active_queue_head;
233 while (slot != NULL &&
234 slot->curl != curl_message->easy_handle)
235 slot = slot->next;
236 if (slot != NULL) {
Eric Wongd8b6b842016-09-13 00:25:56 +0000237 xmulti_remove_handle(slot);
Nick Hengeveld29508e12005-11-18 11:02:58 -0800238 slot->curl_result = curl_result;
239 finish_active_slot(slot);
240 } else {
241 fprintf(stderr, "Received DONE message for unknown request!\n");
242 }
243 } else {
244 fprintf(stderr, "Unknown CURL message received: %d\n",
245 (int)curl_message->msg);
246 }
247 curl_message = curl_multi_info_read(curlm, &num_messages);
248 }
249}
Nick Hengeveld29508e12005-11-18 11:02:58 -0800250
Johannes Schindelinef90d6d2008-05-14 18:46:53 +0100251static int http_options(const char *var, const char *value, void *cb)
Nick Hengeveld29508e12005-11-18 11:02:58 -0800252{
Force Charlied73019f2018-11-08 19:44:14 -0800253 if (!strcmp("http.version", var)) {
254 return git_config_string(&curl_http_version, var, value);
255 }
Nick Hengeveld29508e12005-11-18 11:02:58 -0800256 if (!strcmp("http.sslverify", var)) {
Junio C Hamano7059cd92009-03-09 19:00:30 -0700257 curl_ssl_verify = git_config_bool(var, value);
Nick Hengeveld29508e12005-11-18 11:02:58 -0800258 return 0;
259 }
Lars Kellogg-Stedmanf6f2a9e2015-05-08 09:22:15 -0400260 if (!strcmp("http.sslcipherlist", var))
261 return git_config_string(&ssl_cipherlist, var, value);
Elia Pinto01861cb2015-08-14 21:37:43 +0200262 if (!strcmp("http.sslversion", var))
263 return git_config_string(&ssl_version, var, value);
Junio C Hamano7059cd92009-03-09 19:00:30 -0700264 if (!strcmp("http.sslcert", var))
Junio C Hamano8d154962017-07-20 13:30:52 -0700265 return git_config_pathname(&ssl_cert, var, value);
Junio C Hamano7059cd92009-03-09 19:00:30 -0700266 if (!strcmp("http.sslkey", var))
Junio C Hamano8d154962017-07-20 13:30:52 -0700267 return git_config_pathname(&ssl_key, var, value);
Junio C Hamano7059cd92009-03-09 19:00:30 -0700268 if (!strcmp("http.sslcapath", var))
Charles Baileybf9acba2015-11-23 12:02:40 +0000269 return git_config_pathname(&ssl_capath, var, value);
Junio C Hamano7059cd92009-03-09 19:00:30 -0700270 if (!strcmp("http.sslcainfo", var))
Charles Baileybf9acba2015-11-23 12:02:40 +0000271 return git_config_pathname(&ssl_cainfo, var, value);
Mark Lodato754ae192009-05-27 23:16:03 -0400272 if (!strcmp("http.sslcertpasswordprotected", var)) {
Junio C Hamano3f4ccd22013-07-12 11:52:47 -0700273 ssl_cert_password_required = git_config_bool(var, value);
Mark Lodato754ae192009-05-27 23:16:03 -0400274 return 0;
275 }
Modestas Vainius4bc444e2013-04-07 22:10:39 +0300276 if (!strcmp("http.ssltry", var)) {
277 curl_ssl_try = git_config_bool(var, value);
278 return 0;
279 }
Johannes Schindelin21084e82018-10-15 03:14:43 -0700280 if (!strcmp("http.sslbackend", var)) {
281 free(http_ssl_backend);
282 http_ssl_backend = xstrdup_or_null(value);
283 return 0;
284 }
285
Brendan Forster93aef7c2018-10-25 11:53:55 -0700286 if (!strcmp("http.schannelcheckrevoke", var)) {
287 http_schannel_check_revoke = git_config_bool(var, value);
288 return 0;
289 }
290
Johannes Schindelinb67d40a2018-10-25 11:53:56 -0700291 if (!strcmp("http.schannelusesslcainfo", var)) {
292 http_schannel_use_ssl_cainfo = git_config_bool(var, value);
293 return 0;
294 }
295
Tay Ray Chuanad75ebe2009-11-27 23:42:26 +0800296 if (!strcmp("http.minsessions", var)) {
297 min_curl_sessions = git_config_int(var, value);
Tay Ray Chuanad75ebe2009-11-27 23:42:26 +0800298 if (min_curl_sessions > 1)
299 min_curl_sessions = 1;
Tay Ray Chuanad75ebe2009-11-27 23:42:26 +0800300 return 0;
301 }
Nick Hengeveld29508e12005-11-18 11:02:58 -0800302 if (!strcmp("http.maxrequests", var)) {
Junio C Hamano7059cd92009-03-09 19:00:30 -0700303 max_requests = git_config_int(var, value);
Nick Hengeveld29508e12005-11-18 11:02:58 -0800304 return 0;
305 }
Nick Hengeveld29508e12005-11-18 11:02:58 -0800306 if (!strcmp("http.lowspeedlimit", var)) {
Junio C Hamano7059cd92009-03-09 19:00:30 -0700307 curl_low_speed_limit = (long)git_config_int(var, value);
Nick Hengeveld29508e12005-11-18 11:02:58 -0800308 return 0;
309 }
310 if (!strcmp("http.lowspeedtime", var)) {
Junio C Hamano7059cd92009-03-09 19:00:30 -0700311 curl_low_speed_time = (long)git_config_int(var, value);
Nick Hengeveld29508e12005-11-18 11:02:58 -0800312 return 0;
313 }
314
Sasha Khapyorsky3ea099d2006-09-29 03:10:44 +0300315 if (!strcmp("http.noepsv", var)) {
316 curl_ftp_no_epsv = git_config_bool(var, value);
317 return 0;
318 }
Junio C Hamano7059cd92009-03-09 19:00:30 -0700319 if (!strcmp("http.proxy", var))
320 return git_config_string(&curl_http_proxy, var, value);
Sasha Khapyorsky3ea099d2006-09-29 03:10:44 +0300321
Knut Frankeef976392016-01-26 13:02:47 +0000322 if (!strcmp("http.proxyauthmethod", var))
323 return git_config_string(&http_proxy_authmethod, var, value);
324
Jorge Lopez Silva88238e02020-03-04 18:40:05 +0000325 if (!strcmp("http.proxysslcert", var))
326 return git_config_string(&http_proxy_ssl_cert, var, value);
327
328 if (!strcmp("http.proxysslkey", var))
329 return git_config_string(&http_proxy_ssl_key, var, value);
330
331 if (!strcmp("http.proxysslcainfo", var))
332 return git_config_string(&http_proxy_ssl_ca_info, var, value);
333
334 if (!strcmp("http.proxysslcertpasswordprotected", var)) {
335 proxy_ssl_cert_password_required = git_config_bool(var, value);
336 return 0;
337 }
338
Duncan Brownbcfb95d2011-06-02 16:31:25 -0400339 if (!strcmp("http.cookiefile", var))
Brian Norrise5a39ad2016-05-04 11:42:15 -0700340 return git_config_pathname(&curl_cookie_file, var, value);
Dave Borowitz912b2ac2013-07-23 15:40:17 -0700341 if (!strcmp("http.savecookies", var)) {
342 curl_save_cookies = git_config_bool(var, value);
343 return 0;
344 }
Duncan Brownbcfb95d2011-06-02 16:31:25 -0400345
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700346 if (!strcmp("http.postbuffer", var)) {
David Turner37ee6802017-04-11 14:13:57 -0400347 http_post_buffer = git_config_ssize_t(var, value);
348 if (http_post_buffer < 0)
349 warning(_("negative value for http.postbuffer; defaulting to %d"), LARGE_PACKET_MAX);
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700350 if (http_post_buffer < LARGE_PACKET_MAX)
351 http_post_buffer = LARGE_PACKET_MAX;
352 return 0;
353 }
354
Spencer E. Olsonb1d10582010-08-11 14:40:38 -0600355 if (!strcmp("http.useragent", var))
356 return git_config_string(&user_agent, var, value);
357
brian m. carlson121061f2016-02-15 18:44:46 +0000358 if (!strcmp("http.emptyauth", var)) {
Jeff King40a18fc2017-02-25 14:18:31 -0500359 if (value && !strcmp("auto", value))
360 curl_empty_auth = -1;
361 else
362 curl_empty_auth = git_config_bool(var, value);
brian m. carlson121061f2016-02-15 18:44:46 +0000363 return 0;
364 }
365
Petr Stodulka26a7b232016-09-28 20:01:34 +0200366 if (!strcmp("http.delegation", var)) {
Tom G. Christensendd5df532017-08-11 18:37:34 +0200367#ifdef CURLGSSAPI_DELEGATION_FLAG
Petr Stodulka26a7b232016-09-28 20:01:34 +0200368 return git_config_string(&curl_deleg, var, value);
369#else
370 warning(_("Delegation control is not supported with cURL < 7.22.0"));
371 return 0;
372#endif
373 }
374
Christoph Eggeraeff8a62016-02-15 15:04:22 +0100375 if (!strcmp("http.pinnedpubkey", var)) {
376#if LIBCURL_VERSION_NUM >= 0x072c00
377 return git_config_pathname(&ssl_pinnedkey, var, value);
378#else
379 warning(_("Public key pinning not supported with cURL < 7.44.0"));
380 return 0;
381#endif
382 }
Junio C Hamanoe79112d2016-02-24 13:25:58 -0800383
Johannes Schindelin8cb01e22016-04-27 14:20:37 +0200384 if (!strcmp("http.extraheader", var)) {
385 if (!value) {
386 return config_error_nonbool(var);
387 } else if (!*value) {
Johannes Schindelin4d17fd22019-11-06 10:04:55 +0000388 string_list_clear(&extra_http_headers, 0);
Johannes Schindelin8cb01e22016-04-27 14:20:37 +0200389 } else {
Johannes Schindelin4d17fd22019-11-06 10:04:55 +0000390 string_list_append(&extra_http_headers, value);
Johannes Schindelin8cb01e22016-04-27 14:20:37 +0200391 }
392 return 0;
393 }
394
Jeff King50d34132016-12-06 13:24:41 -0500395 if (!strcmp("http.followredirects", var)) {
396 if (value && !strcmp(value, "initial"))
397 http_follow_config = HTTP_FOLLOW_INITIAL;
398 else if (git_config_bool(var, value))
399 http_follow_config = HTTP_FOLLOW_ALWAYS;
400 else
401 http_follow_config = HTTP_FOLLOW_NONE;
402 return 0;
403 }
404
Nick Hengeveld29508e12005-11-18 11:02:58 -0800405 /* Fall back on the default ones */
Johannes Schindelinef90d6d2008-05-14 18:46:53 +0100406 return git_default_config(var, value, cb);
Nick Hengeveld29508e12005-11-18 11:02:58 -0800407}
408
Jeff King40a18fc2017-02-25 14:18:31 -0500409static int curl_empty_auth_enabled(void)
410{
411 if (curl_empty_auth >= 0)
412 return curl_empty_auth;
413
Jeff King40a18fc2017-02-25 14:18:31 -0500414 /*
415 * In the automatic case, kick in the empty-auth
416 * hack as long as we would potentially try some
417 * method more exotic than "Basic" or "Digest".
418 *
419 * But only do this when this is our second or
420 * subsequent request, as by then we know what
421 * methods are available.
422 */
423 if (http_auth_methods_restricted &&
424 (http_auth_methods & ~empty_auth_useless))
425 return 1;
Jeff King40a18fc2017-02-25 14:18:31 -0500426 return 0;
427}
428
Junio C Hamanoc33976c2009-03-09 23:34:25 -0700429static void init_curl_http_auth(CURL *result)
430{
David Turner5275c302016-10-04 10:53:52 -0400431 if (!http_auth.username || !*http_auth.username) {
Jeff King40a18fc2017-02-25 14:18:31 -0500432 if (curl_empty_auth_enabled())
brian m. carlson121061f2016-02-15 18:44:46 +0000433 curl_easy_setopt(result, CURLOPT_USERPWD, ":");
Jeff King6f4c3472012-04-13 02:19:25 -0400434 return;
brian m. carlson121061f2016-02-15 18:44:46 +0000435 }
Jeff King6f4c3472012-04-13 02:19:25 -0400436
437 credential_fill(&http_auth);
438
Jeff King6f4c3472012-04-13 02:19:25 -0400439 curl_easy_setopt(result, CURLOPT_USERNAME, http_auth.username);
440 curl_easy_setopt(result, CURLOPT_PASSWORD, http_auth.password);
Junio C Hamanoc33976c2009-03-09 23:34:25 -0700441}
442
Knut Frankeef976392016-01-26 13:02:47 +0000443/* *var must be free-able */
444static void var_override(const char **var, char *value)
445{
446 if (value) {
447 free((void *)*var);
448 *var = xstrdup(value);
449 }
450}
451
Knut Franke372370f2016-01-26 13:02:48 +0000452static void set_proxyauth_name_password(CURL *result)
453{
Knut Franke372370f2016-01-26 13:02:48 +0000454 curl_easy_setopt(result, CURLOPT_PROXYUSERNAME,
455 proxy_auth.username);
456 curl_easy_setopt(result, CURLOPT_PROXYPASSWORD,
457 proxy_auth.password);
Knut Franke372370f2016-01-26 13:02:48 +0000458}
459
Knut Frankeef976392016-01-26 13:02:47 +0000460static void init_curl_proxy_auth(CURL *result)
461{
Knut Franke372370f2016-01-26 13:02:48 +0000462 if (proxy_auth.username) {
463 if (!proxy_auth.password)
464 credential_fill(&proxy_auth);
465 set_proxyauth_name_password(result);
466 }
467
Knut Frankeef976392016-01-26 13:02:47 +0000468 var_override(&http_proxy_authmethod, getenv("GIT_HTTP_PROXY_AUTHMETHOD"));
469
Knut Frankeef976392016-01-26 13:02:47 +0000470 if (http_proxy_authmethod) {
471 int i;
472 for (i = 0; i < ARRAY_SIZE(proxy_authmethods); i++) {
473 if (!strcmp(http_proxy_authmethod, proxy_authmethods[i].name)) {
474 curl_easy_setopt(result, CURLOPT_PROXYAUTH,
475 proxy_authmethods[i].curlauth_param);
476 break;
477 }
478 }
479 if (i == ARRAY_SIZE(proxy_authmethods)) {
480 warning("unsupported proxy authentication method %s: using anyauth",
481 http_proxy_authmethod);
482 curl_easy_setopt(result, CURLOPT_PROXYAUTH, CURLAUTH_ANY);
483 }
484 }
485 else
486 curl_easy_setopt(result, CURLOPT_PROXYAUTH, CURLAUTH_ANY);
Knut Frankeef976392016-01-26 13:02:47 +0000487}
488
Mark Lodato30dd9162009-05-27 23:16:02 -0400489static int has_cert_password(void)
490{
Mark Lodato30dd9162009-05-27 23:16:02 -0400491 if (ssl_cert == NULL || ssl_cert_password_required != 1)
492 return 0;
Jeff King148bb6a2011-12-10 05:31:21 -0500493 if (!cert_auth.password) {
494 cert_auth.protocol = xstrdup("cert");
Jeff King24036682020-04-18 20:48:05 -0700495 cert_auth.host = xstrdup("");
Rene Bredlau75e9a402012-12-21 17:31:19 +0100496 cert_auth.username = xstrdup("");
Jeff King148bb6a2011-12-10 05:31:21 -0500497 cert_auth.path = xstrdup(ssl_cert);
498 credential_fill(&cert_auth);
499 }
500 return 1;
Mark Lodato30dd9162009-05-27 23:16:02 -0400501}
502
Jorge Lopez Silva88238e02020-03-04 18:40:05 +0000503#if LIBCURL_VERSION_NUM >= 0x073400
504static int has_proxy_cert_password(void)
505{
506 if (http_proxy_ssl_cert == NULL || proxy_ssl_cert_password_required != 1)
507 return 0;
508 if (!proxy_cert_auth.password) {
509 proxy_cert_auth.protocol = xstrdup("cert");
Junio C Hamano048abe12020-04-19 22:05:56 -0700510 proxy_cert_auth.host = xstrdup("");
Jorge Lopez Silva88238e02020-03-04 18:40:05 +0000511 proxy_cert_auth.username = xstrdup("");
512 proxy_cert_auth.path = xstrdup(http_proxy_ssl_cert);
513 credential_fill(&proxy_cert_auth);
514 }
515 return 1;
516}
517#endif
518
Jeff King47ce1152013-10-14 20:06:14 -0400519#if LIBCURL_VERSION_NUM >= 0x071900
520static void set_curl_keepalive(CURL *c)
521{
522 curl_easy_setopt(c, CURLOPT_TCP_KEEPALIVE, 1);
523}
524
Jeff King013c7e22021-07-30 11:31:54 +0200525#else
Eric Wonga15d0692013-10-12 22:29:40 +0000526static int sockopt_callback(void *client, curl_socket_t fd, curlsocktype type)
527{
528 int ka = 1;
529 int rc;
530 socklen_t len = (socklen_t)sizeof(ka);
531
532 if (type != CURLSOCKTYPE_IPCXN)
533 return 0;
534
535 rc = setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (void *)&ka, len);
536 if (rc < 0)
Nguyễn Thái Ngọc Duyd2e255e2016-05-08 16:47:48 +0700537 warning_errno("unable to set SO_KEEPALIVE on socket");
Eric Wonga15d0692013-10-12 22:29:40 +0000538
539 return 0; /* CURL_SOCKOPT_OK only exists since curl 7.21.5 */
540}
541
Jeff King47ce1152013-10-14 20:06:14 -0400542static void set_curl_keepalive(CURL *c)
543{
544 curl_easy_setopt(c, CURLOPT_SOCKOPTFUNCTION, sockopt_callback);
545}
Jeff King47ce1152013-10-14 20:06:14 -0400546#endif
547
Elia Pinto74c682d2016-05-23 13:44:02 +0000548static void redact_sensitive_header(struct strbuf *header)
549{
550 const char *sensitive_header;
551
Jonathan Tan827e7d42020-06-05 14:21:36 -0700552 if (trace_curl_redact &&
553 (skip_prefix(header->buf, "Authorization:", &sensitive_header) ||
554 skip_prefix(header->buf, "Proxy-Authorization:", &sensitive_header))) {
Elia Pinto74c682d2016-05-23 13:44:02 +0000555 /* The first token is the type, which is OK to log */
556 while (isspace(*sensitive_header))
557 sensitive_header++;
558 while (*sensitive_header && !isspace(*sensitive_header))
559 sensitive_header++;
560 /* Everything else is opaque and possibly sensitive */
561 strbuf_setlen(header, sensitive_header - header->buf);
562 strbuf_addstr(header, " <redacted>");
Jonathan Tan827e7d42020-06-05 14:21:36 -0700563 } else if (trace_curl_redact &&
Jonathan Tan83411782018-01-18 16:28:01 -0800564 skip_prefix(header->buf, "Cookie:", &sensitive_header)) {
565 struct strbuf redacted_header = STRBUF_INIT;
Jonathan Tan827e7d42020-06-05 14:21:36 -0700566 const char *cookie;
Jonathan Tan83411782018-01-18 16:28:01 -0800567
568 while (isspace(*sensitive_header))
569 sensitive_header++;
570
Jonathan Tan827e7d42020-06-05 14:21:36 -0700571 cookie = sensitive_header;
Jonathan Tan83411782018-01-18 16:28:01 -0800572
573 while (cookie) {
574 char *equals;
575 char *semicolon = strstr(cookie, "; ");
576 if (semicolon)
577 *semicolon = 0;
578 equals = strchrnul(cookie, '=');
579 if (!equals) {
580 /* invalid cookie, just append and continue */
581 strbuf_addstr(&redacted_header, cookie);
582 continue;
583 }
Jonathan Tan827e7d42020-06-05 14:21:36 -0700584 strbuf_add(&redacted_header, cookie, equals - cookie);
585 strbuf_addstr(&redacted_header, "=<redacted>");
Jonathan Tan83411782018-01-18 16:28:01 -0800586 if (semicolon) {
587 /*
588 * There are more cookies. (Or, for some
589 * reason, the input string ends in "; ".)
590 */
591 strbuf_addstr(&redacted_header, "; ");
592 cookie = semicolon + strlen("; ");
593 } else {
594 cookie = NULL;
595 }
596 }
597
598 strbuf_setlen(header, sensitive_header - header->buf);
599 strbuf_addbuf(header, &redacted_header);
Elia Pinto74c682d2016-05-23 13:44:02 +0000600 }
601}
602
603static void curl_dump_header(const char *text, unsigned char *ptr, size_t size, int hide_sensitive_header)
604{
605 struct strbuf out = STRBUF_INIT;
606 struct strbuf **headers, **header;
607
608 strbuf_addf(&out, "%s, %10.10ld bytes (0x%8.8lx)\n",
609 text, (long)size, (long)size);
610 trace_strbuf(&trace_curl, &out);
611 strbuf_reset(&out);
612 strbuf_add(&out, ptr, size);
613 headers = strbuf_split_max(&out, '\n', 0);
614
615 for (header = headers; *header; header++) {
616 if (hide_sensitive_header)
617 redact_sensitive_header(*header);
René Scharfea91cc7f2020-02-09 14:44:23 +0100618 strbuf_insertstr((*header), 0, text);
619 strbuf_insertstr((*header), strlen(text), ": ");
Elia Pinto74c682d2016-05-23 13:44:02 +0000620 strbuf_rtrim((*header));
621 strbuf_addch((*header), '\n');
622 trace_strbuf(&trace_curl, (*header));
623 }
624 strbuf_list_free(headers);
625 strbuf_release(&out);
626}
627
628static void curl_dump_data(const char *text, unsigned char *ptr, size_t size)
629{
630 size_t i;
631 struct strbuf out = STRBUF_INIT;
632 unsigned int width = 60;
633
634 strbuf_addf(&out, "%s, %10.10ld bytes (0x%8.8lx)\n",
635 text, (long)size, (long)size);
636 trace_strbuf(&trace_curl, &out);
637
638 for (i = 0; i < size; i += width) {
639 size_t w;
640
641 strbuf_reset(&out);
642 strbuf_addf(&out, "%s: ", text);
643 for (w = 0; (w < width) && (i + w < size); w++) {
644 unsigned char ch = ptr[i + w];
645
646 strbuf_addch(&out,
647 (ch >= 0x20) && (ch < 0x80)
648 ? ch : '.');
649 }
650 strbuf_addch(&out, '\n');
651 trace_strbuf(&trace_curl, &out);
652 }
653 strbuf_release(&out);
654}
655
656static int curl_trace(CURL *handle, curl_infotype type, char *data, size_t size, void *userp)
657{
658 const char *text;
659 enum { NO_FILTER = 0, DO_FILTER = 1 };
660
661 switch (type) {
662 case CURLINFO_TEXT:
663 trace_printf_key(&trace_curl, "== Info: %s", data);
Jeff Kingd0e99832017-09-21 02:23:24 -0400664 break;
Elia Pinto74c682d2016-05-23 13:44:02 +0000665 case CURLINFO_HEADER_OUT:
666 text = "=> Send header";
667 curl_dump_header(text, (unsigned char *)data, size, DO_FILTER);
668 break;
669 case CURLINFO_DATA_OUT:
Jonathan Tan8ba18e62018-01-18 16:28:02 -0800670 if (trace_curl_data) {
671 text = "=> Send data";
672 curl_dump_data(text, (unsigned char *)data, size);
673 }
Elia Pinto74c682d2016-05-23 13:44:02 +0000674 break;
675 case CURLINFO_SSL_DATA_OUT:
Jonathan Tan8ba18e62018-01-18 16:28:02 -0800676 if (trace_curl_data) {
677 text = "=> Send SSL data";
678 curl_dump_data(text, (unsigned char *)data, size);
679 }
Elia Pinto74c682d2016-05-23 13:44:02 +0000680 break;
681 case CURLINFO_HEADER_IN:
682 text = "<= Recv header";
683 curl_dump_header(text, (unsigned char *)data, size, NO_FILTER);
684 break;
685 case CURLINFO_DATA_IN:
Jonathan Tan8ba18e62018-01-18 16:28:02 -0800686 if (trace_curl_data) {
687 text = "<= Recv data";
688 curl_dump_data(text, (unsigned char *)data, size);
689 }
Elia Pinto74c682d2016-05-23 13:44:02 +0000690 break;
691 case CURLINFO_SSL_DATA_IN:
Jonathan Tan8ba18e62018-01-18 16:28:02 -0800692 if (trace_curl_data) {
693 text = "<= Recv SSL data";
694 curl_dump_data(text, (unsigned char *)data, size);
695 }
Elia Pinto74c682d2016-05-23 13:44:02 +0000696 break;
Jeff Kingd0e99832017-09-21 02:23:24 -0400697
698 default: /* we ignore unknown types by default */
699 return 0;
Elia Pinto74c682d2016-05-23 13:44:02 +0000700 }
701 return 0;
702}
703
Jonathan Tan7167a622020-05-11 10:43:10 -0700704void http_trace_curl_no_data(void)
705{
706 trace_override_envvar(&trace_curl, "1");
707 trace_curl_data = 0;
708}
709
Elia Pinto74c682d2016-05-23 13:44:02 +0000710void setup_curl_trace(CURL *handle)
711{
712 if (!trace_want(&trace_curl))
713 return;
714 curl_easy_setopt(handle, CURLOPT_VERBOSE, 1L);
715 curl_easy_setopt(handle, CURLOPT_DEBUGFUNCTION, curl_trace);
716 curl_easy_setopt(handle, CURLOPT_DEBUGDATA, NULL);
717}
718
Brandon Williamsa768a022016-12-14 14:39:54 -0800719static long get_curl_allowed_protocols(int from_user)
Brandon Williamsaeae4db2016-12-14 14:39:53 -0800720{
721 long allowed_protocols = 0;
722
Brandon Williamsa768a022016-12-14 14:39:54 -0800723 if (is_transport_allowed("http", from_user))
Brandon Williamsaeae4db2016-12-14 14:39:53 -0800724 allowed_protocols |= CURLPROTO_HTTP;
Brandon Williamsa768a022016-12-14 14:39:54 -0800725 if (is_transport_allowed("https", from_user))
Brandon Williamsaeae4db2016-12-14 14:39:53 -0800726 allowed_protocols |= CURLPROTO_HTTPS;
Brandon Williamsa768a022016-12-14 14:39:54 -0800727 if (is_transport_allowed("ftp", from_user))
Brandon Williamsaeae4db2016-12-14 14:39:53 -0800728 allowed_protocols |= CURLPROTO_FTP;
Brandon Williamsa768a022016-12-14 14:39:54 -0800729 if (is_transport_allowed("ftps", from_user))
Brandon Williamsaeae4db2016-12-14 14:39:53 -0800730 allowed_protocols |= CURLPROTO_FTPS;
731
732 return allowed_protocols;
733}
Elia Pinto74c682d2016-05-23 13:44:02 +0000734
Force Charlied73019f2018-11-08 19:44:14 -0800735#if LIBCURL_VERSION_NUM >=0x072f00
736static int get_curl_http_version_opt(const char *version_string, long *opt)
737{
738 int i;
739 static struct {
740 const char *name;
741 long opt_token;
742 } choice[] = {
743 { "HTTP/1.1", CURL_HTTP_VERSION_1_1 },
744 { "HTTP/2", CURL_HTTP_VERSION_2 }
745 };
746
747 for (i = 0; i < ARRAY_SIZE(choice); i++) {
748 if (!strcmp(version_string, choice[i].name)) {
749 *opt = choice[i].opt_token;
750 return 0;
751 }
752 }
753
754 warning("unknown value given to http.version: '%s'", version_string);
755 return -1; /* not found */
756}
757
758#endif
759
Junio C Hamano4251ccb2009-03-09 18:47:29 -0700760static CURL *get_curl_handle(void)
Junio C Hamano11979b92005-11-18 17:06:46 -0800761{
Junio C Hamano4251ccb2009-03-09 18:47:29 -0700762 CURL *result = curl_easy_init();
Junio C Hamano11979b92005-11-18 17:06:46 -0800763
Bernhard Reiterfaa38072014-08-13 19:31:24 +0200764 if (!result)
765 die("curl_easy_init failed");
766
Junio C Hamanoa5ccc592008-02-21 15:10:37 -0800767 if (!curl_ssl_verify) {
768 curl_easy_setopt(result, CURLOPT_SSL_VERIFYPEER, 0);
769 curl_easy_setopt(result, CURLOPT_SSL_VERIFYHOST, 0);
770 } else {
771 /* Verify authenticity of the peer's certificate */
772 curl_easy_setopt(result, CURLOPT_SSL_VERIFYPEER, 1);
773 /* The name in the cert must match whom we tried to connect */
774 curl_easy_setopt(result, CURLOPT_SSL_VERIFYHOST, 2);
775 }
776
Force Charlied73019f2018-11-08 19:44:14 -0800777#if LIBCURL_VERSION_NUM >= 0x072f00 // 7.47.0
778 if (curl_http_version) {
779 long opt;
780 if (!get_curl_http_version_opt(curl_http_version, &opt)) {
781 /* Set request use http version */
782 curl_easy_setopt(result, CURLOPT_HTTP_VERSION, opt);
783 }
784 }
785#endif
786
Junio C Hamano11979b92005-11-18 17:06:46 -0800787 curl_easy_setopt(result, CURLOPT_NETRC, CURL_NETRC_OPTIONAL);
Junio C Hamano525ecd22009-12-28 10:04:24 -0800788 curl_easy_setopt(result, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
Junio C Hamano11979b92005-11-18 17:06:46 -0800789
Tom G. Christensendd5df532017-08-11 18:37:34 +0200790#ifdef CURLGSSAPI_DELEGATION_FLAG
Petr Stodulka26a7b232016-09-28 20:01:34 +0200791 if (curl_deleg) {
792 int i;
793 for (i = 0; i < ARRAY_SIZE(curl_deleg_levels); i++) {
794 if (!strcmp(curl_deleg, curl_deleg_levels[i].name)) {
795 curl_easy_setopt(result, CURLOPT_GSSAPI_DELEGATION,
796 curl_deleg_levels[i].curl_deleg_param);
797 break;
798 }
799 }
800 if (i == ARRAY_SIZE(curl_deleg_levels))
801 warning("Unknown delegation method '%s': using default",
802 curl_deleg);
803 }
804#endif
805
Brendan Forster93aef7c2018-10-25 11:53:55 -0700806 if (http_ssl_backend && !strcmp("schannel", http_ssl_backend) &&
807 !http_schannel_check_revoke) {
808#if LIBCURL_VERSION_NUM >= 0x072c00
809 curl_easy_setopt(result, CURLOPT_SSL_OPTIONS, CURLSSLOPT_NO_REVOKE);
810#else
Jean-Noël Avilad355e462018-11-28 22:43:09 +0100811 warning(_("CURLSSLOPT_NO_REVOKE not supported with cURL < 7.44.0"));
Brendan Forster93aef7c2018-10-25 11:53:55 -0700812#endif
813 }
814
Jeff Kinga4ddbc32011-12-13 19:11:56 -0500815 if (http_proactive_auth)
816 init_curl_http_auth(result);
817
Elia Pinto01861cb2015-08-14 21:37:43 +0200818 if (getenv("GIT_SSL_VERSION"))
819 ssl_version = getenv("GIT_SSL_VERSION");
820 if (ssl_version && *ssl_version) {
821 int i;
822 for (i = 0; i < ARRAY_SIZE(sslversions); i++) {
823 if (!strcmp(ssl_version, sslversions[i].name)) {
824 curl_easy_setopt(result, CURLOPT_SSLVERSION,
825 sslversions[i].ssl_version);
826 break;
827 }
828 }
829 if (i == ARRAY_SIZE(sslversions))
830 warning("unsupported ssl version %s: using default",
831 ssl_version);
832 }
833
Lars Kellogg-Stedmanf6f2a9e2015-05-08 09:22:15 -0400834 if (getenv("GIT_SSL_CIPHER_LIST"))
835 ssl_cipherlist = getenv("GIT_SSL_CIPHER_LIST");
Lars Kellogg-Stedmanf6f2a9e2015-05-08 09:22:15 -0400836 if (ssl_cipherlist != NULL && *ssl_cipherlist)
837 curl_easy_setopt(result, CURLOPT_SSL_CIPHER_LIST,
838 ssl_cipherlist);
839
Junio C Hamano11979b92005-11-18 17:06:46 -0800840 if (ssl_cert != NULL)
841 curl_easy_setopt(result, CURLOPT_SSLCERT, ssl_cert);
Mark Lodato30dd9162009-05-27 23:16:02 -0400842 if (has_cert_password())
Jeff King148bb6a2011-12-10 05:31:21 -0500843 curl_easy_setopt(result, CURLOPT_KEYPASSWD, cert_auth.password);
Junio C Hamano11979b92005-11-18 17:06:46 -0800844 if (ssl_key != NULL)
845 curl_easy_setopt(result, CURLOPT_SSLKEY, ssl_key);
Junio C Hamano11979b92005-11-18 17:06:46 -0800846 if (ssl_capath != NULL)
847 curl_easy_setopt(result, CURLOPT_CAPATH, ssl_capath);
Christoph Eggeraeff8a62016-02-15 15:04:22 +0100848#if LIBCURL_VERSION_NUM >= 0x072c00
849 if (ssl_pinnedkey != NULL)
850 curl_easy_setopt(result, CURLOPT_PINNEDPUBLICKEY, ssl_pinnedkey);
851#endif
Johannes Schindelinb67d40a2018-10-25 11:53:56 -0700852 if (http_ssl_backend && !strcmp("schannel", http_ssl_backend) &&
853 !http_schannel_use_ssl_cainfo) {
854 curl_easy_setopt(result, CURLOPT_CAINFO, NULL);
855#if LIBCURL_VERSION_NUM >= 0x073400
856 curl_easy_setopt(result, CURLOPT_PROXY_CAINFO, NULL);
857#endif
Jorge Lopez Silva88238e02020-03-04 18:40:05 +0000858 } else if (ssl_cainfo != NULL || http_proxy_ssl_ca_info != NULL) {
859 if (ssl_cainfo != NULL)
860 curl_easy_setopt(result, CURLOPT_CAINFO, ssl_cainfo);
861#if LIBCURL_VERSION_NUM >= 0x073400
862 if (http_proxy_ssl_ca_info != NULL)
863 curl_easy_setopt(result, CURLOPT_PROXY_CAINFO, http_proxy_ssl_ca_info);
864#endif
865 }
Junio C Hamano11979b92005-11-18 17:06:46 -0800866
867 if (curl_low_speed_limit > 0 && curl_low_speed_time > 0) {
868 curl_easy_setopt(result, CURLOPT_LOW_SPEED_LIMIT,
869 curl_low_speed_limit);
870 curl_easy_setopt(result, CURLOPT_LOW_SPEED_TIME,
871 curl_low_speed_time);
872 }
873
Blake Burkhartb2581162015-09-22 18:06:20 -0400874 curl_easy_setopt(result, CURLOPT_MAXREDIRS, 20);
Tay Ray Chuan311e2ea2010-09-25 12:20:35 +0800875 curl_easy_setopt(result, CURLOPT_POSTREDIR, CURL_REDIR_POST_ALL);
Brandon Williamsaeae4db2016-12-14 14:39:53 -0800876 curl_easy_setopt(result, CURLOPT_REDIR_PROTOCOLS,
Brandon Williamsa768a022016-12-14 14:39:54 -0800877 get_curl_allowed_protocols(0));
Brandon Williamsaeae4db2016-12-14 14:39:53 -0800878 curl_easy_setopt(result, CURLOPT_PROTOCOLS,
Brandon Williamsa768a022016-12-14 14:39:54 -0800879 get_curl_allowed_protocols(-1));
Mark Wooding7982d742006-02-01 11:44:37 +0000880 if (getenv("GIT_CURL_VERBOSE"))
Jonathan Tan7167a622020-05-11 10:43:10 -0700881 http_trace_curl_no_data();
Elia Pinto74c682d2016-05-23 13:44:02 +0000882 setup_curl_trace(result);
Jonathan Tan8ba18e62018-01-18 16:28:02 -0800883 if (getenv("GIT_TRACE_CURL_NO_DATA"))
884 trace_curl_data = 0;
Jonathan Tan827e7d42020-06-05 14:21:36 -0700885 if (!git_env_bool("GIT_TRACE_REDACT", 1))
886 trace_curl_redact = 0;
Mark Wooding7982d742006-02-01 11:44:37 +0000887
Spencer E. Olsonb1d10582010-08-11 14:40:38 -0600888 curl_easy_setopt(result, CURLOPT_USERAGENT,
Jeff King745c7c82012-06-02 15:03:08 -0400889 user_agent ? user_agent : git_user_agent());
Nick Hengeveld20fc9bc2006-04-04 10:11:29 -0700890
Sasha Khapyorsky3ea099d2006-09-29 03:10:44 +0300891 if (curl_ftp_no_epsv)
892 curl_easy_setopt(result, CURLOPT_FTP_USE_EPSV, 0);
893
Modestas Vainius4bc444e2013-04-07 22:10:39 +0300894 if (curl_ssl_try)
895 curl_easy_setopt(result, CURLOPT_USE_SSL, CURLUSESSL_TRY);
Modestas Vainius4bc444e2013-04-07 22:10:39 +0300896
Knut Franke372370f2016-01-26 13:02:48 +0000897 /*
898 * CURL also examines these variables as a fallback; but we need to query
899 * them here in order to decide whether to prompt for missing password (cf.
900 * init_curl_proxy_auth()).
901 *
902 * Unlike many other common environment variables, these are historically
903 * lowercase only. It appears that CURL did not know this and implemented
904 * only uppercase variants, which was later corrected to take both - with
905 * the exception of http_proxy, which is lowercase only also in CURL. As
906 * the lowercase versions are the historical quasi-standard, they take
907 * precedence here, as in CURL.
908 */
909 if (!curl_http_proxy) {
Jeff Kingd63ed6e2016-09-07 16:06:42 -0400910 if (http_auth.protocol && !strcmp(http_auth.protocol, "https")) {
Knut Franke372370f2016-01-26 13:02:48 +0000911 var_override(&curl_http_proxy, getenv("HTTPS_PROXY"));
912 var_override(&curl_http_proxy, getenv("https_proxy"));
913 } else {
914 var_override(&curl_http_proxy, getenv("http_proxy"));
915 }
916 if (!curl_http_proxy) {
917 var_override(&curl_http_proxy, getenv("ALL_PROXY"));
918 var_override(&curl_http_proxy, getenv("all_proxy"));
919 }
920 }
921
Sergey Ryazanov57415082017-04-11 23:22:18 +0300922 if (curl_http_proxy && curl_http_proxy[0] == '\0') {
923 /*
924 * Handle case with the empty http.proxy value here to keep
925 * common code clean.
926 * NB: empty option disables proxying at all.
927 */
928 curl_easy_setopt(result, CURLOPT_PROXY, "");
929 } else if (curl_http_proxy) {
Pat Thoyts6d7afe02015-10-26 14:15:07 +0100930#if LIBCURL_VERSION_NUM >= 0x071800
Junio C Hamano87f8a0b2016-04-08 12:16:06 -0700931 if (starts_with(curl_http_proxy, "socks5h"))
932 curl_easy_setopt(result,
933 CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5_HOSTNAME);
934 else if (starts_with(curl_http_proxy, "socks5"))
Pat Thoyts6d7afe02015-10-26 14:15:07 +0100935 curl_easy_setopt(result,
936 CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
937 else if (starts_with(curl_http_proxy, "socks4a"))
938 curl_easy_setopt(result,
939 CURLOPT_PROXYTYPE, CURLPROXY_SOCKS4A);
940 else if (starts_with(curl_http_proxy, "socks"))
941 curl_easy_setopt(result,
942 CURLOPT_PROXYTYPE, CURLPROXY_SOCKS4);
943#endif
Wei Shuyu82b68032017-12-20 01:24:01 +0800944#if LIBCURL_VERSION_NUM >= 0x073400
Jorge Lopez Silva88238e02020-03-04 18:40:05 +0000945 else if (starts_with(curl_http_proxy, "https")) {
946 curl_easy_setopt(result, CURLOPT_PROXYTYPE, CURLPROXY_HTTPS);
947
948 if (http_proxy_ssl_cert)
949 curl_easy_setopt(result, CURLOPT_PROXY_SSLCERT, http_proxy_ssl_cert);
950
951 if (http_proxy_ssl_key)
952 curl_easy_setopt(result, CURLOPT_PROXY_SSLKEY, http_proxy_ssl_key);
953
954 if (has_proxy_cert_password())
955 curl_easy_setopt(result, CURLOPT_PROXY_KEYPASSWD, proxy_cert_auth.password);
956 }
Wei Shuyu82b68032017-12-20 01:24:01 +0800957#endif
Knut Franke372370f2016-01-26 13:02:48 +0000958 if (strstr(curl_http_proxy, "://"))
959 credential_from_url(&proxy_auth, curl_http_proxy);
960 else {
961 struct strbuf url = STRBUF_INIT;
962 strbuf_addf(&url, "http://%s", curl_http_proxy);
963 credential_from_url(&proxy_auth, url.buf);
964 strbuf_release(&url);
965 }
966
Sergey Ryazanovae51d912017-04-11 23:22:19 +0300967 if (!proxy_auth.host)
968 die("Invalid proxy URL '%s'", curl_http_proxy);
969
Knut Franke372370f2016-01-26 13:02:48 +0000970 curl_easy_setopt(result, CURLOPT_PROXY, proxy_auth.host);
Jiang Xind445fda2016-02-29 23:16:57 +0800971 var_override(&curl_no_proxy, getenv("NO_PROXY"));
972 var_override(&curl_no_proxy, getenv("no_proxy"));
973 curl_easy_setopt(result, CURLOPT_NOPROXY, curl_no_proxy);
Nelson Benitez Leondd613992012-03-02 14:55:57 +0100974 }
Knut Frankeef976392016-01-26 13:02:47 +0000975 init_curl_proxy_auth(result);
Sam Vilain9c5665a2007-11-23 13:07:00 +1300976
Jeff King47ce1152013-10-14 20:06:14 -0400977 set_curl_keepalive(result);
Eric Wonga15d0692013-10-12 22:29:40 +0000978
Junio C Hamano11979b92005-11-18 17:06:46 -0800979 return result;
980}
981
Junio C Hamano7059cd92009-03-09 19:00:30 -0700982static void set_from_env(const char **var, const char *envname)
983{
984 const char *val = getenv(envname);
985 if (val)
986 *var = val;
987}
988
Jeff Kinga4ddbc32011-12-13 19:11:56 -0500989void http_init(struct remote *remote, const char *url, int proactive_auth)
Nick Hengeveld29508e12005-11-18 11:02:58 -0800990{
991 char *low_speed_limit;
992 char *low_speed_time;
Kyle J. McKay6a569932013-08-05 13:20:36 -0700993 char *normalized_url;
994 struct urlmatch_config config = { STRING_LIST_INIT_DUP };
995
996 config.section = "http";
997 config.key = NULL;
998 config.collect_fn = http_options;
999 config.cascade_fn = git_default_config;
1000 config.cb = NULL;
Nick Hengeveld29508e12005-11-18 11:02:58 -08001001
Tay Ray Chuane9176742009-06-06 16:43:41 +08001002 http_is_verbose = 0;
Kyle J. McKay6a569932013-08-05 13:20:36 -07001003 normalized_url = url_normalize(url, &config.url);
Tay Ray Chuane9176742009-06-06 16:43:41 +08001004
Kyle J. McKay6a569932013-08-05 13:20:36 -07001005 git_config(urlmatch_config_entry, &config);
1006 free(normalized_url);
Mike Hommey7e927562019-08-26 16:49:11 +09001007 string_list_clear(&config.vars, 1);
Junio C Hamano7059cd92009-03-09 19:00:30 -07001008
Johannes Schindelin21084e82018-10-15 03:14:43 -07001009#if LIBCURL_VERSION_NUM >= 0x073800
1010 if (http_ssl_backend) {
1011 const curl_ssl_backend **backends;
1012 struct strbuf buf = STRBUF_INIT;
1013 int i;
1014
1015 switch (curl_global_sslset(-1, http_ssl_backend, &backends)) {
1016 case CURLSSLSET_UNKNOWN_BACKEND:
1017 strbuf_addf(&buf, _("Unsupported SSL backend '%s'. "
1018 "Supported SSL backends:"),
1019 http_ssl_backend);
1020 for (i = 0; backends[i]; i++)
1021 strbuf_addf(&buf, "\n\t%s", backends[i]->name);
1022 die("%s", buf.buf);
1023 case CURLSSLSET_NO_BACKENDS:
1024 die(_("Could not set SSL backend to '%s': "
1025 "cURL was built without SSL backends"),
1026 http_ssl_backend);
1027 case CURLSSLSET_TOO_LATE:
1028 die(_("Could not set SSL backend to '%s': already set"),
1029 http_ssl_backend);
1030 case CURLSSLSET_OK:
1031 break; /* Okay! */
1032 }
1033 }
1034#endif
1035
Bernhard Reiterfaa38072014-08-13 19:31:24 +02001036 if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK)
1037 die("curl_global_init failed");
Nick Hengeveld29508e12005-11-18 11:02:58 -08001038
Jeff Kinga4ddbc32011-12-13 19:11:56 -05001039 http_proactive_auth = proactive_auth;
1040
Mike Hommey9fc64402008-02-27 21:35:50 +01001041 if (remote && remote->http_proxy)
1042 curl_http_proxy = xstrdup(remote->http_proxy);
1043
Knut Frankeef976392016-01-26 13:02:47 +00001044 if (remote)
1045 var_override(&http_proxy_authmethod, remote->http_proxy_authmethod);
1046
Johannes Schindelin8cb01e22016-04-27 14:20:37 +02001047 pragma_header = curl_slist_append(http_copy_default_headers(),
1048 "Pragma: no-cache");
1049 no_pragma_header = curl_slist_append(http_copy_default_headers(),
1050 "Pragma:");
Nick Hengeveld29508e12005-11-18 11:02:58 -08001051
Nick Hengeveld29508e12005-11-18 11:02:58 -08001052 {
1053 char *http_max_requests = getenv("GIT_HTTP_MAX_REQUESTS");
1054 if (http_max_requests != NULL)
1055 max_requests = atoi(http_max_requests);
1056 }
1057
1058 curlm = curl_multi_init();
Jeff King8837eb42014-08-17 03:35:53 -04001059 if (!curlm)
1060 die("curl_multi_init failed");
Nick Hengeveld29508e12005-11-18 11:02:58 -08001061
1062 if (getenv("GIT_SSL_NO_VERIFY"))
1063 curl_ssl_verify = 0;
1064
Junio C Hamano7059cd92009-03-09 19:00:30 -07001065 set_from_env(&ssl_cert, "GIT_SSL_CERT");
Junio C Hamano7059cd92009-03-09 19:00:30 -07001066 set_from_env(&ssl_key, "GIT_SSL_KEY");
Junio C Hamano7059cd92009-03-09 19:00:30 -07001067 set_from_env(&ssl_capath, "GIT_SSL_CAPATH");
Junio C Hamano7059cd92009-03-09 19:00:30 -07001068 set_from_env(&ssl_cainfo, "GIT_SSL_CAINFO");
Nick Hengeveld29508e12005-11-18 11:02:58 -08001069
Spencer E. Olsonb1d10582010-08-11 14:40:38 -06001070 set_from_env(&user_agent, "GIT_HTTP_USER_AGENT");
1071
Nick Hengeveld29508e12005-11-18 11:02:58 -08001072 low_speed_limit = getenv("GIT_HTTP_LOW_SPEED_LIMIT");
1073 if (low_speed_limit != NULL)
1074 curl_low_speed_limit = strtol(low_speed_limit, NULL, 10);
1075 low_speed_time = getenv("GIT_HTTP_LOW_SPEED_TIME");
1076 if (low_speed_time != NULL)
1077 curl_low_speed_time = strtol(low_speed_time, NULL, 10);
1078
Nick Hengeveld29508e12005-11-18 11:02:58 -08001079 if (curl_ssl_verify == -1)
1080 curl_ssl_verify = 1;
1081
Tay Ray Chuanad75ebe2009-11-27 23:42:26 +08001082 curl_session_count = 0;
Nick Hengeveld29508e12005-11-18 11:02:58 -08001083 if (max_requests < 1)
1084 max_requests = DEFAULT_MAX_REQUESTS;
Nick Hengeveld29508e12005-11-18 11:02:58 -08001085
Jorge Lopez Silvaaf026512020-03-04 18:40:06 +00001086 set_from_env(&http_proxy_ssl_cert, "GIT_PROXY_SSL_CERT");
1087 set_from_env(&http_proxy_ssl_key, "GIT_PROXY_SSL_KEY");
1088 set_from_env(&http_proxy_ssl_ca_info, "GIT_PROXY_SSL_CAINFO");
1089
1090 if (getenv("GIT_PROXY_SSL_CERT_PASSWORD_PROTECTED"))
1091 proxy_ssl_cert_password_required = 1;
1092
Sasha Khapyorsky3ea099d2006-09-29 03:10:44 +03001093 if (getenv("GIT_CURL_FTP_NO_EPSV"))
1094 curl_ftp_no_epsv = 1;
1095
Jeff Kingdeba4932011-10-14 09:40:40 +02001096 if (url) {
Jeff King148bb6a2011-12-10 05:31:21 -05001097 credential_from_url(&http_auth, url);
Mark Lodato754ae192009-05-27 23:16:03 -04001098 if (!ssl_cert_password_required &&
1099 getenv("GIT_SSL_CERT_PASSWORD_PROTECTED") &&
Christian Couder59556542013-11-30 21:55:40 +01001100 starts_with(url, "https://"))
Mark Lodato30dd9162009-05-27 23:16:02 -04001101 ssl_cert_password_required = 1;
1102 }
Junio C Hamanoc33976c2009-03-09 23:34:25 -07001103
Nick Hengeveld29508e12005-11-18 11:02:58 -08001104 curl_default = get_curl_handle();
Nick Hengeveld29508e12005-11-18 11:02:58 -08001105}
1106
1107void http_cleanup(void)
1108{
1109 struct active_request_slot *slot = active_queue_head;
Nick Hengeveld29508e12005-11-18 11:02:58 -08001110
1111 while (slot != NULL) {
Shawn O. Pearce3278cd02007-09-15 03:23:00 -04001112 struct active_request_slot *next = slot->next;
Mike Hommeyf23d1f72008-03-03 20:30:16 +01001113 if (slot->curl != NULL) {
Eric Wongd8b6b842016-09-13 00:25:56 +00001114 xmulti_remove_handle(slot);
Nick Hengeveld29508e12005-11-18 11:02:58 -08001115 curl_easy_cleanup(slot->curl);
Mike Hommeyf23d1f72008-03-03 20:30:16 +01001116 }
Shawn O. Pearce3278cd02007-09-15 03:23:00 -04001117 free(slot);
1118 slot = next;
Nick Hengeveld29508e12005-11-18 11:02:58 -08001119 }
Shawn O. Pearce3278cd02007-09-15 03:23:00 -04001120 active_queue_head = NULL;
Nick Hengeveld29508e12005-11-18 11:02:58 -08001121
Nick Hengeveld29508e12005-11-18 11:02:58 -08001122 curl_easy_cleanup(curl_default);
Nick Hengeveld29508e12005-11-18 11:02:58 -08001123
Nick Hengeveld29508e12005-11-18 11:02:58 -08001124 curl_multi_cleanup(curlm);
Nick Hengeveld29508e12005-11-18 11:02:58 -08001125 curl_global_cleanup();
Nick Hengeveldb3ca4e4e2006-06-06 09:41:32 -07001126
Johannes Schindelin4d17fd22019-11-06 10:04:55 +00001127 string_list_clear(&extra_http_headers, 0);
Johannes Schindelin8cb01e22016-04-27 14:20:37 +02001128
Nick Hengeveldb3ca4e4e2006-06-06 09:41:32 -07001129 curl_slist_free_all(pragma_header);
Shawn O. Pearce3278cd02007-09-15 03:23:00 -04001130 pragma_header = NULL;
Mike Hommey9fc64402008-02-27 21:35:50 +01001131
Tay Ray Chuane9176742009-06-06 16:43:41 +08001132 curl_slist_free_all(no_pragma_header);
1133 no_pragma_header = NULL;
1134
Mike Hommey9fc64402008-02-27 21:35:50 +01001135 if (curl_http_proxy) {
Miklos Vajnae4a80ec2008-12-07 01:45:37 +01001136 free((void *)curl_http_proxy);
Mike Hommey9fc64402008-02-27 21:35:50 +01001137 curl_http_proxy = NULL;
1138 }
Mark Lodato30dd9162009-05-27 23:16:02 -04001139
Knut Franke372370f2016-01-26 13:02:48 +00001140 if (proxy_auth.password) {
1141 memset(proxy_auth.password, 0, strlen(proxy_auth.password));
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +00001142 FREE_AND_NULL(proxy_auth.password);
Knut Franke372370f2016-01-26 13:02:48 +00001143 }
1144
1145 free((void *)curl_proxyuserpwd);
1146 curl_proxyuserpwd = NULL;
1147
Knut Frankeef976392016-01-26 13:02:47 +00001148 free((void *)http_proxy_authmethod);
1149 http_proxy_authmethod = NULL;
1150
Jeff King148bb6a2011-12-10 05:31:21 -05001151 if (cert_auth.password != NULL) {
1152 memset(cert_auth.password, 0, strlen(cert_auth.password));
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +00001153 FREE_AND_NULL(cert_auth.password);
Mark Lodato30dd9162009-05-27 23:16:02 -04001154 }
1155 ssl_cert_password_required = 0;
Yi EungJunf18604b2015-01-28 21:04:37 +09001156
Jorge Lopez Silva88238e02020-03-04 18:40:05 +00001157 if (proxy_cert_auth.password != NULL) {
1158 memset(proxy_cert_auth.password, 0, strlen(proxy_cert_auth.password));
1159 FREE_AND_NULL(proxy_cert_auth.password);
1160 }
1161 proxy_ssl_cert_password_required = 0;
1162
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +00001163 FREE_AND_NULL(cached_accept_language);
Nick Hengeveld29508e12005-11-18 11:02:58 -08001164}
1165
Nick Hengeveld29508e12005-11-18 11:02:58 -08001166struct active_request_slot *get_active_slot(void)
1167{
1168 struct active_request_slot *slot = active_queue_head;
1169 struct active_request_slot *newslot;
1170
Nick Hengeveld29508e12005-11-18 11:02:58 -08001171 int num_transfers;
1172
1173 /* Wait for a slot to open up if the queue is full */
1174 while (active_requests >= max_requests) {
1175 curl_multi_perform(curlm, &num_transfers);
Junio C Hamano4251ccb2009-03-09 18:47:29 -07001176 if (num_transfers < active_requests)
Nick Hengeveld29508e12005-11-18 11:02:58 -08001177 process_curl_messages();
Nick Hengeveld29508e12005-11-18 11:02:58 -08001178 }
Nick Hengeveld29508e12005-11-18 11:02:58 -08001179
Junio C Hamano4251ccb2009-03-09 18:47:29 -07001180 while (slot != NULL && slot->in_use)
Nick Hengeveld29508e12005-11-18 11:02:58 -08001181 slot = slot->next;
Junio C Hamano4251ccb2009-03-09 18:47:29 -07001182
Nick Hengeveld29508e12005-11-18 11:02:58 -08001183 if (slot == NULL) {
1184 newslot = xmalloc(sizeof(*newslot));
1185 newslot->curl = NULL;
1186 newslot->in_use = 0;
1187 newslot->next = NULL;
1188
1189 slot = active_queue_head;
1190 if (slot == NULL) {
1191 active_queue_head = newslot;
1192 } else {
Junio C Hamano4251ccb2009-03-09 18:47:29 -07001193 while (slot->next != NULL)
Nick Hengeveld29508e12005-11-18 11:02:58 -08001194 slot = slot->next;
Nick Hengeveld29508e12005-11-18 11:02:58 -08001195 slot->next = newslot;
1196 }
1197 slot = newslot;
1198 }
1199
1200 if (slot->curl == NULL) {
Nick Hengeveld29508e12005-11-18 11:02:58 -08001201 slot->curl = curl_easy_duphandle(curl_default);
Tay Ray Chuanad75ebe2009-11-27 23:42:26 +08001202 curl_session_count++;
Nick Hengeveld29508e12005-11-18 11:02:58 -08001203 }
1204
1205 active_requests++;
1206 slot->in_use = 1;
Nick Hengeveldc8568e12006-01-31 11:06:55 -08001207 slot->results = NULL;
Nick Hengeveldbaa7b672006-03-10 20:18:01 -08001208 slot->finished = NULL;
Nick Hengeveld29508e12005-11-18 11:02:58 -08001209 slot->callback_data = NULL;
1210 slot->callback_func = NULL;
Duncan Brownbcfb95d2011-06-02 16:31:25 -04001211 curl_easy_setopt(slot->curl, CURLOPT_COOKIEFILE, curl_cookie_file);
Dave Borowitz912b2ac2013-07-23 15:40:17 -07001212 if (curl_save_cookies)
1213 curl_easy_setopt(slot->curl, CURLOPT_COOKIEJAR, curl_cookie_file);
Nick Hengeveld29508e12005-11-18 11:02:58 -08001214 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, pragma_header);
Nick Hengeveld29508e12005-11-18 11:02:58 -08001215 curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, curl_errorstr);
Nick Hengeveld90949502006-05-31 16:25:03 -07001216 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, NULL);
1217 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, NULL);
1218 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, NULL);
Junio C Hamano1e418272011-04-26 08:04:49 -07001219 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, NULL);
Nick Hengeveld90949502006-05-31 16:25:03 -07001220 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 0);
1221 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
Jeff Kingb793acf2013-04-15 20:30:38 -04001222 curl_easy_setopt(slot->curl, CURLOPT_FAILONERROR, 1);
David Turner835c4d32015-11-02 16:39:58 -05001223 curl_easy_setopt(slot->curl, CURLOPT_RANGE, NULL);
Eric Wongc915f112016-02-03 04:09:14 +00001224
Jeff King50d34132016-12-06 13:24:41 -05001225 /*
1226 * Default following to off unless "ALWAYS" is configured; this gives
1227 * callers a sane starting point, and they can tweak for individual
1228 * HTTP_FOLLOW_* cases themselves.
1229 */
1230 if (http_follow_config == HTTP_FOLLOW_ALWAYS)
1231 curl_easy_setopt(slot->curl, CURLOPT_FOLLOWLOCATION, 1);
1232 else
1233 curl_easy_setopt(slot->curl, CURLOPT_FOLLOWLOCATION, 0);
1234
Eric Wongc915f112016-02-03 04:09:14 +00001235 curl_easy_setopt(slot->curl, CURLOPT_IPRESOLVE, git_curl_ipresolve);
brian m. carlson4dbe6642015-01-08 00:29:20 +00001236 curl_easy_setopt(slot->curl, CURLOPT_HTTPAUTH, http_auth_methods);
Jeff King40a18fc2017-02-25 14:18:31 -05001237 if (http_auth.password || curl_empty_auth_enabled())
Jeff Kingdfa17252012-04-10 11:53:40 +02001238 init_curl_http_auth(slot->curl);
Nick Hengeveld29508e12005-11-18 11:02:58 -08001239
1240 return slot;
1241}
1242
1243int start_active_slot(struct active_request_slot *slot)
1244{
Nick Hengeveld29508e12005-11-18 11:02:58 -08001245 CURLMcode curlm_result = curl_multi_add_handle(curlm, slot->curl);
Daniel Barkalow45c17412007-09-10 23:02:28 -04001246 int num_transfers;
Nick Hengeveld29508e12005-11-18 11:02:58 -08001247
1248 if (curlm_result != CURLM_OK &&
1249 curlm_result != CURLM_CALL_MULTI_PERFORM) {
Eric Wong9f1b5882016-09-13 00:25:55 +00001250 warning("curl_multi_add_handle failed: %s",
1251 curl_multi_strerror(curlm_result));
Nick Hengeveld29508e12005-11-18 11:02:58 -08001252 active_requests--;
1253 slot->in_use = 0;
1254 return 0;
1255 }
Daniel Barkalow45c17412007-09-10 23:02:28 -04001256
1257 /*
1258 * We know there must be something to do, since we just added
1259 * something.
1260 */
1261 curl_multi_perform(curlm, &num_transfers);
Nick Hengeveld29508e12005-11-18 11:02:58 -08001262 return 1;
1263}
1264
Daniel Barkalowfc57b6a2007-09-10 23:02:34 -04001265struct fill_chain {
1266 void *data;
1267 int (*fill)(void *);
1268 struct fill_chain *next;
1269};
1270
Junio C Hamano4251ccb2009-03-09 18:47:29 -07001271static struct fill_chain *fill_cfg;
Daniel Barkalowfc57b6a2007-09-10 23:02:34 -04001272
1273void add_fill_function(void *data, int (*fill)(void *))
1274{
Brandon Williamsee6e0652018-02-14 10:59:42 -08001275 struct fill_chain *new_fill = xmalloc(sizeof(*new_fill));
Daniel Barkalowfc57b6a2007-09-10 23:02:34 -04001276 struct fill_chain **linkp = &fill_cfg;
Brandon Williamsee6e0652018-02-14 10:59:42 -08001277 new_fill->data = data;
1278 new_fill->fill = fill;
1279 new_fill->next = NULL;
Daniel Barkalowfc57b6a2007-09-10 23:02:34 -04001280 while (*linkp)
1281 linkp = &(*linkp)->next;
Brandon Williamsee6e0652018-02-14 10:59:42 -08001282 *linkp = new_fill;
Daniel Barkalowfc57b6a2007-09-10 23:02:34 -04001283}
1284
Daniel Barkalow45c17412007-09-10 23:02:28 -04001285void fill_active_slots(void)
1286{
1287 struct active_request_slot *slot = active_queue_head;
1288
Daniel Barkalowfc57b6a2007-09-10 23:02:34 -04001289 while (active_requests < max_requests) {
1290 struct fill_chain *fill;
1291 for (fill = fill_cfg; fill; fill = fill->next)
1292 if (fill->fill(fill->data))
1293 break;
1294
1295 if (!fill)
Daniel Barkalow45c17412007-09-10 23:02:28 -04001296 break;
Daniel Barkalowfc57b6a2007-09-10 23:02:34 -04001297 }
Daniel Barkalow45c17412007-09-10 23:02:28 -04001298
1299 while (slot != NULL) {
Tay Ray Chuanad75ebe2009-11-27 23:42:26 +08001300 if (!slot->in_use && slot->curl != NULL
1301 && curl_session_count > min_curl_sessions) {
Daniel Barkalow45c17412007-09-10 23:02:28 -04001302 curl_easy_cleanup(slot->curl);
1303 slot->curl = NULL;
Tay Ray Chuanad75ebe2009-11-27 23:42:26 +08001304 curl_session_count--;
Daniel Barkalow45c17412007-09-10 23:02:28 -04001305 }
1306 slot = slot->next;
1307 }
1308}
1309
Nick Hengeveld29508e12005-11-18 11:02:58 -08001310void step_active_slots(void)
1311{
1312 int num_transfers;
1313 CURLMcode curlm_result;
1314
1315 do {
1316 curlm_result = curl_multi_perform(curlm, &num_transfers);
1317 } while (curlm_result == CURLM_CALL_MULTI_PERFORM);
1318 if (num_transfers < active_requests) {
1319 process_curl_messages();
1320 fill_active_slots();
1321 }
1322}
Nick Hengeveld29508e12005-11-18 11:02:58 -08001323
1324void run_active_slot(struct active_request_slot *slot)
1325{
Nick Hengeveld29508e12005-11-18 11:02:58 -08001326 fd_set readfds;
1327 fd_set writefds;
1328 fd_set excfds;
1329 int max_fd;
1330 struct timeval select_timeout;
Nick Hengeveldbaa7b672006-03-10 20:18:01 -08001331 int finished = 0;
Nick Hengeveld29508e12005-11-18 11:02:58 -08001332
Nick Hengeveldbaa7b672006-03-10 20:18:01 -08001333 slot->finished = &finished;
1334 while (!finished) {
Nick Hengeveld29508e12005-11-18 11:02:58 -08001335 step_active_slots();
1336
Mika Fischerdf26c472011-11-04 15:19:27 +01001337 if (slot->in_use) {
Mika Fischereb56c822011-11-04 15:19:26 +01001338 long curl_timeout;
1339 curl_multi_timeout(curlm, &curl_timeout);
1340 if (curl_timeout == 0) {
1341 continue;
1342 } else if (curl_timeout == -1) {
1343 select_timeout.tv_sec = 0;
1344 select_timeout.tv_usec = 50000;
1345 } else {
1346 select_timeout.tv_sec = curl_timeout / 1000;
1347 select_timeout.tv_usec = (curl_timeout % 1000) * 1000;
1348 }
Nick Hengeveld29508e12005-11-18 11:02:58 -08001349
Mika Fischer6f9dd672011-11-04 15:19:25 +01001350 max_fd = -1;
Nick Hengeveld29508e12005-11-18 11:02:58 -08001351 FD_ZERO(&readfds);
1352 FD_ZERO(&writefds);
1353 FD_ZERO(&excfds);
Mika Fischer6f9dd672011-11-04 15:19:25 +01001354 curl_multi_fdset(curlm, &readfds, &writefds, &excfds, &max_fd);
Mika Fischereb56c822011-11-04 15:19:26 +01001355
Stefan Zager7202b812012-10-19 14:04:20 -07001356 /*
1357 * It can happen that curl_multi_timeout returns a pathologically
1358 * long timeout when curl_multi_fdset returns no file descriptors
1359 * to read. See commit message for more details.
1360 */
1361 if (max_fd < 0 &&
1362 (select_timeout.tv_sec > 0 ||
1363 select_timeout.tv_usec > 50000)) {
1364 select_timeout.tv_sec = 0;
1365 select_timeout.tv_usec = 50000;
1366 }
1367
Mika Fischer6f9dd672011-11-04 15:19:25 +01001368 select(max_fd+1, &readfds, &writefds, &excfds, &select_timeout);
Nick Hengeveld29508e12005-11-18 11:02:58 -08001369 }
1370 }
Nick Hengeveld29508e12005-11-18 11:02:58 -08001371}
1372
Junio C Hamano83e41e22010-01-11 22:26:08 -08001373static void release_active_slot(struct active_request_slot *slot)
Mark Wooding53f31382006-02-07 10:07:39 +00001374{
1375 closedown_active_slot(slot);
Eric Wong2abc8482016-09-13 00:25:57 +00001376 if (slot->curl) {
Eric Wongd8b6b842016-09-13 00:25:56 +00001377 xmulti_remove_handle(slot);
Eric Wong2abc8482016-09-13 00:25:57 +00001378 if (curl_session_count > min_curl_sessions) {
1379 curl_easy_cleanup(slot->curl);
1380 slot->curl = NULL;
1381 curl_session_count--;
1382 }
Mark Wooding53f31382006-02-07 10:07:39 +00001383 }
1384 fill_active_slots();
1385}
1386
Nick Hengeveld29508e12005-11-18 11:02:58 -08001387void finish_all_active_slots(void)
1388{
1389 struct active_request_slot *slot = active_queue_head;
1390
1391 while (slot != NULL)
1392 if (slot->in_use) {
1393 run_active_slot(slot);
1394 slot = active_queue_head;
1395 } else {
1396 slot = slot->next;
1397 }
1398}
Mike Hommeyd7e92802007-12-11 00:08:25 +01001399
Tay Ray Chuan5ace9942009-06-06 16:43:43 +08001400/* Helpers for modifying and creating URLs */
Mike Hommeyd7e92802007-12-11 00:08:25 +01001401static inline int needs_quote(int ch)
1402{
1403 if (((ch >= 'A') && (ch <= 'Z'))
1404 || ((ch >= 'a') && (ch <= 'z'))
1405 || ((ch >= '0') && (ch <= '9'))
1406 || (ch == '/')
1407 || (ch == '-')
1408 || (ch == '.'))
1409 return 0;
1410 return 1;
1411}
1412
Mike Hommeyd7e92802007-12-11 00:08:25 +01001413static char *quote_ref_url(const char *base, const char *ref)
1414{
Tay Ray Chuan113106e2009-03-08 00:47:21 +08001415 struct strbuf buf = STRBUF_INIT;
Mike Hommeyd7e92802007-12-11 00:08:25 +01001416 const char *cp;
Tay Ray Chuan113106e2009-03-08 00:47:21 +08001417 int ch;
Mike Hommeyd7e92802007-12-11 00:08:25 +01001418
Tay Ray Chuan5ace9942009-06-06 16:43:43 +08001419 end_url_with_slash(&buf, base);
Tay Ray Chuan113106e2009-03-08 00:47:21 +08001420
1421 for (cp = ref; (ch = *cp) != 0; cp++)
Mike Hommeyd7e92802007-12-11 00:08:25 +01001422 if (needs_quote(ch))
Tay Ray Chuan113106e2009-03-08 00:47:21 +08001423 strbuf_addf(&buf, "%%%02x", ch);
Mike Hommeyd7e92802007-12-11 00:08:25 +01001424 else
Tay Ray Chuan113106e2009-03-08 00:47:21 +08001425 strbuf_addch(&buf, *cp);
Mike Hommeyd7e92802007-12-11 00:08:25 +01001426
Tay Ray Chuan113106e2009-03-08 00:47:21 +08001427 return strbuf_detach(&buf, NULL);
Mike Hommeyd7e92802007-12-11 00:08:25 +01001428}
1429
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08001430void append_remote_object_url(struct strbuf *buf, const char *url,
1431 const char *hex,
1432 int only_two_digit_prefix)
1433{
Tay Ray Chuan800324c2009-08-17 17:09:43 +08001434 end_url_with_slash(buf, url);
1435
1436 strbuf_addf(buf, "objects/%.*s/", 2, hex);
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08001437 if (!only_two_digit_prefix)
René Scharfebc57b9c2016-08-05 22:37:11 +02001438 strbuf_addstr(buf, hex + 2);
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08001439}
1440
1441char *get_remote_object_url(const char *url, const char *hex,
1442 int only_two_digit_prefix)
1443{
1444 struct strbuf buf = STRBUF_INIT;
1445 append_remote_object_url(&buf, url, hex, only_two_digit_prefix);
1446 return strbuf_detach(&buf, NULL);
1447}
1448
Jeff Kinga3722bc2019-03-24 08:08:38 -04001449void normalize_curl_result(CURLcode *result, long http_code,
1450 char *errorstr, size_t errorlen)
Jeff King88097032012-08-27 09:26:04 -04001451{
Jeff King6d052d72013-04-05 18:14:06 -04001452 /*
1453 * If we see a failing http code with CURLE_OK, we have turned off
1454 * FAILONERROR (to keep the server's custom error response), and should
1455 * translate the code into failure here.
Jeff King50d34132016-12-06 13:24:41 -05001456 *
1457 * Likewise, if we see a redirect (30x code), that means we turned off
1458 * redirect-following, and we should treat the result as an error.
Jeff King6d052d72013-04-05 18:14:06 -04001459 */
Jeff Kinga3722bc2019-03-24 08:08:38 -04001460 if (*result == CURLE_OK && http_code >= 300) {
1461 *result = CURLE_HTTP_RETURNED_ERROR;
Jeff King6d052d72013-04-05 18:14:06 -04001462 /*
1463 * Normally curl will already have put the "reason phrase"
1464 * from the server into curl_errorstr; unfortunately without
1465 * FAILONERROR it is lost, so we can give only the numeric
1466 * status code.
1467 */
Jeff Kinga3722bc2019-03-24 08:08:38 -04001468 xsnprintf(errorstr, errorlen,
Jeff King1a168e52017-03-28 15:46:56 -04001469 "The requested URL returned error: %ld",
Jeff Kinga3722bc2019-03-24 08:08:38 -04001470 http_code);
Jeff King6d052d72013-04-05 18:14:06 -04001471 }
Jeff Kinga3722bc2019-03-24 08:08:38 -04001472}
1473
1474static int handle_curl_result(struct slot_results *results)
1475{
1476 normalize_curl_result(&results->curl_result, results->http_code,
1477 curl_errorstr, sizeof(curl_errorstr));
Jeff King6d052d72013-04-05 18:14:06 -04001478
Jeff King88097032012-08-27 09:26:04 -04001479 if (results->curl_result == CURLE_OK) {
1480 credential_approve(&http_auth);
John Szakmeistera4a44392021-03-11 21:40:27 -05001481 credential_approve(&proxy_auth);
John Szakmeistercd27f602021-03-11 21:40:26 -05001482 credential_approve(&cert_auth);
Jeff King88097032012-08-27 09:26:04 -04001483 return HTTP_OK;
John Szakmeistercd27f602021-03-11 21:40:26 -05001484 } else if (results->curl_result == CURLE_SSL_CERTPROBLEM) {
1485 /*
1486 * We can't tell from here whether it's a bad path, bad
1487 * certificate, bad password, or something else wrong
1488 * with the certificate. So we reject the credential to
1489 * avoid caching or saving a bad password.
1490 */
1491 credential_reject(&cert_auth);
1492 return HTTP_NOAUTH;
Jeff King88097032012-08-27 09:26:04 -04001493 } else if (missing_target(results))
1494 return HTTP_MISSING_TARGET;
1495 else if (results->http_code == 401) {
1496 if (http_auth.username && http_auth.password) {
1497 credential_reject(&http_auth);
1498 return HTTP_NOAUTH;
1499 } else {
Jeff Kingecf7b122021-05-18 02:27:42 -04001500 http_auth_methods &= ~CURLAUTH_GSSNEGOTIATE;
1501 if (results->auth_avail) {
1502 http_auth_methods &= results->auth_avail;
1503 http_auth_methods_restricted = 1;
1504 }
Jeff King88097032012-08-27 09:26:04 -04001505 return HTTP_REAUTH;
1506 }
1507 } else {
Knut Franke372370f2016-01-26 13:02:48 +00001508 if (results->http_connectcode == 407)
1509 credential_reject(&proxy_auth);
Jeff King88097032012-08-27 09:26:04 -04001510 if (!curl_errorstr[0])
1511 strlcpy(curl_errorstr,
1512 curl_easy_strerror(results->curl_result),
1513 sizeof(curl_errorstr));
1514 return HTTP_ERROR;
1515 }
1516}
1517
Jeff Kingbeed3362014-02-18 05:34:20 -05001518int run_one_slot(struct active_request_slot *slot,
1519 struct slot_results *results)
1520{
1521 slot->results = results;
1522 if (!start_active_slot(slot)) {
Jeff King1a168e52017-03-28 15:46:56 -04001523 xsnprintf(curl_errorstr, sizeof(curl_errorstr),
1524 "failed to start HTTP request");
Jeff Kingbeed3362014-02-18 05:34:20 -05001525 return HTTP_START_FAILED;
1526 }
1527
1528 run_active_slot(slot);
1529 return handle_curl_result(results);
1530}
1531
Johannes Schindelin8cb01e22016-04-27 14:20:37 +02001532struct curl_slist *http_copy_default_headers(void)
1533{
Johannes Schindelin4d17fd22019-11-06 10:04:55 +00001534 struct curl_slist *headers = NULL;
1535 const struct string_list_item *item;
Johannes Schindelin8cb01e22016-04-27 14:20:37 +02001536
Johannes Schindelin4d17fd22019-11-06 10:04:55 +00001537 for_each_string_list_item(item, &extra_http_headers)
1538 headers = curl_slist_append(headers, item->string);
Johannes Schindelin8cb01e22016-04-27 14:20:37 +02001539
1540 return headers;
1541}
1542
Jeff King132b70a2013-09-28 04:31:11 -04001543static CURLcode curlinfo_strbuf(CURL *curl, CURLINFO info, struct strbuf *buf)
1544{
1545 char *ptr;
1546 CURLcode ret;
1547
1548 strbuf_reset(buf);
1549 ret = curl_easy_getinfo(curl, info, &ptr);
1550 if (!ret && ptr)
1551 strbuf_addstr(buf, ptr);
1552 return ret;
1553}
1554
Jeff Kingbf197fd2014-05-22 05:29:47 -04001555/*
Jeff Kinge3131622014-05-22 05:30:05 -04001556 * Check for and extract a content-type parameter. "raw"
1557 * should be positioned at the start of the potential
1558 * parameter, with any whitespace already removed.
1559 *
1560 * "name" is the name of the parameter. The value is appended
1561 * to "out".
1562 */
1563static int extract_param(const char *raw, const char *name,
1564 struct strbuf *out)
1565{
1566 size_t len = strlen(name);
1567
1568 if (strncasecmp(raw, name, len))
1569 return -1;
1570 raw += len;
1571
1572 if (*raw != '=')
1573 return -1;
1574 raw++;
1575
Yi EungJunf34a6552014-06-18 07:11:53 +09001576 while (*raw && !isspace(*raw) && *raw != ';')
Jeff Kinge3131622014-05-22 05:30:05 -04001577 strbuf_addch(out, *raw++);
1578 return 0;
1579}
1580
1581/*
Jeff Kingbf197fd2014-05-22 05:29:47 -04001582 * Extract a normalized version of the content type, with any
1583 * spaces suppressed, all letters lowercased, and no trailing ";"
1584 * or parameters.
1585 *
1586 * Note that we will silently remove even invalid whitespace. For
1587 * example, "text / plain" is specifically forbidden by RFC 2616,
1588 * but "text/plain" is the only reasonable output, and this keeps
1589 * our code simple.
1590 *
Jeff Kinge3131622014-05-22 05:30:05 -04001591 * If the "charset" argument is not NULL, store the value of any
1592 * charset parameter there.
1593 *
Jeff Kingbf197fd2014-05-22 05:29:47 -04001594 * Example:
Jeff Kinge3131622014-05-22 05:30:05 -04001595 * "TEXT/PLAIN; charset=utf-8" -> "text/plain", "utf-8"
Jeff Kingbf197fd2014-05-22 05:29:47 -04001596 * "text / plain" -> "text/plain"
1597 */
Jeff Kinge3131622014-05-22 05:30:05 -04001598static void extract_content_type(struct strbuf *raw, struct strbuf *type,
1599 struct strbuf *charset)
Jeff Kingbf197fd2014-05-22 05:29:47 -04001600{
1601 const char *p;
1602
1603 strbuf_reset(type);
1604 strbuf_grow(type, raw->len);
1605 for (p = raw->buf; *p; p++) {
1606 if (isspace(*p))
1607 continue;
Jeff Kinge3131622014-05-22 05:30:05 -04001608 if (*p == ';') {
1609 p++;
Jeff Kingbf197fd2014-05-22 05:29:47 -04001610 break;
Jeff Kinge3131622014-05-22 05:30:05 -04001611 }
Jeff Kingbf197fd2014-05-22 05:29:47 -04001612 strbuf_addch(type, tolower(*p));
1613 }
Jeff Kinge3131622014-05-22 05:30:05 -04001614
1615 if (!charset)
1616 return;
1617
1618 strbuf_reset(charset);
1619 while (*p) {
Yi EungJunf34a6552014-06-18 07:11:53 +09001620 while (isspace(*p) || *p == ';')
Jeff Kinge3131622014-05-22 05:30:05 -04001621 p++;
1622 if (!extract_param(p, "charset", charset))
1623 return;
1624 while (*p && !isspace(*p))
1625 p++;
1626 }
Jeff Kingc553fd12014-05-22 05:36:12 -04001627
1628 if (!charset->len && starts_with(type->buf, "text/"))
1629 strbuf_addstr(charset, "ISO-8859-1");
Jeff Kingbf197fd2014-05-22 05:29:47 -04001630}
1631
Yi EungJunf18604b2015-01-28 21:04:37 +09001632static void write_accept_language(struct strbuf *buf)
1633{
1634 /*
1635 * MAX_DECIMAL_PLACES must not be larger than 3. If it is larger than
1636 * that, q-value will be smaller than 0.001, the minimum q-value the
1637 * HTTP specification allows. See
1638 * http://tools.ietf.org/html/rfc7231#section-5.3.1 for q-value.
1639 */
1640 const int MAX_DECIMAL_PLACES = 3;
1641 const int MAX_LANGUAGE_TAGS = 1000;
1642 const int MAX_ACCEPT_LANGUAGE_HEADER_SIZE = 4000;
1643 char **language_tags = NULL;
1644 int num_langs = 0;
1645 const char *s = get_preferred_languages();
1646 int i;
1647 struct strbuf tag = STRBUF_INIT;
1648
1649 /* Don't add Accept-Language header if no language is preferred. */
1650 if (!s)
1651 return;
1652
1653 /*
1654 * Split the colon-separated string of preferred languages into
1655 * language_tags array.
1656 */
1657 do {
1658 /* collect language tag */
1659 for (; *s && (isalnum(*s) || *s == '_'); s++)
1660 strbuf_addch(&tag, *s == '_' ? '-' : *s);
1661
1662 /* skip .codeset, @modifier and any other unnecessary parts */
1663 while (*s && *s != ':')
1664 s++;
1665
1666 if (tag.len) {
1667 num_langs++;
1668 REALLOC_ARRAY(language_tags, num_langs);
1669 language_tags[num_langs - 1] = strbuf_detach(&tag, NULL);
1670 if (num_langs >= MAX_LANGUAGE_TAGS - 1) /* -1 for '*' */
1671 break;
1672 }
1673 } while (*s++);
1674
1675 /* write Accept-Language header into buf */
1676 if (num_langs) {
1677 int last_buf_len = 0;
1678 int max_q;
1679 int decimal_places;
1680 char q_format[32];
1681
1682 /* add '*' */
1683 REALLOC_ARRAY(language_tags, num_langs + 1);
1684 language_tags[num_langs++] = "*"; /* it's OK; this won't be freed */
1685
1686 /* compute decimal_places */
1687 for (max_q = 1, decimal_places = 0;
1688 max_q < num_langs && decimal_places <= MAX_DECIMAL_PLACES;
1689 decimal_places++, max_q *= 10)
1690 ;
1691
Jeff King5096d492015-09-24 17:06:08 -04001692 xsnprintf(q_format, sizeof(q_format), ";q=0.%%0%dd", decimal_places);
Yi EungJunf18604b2015-01-28 21:04:37 +09001693
1694 strbuf_addstr(buf, "Accept-Language: ");
1695
1696 for (i = 0; i < num_langs; i++) {
1697 if (i > 0)
1698 strbuf_addstr(buf, ", ");
1699
1700 strbuf_addstr(buf, language_tags[i]);
1701
1702 if (i > 0)
1703 strbuf_addf(buf, q_format, max_q - i);
1704
1705 if (buf->len > MAX_ACCEPT_LANGUAGE_HEADER_SIZE) {
1706 strbuf_remove(buf, last_buf_len, buf->len - last_buf_len);
1707 break;
1708 }
1709
1710 last_buf_len = buf->len;
1711 }
1712 }
1713
1714 /* free language tags -- last one is a static '*' */
1715 for (i = 0; i < num_langs - 1; i++)
1716 free(language_tags[i]);
1717 free(language_tags);
1718}
1719
1720/*
1721 * Get an Accept-Language header which indicates user's preferred languages.
1722 *
1723 * Examples:
1724 * LANGUAGE= -> ""
1725 * LANGUAGE=ko:en -> "Accept-Language: ko, en; q=0.9, *; q=0.1"
1726 * LANGUAGE=ko_KR.UTF-8:sr@latin -> "Accept-Language: ko-KR, sr; q=0.9, *; q=0.1"
1727 * LANGUAGE=ko LANG=en_US.UTF-8 -> "Accept-Language: ko, *; q=0.1"
1728 * LANGUAGE= LANG=en_US.UTF-8 -> "Accept-Language: en-US, *; q=0.1"
1729 * LANGUAGE= LANG=C -> ""
1730 */
1731static const char *get_accept_language(void)
1732{
1733 if (!cached_accept_language) {
1734 struct strbuf buf = STRBUF_INIT;
1735 write_accept_language(&buf);
1736 if (buf.len > 0)
1737 cached_accept_language = strbuf_detach(&buf, NULL);
1738 }
1739
1740 return cached_accept_language;
1741}
1742
David Turner835c4d32015-11-02 16:39:58 -05001743static void http_opt_request_remainder(CURL *curl, off_t pos)
1744{
1745 char buf[128];
1746 xsnprintf(buf, sizeof(buf), "%"PRIuMAX"-", (uintmax_t)pos);
1747 curl_easy_setopt(curl, CURLOPT_RANGE, buf);
1748}
1749
Mike Hommeye929cd22009-06-06 16:43:53 +08001750/* http_request() targets */
1751#define HTTP_REQUEST_STRBUF 0
1752#define HTTP_REQUEST_FILE 1
1753
Jeff King1bbcc222013-09-28 04:31:23 -04001754static int http_request(const char *url,
1755 void *result, int target,
1756 const struct http_get_options *options)
Mike Hommeye929cd22009-06-06 16:43:53 +08001757{
1758 struct active_request_slot *slot;
1759 struct slot_results results;
Johannes Schindelin8cb01e22016-04-27 14:20:37 +02001760 struct curl_slist *headers = http_copy_default_headers();
Mike Hommeye929cd22009-06-06 16:43:53 +08001761 struct strbuf buf = STRBUF_INIT;
Yi EungJunf18604b2015-01-28 21:04:37 +09001762 const char *accept_language;
Mike Hommeye929cd22009-06-06 16:43:53 +08001763 int ret;
1764
1765 slot = get_active_slot();
Mike Hommeye929cd22009-06-06 16:43:53 +08001766 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
1767
1768 if (result == NULL) {
1769 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 1);
1770 } else {
1771 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
Ævar Arnfjörð Bjarmason8dda4cb2021-07-30 19:59:46 +02001772 curl_easy_setopt(slot->curl, CURLOPT_WRITEDATA, result);
Mike Hommeye929cd22009-06-06 16:43:53 +08001773
1774 if (target == HTTP_REQUEST_FILE) {
Jeff Kingf8117f52015-11-02 17:10:27 -05001775 off_t posn = ftello(result);
Mike Hommeye929cd22009-06-06 16:43:53 +08001776 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION,
1777 fwrite);
David Turner835c4d32015-11-02 16:39:58 -05001778 if (posn > 0)
1779 http_opt_request_remainder(slot->curl, posn);
Mike Hommeye929cd22009-06-06 16:43:53 +08001780 } else
1781 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION,
1782 fwrite_buffer);
1783 }
1784
Yi EungJunf18604b2015-01-28 21:04:37 +09001785 accept_language = get_accept_language();
1786
1787 if (accept_language)
1788 headers = curl_slist_append(headers, accept_language);
1789
Mike Hommeye929cd22009-06-06 16:43:53 +08001790 strbuf_addstr(&buf, "Pragma:");
Jeff King1bbcc222013-09-28 04:31:23 -04001791 if (options && options->no_cache)
Mike Hommeye929cd22009-06-06 16:43:53 +08001792 strbuf_addstr(&buf, " no-cache");
Jeff King50d34132016-12-06 13:24:41 -05001793 if (options && options->initial_request &&
1794 http_follow_config == HTTP_FOLLOW_INITIAL)
1795 curl_easy_setopt(slot->curl, CURLOPT_FOLLOWLOCATION, 1);
Mike Hommeye929cd22009-06-06 16:43:53 +08001796
1797 headers = curl_slist_append(headers, buf.buf);
1798
Brandon Williams8ff14ed2018-03-15 10:31:38 -07001799 /* Add additional headers here */
1800 if (options && options->extra_headers) {
1801 const struct string_list_item *item;
1802 for_each_string_list_item(item, options->extra_headers) {
1803 headers = curl_slist_append(headers, item->string);
1804 }
1805 }
1806
Mike Hommeye929cd22009-06-06 16:43:53 +08001807 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
1808 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
Brandon Williams1a53e692018-05-22 11:42:03 -07001809 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "");
Masaya Suzukie6cf87b2019-01-10 11:33:47 -08001810 curl_easy_setopt(slot->curl, CURLOPT_FAILONERROR, 0);
Mike Hommeye929cd22009-06-06 16:43:53 +08001811
Jeff Kingbeed3362014-02-18 05:34:20 -05001812 ret = run_one_slot(slot, &results);
Mike Hommeye929cd22009-06-06 16:43:53 +08001813
Jeff Kingbf197fd2014-05-22 05:29:47 -04001814 if (options && options->content_type) {
1815 struct strbuf raw = STRBUF_INIT;
1816 curlinfo_strbuf(slot->curl, CURLINFO_CONTENT_TYPE, &raw);
Jeff Kinge3131622014-05-22 05:30:05 -04001817 extract_content_type(&raw, options->content_type,
1818 options->charset);
Jeff Kingbf197fd2014-05-22 05:29:47 -04001819 strbuf_release(&raw);
1820 }
Shawn Pearce4656bf42013-01-31 13:02:07 -08001821
Jeff King78868962013-09-28 04:32:02 -04001822 if (options && options->effective_url)
1823 curlinfo_strbuf(slot->curl, CURLINFO_EFFECTIVE_URL,
1824 options->effective_url);
Mike Hommeye929cd22009-06-06 16:43:53 +08001825
Mike Hommeye929cd22009-06-06 16:43:53 +08001826 curl_slist_free_all(headers);
1827 strbuf_release(&buf);
1828
1829 return ret;
1830}
1831
Jeff Kingc93c92f2013-09-28 04:34:05 -04001832/*
1833 * Update the "base" url to a more appropriate value, as deduced by
1834 * redirects seen when requesting a URL starting with "url".
1835 *
1836 * The "asked" parameter is a URL that we asked curl to access, and must begin
1837 * with "base".
1838 *
1839 * The "got" parameter is the URL that curl reported to us as where we ended
1840 * up.
1841 *
1842 * Returns 1 if we updated the base url, 0 otherwise.
1843 *
1844 * Our basic strategy is to compare "base" and "asked" to find the bits
1845 * specific to our request. We then strip those bits off of "got" to yield the
1846 * new base. So for example, if our base is "http://example.com/foo.git",
1847 * and we ask for "http://example.com/foo.git/info/refs", we might end up
1848 * with "https://other.example.com/foo.git/info/refs". We would want the
1849 * new URL to become "https://other.example.com/foo.git".
1850 *
1851 * Note that this assumes a sane redirect scheme. It's entirely possible
1852 * in the example above to end up at a URL that does not even end in
Jeff King6628eb42016-12-06 13:24:35 -05001853 * "info/refs". In such a case we die. There's not much we can do, such a
1854 * scheme is unlikely to represent a real git repository, and failing to
1855 * rewrite the base opens options for malicious redirects to do funny things.
Jeff Kingc93c92f2013-09-28 04:34:05 -04001856 */
1857static int update_url_from_redirect(struct strbuf *base,
1858 const char *asked,
1859 const struct strbuf *got)
Jeff King8d677ed2011-07-18 03:50:14 -04001860{
Jeff Kingc93c92f2013-09-28 04:34:05 -04001861 const char *tail;
Jeff King986d7f42016-12-06 13:24:29 -05001862 size_t new_len;
Jeff Kingc93c92f2013-09-28 04:34:05 -04001863
1864 if (!strcmp(asked, got->buf))
1865 return 0;
1866
Jeff Kingde8118e2014-06-18 15:57:17 -04001867 if (!skip_prefix(asked, base->buf, &tail))
Johannes Schindelin033abf92018-05-02 11:38:39 +02001868 BUG("update_url_from_redirect: %s is not a superset of %s",
Jeff Kingc93c92f2013-09-28 04:34:05 -04001869 asked, base->buf);
1870
Jeff King986d7f42016-12-06 13:24:29 -05001871 new_len = got->len;
1872 if (!strip_suffix_mem(got->buf, &new_len, tail))
Jeff King6628eb42016-12-06 13:24:35 -05001873 die(_("unable to update url base from redirection:\n"
1874 " asked for: %s\n"
1875 " redirect: %s"),
1876 asked, got->buf);
Jeff Kingc93c92f2013-09-28 04:34:05 -04001877
1878 strbuf_reset(base);
Jeff King986d7f42016-12-06 13:24:29 -05001879 strbuf_add(base, got->buf, new_len);
Jeff King6628eb42016-12-06 13:24:35 -05001880
Jeff Kingc93c92f2013-09-28 04:34:05 -04001881 return 1;
1882}
1883
Mike Hommeye929cd22009-06-06 16:43:53 +08001884static int http_request_reauth(const char *url,
Jeff King8d677ed2011-07-18 03:50:14 -04001885 void *result, int target,
Jeff King1bbcc222013-09-28 04:31:23 -04001886 struct http_get_options *options)
Jeff King8d677ed2011-07-18 03:50:14 -04001887{
Jeff King1bbcc222013-09-28 04:31:23 -04001888 int ret = http_request(url, result, target, options);
Jeff Kingc93c92f2013-09-28 04:34:05 -04001889
Jonathan Tan8e273912017-02-27 18:53:11 -08001890 if (ret != HTTP_OK && ret != HTTP_REAUTH)
1891 return ret;
1892
Jeff Kingc93c92f2013-09-28 04:34:05 -04001893 if (options && options->effective_url && options->base_url) {
1894 if (update_url_from_redirect(options->base_url,
1895 url, options->effective_url)) {
1896 credential_from_url(&http_auth, options->base_url->buf);
1897 url = options->effective_url->buf;
1898 }
1899 }
1900
Jeff King8d677ed2011-07-18 03:50:14 -04001901 if (ret != HTTP_REAUTH)
1902 return ret;
Jeff King6d052d72013-04-05 18:14:06 -04001903
1904 /*
Masaya Suzukie6cf87b2019-01-10 11:33:47 -08001905 * The previous request may have put cruft into our output stream; we
1906 * should clear it out before making our next request.
Jeff King6d052d72013-04-05 18:14:06 -04001907 */
Masaya Suzukie6cf87b2019-01-10 11:33:47 -08001908 switch (target) {
1909 case HTTP_REQUEST_STRBUF:
1910 strbuf_reset(result);
1911 break;
1912 case HTTP_REQUEST_FILE:
1913 if (fflush(result)) {
1914 error_errno("unable to flush a file");
1915 return HTTP_START_FAILED;
Jeff King6d052d72013-04-05 18:14:06 -04001916 }
Masaya Suzukie6cf87b2019-01-10 11:33:47 -08001917 rewind(result);
1918 if (ftruncate(fileno(result), 0) < 0) {
1919 error_errno("unable to truncate a file");
1920 return HTTP_START_FAILED;
1921 }
1922 break;
1923 default:
1924 BUG("Unknown http_request target");
Jeff King6d052d72013-04-05 18:14:06 -04001925 }
Jeff King2501aff2013-09-28 04:31:45 -04001926
1927 credential_fill(&http_auth);
1928
Jeff King1bbcc222013-09-28 04:31:23 -04001929 return http_request(url, result, target, options);
Jeff King8d677ed2011-07-18 03:50:14 -04001930}
1931
Shawn Pearce4656bf42013-01-31 13:02:07 -08001932int http_get_strbuf(const char *url,
Jeff King1bbcc222013-09-28 04:31:23 -04001933 struct strbuf *result,
1934 struct http_get_options *options)
Mike Hommeye929cd22009-06-06 16:43:53 +08001935{
Jeff King1bbcc222013-09-28 04:31:23 -04001936 return http_request_reauth(url, result, HTTP_REQUEST_STRBUF, options);
Mike Hommeye929cd22009-06-06 16:43:53 +08001937}
1938
Junio C Hamano83e41e22010-01-11 22:26:08 -08001939/*
Jim Meyeringa7793a72012-03-28 10:41:54 +02001940 * Downloads a URL and stores the result in the given file.
Junio C Hamano83e41e22010-01-11 22:26:08 -08001941 *
1942 * If a previous interrupted download is detected (i.e. a previous temporary
1943 * file is still around) the download is resumed.
1944 */
Jeff King1bbcc222013-09-28 04:31:23 -04001945static int http_get_file(const char *url, const char *filename,
1946 struct http_get_options *options)
Mike Hommeye929cd22009-06-06 16:43:53 +08001947{
1948 int ret;
1949 struct strbuf tmpfile = STRBUF_INIT;
1950 FILE *result;
1951
1952 strbuf_addf(&tmpfile, "%s.temp", filename);
1953 result = fopen(tmpfile.buf, "a");
Jeff King3d1fb762013-09-28 04:31:00 -04001954 if (!result) {
Mike Hommeye929cd22009-06-06 16:43:53 +08001955 error("Unable to open local file %s", tmpfile.buf);
1956 ret = HTTP_ERROR;
1957 goto cleanup;
1958 }
1959
Jeff King1bbcc222013-09-28 04:31:23 -04001960 ret = http_request_reauth(url, result, HTTP_REQUEST_FILE, options);
Mike Hommeye929cd22009-06-06 16:43:53 +08001961 fclose(result);
1962
Junio C Hamanocb5add52015-08-07 14:40:24 -07001963 if (ret == HTTP_OK && finalize_object_file(tmpfile.buf, filename))
Mike Hommeye929cd22009-06-06 16:43:53 +08001964 ret = HTTP_ERROR;
1965cleanup:
1966 strbuf_release(&tmpfile);
1967 return ret;
1968}
1969
Daniel Barkalowc13b2632008-04-26 15:53:09 -04001970int http_fetch_ref(const char *base, struct ref *ref)
Mike Hommeyd7e92802007-12-11 00:08:25 +01001971{
Jeff King1bbcc222013-09-28 04:31:23 -04001972 struct http_get_options options = {0};
Mike Hommeyd7e92802007-12-11 00:08:25 +01001973 char *url;
1974 struct strbuf buffer = STRBUF_INIT;
Mike Hommey0d5896e2009-06-06 16:43:55 +08001975 int ret = -1;
Mike Hommeyd7e92802007-12-11 00:08:25 +01001976
Jeff King1bbcc222013-09-28 04:31:23 -04001977 options.no_cache = 1;
1978
Daniel Barkalowc13b2632008-04-26 15:53:09 -04001979 url = quote_ref_url(base, ref->name);
Jeff King1bbcc222013-09-28 04:31:23 -04001980 if (http_get_strbuf(url, &buffer, &options) == HTTP_OK) {
Mike Hommey0d5896e2009-06-06 16:43:55 +08001981 strbuf_rtrim(&buffer);
brian m. carlsonae041a02019-02-19 00:05:13 +00001982 if (buffer.len == the_hash_algo->hexsz)
brian m. carlsonf4e54d02015-11-10 02:22:20 +00001983 ret = get_oid_hex(buffer.buf, &ref->old_oid);
Christian Couder59556542013-11-30 21:55:40 +01001984 else if (starts_with(buffer.buf, "ref: ")) {
Mike Hommey0d5896e2009-06-06 16:43:55 +08001985 ref->symref = xstrdup(buffer.buf + 5);
1986 ret = 0;
Mike Hommeyd7e92802007-12-11 00:08:25 +01001987 }
Mike Hommeyd7e92802007-12-11 00:08:25 +01001988 }
1989
1990 strbuf_release(&buffer);
1991 free(url);
1992 return ret;
1993}
Tay Ray Chuanb8caac22009-06-06 16:43:59 +08001994
1995/* Helpers for fetching packs */
brian m. carlson05dfc7c2019-02-19 00:05:15 +00001996static char *fetch_pack_index(unsigned char *hash, const char *base_url)
Tay Ray Chuanb8caac22009-06-06 16:43:59 +08001997{
Shawn O. Pearce750ef422010-04-19 07:23:10 -07001998 char *url, *tmp;
Tay Ray Chuanb8caac22009-06-06 16:43:59 +08001999 struct strbuf buf = STRBUF_INIT;
Tay Ray Chuanb8caac22009-06-06 16:43:59 +08002000
Tay Ray Chuanb8caac22009-06-06 16:43:59 +08002001 if (http_is_verbose)
brian m. carlson05dfc7c2019-02-19 00:05:15 +00002002 fprintf(stderr, "Getting index for pack %s\n", hash_to_hex(hash));
Tay Ray Chuanb8caac22009-06-06 16:43:59 +08002003
2004 end_url_with_slash(&buf, base_url);
brian m. carlson05dfc7c2019-02-19 00:05:15 +00002005 strbuf_addf(&buf, "objects/pack/pack-%s.idx", hash_to_hex(hash));
Tay Ray Chuanb8caac22009-06-06 16:43:59 +08002006 url = strbuf_detach(&buf, NULL);
2007
brian m. carlson05dfc7c2019-02-19 00:05:15 +00002008 strbuf_addf(&buf, "%s.temp", sha1_pack_index_name(hash));
Shawn O. Pearce750ef422010-04-19 07:23:10 -07002009 tmp = strbuf_detach(&buf, NULL);
Tay Ray Chuanb8caac22009-06-06 16:43:59 +08002010
Ramsay Jones70900ed2013-10-24 21:17:19 +01002011 if (http_get_file(url, tmp, NULL) != HTTP_OK) {
Pete Wyckoff82247e92012-04-29 20:28:45 -04002012 error("Unable to get pack index %s", url);
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +00002013 FREE_AND_NULL(tmp);
Shawn O. Pearce750ef422010-04-19 07:23:10 -07002014 }
2015
Tay Ray Chuanb8caac22009-06-06 16:43:59 +08002016 free(url);
Shawn O. Pearce750ef422010-04-19 07:23:10 -07002017 return tmp;
Tay Ray Chuanb8caac22009-06-06 16:43:59 +08002018}
2019
2020static int fetch_and_setup_pack_index(struct packed_git **packs_head,
2021 unsigned char *sha1, const char *base_url)
2022{
2023 struct packed_git *new_pack;
Shawn O. Pearce750ef422010-04-19 07:23:10 -07002024 char *tmp_idx = NULL;
2025 int ret;
Tay Ray Chuanb8caac22009-06-06 16:43:59 +08002026
Shawn O. Pearce750ef422010-04-19 07:23:10 -07002027 if (has_pack_index(sha1)) {
Jeff King8b9c2dd2015-01-27 15:02:27 -05002028 new_pack = parse_pack_index(sha1, sha1_pack_index_name(sha1));
Shawn O. Pearce750ef422010-04-19 07:23:10 -07002029 if (!new_pack)
2030 return -1; /* parse_pack_index() already issued error message */
2031 goto add_pack;
2032 }
2033
2034 tmp_idx = fetch_pack_index(sha1, base_url);
2035 if (!tmp_idx)
Tay Ray Chuanb8caac22009-06-06 16:43:59 +08002036 return -1;
2037
Shawn O. Pearce750ef422010-04-19 07:23:10 -07002038 new_pack = parse_pack_index(sha1, tmp_idx);
2039 if (!new_pack) {
2040 unlink(tmp_idx);
2041 free(tmp_idx);
2042
Tay Ray Chuanb8caac22009-06-06 16:43:59 +08002043 return -1; /* parse_pack_index() already issued error message */
Shawn O. Pearce750ef422010-04-19 07:23:10 -07002044 }
2045
2046 ret = verify_pack_index(new_pack);
2047 if (!ret) {
2048 close_pack_index(new_pack);
Junio C Hamanocb5add52015-08-07 14:40:24 -07002049 ret = finalize_object_file(tmp_idx, sha1_pack_index_name(sha1));
Shawn O. Pearce750ef422010-04-19 07:23:10 -07002050 }
2051 free(tmp_idx);
2052 if (ret)
2053 return -1;
2054
2055add_pack:
Tay Ray Chuanb8caac22009-06-06 16:43:59 +08002056 new_pack->next = *packs_head;
2057 *packs_head = new_pack;
2058 return 0;
2059}
2060
2061int http_get_info_packs(const char *base_url, struct packed_git **packs_head)
2062{
Jeff King1bbcc222013-09-28 04:31:23 -04002063 struct http_get_options options = {0};
Jeff Kingddc56d42019-04-05 14:12:55 -04002064 int ret = 0;
2065 char *url;
2066 const char *data;
Tay Ray Chuanb8caac22009-06-06 16:43:59 +08002067 struct strbuf buf = STRBUF_INIT;
Jeff Kingddc56d42019-04-05 14:12:55 -04002068 struct object_id oid;
Tay Ray Chuanb8caac22009-06-06 16:43:59 +08002069
2070 end_url_with_slash(&buf, base_url);
2071 strbuf_addstr(&buf, "objects/info/packs");
2072 url = strbuf_detach(&buf, NULL);
2073
Jeff King1bbcc222013-09-28 04:31:23 -04002074 options.no_cache = 1;
2075 ret = http_get_strbuf(url, &buf, &options);
Tay Ray Chuanb8caac22009-06-06 16:43:59 +08002076 if (ret != HTTP_OK)
2077 goto cleanup;
2078
2079 data = buf.buf;
Jeff Kingddc56d42019-04-05 14:12:55 -04002080 while (*data) {
2081 if (skip_prefix(data, "P pack-", &data) &&
2082 !parse_oid_hex(data, &oid, &data) &&
2083 skip_prefix(data, ".pack", &data) &&
2084 (*data == '\n' || *data == '\0')) {
2085 fetch_and_setup_pack_index(packs_head, oid.hash, base_url);
2086 } else {
2087 data = strchrnul(data, '\n');
Tay Ray Chuanb8caac22009-06-06 16:43:59 +08002088 }
Jeff Kingddc56d42019-04-05 14:12:55 -04002089 if (*data)
2090 data++; /* skip past newline */
Tay Ray Chuanb8caac22009-06-06 16:43:59 +08002091 }
2092
2093cleanup:
2094 free(url);
2095 return ret;
2096}
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +08002097
2098void release_http_pack_request(struct http_pack_request *preq)
2099{
2100 if (preq->packfile != NULL) {
2101 fclose(preq->packfile);
2102 preq->packfile = NULL;
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +08002103 }
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +08002104 preq->slot = NULL;
Jeff King390c6cb2018-05-18 18:56:37 -07002105 strbuf_release(&preq->tmpfile);
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +08002106 free(preq->url);
Stefan Beller826aed52015-03-20 17:28:06 -07002107 free(preq);
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +08002108}
2109
Jonathan Tan726b25a2021-02-22 11:20:06 -08002110static const char *default_index_pack_args[] =
2111 {"index-pack", "--stdin", NULL};
2112
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +08002113int finish_http_pack_request(struct http_pack_request *preq)
2114{
René Scharfed3180272014-08-19 21:09:35 +02002115 struct child_process ip = CHILD_PROCESS_INIT;
Jonathan Tan9cb3cab2020-06-10 13:57:15 -07002116 int tmpfile_fd;
2117 int ret = 0;
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +08002118
Shawn O. Pearce30652742010-04-17 13:07:37 -07002119 fclose(preq->packfile);
2120 preq->packfile = NULL;
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +08002121
Jonathan Tan9cb3cab2020-06-10 13:57:15 -07002122 tmpfile_fd = xopen(preq->tmpfile.buf, O_RDONLY);
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +08002123
Shawn O. Pearcefe72d422010-04-19 07:23:09 -07002124 ip.git_cmd = 1;
Jonathan Tan9cb3cab2020-06-10 13:57:15 -07002125 ip.in = tmpfile_fd;
Jonathan Tan726b25a2021-02-22 11:20:06 -08002126 ip.argv = preq->index_pack_args ? preq->index_pack_args
2127 : default_index_pack_args;
2128
2129 if (preq->preserve_index_pack_stdout)
Jonathan Tan8d5d2a32020-06-10 13:57:18 -07002130 ip.out = 0;
Jonathan Tan726b25a2021-02-22 11:20:06 -08002131 else
Jonathan Tan8d5d2a32020-06-10 13:57:18 -07002132 ip.no_stdout = 1;
Shawn O. Pearcefe72d422010-04-19 07:23:09 -07002133
2134 if (run_command(&ip)) {
Jonathan Tan9cb3cab2020-06-10 13:57:15 -07002135 ret = -1;
2136 goto cleanup;
Shawn O. Pearcefe72d422010-04-19 07:23:09 -07002137 }
2138
Jonathan Tan9cb3cab2020-06-10 13:57:15 -07002139cleanup:
2140 close(tmpfile_fd);
2141 unlink(preq->tmpfile.buf);
2142 return ret;
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +08002143}
2144
Jonathan Taneb053492020-06-10 13:57:16 -07002145void http_install_packfile(struct packed_git *p,
2146 struct packed_git **list_to_remove_from)
2147{
2148 struct packed_git **lst = list_to_remove_from;
2149
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +08002150 while (*lst != p)
2151 lst = &((*lst)->next);
2152 *lst = (*lst)->next;
2153
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +08002154 install_packed_git(the_repository, p);
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +08002155}
2156
2157struct http_pack_request *new_http_pack_request(
Jonathan Tan8d5d2a32020-06-10 13:57:18 -07002158 const unsigned char *packed_git_hash, const char *base_url) {
2159
2160 struct strbuf buf = STRBUF_INIT;
2161
2162 end_url_with_slash(&buf, base_url);
2163 strbuf_addf(&buf, "objects/pack/pack-%s.pack",
2164 hash_to_hex(packed_git_hash));
2165 return new_direct_http_pack_request(packed_git_hash,
2166 strbuf_detach(&buf, NULL));
2167}
2168
2169struct http_pack_request *new_direct_http_pack_request(
2170 const unsigned char *packed_git_hash, char *url)
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +08002171{
Jeff Kingf8117f52015-11-02 17:10:27 -05002172 off_t prev_posn = 0;
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +08002173 struct http_pack_request *preq;
2174
René Scharfeca56dad2021-03-13 17:17:22 +01002175 CALLOC_ARRAY(preq, 1);
Jeff King390c6cb2018-05-18 18:56:37 -07002176 strbuf_init(&preq->tmpfile, 0);
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +08002177
Jonathan Tan8d5d2a32020-06-10 13:57:18 -07002178 preq->url = url;
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +08002179
Jonathan Taneb053492020-06-10 13:57:16 -07002180 strbuf_addf(&preq->tmpfile, "%s.temp", sha1_pack_name(packed_git_hash));
Jeff King390c6cb2018-05-18 18:56:37 -07002181 preq->packfile = fopen(preq->tmpfile.buf, "a");
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +08002182 if (!preq->packfile) {
2183 error("Unable to open local file %s for pack",
Jeff King390c6cb2018-05-18 18:56:37 -07002184 preq->tmpfile.buf);
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +08002185 goto abort;
2186 }
2187
2188 preq->slot = get_active_slot();
Ævar Arnfjörð Bjarmason8dda4cb2021-07-30 19:59:46 +02002189 curl_easy_setopt(preq->slot->curl, CURLOPT_WRITEDATA, preq->packfile);
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +08002190 curl_easy_setopt(preq->slot->curl, CURLOPT_WRITEFUNCTION, fwrite);
Tay Ray Chuanbb991902009-08-10 23:59:55 +08002191 curl_easy_setopt(preq->slot->curl, CURLOPT_URL, preq->url);
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +08002192 curl_easy_setopt(preq->slot->curl, CURLOPT_HTTPHEADER,
2193 no_pragma_header);
2194
2195 /*
2196 * If there is data present from a previous transfer attempt,
2197 * resume where it left off
2198 */
Jeff Kingf8117f52015-11-02 17:10:27 -05002199 prev_posn = ftello(preq->packfile);
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +08002200 if (prev_posn>0) {
2201 if (http_is_verbose)
2202 fprintf(stderr,
Ramsay Jones838ecf02015-11-12 00:07:42 +00002203 "Resuming fetch of pack %s at byte %"PRIuMAX"\n",
Jonathan Taneb053492020-06-10 13:57:16 -07002204 hash_to_hex(packed_git_hash),
brian m. carlson538b1522019-02-19 00:05:03 +00002205 (uintmax_t)prev_posn);
David Turner835c4d32015-11-02 16:39:58 -05002206 http_opt_request_remainder(preq->slot->curl, prev_posn);
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +08002207 }
2208
2209 return preq;
2210
2211abort:
Jeff King390c6cb2018-05-18 18:56:37 -07002212 strbuf_release(&preq->tmpfile);
Tay Ray Chuanbb991902009-08-10 23:59:55 +08002213 free(preq->url);
Tay Ray Chuan5ae9ebf2009-08-10 23:55:48 +08002214 free(preq);
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +08002215 return NULL;
2216}
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002217
2218/* Helpers for fetching objects (loose) */
Dan McGeea04ff3e2011-05-03 23:47:27 +08002219static size_t fwrite_sha1_file(char *ptr, size_t eltsize, size_t nmemb,
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002220 void *data)
2221{
2222 unsigned char expn[4096];
2223 size_t size = eltsize * nmemb;
2224 int posn = 0;
Eric Wong17966c02016-07-11 20:51:30 +00002225 struct http_object_request *freq = data;
2226 struct active_request_slot *slot = freq->slot;
2227
2228 if (slot) {
2229 CURLcode c = curl_easy_getinfo(slot->curl, CURLINFO_HTTP_CODE,
2230 &slot->http_code);
2231 if (c != CURLE_OK)
Johannes Schindelin033abf92018-05-02 11:38:39 +02002232 BUG("curl_easy_getinfo for HTTP code failed: %s",
Eric Wong17966c02016-07-11 20:51:30 +00002233 curl_easy_strerror(c));
Jeff King3680f162016-12-06 13:25:39 -05002234 if (slot->http_code >= 300)
Mike Hommey5c3d5a32019-05-08 08:03:54 +09002235 return nmemb;
Eric Wong17966c02016-07-11 20:51:30 +00002236 }
2237
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002238 do {
2239 ssize_t retval = xwrite(freq->localfile,
2240 (char *) ptr + posn, size - posn);
2241 if (retval < 0)
Mike Hommey5c3d5a32019-05-08 08:03:54 +09002242 return posn / eltsize;
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002243 posn += retval;
2244 } while (posn < size);
2245
2246 freq->stream.avail_in = size;
Dan McGeea04ff3e2011-05-03 23:47:27 +08002247 freq->stream.next_in = (void *)ptr;
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002248 do {
2249 freq->stream.next_out = expn;
2250 freq->stream.avail_out = sizeof(expn);
2251 freq->zret = git_inflate(&freq->stream, Z_SYNC_FLUSH);
brian m. carlsoneed0e602019-02-19 00:05:14 +00002252 the_hash_algo->update_fn(&freq->c, expn,
2253 sizeof(expn) - freq->stream.avail_out);
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002254 } while (freq->stream.avail_in && freq->zret == Z_OK);
Mike Hommey5c3d5a32019-05-08 08:03:54 +09002255 return nmemb;
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002256}
2257
2258struct http_object_request *new_http_object_request(const char *base_url,
Jeff Kingf0be0db2019-01-07 03:34:40 -05002259 const struct object_id *oid)
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002260{
Jeff Kingf0be0db2019-01-07 03:34:40 -05002261 char *hex = oid_to_hex(oid);
Christian Couderea657732018-01-17 18:54:54 +01002262 struct strbuf filename = STRBUF_INIT;
Jeff King390c6cb2018-05-18 18:56:37 -07002263 struct strbuf prevfile = STRBUF_INIT;
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002264 int prevlocal;
Dan McGeea04ff3e2011-05-03 23:47:27 +08002265 char prev_buf[PREV_BUF_SIZE];
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002266 ssize_t prev_read = 0;
Jeff Kingf8117f52015-11-02 17:10:27 -05002267 off_t prev_posn = 0;
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002268 struct http_object_request *freq;
2269
René Scharfeca56dad2021-03-13 17:17:22 +01002270 CALLOC_ARRAY(freq, 1);
Jeff King390c6cb2018-05-18 18:56:37 -07002271 strbuf_init(&freq->tmpfile, 0);
Jeff Kingf0be0db2019-01-07 03:34:40 -05002272 oidcpy(&freq->oid, oid);
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002273 freq->localfile = -1;
2274
Jeff King514c5fd2019-01-07 03:35:42 -05002275 loose_object_path(the_repository, &filename, oid);
Jeff King390c6cb2018-05-18 18:56:37 -07002276 strbuf_addf(&freq->tmpfile, "%s.temp", filename.buf);
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002277
Jeff King390c6cb2018-05-18 18:56:37 -07002278 strbuf_addf(&prevfile, "%s.prev", filename.buf);
2279 unlink_or_warn(prevfile.buf);
2280 rename(freq->tmpfile.buf, prevfile.buf);
2281 unlink_or_warn(freq->tmpfile.buf);
Christian Couderea657732018-01-17 18:54:54 +01002282 strbuf_release(&filename);
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002283
2284 if (freq->localfile != -1)
2285 error("fd leakage in start: %d", freq->localfile);
Jeff King390c6cb2018-05-18 18:56:37 -07002286 freq->localfile = open(freq->tmpfile.buf,
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002287 O_WRONLY | O_CREAT | O_EXCL, 0666);
2288 /*
2289 * This could have failed due to the "lazy directory creation";
2290 * try to mkdir the last path component.
2291 */
2292 if (freq->localfile < 0 && errno == ENOENT) {
Jeff King390c6cb2018-05-18 18:56:37 -07002293 char *dir = strrchr(freq->tmpfile.buf, '/');
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002294 if (dir) {
2295 *dir = 0;
Jeff King390c6cb2018-05-18 18:56:37 -07002296 mkdir(freq->tmpfile.buf, 0777);
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002297 *dir = '/';
2298 }
Jeff King390c6cb2018-05-18 18:56:37 -07002299 freq->localfile = open(freq->tmpfile.buf,
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002300 O_WRONLY | O_CREAT | O_EXCL, 0666);
2301 }
2302
2303 if (freq->localfile < 0) {
Jeff King390c6cb2018-05-18 18:56:37 -07002304 error_errno("Couldn't create temporary file %s",
2305 freq->tmpfile.buf);
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002306 goto abort;
2307 }
2308
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002309 git_inflate_init(&freq->stream);
2310
brian m. carlsoneed0e602019-02-19 00:05:14 +00002311 the_hash_algo->init_fn(&freq->c);
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002312
Tay Ray Chuanbb991902009-08-10 23:59:55 +08002313 freq->url = get_remote_object_url(base_url, hex, 0);
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002314
2315 /*
2316 * If a previous temp file is present, process what was already
2317 * fetched.
2318 */
Jeff King390c6cb2018-05-18 18:56:37 -07002319 prevlocal = open(prevfile.buf, O_RDONLY);
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002320 if (prevlocal != -1) {
2321 do {
2322 prev_read = xread(prevlocal, prev_buf, PREV_BUF_SIZE);
2323 if (prev_read>0) {
2324 if (fwrite_sha1_file(prev_buf,
2325 1,
2326 prev_read,
2327 freq) == prev_read) {
2328 prev_posn += prev_read;
2329 } else {
2330 prev_read = -1;
2331 }
2332 }
2333 } while (prev_read > 0);
2334 close(prevlocal);
2335 }
Jeff King390c6cb2018-05-18 18:56:37 -07002336 unlink_or_warn(prevfile.buf);
2337 strbuf_release(&prevfile);
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002338
2339 /*
2340 * Reset inflate/SHA1 if there was an error reading the previous temp
2341 * file; also rewind to the beginning of the local file.
2342 */
2343 if (prev_read == -1) {
2344 memset(&freq->stream, 0, sizeof(freq->stream));
2345 git_inflate_init(&freq->stream);
brian m. carlsoneed0e602019-02-19 00:05:14 +00002346 the_hash_algo->init_fn(&freq->c);
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002347 if (prev_posn>0) {
2348 prev_posn = 0;
2349 lseek(freq->localfile, 0, SEEK_SET);
Jeff Lasslett0c4f21e2009-08-11 00:05:06 +08002350 if (ftruncate(freq->localfile, 0) < 0) {
Nguyễn Thái Ngọc Duyd2e255e2016-05-08 16:47:48 +07002351 error_errno("Couldn't truncate temporary file %s",
Jeff King390c6cb2018-05-18 18:56:37 -07002352 freq->tmpfile.buf);
Jeff Lasslett0c4f21e2009-08-11 00:05:06 +08002353 goto abort;
2354 }
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002355 }
2356 }
2357
2358 freq->slot = get_active_slot();
2359
Ævar Arnfjörð Bjarmason8dda4cb2021-07-30 19:59:46 +02002360 curl_easy_setopt(freq->slot->curl, CURLOPT_WRITEDATA, freq);
Eric Wong17966c02016-07-11 20:51:30 +00002361 curl_easy_setopt(freq->slot->curl, CURLOPT_FAILONERROR, 0);
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002362 curl_easy_setopt(freq->slot->curl, CURLOPT_WRITEFUNCTION, fwrite_sha1_file);
2363 curl_easy_setopt(freq->slot->curl, CURLOPT_ERRORBUFFER, freq->errorstr);
Tay Ray Chuanbb991902009-08-10 23:59:55 +08002364 curl_easy_setopt(freq->slot->curl, CURLOPT_URL, freq->url);
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002365 curl_easy_setopt(freq->slot->curl, CURLOPT_HTTPHEADER, no_pragma_header);
2366
2367 /*
2368 * If we have successfully processed data from a previous fetch
2369 * attempt, only fetch the data we don't already have.
2370 */
2371 if (prev_posn>0) {
2372 if (http_is_verbose)
2373 fprintf(stderr,
Ramsay Jones838ecf02015-11-12 00:07:42 +00002374 "Resuming fetch of object %s at byte %"PRIuMAX"\n",
2375 hex, (uintmax_t)prev_posn);
David Turner835c4d32015-11-02 16:39:58 -05002376 http_opt_request_remainder(freq->slot->curl, prev_posn);
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002377 }
2378
2379 return freq;
2380
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002381abort:
Jeff King390c6cb2018-05-18 18:56:37 -07002382 strbuf_release(&prevfile);
Tay Ray Chuanbb991902009-08-10 23:59:55 +08002383 free(freq->url);
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002384 free(freq);
2385 return NULL;
2386}
2387
2388void process_http_object_request(struct http_object_request *freq)
2389{
2390 if (freq->slot == NULL)
2391 return;
2392 freq->curl_result = freq->slot->curl_result;
2393 freq->http_code = freq->slot->http_code;
2394 freq->slot = NULL;
2395}
2396
2397int finish_http_object_request(struct http_object_request *freq)
2398{
2399 struct stat st;
Christian Couderea657732018-01-17 18:54:54 +01002400 struct strbuf filename = STRBUF_INIT;
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002401
2402 close(freq->localfile);
2403 freq->localfile = -1;
2404
2405 process_http_object_request(freq);
2406
2407 if (freq->http_code == 416) {
Thiago Farinabd757c12010-01-03 11:20:30 -05002408 warning("requested range invalid; we may already have all the data.");
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002409 } else if (freq->curl_result != CURLE_OK) {
Jeff King390c6cb2018-05-18 18:56:37 -07002410 if (stat(freq->tmpfile.buf, &st) == 0)
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002411 if (st.st_size == 0)
Jeff King390c6cb2018-05-18 18:56:37 -07002412 unlink_or_warn(freq->tmpfile.buf);
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002413 return -1;
2414 }
2415
2416 git_inflate_end(&freq->stream);
brian m. carlson5951bf42021-04-26 01:02:53 +00002417 the_hash_algo->final_oid_fn(&freq->real_oid, &freq->c);
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002418 if (freq->zret != Z_STREAM_END) {
Jeff King390c6cb2018-05-18 18:56:37 -07002419 unlink_or_warn(freq->tmpfile.buf);
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002420 return -1;
2421 }
Jeff Kingf0be0db2019-01-07 03:34:40 -05002422 if (!oideq(&freq->oid, &freq->real_oid)) {
Jeff King390c6cb2018-05-18 18:56:37 -07002423 unlink_or_warn(freq->tmpfile.buf);
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002424 return -1;
2425 }
Jeff King514c5fd2019-01-07 03:35:42 -05002426 loose_object_path(the_repository, &filename, &freq->oid);
Jeff King390c6cb2018-05-18 18:56:37 -07002427 freq->rename = finalize_object_file(freq->tmpfile.buf, filename.buf);
Christian Couderea657732018-01-17 18:54:54 +01002428 strbuf_release(&filename);
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002429
2430 return freq->rename;
2431}
2432
2433void abort_http_object_request(struct http_object_request *freq)
2434{
Jeff King390c6cb2018-05-18 18:56:37 -07002435 unlink_or_warn(freq->tmpfile.buf);
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002436
2437 release_http_object_request(freq);
2438}
2439
2440void release_http_object_request(struct http_object_request *freq)
2441{
2442 if (freq->localfile != -1) {
2443 close(freq->localfile);
2444 freq->localfile = -1;
2445 }
Ævar Arnfjörð Bjarmasonce528de2018-08-17 13:02:50 +00002446 FREE_AND_NULL(freq->url);
Tay Ray Chuan4b9fa0e2009-08-26 20:20:53 +08002447 if (freq->slot != NULL) {
2448 freq->slot->callback_func = NULL;
2449 freq->slot->callback_data = NULL;
2450 release_active_slot(freq->slot);
2451 freq->slot = NULL;
2452 }
Jeff King390c6cb2018-05-18 18:56:37 -07002453 strbuf_release(&freq->tmpfile);
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002454}