blob: 5d0502f51fd85d49ab2fa03995b8afcc585b12cc [file] [log] [blame]
David Aguilar1c4b6602014-09-14 00:40:45 -07001#include "git-compat-util.h"
Ævar Arnfjörð Bjarmasone4ff3b62021-09-13 16:51:28 +02002#include "git-curl-compat.h"
Nick Hengeveld29508e12005-11-18 11:02:58 -08003#include "http.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07004#include "config.h"
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +08005#include "pack.h"
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -07006#include "sideband.h"
Shawn O. Pearcefe72d422010-04-19 07:23:09 -07007#include "run-command.h"
Gabriel Coronaf39f72d2010-11-14 02:51:15 +01008#include "url.h"
Kyle J. McKay6a569932013-08-05 13:20:36 -07009#include "urlmatch.h"
Jeff King148bb6a2011-12-10 05:31:21 -050010#include "credential.h"
Jeff King745c7c82012-06-02 15:03:08 -040011#include "version.h"
Jeff King047ec602013-02-20 15:02:45 -050012#include "pkt-line.h"
Jeff King93f7d912015-02-25 22:04:16 -050013#include "gettext.h"
Blake Burkhartf4113ca2015-09-22 18:06:04 -040014#include "transport.h"
Jonathan Tan4f39cd82017-08-18 15:20:16 -070015#include "packfile.h"
Brandon Williams19113a22017-10-16 10:55:29 -070016#include "protocol.h"
Jonathan Tan83411782018-01-18 16:28:01 -080017#include "string-list.h"
Stefan Bellera80d72d2018-03-23 18:20:59 +010018#include "object-store.h"
Nick Hengeveld29508e12005-11-18 11:02:58 -080019
Elia Pinto74c682d2016-05-23 13:44:02 +000020static struct trace_key trace_curl = TRACE_KEY_INIT(CURL);
Jonathan Tan8ba18e62018-01-18 16:28:02 -080021static int trace_curl_data = 1;
Jonathan Tan827e7d42020-06-05 14:21:36 -070022static int trace_curl_redact = 1;
Eric Wongc915f112016-02-03 04:09:14 +000023long int git_curl_ipresolve = CURL_IPRESOLVE_WHATEVER;
Junio C Hamano4251ccb2009-03-09 18:47:29 -070024int active_requests;
Tay Ray Chuane9176742009-06-06 16:43:41 +080025int http_is_verbose;
David Turner37ee6802017-04-11 14:13:57 -040026ssize_t http_post_buffer = 16 * LARGE_PACKET_MAX;
Nick Hengeveld29508e12005-11-18 11:02:58 -080027
Tay Ray Chuanad75ebe2009-11-27 23:42:26 +080028static int min_curl_sessions = 1;
29static int curl_session_count;
Mike Hommeycc3530e2007-12-09 18:04:57 +010030static int max_requests = -1;
31static CURLM *curlm;
Mike Hommeycc3530e2007-12-09 18:04:57 +010032static CURL *curl_default;
Tay Ray Chuan5424bc52009-06-06 16:44:02 +080033
34#define PREV_BUF_SIZE 4096
Tay Ray Chuan5424bc52009-06-06 16:44:02 +080035
Nick Hengeveld29508e12005-11-18 11:02:58 -080036char curl_errorstr[CURL_ERROR_SIZE];
37
Mike Hommeycc3530e2007-12-09 18:04:57 +010038static int curl_ssl_verify = -1;
Modestas Vainius4bc444e2013-04-07 22:10:39 +030039static int curl_ssl_try;
Force Charlied73019f2018-11-08 19:44:14 -080040static const char *curl_http_version = NULL;
Junio C Hamano4251ccb2009-03-09 18:47:29 -070041static const char *ssl_cert;
Lars Kellogg-Stedmanf6f2a9e2015-05-08 09:22:15 -040042static const char *ssl_cipherlist;
Elia Pinto01861cb2015-08-14 21:37:43 +020043static const char *ssl_version;
44static struct {
45 const char *name;
46 long ssl_version;
47} sslversions[] = {
48 { "sslv2", CURL_SSLVERSION_SSLv2 },
49 { "sslv3", CURL_SSLVERSION_SSLv3 },
50 { "tlsv1", CURL_SSLVERSION_TLSv1 },
Ævar Arnfjörð Bjarmasone4ff3b62021-09-13 16:51:28 +020051#ifdef GIT_CURL_HAVE_CURL_SSLVERSION_TLSv1_0
Elia Pinto01861cb2015-08-14 21:37:43 +020052 { "tlsv1.0", CURL_SSLVERSION_TLSv1_0 },
53 { "tlsv1.1", CURL_SSLVERSION_TLSv1_1 },
54 { "tlsv1.2", CURL_SSLVERSION_TLSv1_2 },
55#endif
Ævar Arnfjörð Bjarmasone4ff3b62021-09-13 16:51:28 +020056#ifdef GIT_CURL_HAVE_CURL_SSLVERSION_TLSv1_3
Loganaden Velvindrond81b6512018-03-29 14:14:18 +040057 { "tlsv1.3", CURL_SSLVERSION_TLSv1_3 },
58#endif
Elia Pinto01861cb2015-08-14 21:37:43 +020059};
Junio C Hamano4251ccb2009-03-09 18:47:29 -070060static const char *ssl_key;
Junio C Hamano4251ccb2009-03-09 18:47:29 -070061static const char *ssl_capath;
Ramsay Jonesb8fd6002018-03-14 21:56:06 +000062static const char *curl_no_proxy;
Ævar Arnfjörð Bjarmasone4ff3b62021-09-13 16:51:28 +020063#ifdef GIT_CURL_HAVE_CURLOPT_PINNEDPUBLICKEY
Christoph Eggeraeff8a62016-02-15 15:04:22 +010064static const char *ssl_pinnedkey;
65#endif
Junio C Hamano4251ccb2009-03-09 18:47:29 -070066static const char *ssl_cainfo;
Mike Hommeycc3530e2007-12-09 18:04:57 +010067static long curl_low_speed_limit = -1;
68static long curl_low_speed_time = -1;
Junio C Hamano4251ccb2009-03-09 18:47:29 -070069static int curl_ftp_no_epsv;
70static const char *curl_http_proxy;
Knut Frankeef976392016-01-26 13:02:47 +000071static const char *http_proxy_authmethod;
Jorge Lopez Silva88238e02020-03-04 18:40:05 +000072
73static const char *http_proxy_ssl_cert;
74static const char *http_proxy_ssl_key;
75static const char *http_proxy_ssl_ca_info;
76static struct credential proxy_cert_auth = CREDENTIAL_INIT;
77static int proxy_ssl_cert_password_required;
78
Knut Frankeef976392016-01-26 13:02:47 +000079static struct {
80 const char *name;
81 long curlauth_param;
82} proxy_authmethods[] = {
83 { "basic", CURLAUTH_BASIC },
84 { "digest", CURLAUTH_DIGEST },
85 { "negotiate", CURLAUTH_GSSNEGOTIATE },
86 { "ntlm", CURLAUTH_NTLM },
Knut Frankeef976392016-01-26 13:02:47 +000087 { "anyauth", CURLAUTH_ANY },
Knut Frankeef976392016-01-26 13:02:47 +000088 /*
89 * CURLAUTH_DIGEST_IE has no corresponding command-line option in
90 * curl(1) and is not included in CURLAUTH_ANY, so we leave it out
91 * here, too
92 */
93};
Tom G. Christensendd5df532017-08-11 18:37:34 +020094#ifdef CURLGSSAPI_DELEGATION_FLAG
Petr Stodulka26a7b232016-09-28 20:01:34 +020095static const char *curl_deleg;
96static struct {
97 const char *name;
98 long curl_deleg_param;
99} curl_deleg_levels[] = {
100 { "none", CURLGSSAPI_DELEGATION_NONE },
101 { "policy", CURLGSSAPI_DELEGATION_POLICY_FLAG },
102 { "always", CURLGSSAPI_DELEGATION_FLAG },
103};
104#endif
105
Knut Franke372370f2016-01-26 13:02:48 +0000106static struct credential proxy_auth = CREDENTIAL_INIT;
107static const char *curl_proxyuserpwd;
Duncan Brownbcfb95d2011-06-02 16:31:25 -0400108static const char *curl_cookie_file;
Dave Borowitz912b2ac2013-07-23 15:40:17 -0700109static int curl_save_cookies;
Jeff King2501aff2013-09-28 04:31:45 -0400110struct credential http_auth = CREDENTIAL_INIT;
Jeff Kinga4ddbc32011-12-13 19:11:56 -0500111static int http_proactive_auth;
Spencer E. Olsonb1d10582010-08-11 14:40:38 -0600112static const char *user_agent;
Jeff King40a18fc2017-02-25 14:18:31 -0500113static int curl_empty_auth = -1;
Nick Hengeveld29508e12005-11-18 11:02:58 -0800114
Jeff King50d34132016-12-06 13:24:41 -0500115enum http_follow_config http_follow_config = HTTP_FOLLOW_INITIAL;
116
Jeff King148bb6a2011-12-10 05:31:21 -0500117static struct credential cert_auth = CREDENTIAL_INIT;
Mark Lodato30dd9162009-05-27 23:16:02 -0400118static int ssl_cert_password_required;
brian m. carlson4dbe6642015-01-08 00:29:20 +0000119static unsigned long http_auth_methods = CURLAUTH_ANY;
Jeff King40a18fc2017-02-25 14:18:31 -0500120static int http_auth_methods_restricted;
121/* Modes for which empty_auth cannot actually help us. */
122static unsigned long empty_auth_useless =
123 CURLAUTH_BASIC
Jeff King40a18fc2017-02-25 14:18:31 -0500124 | CURLAUTH_DIGEST_IE
Jeff King40a18fc2017-02-25 14:18:31 -0500125 | CURLAUTH_DIGEST;
Mark Lodato30dd9162009-05-27 23:16:02 -0400126
Mike Hommeycc3530e2007-12-09 18:04:57 +0100127static struct curl_slist *pragma_header;
Tay Ray Chuan5424bc52009-06-06 16:44:02 +0800128static struct curl_slist *no_pragma_header;
Johannes Schindelin4d17fd22019-11-06 10:04:55 +0000129static struct string_list extra_http_headers = STRING_LIST_INIT_DUP;
Tay Ray Chuane9176742009-06-06 16:43:41 +0800130
Christian Couder511cfd32022-05-16 10:38:51 +0200131static struct curl_slist *host_resolutions;
132
Junio C Hamano4251ccb2009-03-09 18:47:29 -0700133static struct active_request_slot *active_queue_head;
Nick Hengeveld29508e12005-11-18 11:02:58 -0800134
Yi EungJunf18604b2015-01-28 21:04:37 +0900135static char *cached_accept_language;
136
Johannes Schindelin21084e82018-10-15 03:14:43 -0700137static char *http_ssl_backend;
138
Brendan Forster93aef7c2018-10-25 11:53:55 -0700139static int http_schannel_check_revoke = 1;
Johannes Schindelinb67d40a2018-10-25 11:53:56 -0700140/*
141 * With the backend being set to `schannel`, setting sslCAinfo would override
142 * the Certificate Store in cURL v7.60.0 and later, which is not what we want
143 * by default.
144 */
145static int http_schannel_use_ssl_cainfo;
Brendan Forster93aef7c2018-10-25 11:53:55 -0700146
Dan McGeea04ff3e2011-05-03 23:47:27 +0800147size_t fread_buffer(char *ptr, size_t eltsize, size_t nmemb, void *buffer_)
Nick Hengeveld29508e12005-11-18 11:02:58 -0800148{
149 size_t size = eltsize * nmemb;
Junio C Hamanof444e522008-07-04 00:37:40 -0700150 struct buffer *buffer = buffer_;
151
Mike Hommey028c2972007-12-09 20:30:59 +0100152 if (size > buffer->buf.len - buffer->posn)
153 size = buffer->buf.len - buffer->posn;
154 memcpy(ptr, buffer->buf.buf + buffer->posn, size);
Nick Hengeveld29508e12005-11-18 11:02:58 -0800155 buffer->posn += size;
Mike Hommey028c2972007-12-09 20:30:59 +0100156
Mike Hommey5c3d5a32019-05-08 08:03:54 +0900157 return size / eltsize;
Nick Hengeveld29508e12005-11-18 11:02:58 -0800158}
159
Martin Storsjö3944ba02009-04-01 19:48:24 +0300160curlioerr ioctl_buffer(CURL *handle, int cmd, void *clientp)
161{
162 struct buffer *buffer = clientp;
163
164 switch (cmd) {
165 case CURLIOCMD_NOP:
166 return CURLIOE_OK;
167
168 case CURLIOCMD_RESTARTREAD:
169 buffer->posn = 0;
170 return CURLIOE_OK;
171
172 default:
173 return CURLIOE_UNKNOWNCMD;
174 }
175}
Martin Storsjö3944ba02009-04-01 19:48:24 +0300176
Dan McGeea04ff3e2011-05-03 23:47:27 +0800177size_t fwrite_buffer(char *ptr, size_t eltsize, size_t nmemb, void *buffer_)
Nick Hengeveld29508e12005-11-18 11:02:58 -0800178{
179 size_t size = eltsize * nmemb;
Junio C Hamanof444e522008-07-04 00:37:40 -0700180 struct strbuf *buffer = buffer_;
181
Mike Hommey028c2972007-12-09 20:30:59 +0100182 strbuf_add(buffer, ptr, size);
Mike Hommey5c3d5a32019-05-08 08:03:54 +0900183 return nmemb;
Nick Hengeveld29508e12005-11-18 11:02:58 -0800184}
185
Dan McGeea04ff3e2011-05-03 23:47:27 +0800186size_t fwrite_null(char *ptr, size_t eltsize, size_t nmemb, void *strbuf)
Nick Hengeveld29508e12005-11-18 11:02:58 -0800187{
Mike Hommey5c3d5a32019-05-08 08:03:54 +0900188 return nmemb;
Nick Hengeveld29508e12005-11-18 11:02:58 -0800189}
190
Junio C Hamanob90a3d72015-01-14 15:40:46 -0800191static void closedown_active_slot(struct active_request_slot *slot)
192{
193 active_requests--;
194 slot->in_use = 0;
195}
196
197static void finish_active_slot(struct active_request_slot *slot)
198{
199 closedown_active_slot(slot);
200 curl_easy_getinfo(slot->curl, CURLINFO_HTTP_CODE, &slot->http_code);
201
Junio C Hamanoafe8a902022-05-02 09:50:37 -0700202 if (slot->finished)
Junio C Hamanob90a3d72015-01-14 15:40:46 -0800203 (*slot->finished) = 1;
204
205 /* Store slot results so they can be read after the slot is reused */
Junio C Hamanoafe8a902022-05-02 09:50:37 -0700206 if (slot->results) {
Junio C Hamanob90a3d72015-01-14 15:40:46 -0800207 slot->results->curl_result = slot->curl_result;
208 slot->results->http_code = slot->http_code;
Junio C Hamanob90a3d72015-01-14 15:40:46 -0800209 curl_easy_getinfo(slot->curl, CURLINFO_HTTPAUTH_AVAIL,
210 &slot->results->auth_avail);
Knut Franke372370f2016-01-26 13:02:48 +0000211
212 curl_easy_getinfo(slot->curl, CURLINFO_HTTP_CONNECTCODE,
213 &slot->results->http_connectcode);
Junio C Hamanob90a3d72015-01-14 15:40:46 -0800214 }
215
216 /* Run callback if appropriate */
Junio C Hamanoafe8a902022-05-02 09:50:37 -0700217 if (slot->callback_func)
Junio C Hamanob90a3d72015-01-14 15:40:46 -0800218 slot->callback_func(slot->callback_data);
219}
220
Eric Wongd8b6b842016-09-13 00:25:56 +0000221static void xmulti_remove_handle(struct active_request_slot *slot)
222{
Eric Wongd8b6b842016-09-13 00:25:56 +0000223 curl_multi_remove_handle(curlm, slot->curl);
Eric Wongd8b6b842016-09-13 00:25:56 +0000224}
225
Nick Hengeveld29508e12005-11-18 11:02:58 -0800226static void process_curl_messages(void)
227{
228 int num_messages;
229 struct active_request_slot *slot;
230 CURLMsg *curl_message = curl_multi_info_read(curlm, &num_messages);
231
232 while (curl_message != NULL) {
233 if (curl_message->msg == CURLMSG_DONE) {
234 int curl_result = curl_message->data.result;
235 slot = active_queue_head;
236 while (slot != NULL &&
237 slot->curl != curl_message->easy_handle)
238 slot = slot->next;
Junio C Hamanoafe8a902022-05-02 09:50:37 -0700239 if (slot) {
Eric Wongd8b6b842016-09-13 00:25:56 +0000240 xmulti_remove_handle(slot);
Nick Hengeveld29508e12005-11-18 11:02:58 -0800241 slot->curl_result = curl_result;
242 finish_active_slot(slot);
243 } else {
244 fprintf(stderr, "Received DONE message for unknown request!\n");
245 }
246 } else {
247 fprintf(stderr, "Unknown CURL message received: %d\n",
248 (int)curl_message->msg);
249 }
250 curl_message = curl_multi_info_read(curlm, &num_messages);
251 }
252}
Nick Hengeveld29508e12005-11-18 11:02:58 -0800253
Johannes Schindelinef90d6d2008-05-14 18:46:53 +0100254static int http_options(const char *var, const char *value, void *cb)
Nick Hengeveld29508e12005-11-18 11:02:58 -0800255{
Force Charlied73019f2018-11-08 19:44:14 -0800256 if (!strcmp("http.version", var)) {
257 return git_config_string(&curl_http_version, var, value);
258 }
Nick Hengeveld29508e12005-11-18 11:02:58 -0800259 if (!strcmp("http.sslverify", var)) {
Junio C Hamano7059cd92009-03-09 19:00:30 -0700260 curl_ssl_verify = git_config_bool(var, value);
Nick Hengeveld29508e12005-11-18 11:02:58 -0800261 return 0;
262 }
Lars Kellogg-Stedmanf6f2a9e2015-05-08 09:22:15 -0400263 if (!strcmp("http.sslcipherlist", var))
264 return git_config_string(&ssl_cipherlist, var, value);
Elia Pinto01861cb2015-08-14 21:37:43 +0200265 if (!strcmp("http.sslversion", var))
266 return git_config_string(&ssl_version, var, value);
Junio C Hamano7059cd92009-03-09 19:00:30 -0700267 if (!strcmp("http.sslcert", var))
Junio C Hamano8d154962017-07-20 13:30:52 -0700268 return git_config_pathname(&ssl_cert, var, value);
Junio C Hamano7059cd92009-03-09 19:00:30 -0700269 if (!strcmp("http.sslkey", var))
Junio C Hamano8d154962017-07-20 13:30:52 -0700270 return git_config_pathname(&ssl_key, var, value);
Junio C Hamano7059cd92009-03-09 19:00:30 -0700271 if (!strcmp("http.sslcapath", var))
Charles Baileybf9acba2015-11-23 12:02:40 +0000272 return git_config_pathname(&ssl_capath, var, value);
Junio C Hamano7059cd92009-03-09 19:00:30 -0700273 if (!strcmp("http.sslcainfo", var))
Charles Baileybf9acba2015-11-23 12:02:40 +0000274 return git_config_pathname(&ssl_cainfo, var, value);
Mark Lodato754ae192009-05-27 23:16:03 -0400275 if (!strcmp("http.sslcertpasswordprotected", var)) {
Junio C Hamano3f4ccd22013-07-12 11:52:47 -0700276 ssl_cert_password_required = git_config_bool(var, value);
Mark Lodato754ae192009-05-27 23:16:03 -0400277 return 0;
278 }
Modestas Vainius4bc444e2013-04-07 22:10:39 +0300279 if (!strcmp("http.ssltry", var)) {
280 curl_ssl_try = git_config_bool(var, value);
281 return 0;
282 }
Johannes Schindelin21084e82018-10-15 03:14:43 -0700283 if (!strcmp("http.sslbackend", var)) {
284 free(http_ssl_backend);
285 http_ssl_backend = xstrdup_or_null(value);
286 return 0;
287 }
288
Brendan Forster93aef7c2018-10-25 11:53:55 -0700289 if (!strcmp("http.schannelcheckrevoke", var)) {
290 http_schannel_check_revoke = git_config_bool(var, value);
291 return 0;
292 }
293
Johannes Schindelinb67d40a2018-10-25 11:53:56 -0700294 if (!strcmp("http.schannelusesslcainfo", var)) {
295 http_schannel_use_ssl_cainfo = git_config_bool(var, value);
296 return 0;
297 }
298
Tay Ray Chuanad75ebe2009-11-27 23:42:26 +0800299 if (!strcmp("http.minsessions", var)) {
300 min_curl_sessions = git_config_int(var, value);
Tay Ray Chuanad75ebe2009-11-27 23:42:26 +0800301 if (min_curl_sessions > 1)
302 min_curl_sessions = 1;
Tay Ray Chuanad75ebe2009-11-27 23:42:26 +0800303 return 0;
304 }
Nick Hengeveld29508e12005-11-18 11:02:58 -0800305 if (!strcmp("http.maxrequests", var)) {
Junio C Hamano7059cd92009-03-09 19:00:30 -0700306 max_requests = git_config_int(var, value);
Nick Hengeveld29508e12005-11-18 11:02:58 -0800307 return 0;
308 }
Nick Hengeveld29508e12005-11-18 11:02:58 -0800309 if (!strcmp("http.lowspeedlimit", var)) {
Junio C Hamano7059cd92009-03-09 19:00:30 -0700310 curl_low_speed_limit = (long)git_config_int(var, value);
Nick Hengeveld29508e12005-11-18 11:02:58 -0800311 return 0;
312 }
313 if (!strcmp("http.lowspeedtime", var)) {
Junio C Hamano7059cd92009-03-09 19:00:30 -0700314 curl_low_speed_time = (long)git_config_int(var, value);
Nick Hengeveld29508e12005-11-18 11:02:58 -0800315 return 0;
316 }
317
Sasha Khapyorsky3ea099d2006-09-29 03:10:44 +0300318 if (!strcmp("http.noepsv", var)) {
319 curl_ftp_no_epsv = git_config_bool(var, value);
320 return 0;
321 }
Junio C Hamano7059cd92009-03-09 19:00:30 -0700322 if (!strcmp("http.proxy", var))
323 return git_config_string(&curl_http_proxy, var, value);
Sasha Khapyorsky3ea099d2006-09-29 03:10:44 +0300324
Knut Frankeef976392016-01-26 13:02:47 +0000325 if (!strcmp("http.proxyauthmethod", var))
326 return git_config_string(&http_proxy_authmethod, var, value);
327
Jorge Lopez Silva88238e02020-03-04 18:40:05 +0000328 if (!strcmp("http.proxysslcert", var))
329 return git_config_string(&http_proxy_ssl_cert, var, value);
330
331 if (!strcmp("http.proxysslkey", var))
332 return git_config_string(&http_proxy_ssl_key, var, value);
333
334 if (!strcmp("http.proxysslcainfo", var))
335 return git_config_string(&http_proxy_ssl_ca_info, var, value);
336
337 if (!strcmp("http.proxysslcertpasswordprotected", var)) {
338 proxy_ssl_cert_password_required = git_config_bool(var, value);
339 return 0;
340 }
341
Duncan Brownbcfb95d2011-06-02 16:31:25 -0400342 if (!strcmp("http.cookiefile", var))
Brian Norrise5a39ad2016-05-04 11:42:15 -0700343 return git_config_pathname(&curl_cookie_file, var, value);
Dave Borowitz912b2ac2013-07-23 15:40:17 -0700344 if (!strcmp("http.savecookies", var)) {
345 curl_save_cookies = git_config_bool(var, value);
346 return 0;
347 }
Duncan Brownbcfb95d2011-06-02 16:31:25 -0400348
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700349 if (!strcmp("http.postbuffer", var)) {
David Turner37ee6802017-04-11 14:13:57 -0400350 http_post_buffer = git_config_ssize_t(var, value);
351 if (http_post_buffer < 0)
Jiang Xinb4eda052022-06-17 18:03:09 +0800352 warning(_("negative value for http.postBuffer; defaulting to %d"), LARGE_PACKET_MAX);
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700353 if (http_post_buffer < LARGE_PACKET_MAX)
354 http_post_buffer = LARGE_PACKET_MAX;
355 return 0;
356 }
357
Spencer E. Olsonb1d10582010-08-11 14:40:38 -0600358 if (!strcmp("http.useragent", var))
359 return git_config_string(&user_agent, var, value);
360
brian m. carlson121061f2016-02-15 18:44:46 +0000361 if (!strcmp("http.emptyauth", var)) {
Jeff King40a18fc2017-02-25 14:18:31 -0500362 if (value && !strcmp("auto", value))
363 curl_empty_auth = -1;
364 else
365 curl_empty_auth = git_config_bool(var, value);
brian m. carlson121061f2016-02-15 18:44:46 +0000366 return 0;
367 }
368
Petr Stodulka26a7b232016-09-28 20:01:34 +0200369 if (!strcmp("http.delegation", var)) {
Tom G. Christensendd5df532017-08-11 18:37:34 +0200370#ifdef CURLGSSAPI_DELEGATION_FLAG
Petr Stodulka26a7b232016-09-28 20:01:34 +0200371 return git_config_string(&curl_deleg, var, value);
372#else
373 warning(_("Delegation control is not supported with cURL < 7.22.0"));
374 return 0;
375#endif
376 }
377
Christoph Eggeraeff8a62016-02-15 15:04:22 +0100378 if (!strcmp("http.pinnedpubkey", var)) {
Ævar Arnfjörð Bjarmasone4ff3b62021-09-13 16:51:28 +0200379#ifdef GIT_CURL_HAVE_CURLOPT_PINNEDPUBLICKEY
Christoph Eggeraeff8a62016-02-15 15:04:22 +0100380 return git_config_pathname(&ssl_pinnedkey, var, value);
381#else
Ævar Arnfjörð Bjarmason905a0282021-09-13 16:51:27 +0200382 warning(_("Public key pinning not supported with cURL < 7.39.0"));
Christoph Eggeraeff8a62016-02-15 15:04:22 +0100383 return 0;
384#endif
385 }
Junio C Hamanoe79112d2016-02-24 13:25:58 -0800386
Johannes Schindelin8cb01e22016-04-27 14:20:37 +0200387 if (!strcmp("http.extraheader", var)) {
388 if (!value) {
389 return config_error_nonbool(var);
390 } else if (!*value) {
Johannes Schindelin4d17fd22019-11-06 10:04:55 +0000391 string_list_clear(&extra_http_headers, 0);
Johannes Schindelin8cb01e22016-04-27 14:20:37 +0200392 } else {
Johannes Schindelin4d17fd22019-11-06 10:04:55 +0000393 string_list_append(&extra_http_headers, value);
Johannes Schindelin8cb01e22016-04-27 14:20:37 +0200394 }
395 return 0;
396 }
397
Christian Couder511cfd32022-05-16 10:38:51 +0200398 if (!strcmp("http.curloptresolve", var)) {
399 if (!value) {
400 return config_error_nonbool(var);
401 } else if (!*value) {
402 curl_slist_free_all(host_resolutions);
403 host_resolutions = NULL;
404 } else {
405 host_resolutions = curl_slist_append(host_resolutions, value);
406 }
407 return 0;
408 }
409
Jeff King50d34132016-12-06 13:24:41 -0500410 if (!strcmp("http.followredirects", var)) {
411 if (value && !strcmp(value, "initial"))
412 http_follow_config = HTTP_FOLLOW_INITIAL;
413 else if (git_config_bool(var, value))
414 http_follow_config = HTTP_FOLLOW_ALWAYS;
415 else
416 http_follow_config = HTTP_FOLLOW_NONE;
417 return 0;
418 }
419
Nick Hengeveld29508e12005-11-18 11:02:58 -0800420 /* Fall back on the default ones */
Johannes Schindelinef90d6d2008-05-14 18:46:53 +0100421 return git_default_config(var, value, cb);
Nick Hengeveld29508e12005-11-18 11:02:58 -0800422}
423
Jeff King40a18fc2017-02-25 14:18:31 -0500424static int curl_empty_auth_enabled(void)
425{
426 if (curl_empty_auth >= 0)
427 return curl_empty_auth;
428
Jeff King40a18fc2017-02-25 14:18:31 -0500429 /*
430 * In the automatic case, kick in the empty-auth
431 * hack as long as we would potentially try some
432 * method more exotic than "Basic" or "Digest".
433 *
434 * But only do this when this is our second or
435 * subsequent request, as by then we know what
436 * methods are available.
437 */
438 if (http_auth_methods_restricted &&
439 (http_auth_methods & ~empty_auth_useless))
440 return 1;
Jeff King40a18fc2017-02-25 14:18:31 -0500441 return 0;
442}
443
Junio C Hamanoc33976c2009-03-09 23:34:25 -0700444static void init_curl_http_auth(CURL *result)
445{
David Turner5275c302016-10-04 10:53:52 -0400446 if (!http_auth.username || !*http_auth.username) {
Jeff King40a18fc2017-02-25 14:18:31 -0500447 if (curl_empty_auth_enabled())
brian m. carlson121061f2016-02-15 18:44:46 +0000448 curl_easy_setopt(result, CURLOPT_USERPWD, ":");
Jeff King6f4c3472012-04-13 02:19:25 -0400449 return;
brian m. carlson121061f2016-02-15 18:44:46 +0000450 }
Jeff King6f4c3472012-04-13 02:19:25 -0400451
452 credential_fill(&http_auth);
453
Jeff King6f4c3472012-04-13 02:19:25 -0400454 curl_easy_setopt(result, CURLOPT_USERNAME, http_auth.username);
455 curl_easy_setopt(result, CURLOPT_PASSWORD, http_auth.password);
Junio C Hamanoc33976c2009-03-09 23:34:25 -0700456}
457
Knut Frankeef976392016-01-26 13:02:47 +0000458/* *var must be free-able */
459static void var_override(const char **var, char *value)
460{
461 if (value) {
462 free((void *)*var);
463 *var = xstrdup(value);
464 }
465}
466
Knut Franke372370f2016-01-26 13:02:48 +0000467static void set_proxyauth_name_password(CURL *result)
468{
Knut Franke372370f2016-01-26 13:02:48 +0000469 curl_easy_setopt(result, CURLOPT_PROXYUSERNAME,
470 proxy_auth.username);
471 curl_easy_setopt(result, CURLOPT_PROXYPASSWORD,
472 proxy_auth.password);
Knut Franke372370f2016-01-26 13:02:48 +0000473}
474
Knut Frankeef976392016-01-26 13:02:47 +0000475static void init_curl_proxy_auth(CURL *result)
476{
Knut Franke372370f2016-01-26 13:02:48 +0000477 if (proxy_auth.username) {
478 if (!proxy_auth.password)
479 credential_fill(&proxy_auth);
480 set_proxyauth_name_password(result);
481 }
482
Knut Frankeef976392016-01-26 13:02:47 +0000483 var_override(&http_proxy_authmethod, getenv("GIT_HTTP_PROXY_AUTHMETHOD"));
484
Knut Frankeef976392016-01-26 13:02:47 +0000485 if (http_proxy_authmethod) {
486 int i;
487 for (i = 0; i < ARRAY_SIZE(proxy_authmethods); i++) {
488 if (!strcmp(http_proxy_authmethod, proxy_authmethods[i].name)) {
489 curl_easy_setopt(result, CURLOPT_PROXYAUTH,
490 proxy_authmethods[i].curlauth_param);
491 break;
492 }
493 }
494 if (i == ARRAY_SIZE(proxy_authmethods)) {
495 warning("unsupported proxy authentication method %s: using anyauth",
496 http_proxy_authmethod);
497 curl_easy_setopt(result, CURLOPT_PROXYAUTH, CURLAUTH_ANY);
498 }
499 }
500 else
501 curl_easy_setopt(result, CURLOPT_PROXYAUTH, CURLAUTH_ANY);
Knut Frankeef976392016-01-26 13:02:47 +0000502}
503
Mark Lodato30dd9162009-05-27 23:16:02 -0400504static int has_cert_password(void)
505{
Mark Lodato30dd9162009-05-27 23:16:02 -0400506 if (ssl_cert == NULL || ssl_cert_password_required != 1)
507 return 0;
Jeff King148bb6a2011-12-10 05:31:21 -0500508 if (!cert_auth.password) {
509 cert_auth.protocol = xstrdup("cert");
Jeff King24036682020-04-18 20:48:05 -0700510 cert_auth.host = xstrdup("");
Rene Bredlau75e9a402012-12-21 17:31:19 +0100511 cert_auth.username = xstrdup("");
Jeff King148bb6a2011-12-10 05:31:21 -0500512 cert_auth.path = xstrdup(ssl_cert);
513 credential_fill(&cert_auth);
514 }
515 return 1;
Mark Lodato30dd9162009-05-27 23:16:02 -0400516}
517
Ævar Arnfjörð Bjarmasone4ff3b62021-09-13 16:51:28 +0200518#ifdef GIT_CURL_HAVE_CURLOPT_PROXY_KEYPASSWD
Jorge Lopez Silva88238e02020-03-04 18:40:05 +0000519static int has_proxy_cert_password(void)
520{
521 if (http_proxy_ssl_cert == NULL || proxy_ssl_cert_password_required != 1)
522 return 0;
523 if (!proxy_cert_auth.password) {
524 proxy_cert_auth.protocol = xstrdup("cert");
Junio C Hamano048abe12020-04-19 22:05:56 -0700525 proxy_cert_auth.host = xstrdup("");
Jorge Lopez Silva88238e02020-03-04 18:40:05 +0000526 proxy_cert_auth.username = xstrdup("");
527 proxy_cert_auth.path = xstrdup(http_proxy_ssl_cert);
528 credential_fill(&proxy_cert_auth);
529 }
530 return 1;
531}
532#endif
533
Ævar Arnfjörð Bjarmasone4ff3b62021-09-13 16:51:28 +0200534#ifdef GITCURL_HAVE_CURLOPT_TCP_KEEPALIVE
Jeff King47ce1152013-10-14 20:06:14 -0400535static void set_curl_keepalive(CURL *c)
536{
537 curl_easy_setopt(c, CURLOPT_TCP_KEEPALIVE, 1);
538}
539
Jeff King013c7e22021-07-30 11:31:54 +0200540#else
Eric Wonga15d0692013-10-12 22:29:40 +0000541static int sockopt_callback(void *client, curl_socket_t fd, curlsocktype type)
542{
543 int ka = 1;
544 int rc;
545 socklen_t len = (socklen_t)sizeof(ka);
546
547 if (type != CURLSOCKTYPE_IPCXN)
548 return 0;
549
550 rc = setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (void *)&ka, len);
551 if (rc < 0)
Nguyễn Thái Ngọc Duyd2e255e2016-05-08 16:47:48 +0700552 warning_errno("unable to set SO_KEEPALIVE on socket");
Eric Wonga15d0692013-10-12 22:29:40 +0000553
Ævar Arnfjörð Bjarmason32da6e62021-09-13 16:51:29 +0200554 return CURL_SOCKOPT_OK;
Eric Wonga15d0692013-10-12 22:29:40 +0000555}
556
Jeff King47ce1152013-10-14 20:06:14 -0400557static void set_curl_keepalive(CURL *c)
558{
559 curl_easy_setopt(c, CURLOPT_SOCKOPTFUNCTION, sockopt_callback);
560}
Jeff King47ce1152013-10-14 20:06:14 -0400561#endif
562
Elia Pinto74c682d2016-05-23 13:44:02 +0000563static void redact_sensitive_header(struct strbuf *header)
564{
565 const char *sensitive_header;
566
Jonathan Tan827e7d42020-06-05 14:21:36 -0700567 if (trace_curl_redact &&
Jeff Kingb66c77a2021-09-22 18:11:53 -0400568 (skip_iprefix(header->buf, "Authorization:", &sensitive_header) ||
569 skip_iprefix(header->buf, "Proxy-Authorization:", &sensitive_header))) {
Elia Pinto74c682d2016-05-23 13:44:02 +0000570 /* The first token is the type, which is OK to log */
571 while (isspace(*sensitive_header))
572 sensitive_header++;
573 while (*sensitive_header && !isspace(*sensitive_header))
574 sensitive_header++;
575 /* Everything else is opaque and possibly sensitive */
576 strbuf_setlen(header, sensitive_header - header->buf);
577 strbuf_addstr(header, " <redacted>");
Jonathan Tan827e7d42020-06-05 14:21:36 -0700578 } else if (trace_curl_redact &&
Jeff Kingb66c77a2021-09-22 18:11:53 -0400579 skip_iprefix(header->buf, "Cookie:", &sensitive_header)) {
Jonathan Tan83411782018-01-18 16:28:01 -0800580 struct strbuf redacted_header = STRBUF_INIT;
Jonathan Tan827e7d42020-06-05 14:21:36 -0700581 const char *cookie;
Jonathan Tan83411782018-01-18 16:28:01 -0800582
583 while (isspace(*sensitive_header))
584 sensitive_header++;
585
Jonathan Tan827e7d42020-06-05 14:21:36 -0700586 cookie = sensitive_header;
Jonathan Tan83411782018-01-18 16:28:01 -0800587
588 while (cookie) {
589 char *equals;
590 char *semicolon = strstr(cookie, "; ");
591 if (semicolon)
592 *semicolon = 0;
593 equals = strchrnul(cookie, '=');
594 if (!equals) {
595 /* invalid cookie, just append and continue */
596 strbuf_addstr(&redacted_header, cookie);
597 continue;
598 }
Jonathan Tan827e7d42020-06-05 14:21:36 -0700599 strbuf_add(&redacted_header, cookie, equals - cookie);
600 strbuf_addstr(&redacted_header, "=<redacted>");
Jonathan Tan83411782018-01-18 16:28:01 -0800601 if (semicolon) {
602 /*
603 * There are more cookies. (Or, for some
604 * reason, the input string ends in "; ".)
605 */
606 strbuf_addstr(&redacted_header, "; ");
607 cookie = semicolon + strlen("; ");
608 } else {
609 cookie = NULL;
610 }
611 }
612
613 strbuf_setlen(header, sensitive_header - header->buf);
614 strbuf_addbuf(header, &redacted_header);
Elia Pinto74c682d2016-05-23 13:44:02 +0000615 }
616}
617
618static void curl_dump_header(const char *text, unsigned char *ptr, size_t size, int hide_sensitive_header)
619{
620 struct strbuf out = STRBUF_INIT;
621 struct strbuf **headers, **header;
622
623 strbuf_addf(&out, "%s, %10.10ld bytes (0x%8.8lx)\n",
624 text, (long)size, (long)size);
625 trace_strbuf(&trace_curl, &out);
626 strbuf_reset(&out);
627 strbuf_add(&out, ptr, size);
628 headers = strbuf_split_max(&out, '\n', 0);
629
630 for (header = headers; *header; header++) {
631 if (hide_sensitive_header)
632 redact_sensitive_header(*header);
René Scharfea91cc7f2020-02-09 14:44:23 +0100633 strbuf_insertstr((*header), 0, text);
634 strbuf_insertstr((*header), strlen(text), ": ");
Elia Pinto74c682d2016-05-23 13:44:02 +0000635 strbuf_rtrim((*header));
636 strbuf_addch((*header), '\n');
637 trace_strbuf(&trace_curl, (*header));
638 }
639 strbuf_list_free(headers);
640 strbuf_release(&out);
641}
642
643static void curl_dump_data(const char *text, unsigned char *ptr, size_t size)
644{
645 size_t i;
646 struct strbuf out = STRBUF_INIT;
647 unsigned int width = 60;
648
649 strbuf_addf(&out, "%s, %10.10ld bytes (0x%8.8lx)\n",
650 text, (long)size, (long)size);
651 trace_strbuf(&trace_curl, &out);
652
653 for (i = 0; i < size; i += width) {
654 size_t w;
655
656 strbuf_reset(&out);
657 strbuf_addf(&out, "%s: ", text);
658 for (w = 0; (w < width) && (i + w < size); w++) {
659 unsigned char ch = ptr[i + w];
660
661 strbuf_addch(&out,
662 (ch >= 0x20) && (ch < 0x80)
663 ? ch : '.');
664 }
665 strbuf_addch(&out, '\n');
666 trace_strbuf(&trace_curl, &out);
667 }
668 strbuf_release(&out);
669}
670
671static int curl_trace(CURL *handle, curl_infotype type, char *data, size_t size, void *userp)
672{
673 const char *text;
674 enum { NO_FILTER = 0, DO_FILTER = 1 };
675
676 switch (type) {
677 case CURLINFO_TEXT:
678 trace_printf_key(&trace_curl, "== Info: %s", data);
Jeff Kingd0e99832017-09-21 02:23:24 -0400679 break;
Elia Pinto74c682d2016-05-23 13:44:02 +0000680 case CURLINFO_HEADER_OUT:
681 text = "=> Send header";
682 curl_dump_header(text, (unsigned char *)data, size, DO_FILTER);
683 break;
684 case CURLINFO_DATA_OUT:
Jonathan Tan8ba18e62018-01-18 16:28:02 -0800685 if (trace_curl_data) {
686 text = "=> Send data";
687 curl_dump_data(text, (unsigned char *)data, size);
688 }
Elia Pinto74c682d2016-05-23 13:44:02 +0000689 break;
690 case CURLINFO_SSL_DATA_OUT:
Jonathan Tan8ba18e62018-01-18 16:28:02 -0800691 if (trace_curl_data) {
692 text = "=> Send SSL data";
693 curl_dump_data(text, (unsigned char *)data, size);
694 }
Elia Pinto74c682d2016-05-23 13:44:02 +0000695 break;
696 case CURLINFO_HEADER_IN:
697 text = "<= Recv header";
698 curl_dump_header(text, (unsigned char *)data, size, NO_FILTER);
699 break;
700 case CURLINFO_DATA_IN:
Jonathan Tan8ba18e62018-01-18 16:28:02 -0800701 if (trace_curl_data) {
702 text = "<= Recv data";
703 curl_dump_data(text, (unsigned char *)data, size);
704 }
Elia Pinto74c682d2016-05-23 13:44:02 +0000705 break;
706 case CURLINFO_SSL_DATA_IN:
Jonathan Tan8ba18e62018-01-18 16:28:02 -0800707 if (trace_curl_data) {
708 text = "<= Recv SSL data";
709 curl_dump_data(text, (unsigned char *)data, size);
710 }
Elia Pinto74c682d2016-05-23 13:44:02 +0000711 break;
Jeff Kingd0e99832017-09-21 02:23:24 -0400712
713 default: /* we ignore unknown types by default */
714 return 0;
Elia Pinto74c682d2016-05-23 13:44:02 +0000715 }
716 return 0;
717}
718
Jonathan Tan7167a622020-05-11 10:43:10 -0700719void http_trace_curl_no_data(void)
720{
721 trace_override_envvar(&trace_curl, "1");
722 trace_curl_data = 0;
723}
724
Elia Pinto74c682d2016-05-23 13:44:02 +0000725void setup_curl_trace(CURL *handle)
726{
727 if (!trace_want(&trace_curl))
728 return;
729 curl_easy_setopt(handle, CURLOPT_VERBOSE, 1L);
730 curl_easy_setopt(handle, CURLOPT_DEBUGFUNCTION, curl_trace);
731 curl_easy_setopt(handle, CURLOPT_DEBUGDATA, NULL);
732}
733
Brandon Williamsa768a022016-12-14 14:39:54 -0800734static long get_curl_allowed_protocols(int from_user)
Brandon Williamsaeae4db2016-12-14 14:39:53 -0800735{
736 long allowed_protocols = 0;
737
Brandon Williamsa768a022016-12-14 14:39:54 -0800738 if (is_transport_allowed("http", from_user))
Brandon Williamsaeae4db2016-12-14 14:39:53 -0800739 allowed_protocols |= CURLPROTO_HTTP;
Brandon Williamsa768a022016-12-14 14:39:54 -0800740 if (is_transport_allowed("https", from_user))
Brandon Williamsaeae4db2016-12-14 14:39:53 -0800741 allowed_protocols |= CURLPROTO_HTTPS;
Brandon Williamsa768a022016-12-14 14:39:54 -0800742 if (is_transport_allowed("ftp", from_user))
Brandon Williamsaeae4db2016-12-14 14:39:53 -0800743 allowed_protocols |= CURLPROTO_FTP;
Brandon Williamsa768a022016-12-14 14:39:54 -0800744 if (is_transport_allowed("ftps", from_user))
Brandon Williamsaeae4db2016-12-14 14:39:53 -0800745 allowed_protocols |= CURLPROTO_FTPS;
746
747 return allowed_protocols;
748}
Elia Pinto74c682d2016-05-23 13:44:02 +0000749
Ævar Arnfjörð Bjarmasone4ff3b62021-09-13 16:51:28 +0200750#ifdef GIT_CURL_HAVE_CURL_HTTP_VERSION_2
Force Charlied73019f2018-11-08 19:44:14 -0800751static int get_curl_http_version_opt(const char *version_string, long *opt)
752{
753 int i;
754 static struct {
755 const char *name;
756 long opt_token;
757 } choice[] = {
758 { "HTTP/1.1", CURL_HTTP_VERSION_1_1 },
759 { "HTTP/2", CURL_HTTP_VERSION_2 }
760 };
761
762 for (i = 0; i < ARRAY_SIZE(choice); i++) {
763 if (!strcmp(version_string, choice[i].name)) {
764 *opt = choice[i].opt_token;
765 return 0;
766 }
767 }
768
769 warning("unknown value given to http.version: '%s'", version_string);
770 return -1; /* not found */
771}
772
773#endif
774
Junio C Hamano4251ccb2009-03-09 18:47:29 -0700775static CURL *get_curl_handle(void)
Junio C Hamano11979b92005-11-18 17:06:46 -0800776{
Junio C Hamano4251ccb2009-03-09 18:47:29 -0700777 CURL *result = curl_easy_init();
Junio C Hamano11979b92005-11-18 17:06:46 -0800778
Bernhard Reiterfaa38072014-08-13 19:31:24 +0200779 if (!result)
780 die("curl_easy_init failed");
781
Junio C Hamanoa5ccc592008-02-21 15:10:37 -0800782 if (!curl_ssl_verify) {
783 curl_easy_setopt(result, CURLOPT_SSL_VERIFYPEER, 0);
784 curl_easy_setopt(result, CURLOPT_SSL_VERIFYHOST, 0);
785 } else {
786 /* Verify authenticity of the peer's certificate */
787 curl_easy_setopt(result, CURLOPT_SSL_VERIFYPEER, 1);
788 /* The name in the cert must match whom we tried to connect */
789 curl_easy_setopt(result, CURLOPT_SSL_VERIFYHOST, 2);
790 }
791
Ævar Arnfjörð Bjarmasone4ff3b62021-09-13 16:51:28 +0200792#ifdef GIT_CURL_HAVE_CURL_HTTP_VERSION_2
Force Charlied73019f2018-11-08 19:44:14 -0800793 if (curl_http_version) {
794 long opt;
795 if (!get_curl_http_version_opt(curl_http_version, &opt)) {
796 /* Set request use http version */
797 curl_easy_setopt(result, CURLOPT_HTTP_VERSION, opt);
798 }
799 }
800#endif
801
Junio C Hamano11979b92005-11-18 17:06:46 -0800802 curl_easy_setopt(result, CURLOPT_NETRC, CURL_NETRC_OPTIONAL);
Junio C Hamano525ecd22009-12-28 10:04:24 -0800803 curl_easy_setopt(result, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
Junio C Hamano11979b92005-11-18 17:06:46 -0800804
Tom G. Christensendd5df532017-08-11 18:37:34 +0200805#ifdef CURLGSSAPI_DELEGATION_FLAG
Petr Stodulka26a7b232016-09-28 20:01:34 +0200806 if (curl_deleg) {
807 int i;
808 for (i = 0; i < ARRAY_SIZE(curl_deleg_levels); i++) {
809 if (!strcmp(curl_deleg, curl_deleg_levels[i].name)) {
810 curl_easy_setopt(result, CURLOPT_GSSAPI_DELEGATION,
811 curl_deleg_levels[i].curl_deleg_param);
812 break;
813 }
814 }
815 if (i == ARRAY_SIZE(curl_deleg_levels))
816 warning("Unknown delegation method '%s': using default",
817 curl_deleg);
818 }
819#endif
820
Brendan Forster93aef7c2018-10-25 11:53:55 -0700821 if (http_ssl_backend && !strcmp("schannel", http_ssl_backend) &&
822 !http_schannel_check_revoke) {
Ævar Arnfjörð Bjarmasone4ff3b62021-09-13 16:51:28 +0200823#ifdef GIT_CURL_HAVE_CURLSSLOPT_NO_REVOKE
Brendan Forster93aef7c2018-10-25 11:53:55 -0700824 curl_easy_setopt(result, CURLOPT_SSL_OPTIONS, CURLSSLOPT_NO_REVOKE);
825#else
Jean-Noël Avilad355e462018-11-28 22:43:09 +0100826 warning(_("CURLSSLOPT_NO_REVOKE not supported with cURL < 7.44.0"));
Brendan Forster93aef7c2018-10-25 11:53:55 -0700827#endif
828 }
829
Jeff Kinga4ddbc32011-12-13 19:11:56 -0500830 if (http_proactive_auth)
831 init_curl_http_auth(result);
832
Elia Pinto01861cb2015-08-14 21:37:43 +0200833 if (getenv("GIT_SSL_VERSION"))
834 ssl_version = getenv("GIT_SSL_VERSION");
835 if (ssl_version && *ssl_version) {
836 int i;
837 for (i = 0; i < ARRAY_SIZE(sslversions); i++) {
838 if (!strcmp(ssl_version, sslversions[i].name)) {
839 curl_easy_setopt(result, CURLOPT_SSLVERSION,
840 sslversions[i].ssl_version);
841 break;
842 }
843 }
844 if (i == ARRAY_SIZE(sslversions))
845 warning("unsupported ssl version %s: using default",
846 ssl_version);
847 }
848
Lars Kellogg-Stedmanf6f2a9e2015-05-08 09:22:15 -0400849 if (getenv("GIT_SSL_CIPHER_LIST"))
850 ssl_cipherlist = getenv("GIT_SSL_CIPHER_LIST");
Lars Kellogg-Stedmanf6f2a9e2015-05-08 09:22:15 -0400851 if (ssl_cipherlist != NULL && *ssl_cipherlist)
852 curl_easy_setopt(result, CURLOPT_SSL_CIPHER_LIST,
853 ssl_cipherlist);
854
Junio C Hamanoafe8a902022-05-02 09:50:37 -0700855 if (ssl_cert)
Junio C Hamano11979b92005-11-18 17:06:46 -0800856 curl_easy_setopt(result, CURLOPT_SSLCERT, ssl_cert);
Mark Lodato30dd9162009-05-27 23:16:02 -0400857 if (has_cert_password())
Jeff King148bb6a2011-12-10 05:31:21 -0500858 curl_easy_setopt(result, CURLOPT_KEYPASSWD, cert_auth.password);
Junio C Hamanoafe8a902022-05-02 09:50:37 -0700859 if (ssl_key)
Junio C Hamano11979b92005-11-18 17:06:46 -0800860 curl_easy_setopt(result, CURLOPT_SSLKEY, ssl_key);
Junio C Hamanoafe8a902022-05-02 09:50:37 -0700861 if (ssl_capath)
Junio C Hamano11979b92005-11-18 17:06:46 -0800862 curl_easy_setopt(result, CURLOPT_CAPATH, ssl_capath);
Ævar Arnfjörð Bjarmasone4ff3b62021-09-13 16:51:28 +0200863#ifdef GIT_CURL_HAVE_CURLOPT_PINNEDPUBLICKEY
Junio C Hamanoafe8a902022-05-02 09:50:37 -0700864 if (ssl_pinnedkey)
Christoph Eggeraeff8a62016-02-15 15:04:22 +0100865 curl_easy_setopt(result, CURLOPT_PINNEDPUBLICKEY, ssl_pinnedkey);
866#endif
Johannes Schindelinb67d40a2018-10-25 11:53:56 -0700867 if (http_ssl_backend && !strcmp("schannel", http_ssl_backend) &&
868 !http_schannel_use_ssl_cainfo) {
869 curl_easy_setopt(result, CURLOPT_CAINFO, NULL);
Ævar Arnfjörð Bjarmasone4ff3b62021-09-13 16:51:28 +0200870#ifdef GIT_CURL_HAVE_CURLOPT_PROXY_CAINFO
Johannes Schindelinb67d40a2018-10-25 11:53:56 -0700871 curl_easy_setopt(result, CURLOPT_PROXY_CAINFO, NULL);
872#endif
Jorge Lopez Silva88238e02020-03-04 18:40:05 +0000873 } else if (ssl_cainfo != NULL || http_proxy_ssl_ca_info != NULL) {
Junio C Hamanoafe8a902022-05-02 09:50:37 -0700874 if (ssl_cainfo)
Jorge Lopez Silva88238e02020-03-04 18:40:05 +0000875 curl_easy_setopt(result, CURLOPT_CAINFO, ssl_cainfo);
Ævar Arnfjörð Bjarmasone4ff3b62021-09-13 16:51:28 +0200876#ifdef GIT_CURL_HAVE_CURLOPT_PROXY_CAINFO
Junio C Hamanoafe8a902022-05-02 09:50:37 -0700877 if (http_proxy_ssl_ca_info)
Jorge Lopez Silva88238e02020-03-04 18:40:05 +0000878 curl_easy_setopt(result, CURLOPT_PROXY_CAINFO, http_proxy_ssl_ca_info);
879#endif
880 }
Junio C Hamano11979b92005-11-18 17:06:46 -0800881
882 if (curl_low_speed_limit > 0 && curl_low_speed_time > 0) {
883 curl_easy_setopt(result, CURLOPT_LOW_SPEED_LIMIT,
884 curl_low_speed_limit);
885 curl_easy_setopt(result, CURLOPT_LOW_SPEED_TIME,
886 curl_low_speed_time);
887 }
888
Blake Burkhartb2581162015-09-22 18:06:20 -0400889 curl_easy_setopt(result, CURLOPT_MAXREDIRS, 20);
Tay Ray Chuan311e2ea2010-09-25 12:20:35 +0800890 curl_easy_setopt(result, CURLOPT_POSTREDIR, CURL_REDIR_POST_ALL);
Brandon Williamsaeae4db2016-12-14 14:39:53 -0800891 curl_easy_setopt(result, CURLOPT_REDIR_PROTOCOLS,
Brandon Williamsa768a022016-12-14 14:39:54 -0800892 get_curl_allowed_protocols(0));
Brandon Williamsaeae4db2016-12-14 14:39:53 -0800893 curl_easy_setopt(result, CURLOPT_PROTOCOLS,
Brandon Williamsa768a022016-12-14 14:39:54 -0800894 get_curl_allowed_protocols(-1));
Mark Wooding7982d742006-02-01 11:44:37 +0000895 if (getenv("GIT_CURL_VERBOSE"))
Jonathan Tan7167a622020-05-11 10:43:10 -0700896 http_trace_curl_no_data();
Elia Pinto74c682d2016-05-23 13:44:02 +0000897 setup_curl_trace(result);
Jonathan Tan8ba18e62018-01-18 16:28:02 -0800898 if (getenv("GIT_TRACE_CURL_NO_DATA"))
899 trace_curl_data = 0;
Jonathan Tan827e7d42020-06-05 14:21:36 -0700900 if (!git_env_bool("GIT_TRACE_REDACT", 1))
901 trace_curl_redact = 0;
Mark Wooding7982d742006-02-01 11:44:37 +0000902
Spencer E. Olsonb1d10582010-08-11 14:40:38 -0600903 curl_easy_setopt(result, CURLOPT_USERAGENT,
Jeff King745c7c82012-06-02 15:03:08 -0400904 user_agent ? user_agent : git_user_agent());
Nick Hengeveld20fc9bc2006-04-04 10:11:29 -0700905
Sasha Khapyorsky3ea099d2006-09-29 03:10:44 +0300906 if (curl_ftp_no_epsv)
907 curl_easy_setopt(result, CURLOPT_FTP_USE_EPSV, 0);
908
Modestas Vainius4bc444e2013-04-07 22:10:39 +0300909 if (curl_ssl_try)
910 curl_easy_setopt(result, CURLOPT_USE_SSL, CURLUSESSL_TRY);
Modestas Vainius4bc444e2013-04-07 22:10:39 +0300911
Knut Franke372370f2016-01-26 13:02:48 +0000912 /*
913 * CURL also examines these variables as a fallback; but we need to query
914 * them here in order to decide whether to prompt for missing password (cf.
915 * init_curl_proxy_auth()).
916 *
917 * Unlike many other common environment variables, these are historically
918 * lowercase only. It appears that CURL did not know this and implemented
919 * only uppercase variants, which was later corrected to take both - with
920 * the exception of http_proxy, which is lowercase only also in CURL. As
921 * the lowercase versions are the historical quasi-standard, they take
922 * precedence here, as in CURL.
923 */
924 if (!curl_http_proxy) {
Jeff Kingd63ed6e2016-09-07 16:06:42 -0400925 if (http_auth.protocol && !strcmp(http_auth.protocol, "https")) {
Knut Franke372370f2016-01-26 13:02:48 +0000926 var_override(&curl_http_proxy, getenv("HTTPS_PROXY"));
927 var_override(&curl_http_proxy, getenv("https_proxy"));
928 } else {
929 var_override(&curl_http_proxy, getenv("http_proxy"));
930 }
931 if (!curl_http_proxy) {
932 var_override(&curl_http_proxy, getenv("ALL_PROXY"));
933 var_override(&curl_http_proxy, getenv("all_proxy"));
934 }
935 }
936
Sergey Ryazanov57415082017-04-11 23:22:18 +0300937 if (curl_http_proxy && curl_http_proxy[0] == '\0') {
938 /*
939 * Handle case with the empty http.proxy value here to keep
940 * common code clean.
941 * NB: empty option disables proxying at all.
942 */
943 curl_easy_setopt(result, CURLOPT_PROXY, "");
944 } else if (curl_http_proxy) {
Junio C Hamano87f8a0b2016-04-08 12:16:06 -0700945 if (starts_with(curl_http_proxy, "socks5h"))
946 curl_easy_setopt(result,
947 CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5_HOSTNAME);
948 else if (starts_with(curl_http_proxy, "socks5"))
Pat Thoyts6d7afe02015-10-26 14:15:07 +0100949 curl_easy_setopt(result,
950 CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
951 else if (starts_with(curl_http_proxy, "socks4a"))
952 curl_easy_setopt(result,
953 CURLOPT_PROXYTYPE, CURLPROXY_SOCKS4A);
954 else if (starts_with(curl_http_proxy, "socks"))
955 curl_easy_setopt(result,
956 CURLOPT_PROXYTYPE, CURLPROXY_SOCKS4);
Ævar Arnfjörð Bjarmasone4ff3b62021-09-13 16:51:28 +0200957#ifdef GIT_CURL_HAVE_CURLOPT_PROXY_KEYPASSWD
Jorge Lopez Silva88238e02020-03-04 18:40:05 +0000958 else if (starts_with(curl_http_proxy, "https")) {
959 curl_easy_setopt(result, CURLOPT_PROXYTYPE, CURLPROXY_HTTPS);
960
961 if (http_proxy_ssl_cert)
962 curl_easy_setopt(result, CURLOPT_PROXY_SSLCERT, http_proxy_ssl_cert);
963
964 if (http_proxy_ssl_key)
965 curl_easy_setopt(result, CURLOPT_PROXY_SSLKEY, http_proxy_ssl_key);
966
967 if (has_proxy_cert_password())
968 curl_easy_setopt(result, CURLOPT_PROXY_KEYPASSWD, proxy_cert_auth.password);
969 }
Wei Shuyu82b68032017-12-20 01:24:01 +0800970#endif
Knut Franke372370f2016-01-26 13:02:48 +0000971 if (strstr(curl_http_proxy, "://"))
972 credential_from_url(&proxy_auth, curl_http_proxy);
973 else {
974 struct strbuf url = STRBUF_INIT;
975 strbuf_addf(&url, "http://%s", curl_http_proxy);
976 credential_from_url(&proxy_auth, url.buf);
977 strbuf_release(&url);
978 }
979
Sergey Ryazanovae51d912017-04-11 23:22:19 +0300980 if (!proxy_auth.host)
981 die("Invalid proxy URL '%s'", curl_http_proxy);
982
Knut Franke372370f2016-01-26 13:02:48 +0000983 curl_easy_setopt(result, CURLOPT_PROXY, proxy_auth.host);
Jiang Xind445fda2016-02-29 23:16:57 +0800984 var_override(&curl_no_proxy, getenv("NO_PROXY"));
985 var_override(&curl_no_proxy, getenv("no_proxy"));
986 curl_easy_setopt(result, CURLOPT_NOPROXY, curl_no_proxy);
Nelson Benitez Leondd613992012-03-02 14:55:57 +0100987 }
Knut Frankeef976392016-01-26 13:02:47 +0000988 init_curl_proxy_auth(result);
Sam Vilain9c5665a2007-11-23 13:07:00 +1300989
Jeff King47ce1152013-10-14 20:06:14 -0400990 set_curl_keepalive(result);
Eric Wonga15d0692013-10-12 22:29:40 +0000991
Junio C Hamano11979b92005-11-18 17:06:46 -0800992 return result;
993}
994
Junio C Hamano7059cd92009-03-09 19:00:30 -0700995static void set_from_env(const char **var, const char *envname)
996{
997 const char *val = getenv(envname);
998 if (val)
999 *var = val;
1000}
1001
Jeff Kinga4ddbc32011-12-13 19:11:56 -05001002void http_init(struct remote *remote, const char *url, int proactive_auth)
Nick Hengeveld29508e12005-11-18 11:02:58 -08001003{
1004 char *low_speed_limit;
1005 char *low_speed_time;
Kyle J. McKay6a569932013-08-05 13:20:36 -07001006 char *normalized_url;
Ævar Arnfjörð Bjarmason73ee4492021-10-01 12:27:33 +02001007 struct urlmatch_config config = URLMATCH_CONFIG_INIT;
Kyle J. McKay6a569932013-08-05 13:20:36 -07001008
1009 config.section = "http";
1010 config.key = NULL;
1011 config.collect_fn = http_options;
1012 config.cascade_fn = git_default_config;
1013 config.cb = NULL;
Nick Hengeveld29508e12005-11-18 11:02:58 -08001014
Tay Ray Chuane9176742009-06-06 16:43:41 +08001015 http_is_verbose = 0;
Kyle J. McKay6a569932013-08-05 13:20:36 -07001016 normalized_url = url_normalize(url, &config.url);
Tay Ray Chuane9176742009-06-06 16:43:41 +08001017
Kyle J. McKay6a569932013-08-05 13:20:36 -07001018 git_config(urlmatch_config_entry, &config);
1019 free(normalized_url);
Mike Hommey7e927562019-08-26 16:49:11 +09001020 string_list_clear(&config.vars, 1);
Junio C Hamano7059cd92009-03-09 19:00:30 -07001021
Ævar Arnfjörð Bjarmasone4ff3b62021-09-13 16:51:28 +02001022#ifdef GIT_CURL_HAVE_CURLSSLSET_NO_BACKENDS
Johannes Schindelin21084e82018-10-15 03:14:43 -07001023 if (http_ssl_backend) {
1024 const curl_ssl_backend **backends;
1025 struct strbuf buf = STRBUF_INIT;
1026 int i;
1027
1028 switch (curl_global_sslset(-1, http_ssl_backend, &backends)) {
1029 case CURLSSLSET_UNKNOWN_BACKEND:
1030 strbuf_addf(&buf, _("Unsupported SSL backend '%s'. "
1031 "Supported SSL backends:"),
1032 http_ssl_backend);
1033 for (i = 0; backends[i]; i++)
1034 strbuf_addf(&buf, "\n\t%s", backends[i]->name);
1035 die("%s", buf.buf);
1036 case CURLSSLSET_NO_BACKENDS:
1037 die(_("Could not set SSL backend to '%s': "
1038 "cURL was built without SSL backends"),
1039 http_ssl_backend);
1040 case CURLSSLSET_TOO_LATE:
1041 die(_("Could not set SSL backend to '%s': already set"),
1042 http_ssl_backend);
1043 case CURLSSLSET_OK:
1044 break; /* Okay! */
1045 }
1046 }
1047#endif
1048
Bernhard Reiterfaa38072014-08-13 19:31:24 +02001049 if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK)
1050 die("curl_global_init failed");
Nick Hengeveld29508e12005-11-18 11:02:58 -08001051
Jeff Kinga4ddbc32011-12-13 19:11:56 -05001052 http_proactive_auth = proactive_auth;
1053
Mike Hommey9fc64402008-02-27 21:35:50 +01001054 if (remote && remote->http_proxy)
1055 curl_http_proxy = xstrdup(remote->http_proxy);
1056
Knut Frankeef976392016-01-26 13:02:47 +00001057 if (remote)
1058 var_override(&http_proxy_authmethod, remote->http_proxy_authmethod);
1059
Johannes Schindelin8cb01e22016-04-27 14:20:37 +02001060 pragma_header = curl_slist_append(http_copy_default_headers(),
1061 "Pragma: no-cache");
1062 no_pragma_header = curl_slist_append(http_copy_default_headers(),
1063 "Pragma:");
Nick Hengeveld29508e12005-11-18 11:02:58 -08001064
Nick Hengeveld29508e12005-11-18 11:02:58 -08001065 {
1066 char *http_max_requests = getenv("GIT_HTTP_MAX_REQUESTS");
Junio C Hamanoafe8a902022-05-02 09:50:37 -07001067 if (http_max_requests)
Nick Hengeveld29508e12005-11-18 11:02:58 -08001068 max_requests = atoi(http_max_requests);
1069 }
1070
1071 curlm = curl_multi_init();
Jeff King8837eb42014-08-17 03:35:53 -04001072 if (!curlm)
1073 die("curl_multi_init failed");
Nick Hengeveld29508e12005-11-18 11:02:58 -08001074
1075 if (getenv("GIT_SSL_NO_VERIFY"))
1076 curl_ssl_verify = 0;
1077
Junio C Hamano7059cd92009-03-09 19:00:30 -07001078 set_from_env(&ssl_cert, "GIT_SSL_CERT");
Junio C Hamano7059cd92009-03-09 19:00:30 -07001079 set_from_env(&ssl_key, "GIT_SSL_KEY");
Junio C Hamano7059cd92009-03-09 19:00:30 -07001080 set_from_env(&ssl_capath, "GIT_SSL_CAPATH");
Junio C Hamano7059cd92009-03-09 19:00:30 -07001081 set_from_env(&ssl_cainfo, "GIT_SSL_CAINFO");
Nick Hengeveld29508e12005-11-18 11:02:58 -08001082
Spencer E. Olsonb1d10582010-08-11 14:40:38 -06001083 set_from_env(&user_agent, "GIT_HTTP_USER_AGENT");
1084
Nick Hengeveld29508e12005-11-18 11:02:58 -08001085 low_speed_limit = getenv("GIT_HTTP_LOW_SPEED_LIMIT");
Junio C Hamanoafe8a902022-05-02 09:50:37 -07001086 if (low_speed_limit)
Nick Hengeveld29508e12005-11-18 11:02:58 -08001087 curl_low_speed_limit = strtol(low_speed_limit, NULL, 10);
1088 low_speed_time = getenv("GIT_HTTP_LOW_SPEED_TIME");
Junio C Hamanoafe8a902022-05-02 09:50:37 -07001089 if (low_speed_time)
Nick Hengeveld29508e12005-11-18 11:02:58 -08001090 curl_low_speed_time = strtol(low_speed_time, NULL, 10);
1091
Nick Hengeveld29508e12005-11-18 11:02:58 -08001092 if (curl_ssl_verify == -1)
1093 curl_ssl_verify = 1;
1094
Tay Ray Chuanad75ebe2009-11-27 23:42:26 +08001095 curl_session_count = 0;
Nick Hengeveld29508e12005-11-18 11:02:58 -08001096 if (max_requests < 1)
1097 max_requests = DEFAULT_MAX_REQUESTS;
Nick Hengeveld29508e12005-11-18 11:02:58 -08001098
Jorge Lopez Silvaaf026512020-03-04 18:40:06 +00001099 set_from_env(&http_proxy_ssl_cert, "GIT_PROXY_SSL_CERT");
1100 set_from_env(&http_proxy_ssl_key, "GIT_PROXY_SSL_KEY");
1101 set_from_env(&http_proxy_ssl_ca_info, "GIT_PROXY_SSL_CAINFO");
1102
1103 if (getenv("GIT_PROXY_SSL_CERT_PASSWORD_PROTECTED"))
1104 proxy_ssl_cert_password_required = 1;
1105
Sasha Khapyorsky3ea099d2006-09-29 03:10:44 +03001106 if (getenv("GIT_CURL_FTP_NO_EPSV"))
1107 curl_ftp_no_epsv = 1;
1108
Jeff Kingdeba4932011-10-14 09:40:40 +02001109 if (url) {
Jeff King148bb6a2011-12-10 05:31:21 -05001110 credential_from_url(&http_auth, url);
Mark Lodato754ae192009-05-27 23:16:03 -04001111 if (!ssl_cert_password_required &&
1112 getenv("GIT_SSL_CERT_PASSWORD_PROTECTED") &&
Christian Couder59556542013-11-30 21:55:40 +01001113 starts_with(url, "https://"))
Mark Lodato30dd9162009-05-27 23:16:02 -04001114 ssl_cert_password_required = 1;
1115 }
Junio C Hamanoc33976c2009-03-09 23:34:25 -07001116
Nick Hengeveld29508e12005-11-18 11:02:58 -08001117 curl_default = get_curl_handle();
Nick Hengeveld29508e12005-11-18 11:02:58 -08001118}
1119
1120void http_cleanup(void)
1121{
1122 struct active_request_slot *slot = active_queue_head;
Nick Hengeveld29508e12005-11-18 11:02:58 -08001123
1124 while (slot != NULL) {
Shawn O. Pearce3278cd02007-09-15 03:23:00 -04001125 struct active_request_slot *next = slot->next;
Junio C Hamanoafe8a902022-05-02 09:50:37 -07001126 if (slot->curl) {
Eric Wongd8b6b842016-09-13 00:25:56 +00001127 xmulti_remove_handle(slot);
Nick Hengeveld29508e12005-11-18 11:02:58 -08001128 curl_easy_cleanup(slot->curl);
Mike Hommeyf23d1f72008-03-03 20:30:16 +01001129 }
Shawn O. Pearce3278cd02007-09-15 03:23:00 -04001130 free(slot);
1131 slot = next;
Nick Hengeveld29508e12005-11-18 11:02:58 -08001132 }
Shawn O. Pearce3278cd02007-09-15 03:23:00 -04001133 active_queue_head = NULL;
Nick Hengeveld29508e12005-11-18 11:02:58 -08001134
Nick Hengeveld29508e12005-11-18 11:02:58 -08001135 curl_easy_cleanup(curl_default);
Nick Hengeveld29508e12005-11-18 11:02:58 -08001136
Nick Hengeveld29508e12005-11-18 11:02:58 -08001137 curl_multi_cleanup(curlm);
Nick Hengeveld29508e12005-11-18 11:02:58 -08001138 curl_global_cleanup();
Nick Hengeveldb3ca4e4e2006-06-06 09:41:32 -07001139
Johannes Schindelin4d17fd22019-11-06 10:04:55 +00001140 string_list_clear(&extra_http_headers, 0);
Johannes Schindelin8cb01e22016-04-27 14:20:37 +02001141
Nick Hengeveldb3ca4e4e2006-06-06 09:41:32 -07001142 curl_slist_free_all(pragma_header);
Shawn O. Pearce3278cd02007-09-15 03:23:00 -04001143 pragma_header = NULL;
Mike Hommey9fc64402008-02-27 21:35:50 +01001144
Tay Ray Chuane9176742009-06-06 16:43:41 +08001145 curl_slist_free_all(no_pragma_header);
1146 no_pragma_header = NULL;
1147
Christian Couder511cfd32022-05-16 10:38:51 +02001148 curl_slist_free_all(host_resolutions);
1149 host_resolutions = NULL;
1150
Mike Hommey9fc64402008-02-27 21:35:50 +01001151 if (curl_http_proxy) {
Miklos Vajnae4a80ec2008-12-07 01:45:37 +01001152 free((void *)curl_http_proxy);
Mike Hommey9fc64402008-02-27 21:35:50 +01001153 curl_http_proxy = NULL;
1154 }
Mark Lodato30dd9162009-05-27 23:16:02 -04001155
Knut Franke372370f2016-01-26 13:02:48 +00001156 if (proxy_auth.password) {
1157 memset(proxy_auth.password, 0, strlen(proxy_auth.password));
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +00001158 FREE_AND_NULL(proxy_auth.password);
Knut Franke372370f2016-01-26 13:02:48 +00001159 }
1160
1161 free((void *)curl_proxyuserpwd);
1162 curl_proxyuserpwd = NULL;
1163
Knut Frankeef976392016-01-26 13:02:47 +00001164 free((void *)http_proxy_authmethod);
1165 http_proxy_authmethod = NULL;
1166
Junio C Hamanoafe8a902022-05-02 09:50:37 -07001167 if (cert_auth.password) {
Jeff King148bb6a2011-12-10 05:31:21 -05001168 memset(cert_auth.password, 0, strlen(cert_auth.password));
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +00001169 FREE_AND_NULL(cert_auth.password);
Mark Lodato30dd9162009-05-27 23:16:02 -04001170 }
1171 ssl_cert_password_required = 0;
Yi EungJunf18604b2015-01-28 21:04:37 +09001172
Junio C Hamanoafe8a902022-05-02 09:50:37 -07001173 if (proxy_cert_auth.password) {
Jorge Lopez Silva88238e02020-03-04 18:40:05 +00001174 memset(proxy_cert_auth.password, 0, strlen(proxy_cert_auth.password));
1175 FREE_AND_NULL(proxy_cert_auth.password);
1176 }
1177 proxy_ssl_cert_password_required = 0;
1178
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +00001179 FREE_AND_NULL(cached_accept_language);
Nick Hengeveld29508e12005-11-18 11:02:58 -08001180}
1181
Nick Hengeveld29508e12005-11-18 11:02:58 -08001182struct active_request_slot *get_active_slot(void)
1183{
1184 struct active_request_slot *slot = active_queue_head;
1185 struct active_request_slot *newslot;
1186
Nick Hengeveld29508e12005-11-18 11:02:58 -08001187 int num_transfers;
1188
1189 /* Wait for a slot to open up if the queue is full */
1190 while (active_requests >= max_requests) {
1191 curl_multi_perform(curlm, &num_transfers);
Junio C Hamano4251ccb2009-03-09 18:47:29 -07001192 if (num_transfers < active_requests)
Nick Hengeveld29508e12005-11-18 11:02:58 -08001193 process_curl_messages();
Nick Hengeveld29508e12005-11-18 11:02:58 -08001194 }
Nick Hengeveld29508e12005-11-18 11:02:58 -08001195
Junio C Hamano4251ccb2009-03-09 18:47:29 -07001196 while (slot != NULL && slot->in_use)
Nick Hengeveld29508e12005-11-18 11:02:58 -08001197 slot = slot->next;
Junio C Hamano4251ccb2009-03-09 18:47:29 -07001198
Junio C Hamanoafe8a902022-05-02 09:50:37 -07001199 if (!slot) {
Nick Hengeveld29508e12005-11-18 11:02:58 -08001200 newslot = xmalloc(sizeof(*newslot));
1201 newslot->curl = NULL;
1202 newslot->in_use = 0;
1203 newslot->next = NULL;
1204
1205 slot = active_queue_head;
Junio C Hamanoafe8a902022-05-02 09:50:37 -07001206 if (!slot) {
Nick Hengeveld29508e12005-11-18 11:02:58 -08001207 active_queue_head = newslot;
1208 } else {
Junio C Hamano4251ccb2009-03-09 18:47:29 -07001209 while (slot->next != NULL)
Nick Hengeveld29508e12005-11-18 11:02:58 -08001210 slot = slot->next;
Nick Hengeveld29508e12005-11-18 11:02:58 -08001211 slot->next = newslot;
1212 }
1213 slot = newslot;
1214 }
1215
Junio C Hamanoafe8a902022-05-02 09:50:37 -07001216 if (!slot->curl) {
Nick Hengeveld29508e12005-11-18 11:02:58 -08001217 slot->curl = curl_easy_duphandle(curl_default);
Tay Ray Chuanad75ebe2009-11-27 23:42:26 +08001218 curl_session_count++;
Nick Hengeveld29508e12005-11-18 11:02:58 -08001219 }
1220
1221 active_requests++;
1222 slot->in_use = 1;
Nick Hengeveldc8568e12006-01-31 11:06:55 -08001223 slot->results = NULL;
Nick Hengeveldbaa7b672006-03-10 20:18:01 -08001224 slot->finished = NULL;
Nick Hengeveld29508e12005-11-18 11:02:58 -08001225 slot->callback_data = NULL;
1226 slot->callback_func = NULL;
Duncan Brownbcfb95d2011-06-02 16:31:25 -04001227 curl_easy_setopt(slot->curl, CURLOPT_COOKIEFILE, curl_cookie_file);
Dave Borowitz912b2ac2013-07-23 15:40:17 -07001228 if (curl_save_cookies)
1229 curl_easy_setopt(slot->curl, CURLOPT_COOKIEJAR, curl_cookie_file);
Nick Hengeveld29508e12005-11-18 11:02:58 -08001230 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, pragma_header);
Christian Couder511cfd32022-05-16 10:38:51 +02001231 curl_easy_setopt(slot->curl, CURLOPT_RESOLVE, host_resolutions);
Nick Hengeveld29508e12005-11-18 11:02:58 -08001232 curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, curl_errorstr);
Nick Hengeveld90949502006-05-31 16:25:03 -07001233 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, NULL);
1234 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, NULL);
1235 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, NULL);
Junio C Hamano1e418272011-04-26 08:04:49 -07001236 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, NULL);
Nick Hengeveld90949502006-05-31 16:25:03 -07001237 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 0);
1238 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
Jeff Kingb793acf2013-04-15 20:30:38 -04001239 curl_easy_setopt(slot->curl, CURLOPT_FAILONERROR, 1);
David Turner835c4d32015-11-02 16:39:58 -05001240 curl_easy_setopt(slot->curl, CURLOPT_RANGE, NULL);
Eric Wongc915f112016-02-03 04:09:14 +00001241
Jeff King50d34132016-12-06 13:24:41 -05001242 /*
1243 * Default following to off unless "ALWAYS" is configured; this gives
1244 * callers a sane starting point, and they can tweak for individual
1245 * HTTP_FOLLOW_* cases themselves.
1246 */
1247 if (http_follow_config == HTTP_FOLLOW_ALWAYS)
1248 curl_easy_setopt(slot->curl, CURLOPT_FOLLOWLOCATION, 1);
1249 else
1250 curl_easy_setopt(slot->curl, CURLOPT_FOLLOWLOCATION, 0);
1251
Eric Wongc915f112016-02-03 04:09:14 +00001252 curl_easy_setopt(slot->curl, CURLOPT_IPRESOLVE, git_curl_ipresolve);
brian m. carlson4dbe6642015-01-08 00:29:20 +00001253 curl_easy_setopt(slot->curl, CURLOPT_HTTPAUTH, http_auth_methods);
Jeff King40a18fc2017-02-25 14:18:31 -05001254 if (http_auth.password || curl_empty_auth_enabled())
Jeff Kingdfa17252012-04-10 11:53:40 +02001255 init_curl_http_auth(slot->curl);
Nick Hengeveld29508e12005-11-18 11:02:58 -08001256
1257 return slot;
1258}
1259
1260int start_active_slot(struct active_request_slot *slot)
1261{
Nick Hengeveld29508e12005-11-18 11:02:58 -08001262 CURLMcode curlm_result = curl_multi_add_handle(curlm, slot->curl);
Daniel Barkalow45c17412007-09-10 23:02:28 -04001263 int num_transfers;
Nick Hengeveld29508e12005-11-18 11:02:58 -08001264
1265 if (curlm_result != CURLM_OK &&
1266 curlm_result != CURLM_CALL_MULTI_PERFORM) {
Eric Wong9f1b5882016-09-13 00:25:55 +00001267 warning("curl_multi_add_handle failed: %s",
1268 curl_multi_strerror(curlm_result));
Nick Hengeveld29508e12005-11-18 11:02:58 -08001269 active_requests--;
1270 slot->in_use = 0;
1271 return 0;
1272 }
Daniel Barkalow45c17412007-09-10 23:02:28 -04001273
1274 /*
1275 * We know there must be something to do, since we just added
1276 * something.
1277 */
1278 curl_multi_perform(curlm, &num_transfers);
Nick Hengeveld29508e12005-11-18 11:02:58 -08001279 return 1;
1280}
1281
Daniel Barkalowfc57b6a2007-09-10 23:02:34 -04001282struct fill_chain {
1283 void *data;
1284 int (*fill)(void *);
1285 struct fill_chain *next;
1286};
1287
Junio C Hamano4251ccb2009-03-09 18:47:29 -07001288static struct fill_chain *fill_cfg;
Daniel Barkalowfc57b6a2007-09-10 23:02:34 -04001289
1290void add_fill_function(void *data, int (*fill)(void *))
1291{
Brandon Williamsee6e0652018-02-14 10:59:42 -08001292 struct fill_chain *new_fill = xmalloc(sizeof(*new_fill));
Daniel Barkalowfc57b6a2007-09-10 23:02:34 -04001293 struct fill_chain **linkp = &fill_cfg;
Brandon Williamsee6e0652018-02-14 10:59:42 -08001294 new_fill->data = data;
1295 new_fill->fill = fill;
1296 new_fill->next = NULL;
Daniel Barkalowfc57b6a2007-09-10 23:02:34 -04001297 while (*linkp)
1298 linkp = &(*linkp)->next;
Brandon Williamsee6e0652018-02-14 10:59:42 -08001299 *linkp = new_fill;
Daniel Barkalowfc57b6a2007-09-10 23:02:34 -04001300}
1301
Daniel Barkalow45c17412007-09-10 23:02:28 -04001302void fill_active_slots(void)
1303{
1304 struct active_request_slot *slot = active_queue_head;
1305
Daniel Barkalowfc57b6a2007-09-10 23:02:34 -04001306 while (active_requests < max_requests) {
1307 struct fill_chain *fill;
1308 for (fill = fill_cfg; fill; fill = fill->next)
1309 if (fill->fill(fill->data))
1310 break;
1311
1312 if (!fill)
Daniel Barkalow45c17412007-09-10 23:02:28 -04001313 break;
Daniel Barkalowfc57b6a2007-09-10 23:02:34 -04001314 }
Daniel Barkalow45c17412007-09-10 23:02:28 -04001315
1316 while (slot != NULL) {
Tay Ray Chuanad75ebe2009-11-27 23:42:26 +08001317 if (!slot->in_use && slot->curl != NULL
1318 && curl_session_count > min_curl_sessions) {
Daniel Barkalow45c17412007-09-10 23:02:28 -04001319 curl_easy_cleanup(slot->curl);
1320 slot->curl = NULL;
Tay Ray Chuanad75ebe2009-11-27 23:42:26 +08001321 curl_session_count--;
Daniel Barkalow45c17412007-09-10 23:02:28 -04001322 }
1323 slot = slot->next;
1324 }
1325}
1326
Nick Hengeveld29508e12005-11-18 11:02:58 -08001327void step_active_slots(void)
1328{
1329 int num_transfers;
1330 CURLMcode curlm_result;
1331
1332 do {
1333 curlm_result = curl_multi_perform(curlm, &num_transfers);
1334 } while (curlm_result == CURLM_CALL_MULTI_PERFORM);
1335 if (num_transfers < active_requests) {
1336 process_curl_messages();
1337 fill_active_slots();
1338 }
1339}
Nick Hengeveld29508e12005-11-18 11:02:58 -08001340
1341void run_active_slot(struct active_request_slot *slot)
1342{
Nick Hengeveld29508e12005-11-18 11:02:58 -08001343 fd_set readfds;
1344 fd_set writefds;
1345 fd_set excfds;
1346 int max_fd;
1347 struct timeval select_timeout;
Nick Hengeveldbaa7b672006-03-10 20:18:01 -08001348 int finished = 0;
Nick Hengeveld29508e12005-11-18 11:02:58 -08001349
Nick Hengeveldbaa7b672006-03-10 20:18:01 -08001350 slot->finished = &finished;
1351 while (!finished) {
Nick Hengeveld29508e12005-11-18 11:02:58 -08001352 step_active_slots();
1353
Mika Fischerdf26c472011-11-04 15:19:27 +01001354 if (slot->in_use) {
Mika Fischereb56c822011-11-04 15:19:26 +01001355 long curl_timeout;
1356 curl_multi_timeout(curlm, &curl_timeout);
1357 if (curl_timeout == 0) {
1358 continue;
1359 } else if (curl_timeout == -1) {
1360 select_timeout.tv_sec = 0;
1361 select_timeout.tv_usec = 50000;
1362 } else {
1363 select_timeout.tv_sec = curl_timeout / 1000;
1364 select_timeout.tv_usec = (curl_timeout % 1000) * 1000;
1365 }
Nick Hengeveld29508e12005-11-18 11:02:58 -08001366
Mika Fischer6f9dd672011-11-04 15:19:25 +01001367 max_fd = -1;
Nick Hengeveld29508e12005-11-18 11:02:58 -08001368 FD_ZERO(&readfds);
1369 FD_ZERO(&writefds);
1370 FD_ZERO(&excfds);
Mika Fischer6f9dd672011-11-04 15:19:25 +01001371 curl_multi_fdset(curlm, &readfds, &writefds, &excfds, &max_fd);
Mika Fischereb56c822011-11-04 15:19:26 +01001372
Stefan Zager7202b812012-10-19 14:04:20 -07001373 /*
1374 * It can happen that curl_multi_timeout returns a pathologically
1375 * long timeout when curl_multi_fdset returns no file descriptors
1376 * to read. See commit message for more details.
1377 */
1378 if (max_fd < 0 &&
1379 (select_timeout.tv_sec > 0 ||
1380 select_timeout.tv_usec > 50000)) {
1381 select_timeout.tv_sec = 0;
1382 select_timeout.tv_usec = 50000;
1383 }
1384
Mika Fischer6f9dd672011-11-04 15:19:25 +01001385 select(max_fd+1, &readfds, &writefds, &excfds, &select_timeout);
Nick Hengeveld29508e12005-11-18 11:02:58 -08001386 }
1387 }
Junio C Hamano05e280c2022-05-26 12:37:31 -07001388
1389 /*
1390 * The value of slot->finished we set before the loop was used
1391 * to set our "finished" variable when our request completed.
1392 *
1393 * 1. The slot may not have been reused for another requst
1394 * yet, in which case it still has &finished.
1395 *
1396 * 2. The slot may already be in-use to serve another request,
1397 * which can further be divided into two cases:
1398 *
1399 * (a) If call run_active_slot() hasn't been called for that
1400 * other request, slot->finished would have been cleared
1401 * by get_active_slot() and has NULL.
1402 *
1403 * (b) If the request did call run_active_slot(), then the
1404 * call would have updated slot->finished at the beginning
1405 * of this function, and with the clearing of the member
1406 * below, we would find that slot->finished is now NULL.
1407 *
1408 * In all cases, slot->finished has no useful information to
1409 * anybody at this point. Some compilers warn us for
1410 * attempting to smuggle a pointer that is about to become
1411 * invalid, i.e. &finished. We clear it here to assure them.
1412 */
1413 slot->finished = NULL;
Nick Hengeveld29508e12005-11-18 11:02:58 -08001414}
1415
Junio C Hamano83e41e22010-01-11 22:26:08 -08001416static void release_active_slot(struct active_request_slot *slot)
Mark Wooding53f31382006-02-07 10:07:39 +00001417{
1418 closedown_active_slot(slot);
Eric Wong2abc8482016-09-13 00:25:57 +00001419 if (slot->curl) {
Eric Wongd8b6b842016-09-13 00:25:56 +00001420 xmulti_remove_handle(slot);
Eric Wong2abc8482016-09-13 00:25:57 +00001421 if (curl_session_count > min_curl_sessions) {
1422 curl_easy_cleanup(slot->curl);
1423 slot->curl = NULL;
1424 curl_session_count--;
1425 }
Mark Wooding53f31382006-02-07 10:07:39 +00001426 }
1427 fill_active_slots();
1428}
1429
Nick Hengeveld29508e12005-11-18 11:02:58 -08001430void finish_all_active_slots(void)
1431{
1432 struct active_request_slot *slot = active_queue_head;
1433
1434 while (slot != NULL)
1435 if (slot->in_use) {
1436 run_active_slot(slot);
1437 slot = active_queue_head;
1438 } else {
1439 slot = slot->next;
1440 }
1441}
Mike Hommeyd7e92802007-12-11 00:08:25 +01001442
Tay Ray Chuan5ace9942009-06-06 16:43:43 +08001443/* Helpers for modifying and creating URLs */
Mike Hommeyd7e92802007-12-11 00:08:25 +01001444static inline int needs_quote(int ch)
1445{
1446 if (((ch >= 'A') && (ch <= 'Z'))
1447 || ((ch >= 'a') && (ch <= 'z'))
1448 || ((ch >= '0') && (ch <= '9'))
1449 || (ch == '/')
1450 || (ch == '-')
1451 || (ch == '.'))
1452 return 0;
1453 return 1;
1454}
1455
Mike Hommeyd7e92802007-12-11 00:08:25 +01001456static char *quote_ref_url(const char *base, const char *ref)
1457{
Tay Ray Chuan113106e2009-03-08 00:47:21 +08001458 struct strbuf buf = STRBUF_INIT;
Mike Hommeyd7e92802007-12-11 00:08:25 +01001459 const char *cp;
Tay Ray Chuan113106e2009-03-08 00:47:21 +08001460 int ch;
Mike Hommeyd7e92802007-12-11 00:08:25 +01001461
Tay Ray Chuan5ace9942009-06-06 16:43:43 +08001462 end_url_with_slash(&buf, base);
Tay Ray Chuan113106e2009-03-08 00:47:21 +08001463
1464 for (cp = ref; (ch = *cp) != 0; cp++)
Mike Hommeyd7e92802007-12-11 00:08:25 +01001465 if (needs_quote(ch))
Tay Ray Chuan113106e2009-03-08 00:47:21 +08001466 strbuf_addf(&buf, "%%%02x", ch);
Mike Hommeyd7e92802007-12-11 00:08:25 +01001467 else
Tay Ray Chuan113106e2009-03-08 00:47:21 +08001468 strbuf_addch(&buf, *cp);
Mike Hommeyd7e92802007-12-11 00:08:25 +01001469
Tay Ray Chuan113106e2009-03-08 00:47:21 +08001470 return strbuf_detach(&buf, NULL);
Mike Hommeyd7e92802007-12-11 00:08:25 +01001471}
1472
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08001473void append_remote_object_url(struct strbuf *buf, const char *url,
1474 const char *hex,
1475 int only_two_digit_prefix)
1476{
Tay Ray Chuan800324c2009-08-17 17:09:43 +08001477 end_url_with_slash(buf, url);
1478
1479 strbuf_addf(buf, "objects/%.*s/", 2, hex);
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08001480 if (!only_two_digit_prefix)
René Scharfebc57b9c2016-08-05 22:37:11 +02001481 strbuf_addstr(buf, hex + 2);
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08001482}
1483
1484char *get_remote_object_url(const char *url, const char *hex,
1485 int only_two_digit_prefix)
1486{
1487 struct strbuf buf = STRBUF_INIT;
1488 append_remote_object_url(&buf, url, hex, only_two_digit_prefix);
1489 return strbuf_detach(&buf, NULL);
1490}
1491
Jeff Kinga3722bc2019-03-24 08:08:38 -04001492void normalize_curl_result(CURLcode *result, long http_code,
1493 char *errorstr, size_t errorlen)
Jeff King88097032012-08-27 09:26:04 -04001494{
Jeff King6d052d72013-04-05 18:14:06 -04001495 /*
1496 * If we see a failing http code with CURLE_OK, we have turned off
1497 * FAILONERROR (to keep the server's custom error response), and should
1498 * translate the code into failure here.
Jeff King50d34132016-12-06 13:24:41 -05001499 *
1500 * Likewise, if we see a redirect (30x code), that means we turned off
1501 * redirect-following, and we should treat the result as an error.
Jeff King6d052d72013-04-05 18:14:06 -04001502 */
Jeff Kinga3722bc2019-03-24 08:08:38 -04001503 if (*result == CURLE_OK && http_code >= 300) {
1504 *result = CURLE_HTTP_RETURNED_ERROR;
Jeff King6d052d72013-04-05 18:14:06 -04001505 /*
1506 * Normally curl will already have put the "reason phrase"
1507 * from the server into curl_errorstr; unfortunately without
1508 * FAILONERROR it is lost, so we can give only the numeric
1509 * status code.
1510 */
Jeff Kinga3722bc2019-03-24 08:08:38 -04001511 xsnprintf(errorstr, errorlen,
Jeff King1a168e52017-03-28 15:46:56 -04001512 "The requested URL returned error: %ld",
Jeff Kinga3722bc2019-03-24 08:08:38 -04001513 http_code);
Jeff King6d052d72013-04-05 18:14:06 -04001514 }
Jeff Kinga3722bc2019-03-24 08:08:38 -04001515}
1516
1517static int handle_curl_result(struct slot_results *results)
1518{
1519 normalize_curl_result(&results->curl_result, results->http_code,
1520 curl_errorstr, sizeof(curl_errorstr));
Jeff King6d052d72013-04-05 18:14:06 -04001521
Jeff King88097032012-08-27 09:26:04 -04001522 if (results->curl_result == CURLE_OK) {
1523 credential_approve(&http_auth);
John Szakmeistera4a44392021-03-11 21:40:27 -05001524 credential_approve(&proxy_auth);
John Szakmeistercd27f602021-03-11 21:40:26 -05001525 credential_approve(&cert_auth);
Jeff King88097032012-08-27 09:26:04 -04001526 return HTTP_OK;
John Szakmeistercd27f602021-03-11 21:40:26 -05001527 } else if (results->curl_result == CURLE_SSL_CERTPROBLEM) {
1528 /*
1529 * We can't tell from here whether it's a bad path, bad
1530 * certificate, bad password, or something else wrong
1531 * with the certificate. So we reject the credential to
1532 * avoid caching or saving a bad password.
1533 */
1534 credential_reject(&cert_auth);
1535 return HTTP_NOAUTH;
Ævar Arnfjörð Bjarmason3e8084f2021-09-24 12:08:20 +02001536#ifdef GIT_CURL_HAVE_CURLE_SSL_PINNEDPUBKEYNOTMATCH
1537 } else if (results->curl_result == CURLE_SSL_PINNEDPUBKEYNOTMATCH) {
1538 return HTTP_NOMATCHPUBLICKEY;
1539#endif
Jeff King88097032012-08-27 09:26:04 -04001540 } else if (missing_target(results))
1541 return HTTP_MISSING_TARGET;
1542 else if (results->http_code == 401) {
1543 if (http_auth.username && http_auth.password) {
1544 credential_reject(&http_auth);
1545 return HTTP_NOAUTH;
1546 } else {
Jeff Kingecf7b122021-05-18 02:27:42 -04001547 http_auth_methods &= ~CURLAUTH_GSSNEGOTIATE;
1548 if (results->auth_avail) {
1549 http_auth_methods &= results->auth_avail;
1550 http_auth_methods_restricted = 1;
1551 }
Jeff King88097032012-08-27 09:26:04 -04001552 return HTTP_REAUTH;
1553 }
1554 } else {
Knut Franke372370f2016-01-26 13:02:48 +00001555 if (results->http_connectcode == 407)
1556 credential_reject(&proxy_auth);
Jeff King88097032012-08-27 09:26:04 -04001557 if (!curl_errorstr[0])
1558 strlcpy(curl_errorstr,
1559 curl_easy_strerror(results->curl_result),
1560 sizeof(curl_errorstr));
1561 return HTTP_ERROR;
1562 }
1563}
1564
Jeff Kingbeed3362014-02-18 05:34:20 -05001565int run_one_slot(struct active_request_slot *slot,
1566 struct slot_results *results)
1567{
1568 slot->results = results;
1569 if (!start_active_slot(slot)) {
Jeff King1a168e52017-03-28 15:46:56 -04001570 xsnprintf(curl_errorstr, sizeof(curl_errorstr),
1571 "failed to start HTTP request");
Jeff Kingbeed3362014-02-18 05:34:20 -05001572 return HTTP_START_FAILED;
1573 }
1574
1575 run_active_slot(slot);
1576 return handle_curl_result(results);
1577}
1578
Johannes Schindelin8cb01e22016-04-27 14:20:37 +02001579struct curl_slist *http_copy_default_headers(void)
1580{
Johannes Schindelin4d17fd22019-11-06 10:04:55 +00001581 struct curl_slist *headers = NULL;
1582 const struct string_list_item *item;
Johannes Schindelin8cb01e22016-04-27 14:20:37 +02001583
Johannes Schindelin4d17fd22019-11-06 10:04:55 +00001584 for_each_string_list_item(item, &extra_http_headers)
1585 headers = curl_slist_append(headers, item->string);
Johannes Schindelin8cb01e22016-04-27 14:20:37 +02001586
1587 return headers;
1588}
1589
Jeff King132b70a2013-09-28 04:31:11 -04001590static CURLcode curlinfo_strbuf(CURL *curl, CURLINFO info, struct strbuf *buf)
1591{
1592 char *ptr;
1593 CURLcode ret;
1594
1595 strbuf_reset(buf);
1596 ret = curl_easy_getinfo(curl, info, &ptr);
1597 if (!ret && ptr)
1598 strbuf_addstr(buf, ptr);
1599 return ret;
1600}
1601
Jeff Kingbf197fd2014-05-22 05:29:47 -04001602/*
Jeff Kinge3131622014-05-22 05:30:05 -04001603 * Check for and extract a content-type parameter. "raw"
1604 * should be positioned at the start of the potential
1605 * parameter, with any whitespace already removed.
1606 *
1607 * "name" is the name of the parameter. The value is appended
1608 * to "out".
1609 */
1610static int extract_param(const char *raw, const char *name,
1611 struct strbuf *out)
1612{
1613 size_t len = strlen(name);
1614
1615 if (strncasecmp(raw, name, len))
1616 return -1;
1617 raw += len;
1618
1619 if (*raw != '=')
1620 return -1;
1621 raw++;
1622
Yi EungJunf34a6552014-06-18 07:11:53 +09001623 while (*raw && !isspace(*raw) && *raw != ';')
Jeff Kinge3131622014-05-22 05:30:05 -04001624 strbuf_addch(out, *raw++);
1625 return 0;
1626}
1627
1628/*
Jeff Kingbf197fd2014-05-22 05:29:47 -04001629 * Extract a normalized version of the content type, with any
1630 * spaces suppressed, all letters lowercased, and no trailing ";"
1631 * or parameters.
1632 *
1633 * Note that we will silently remove even invalid whitespace. For
1634 * example, "text / plain" is specifically forbidden by RFC 2616,
1635 * but "text/plain" is the only reasonable output, and this keeps
1636 * our code simple.
1637 *
Jeff Kinge3131622014-05-22 05:30:05 -04001638 * If the "charset" argument is not NULL, store the value of any
1639 * charset parameter there.
1640 *
Jeff Kingbf197fd2014-05-22 05:29:47 -04001641 * Example:
Jeff Kinge3131622014-05-22 05:30:05 -04001642 * "TEXT/PLAIN; charset=utf-8" -> "text/plain", "utf-8"
Jeff Kingbf197fd2014-05-22 05:29:47 -04001643 * "text / plain" -> "text/plain"
1644 */
Jeff Kinge3131622014-05-22 05:30:05 -04001645static void extract_content_type(struct strbuf *raw, struct strbuf *type,
1646 struct strbuf *charset)
Jeff Kingbf197fd2014-05-22 05:29:47 -04001647{
1648 const char *p;
1649
1650 strbuf_reset(type);
1651 strbuf_grow(type, raw->len);
1652 for (p = raw->buf; *p; p++) {
1653 if (isspace(*p))
1654 continue;
Jeff Kinge3131622014-05-22 05:30:05 -04001655 if (*p == ';') {
1656 p++;
Jeff Kingbf197fd2014-05-22 05:29:47 -04001657 break;
Jeff Kinge3131622014-05-22 05:30:05 -04001658 }
Jeff Kingbf197fd2014-05-22 05:29:47 -04001659 strbuf_addch(type, tolower(*p));
1660 }
Jeff Kinge3131622014-05-22 05:30:05 -04001661
1662 if (!charset)
1663 return;
1664
1665 strbuf_reset(charset);
1666 while (*p) {
Yi EungJunf34a6552014-06-18 07:11:53 +09001667 while (isspace(*p) || *p == ';')
Jeff Kinge3131622014-05-22 05:30:05 -04001668 p++;
1669 if (!extract_param(p, "charset", charset))
1670 return;
1671 while (*p && !isspace(*p))
1672 p++;
1673 }
Jeff Kingc553fd12014-05-22 05:36:12 -04001674
1675 if (!charset->len && starts_with(type->buf, "text/"))
1676 strbuf_addstr(charset, "ISO-8859-1");
Jeff Kingbf197fd2014-05-22 05:29:47 -04001677}
1678
Yi EungJunf18604b2015-01-28 21:04:37 +09001679static void write_accept_language(struct strbuf *buf)
1680{
1681 /*
1682 * MAX_DECIMAL_PLACES must not be larger than 3. If it is larger than
1683 * that, q-value will be smaller than 0.001, the minimum q-value the
1684 * HTTP specification allows. See
1685 * http://tools.ietf.org/html/rfc7231#section-5.3.1 for q-value.
1686 */
1687 const int MAX_DECIMAL_PLACES = 3;
1688 const int MAX_LANGUAGE_TAGS = 1000;
1689 const int MAX_ACCEPT_LANGUAGE_HEADER_SIZE = 4000;
1690 char **language_tags = NULL;
1691 int num_langs = 0;
1692 const char *s = get_preferred_languages();
1693 int i;
1694 struct strbuf tag = STRBUF_INIT;
1695
1696 /* Don't add Accept-Language header if no language is preferred. */
1697 if (!s)
1698 return;
1699
1700 /*
1701 * Split the colon-separated string of preferred languages into
1702 * language_tags array.
1703 */
1704 do {
1705 /* collect language tag */
1706 for (; *s && (isalnum(*s) || *s == '_'); s++)
1707 strbuf_addch(&tag, *s == '_' ? '-' : *s);
1708
1709 /* skip .codeset, @modifier and any other unnecessary parts */
1710 while (*s && *s != ':')
1711 s++;
1712
1713 if (tag.len) {
1714 num_langs++;
1715 REALLOC_ARRAY(language_tags, num_langs);
1716 language_tags[num_langs - 1] = strbuf_detach(&tag, NULL);
1717 if (num_langs >= MAX_LANGUAGE_TAGS - 1) /* -1 for '*' */
1718 break;
1719 }
1720 } while (*s++);
1721
1722 /* write Accept-Language header into buf */
1723 if (num_langs) {
1724 int last_buf_len = 0;
1725 int max_q;
1726 int decimal_places;
1727 char q_format[32];
1728
1729 /* add '*' */
1730 REALLOC_ARRAY(language_tags, num_langs + 1);
1731 language_tags[num_langs++] = "*"; /* it's OK; this won't be freed */
1732
1733 /* compute decimal_places */
1734 for (max_q = 1, decimal_places = 0;
1735 max_q < num_langs && decimal_places <= MAX_DECIMAL_PLACES;
1736 decimal_places++, max_q *= 10)
1737 ;
1738
Jeff King5096d492015-09-24 17:06:08 -04001739 xsnprintf(q_format, sizeof(q_format), ";q=0.%%0%dd", decimal_places);
Yi EungJunf18604b2015-01-28 21:04:37 +09001740
1741 strbuf_addstr(buf, "Accept-Language: ");
1742
1743 for (i = 0; i < num_langs; i++) {
1744 if (i > 0)
1745 strbuf_addstr(buf, ", ");
1746
1747 strbuf_addstr(buf, language_tags[i]);
1748
1749 if (i > 0)
1750 strbuf_addf(buf, q_format, max_q - i);
1751
1752 if (buf->len > MAX_ACCEPT_LANGUAGE_HEADER_SIZE) {
1753 strbuf_remove(buf, last_buf_len, buf->len - last_buf_len);
1754 break;
1755 }
1756
1757 last_buf_len = buf->len;
1758 }
1759 }
1760
1761 /* free language tags -- last one is a static '*' */
1762 for (i = 0; i < num_langs - 1; i++)
1763 free(language_tags[i]);
1764 free(language_tags);
1765}
1766
1767/*
1768 * Get an Accept-Language header which indicates user's preferred languages.
1769 *
1770 * Examples:
1771 * LANGUAGE= -> ""
1772 * LANGUAGE=ko:en -> "Accept-Language: ko, en; q=0.9, *; q=0.1"
1773 * LANGUAGE=ko_KR.UTF-8:sr@latin -> "Accept-Language: ko-KR, sr; q=0.9, *; q=0.1"
1774 * LANGUAGE=ko LANG=en_US.UTF-8 -> "Accept-Language: ko, *; q=0.1"
1775 * LANGUAGE= LANG=en_US.UTF-8 -> "Accept-Language: en-US, *; q=0.1"
1776 * LANGUAGE= LANG=C -> ""
1777 */
Li Linchaob0c4adc2022-07-11 05:58:54 +00001778const char *http_get_accept_language_header(void)
Yi EungJunf18604b2015-01-28 21:04:37 +09001779{
1780 if (!cached_accept_language) {
1781 struct strbuf buf = STRBUF_INIT;
1782 write_accept_language(&buf);
1783 if (buf.len > 0)
1784 cached_accept_language = strbuf_detach(&buf, NULL);
1785 }
1786
1787 return cached_accept_language;
1788}
1789
David Turner835c4d32015-11-02 16:39:58 -05001790static void http_opt_request_remainder(CURL *curl, off_t pos)
1791{
1792 char buf[128];
1793 xsnprintf(buf, sizeof(buf), "%"PRIuMAX"-", (uintmax_t)pos);
1794 curl_easy_setopt(curl, CURLOPT_RANGE, buf);
1795}
1796
Mike Hommeye929cd22009-06-06 16:43:53 +08001797/* http_request() targets */
1798#define HTTP_REQUEST_STRBUF 0
1799#define HTTP_REQUEST_FILE 1
1800
Jeff King1bbcc222013-09-28 04:31:23 -04001801static int http_request(const char *url,
1802 void *result, int target,
1803 const struct http_get_options *options)
Mike Hommeye929cd22009-06-06 16:43:53 +08001804{
1805 struct active_request_slot *slot;
1806 struct slot_results results;
Johannes Schindelin8cb01e22016-04-27 14:20:37 +02001807 struct curl_slist *headers = http_copy_default_headers();
Mike Hommeye929cd22009-06-06 16:43:53 +08001808 struct strbuf buf = STRBUF_INIT;
Yi EungJunf18604b2015-01-28 21:04:37 +09001809 const char *accept_language;
Mike Hommeye929cd22009-06-06 16:43:53 +08001810 int ret;
1811
1812 slot = get_active_slot();
Mike Hommeye929cd22009-06-06 16:43:53 +08001813 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
1814
Junio C Hamanoafe8a902022-05-02 09:50:37 -07001815 if (!result) {
Mike Hommeye929cd22009-06-06 16:43:53 +08001816 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 1);
1817 } else {
1818 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
Ævar Arnfjörð Bjarmason8dda4cb2021-07-30 19:59:46 +02001819 curl_easy_setopt(slot->curl, CURLOPT_WRITEDATA, result);
Mike Hommeye929cd22009-06-06 16:43:53 +08001820
1821 if (target == HTTP_REQUEST_FILE) {
Jeff Kingf8117f52015-11-02 17:10:27 -05001822 off_t posn = ftello(result);
Mike Hommeye929cd22009-06-06 16:43:53 +08001823 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION,
1824 fwrite);
David Turner835c4d32015-11-02 16:39:58 -05001825 if (posn > 0)
1826 http_opt_request_remainder(slot->curl, posn);
Mike Hommeye929cd22009-06-06 16:43:53 +08001827 } else
1828 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION,
1829 fwrite_buffer);
1830 }
1831
Li Linchaob0c4adc2022-07-11 05:58:54 +00001832 accept_language = http_get_accept_language_header();
Yi EungJunf18604b2015-01-28 21:04:37 +09001833
1834 if (accept_language)
1835 headers = curl_slist_append(headers, accept_language);
1836
Mike Hommeye929cd22009-06-06 16:43:53 +08001837 strbuf_addstr(&buf, "Pragma:");
Jeff King1bbcc222013-09-28 04:31:23 -04001838 if (options && options->no_cache)
Mike Hommeye929cd22009-06-06 16:43:53 +08001839 strbuf_addstr(&buf, " no-cache");
Jeff King50d34132016-12-06 13:24:41 -05001840 if (options && options->initial_request &&
1841 http_follow_config == HTTP_FOLLOW_INITIAL)
1842 curl_easy_setopt(slot->curl, CURLOPT_FOLLOWLOCATION, 1);
Mike Hommeye929cd22009-06-06 16:43:53 +08001843
1844 headers = curl_slist_append(headers, buf.buf);
1845
Brandon Williams8ff14ed2018-03-15 10:31:38 -07001846 /* Add additional headers here */
1847 if (options && options->extra_headers) {
1848 const struct string_list_item *item;
1849 for_each_string_list_item(item, options->extra_headers) {
1850 headers = curl_slist_append(headers, item->string);
1851 }
1852 }
1853
Mike Hommeye929cd22009-06-06 16:43:53 +08001854 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
1855 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
Brandon Williams1a53e692018-05-22 11:42:03 -07001856 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "");
Masaya Suzukie6cf87b2019-01-10 11:33:47 -08001857 curl_easy_setopt(slot->curl, CURLOPT_FAILONERROR, 0);
Mike Hommeye929cd22009-06-06 16:43:53 +08001858
Jeff Kingbeed3362014-02-18 05:34:20 -05001859 ret = run_one_slot(slot, &results);
Mike Hommeye929cd22009-06-06 16:43:53 +08001860
Jeff Kingbf197fd2014-05-22 05:29:47 -04001861 if (options && options->content_type) {
1862 struct strbuf raw = STRBUF_INIT;
1863 curlinfo_strbuf(slot->curl, CURLINFO_CONTENT_TYPE, &raw);
Jeff Kinge3131622014-05-22 05:30:05 -04001864 extract_content_type(&raw, options->content_type,
1865 options->charset);
Jeff Kingbf197fd2014-05-22 05:29:47 -04001866 strbuf_release(&raw);
1867 }
Shawn Pearce4656bf42013-01-31 13:02:07 -08001868
Jeff King78868962013-09-28 04:32:02 -04001869 if (options && options->effective_url)
1870 curlinfo_strbuf(slot->curl, CURLINFO_EFFECTIVE_URL,
1871 options->effective_url);
Mike Hommeye929cd22009-06-06 16:43:53 +08001872
Mike Hommeye929cd22009-06-06 16:43:53 +08001873 curl_slist_free_all(headers);
1874 strbuf_release(&buf);
1875
1876 return ret;
1877}
1878
Jeff Kingc93c92f2013-09-28 04:34:05 -04001879/*
1880 * Update the "base" url to a more appropriate value, as deduced by
1881 * redirects seen when requesting a URL starting with "url".
1882 *
1883 * The "asked" parameter is a URL that we asked curl to access, and must begin
1884 * with "base".
1885 *
1886 * The "got" parameter is the URL that curl reported to us as where we ended
1887 * up.
1888 *
1889 * Returns 1 if we updated the base url, 0 otherwise.
1890 *
1891 * Our basic strategy is to compare "base" and "asked" to find the bits
1892 * specific to our request. We then strip those bits off of "got" to yield the
1893 * new base. So for example, if our base is "http://example.com/foo.git",
1894 * and we ask for "http://example.com/foo.git/info/refs", we might end up
1895 * with "https://other.example.com/foo.git/info/refs". We would want the
1896 * new URL to become "https://other.example.com/foo.git".
1897 *
1898 * Note that this assumes a sane redirect scheme. It's entirely possible
1899 * in the example above to end up at a URL that does not even end in
Jeff King6628eb42016-12-06 13:24:35 -05001900 * "info/refs". In such a case we die. There's not much we can do, such a
1901 * scheme is unlikely to represent a real git repository, and failing to
1902 * rewrite the base opens options for malicious redirects to do funny things.
Jeff Kingc93c92f2013-09-28 04:34:05 -04001903 */
1904static int update_url_from_redirect(struct strbuf *base,
1905 const char *asked,
1906 const struct strbuf *got)
Jeff King8d677ed2011-07-18 03:50:14 -04001907{
Jeff Kingc93c92f2013-09-28 04:34:05 -04001908 const char *tail;
Jeff King986d7f42016-12-06 13:24:29 -05001909 size_t new_len;
Jeff Kingc93c92f2013-09-28 04:34:05 -04001910
1911 if (!strcmp(asked, got->buf))
1912 return 0;
1913
Jeff Kingde8118e2014-06-18 15:57:17 -04001914 if (!skip_prefix(asked, base->buf, &tail))
Johannes Schindelin033abf92018-05-02 11:38:39 +02001915 BUG("update_url_from_redirect: %s is not a superset of %s",
Jeff Kingc93c92f2013-09-28 04:34:05 -04001916 asked, base->buf);
1917
Jeff King986d7f42016-12-06 13:24:29 -05001918 new_len = got->len;
1919 if (!strip_suffix_mem(got->buf, &new_len, tail))
Jeff King6628eb42016-12-06 13:24:35 -05001920 die(_("unable to update url base from redirection:\n"
1921 " asked for: %s\n"
1922 " redirect: %s"),
1923 asked, got->buf);
Jeff Kingc93c92f2013-09-28 04:34:05 -04001924
1925 strbuf_reset(base);
Jeff King986d7f42016-12-06 13:24:29 -05001926 strbuf_add(base, got->buf, new_len);
Jeff King6628eb42016-12-06 13:24:35 -05001927
Jeff Kingc93c92f2013-09-28 04:34:05 -04001928 return 1;
1929}
1930
Mike Hommeye929cd22009-06-06 16:43:53 +08001931static int http_request_reauth(const char *url,
Jeff King8d677ed2011-07-18 03:50:14 -04001932 void *result, int target,
Jeff King1bbcc222013-09-28 04:31:23 -04001933 struct http_get_options *options)
Jeff King8d677ed2011-07-18 03:50:14 -04001934{
Jeff King1bbcc222013-09-28 04:31:23 -04001935 int ret = http_request(url, result, target, options);
Jeff Kingc93c92f2013-09-28 04:34:05 -04001936
Jonathan Tan8e273912017-02-27 18:53:11 -08001937 if (ret != HTTP_OK && ret != HTTP_REAUTH)
1938 return ret;
1939
Jeff Kingc93c92f2013-09-28 04:34:05 -04001940 if (options && options->effective_url && options->base_url) {
1941 if (update_url_from_redirect(options->base_url,
1942 url, options->effective_url)) {
1943 credential_from_url(&http_auth, options->base_url->buf);
1944 url = options->effective_url->buf;
1945 }
1946 }
1947
Jeff King8d677ed2011-07-18 03:50:14 -04001948 if (ret != HTTP_REAUTH)
1949 return ret;
Jeff King6d052d72013-04-05 18:14:06 -04001950
1951 /*
Masaya Suzukie6cf87b2019-01-10 11:33:47 -08001952 * The previous request may have put cruft into our output stream; we
1953 * should clear it out before making our next request.
Jeff King6d052d72013-04-05 18:14:06 -04001954 */
Masaya Suzukie6cf87b2019-01-10 11:33:47 -08001955 switch (target) {
1956 case HTTP_REQUEST_STRBUF:
1957 strbuf_reset(result);
1958 break;
1959 case HTTP_REQUEST_FILE:
1960 if (fflush(result)) {
1961 error_errno("unable to flush a file");
1962 return HTTP_START_FAILED;
Jeff King6d052d72013-04-05 18:14:06 -04001963 }
Masaya Suzukie6cf87b2019-01-10 11:33:47 -08001964 rewind(result);
1965 if (ftruncate(fileno(result), 0) < 0) {
1966 error_errno("unable to truncate a file");
1967 return HTTP_START_FAILED;
1968 }
1969 break;
1970 default:
1971 BUG("Unknown http_request target");
Jeff King6d052d72013-04-05 18:14:06 -04001972 }
Jeff King2501aff2013-09-28 04:31:45 -04001973
1974 credential_fill(&http_auth);
1975
Jeff King1bbcc222013-09-28 04:31:23 -04001976 return http_request(url, result, target, options);
Jeff King8d677ed2011-07-18 03:50:14 -04001977}
1978
Shawn Pearce4656bf42013-01-31 13:02:07 -08001979int http_get_strbuf(const char *url,
Jeff King1bbcc222013-09-28 04:31:23 -04001980 struct strbuf *result,
1981 struct http_get_options *options)
Mike Hommeye929cd22009-06-06 16:43:53 +08001982{
Jeff King1bbcc222013-09-28 04:31:23 -04001983 return http_request_reauth(url, result, HTTP_REQUEST_STRBUF, options);
Mike Hommeye929cd22009-06-06 16:43:53 +08001984}
1985
Junio C Hamano83e41e22010-01-11 22:26:08 -08001986/*
Jim Meyeringa7793a72012-03-28 10:41:54 +02001987 * Downloads a URL and stores the result in the given file.
Junio C Hamano83e41e22010-01-11 22:26:08 -08001988 *
1989 * If a previous interrupted download is detected (i.e. a previous temporary
1990 * file is still around) the download is resumed.
1991 */
Derrick Stoleec1d024b2022-05-16 20:11:02 +00001992int http_get_file(const char *url, const char *filename,
1993 struct http_get_options *options)
Mike Hommeye929cd22009-06-06 16:43:53 +08001994{
1995 int ret;
1996 struct strbuf tmpfile = STRBUF_INIT;
1997 FILE *result;
1998
1999 strbuf_addf(&tmpfile, "%s.temp", filename);
2000 result = fopen(tmpfile.buf, "a");
Jeff King3d1fb762013-09-28 04:31:00 -04002001 if (!result) {
Mike Hommeye929cd22009-06-06 16:43:53 +08002002 error("Unable to open local file %s", tmpfile.buf);
2003 ret = HTTP_ERROR;
2004 goto cleanup;
2005 }
2006
Jeff King1bbcc222013-09-28 04:31:23 -04002007 ret = http_request_reauth(url, result, HTTP_REQUEST_FILE, options);
Mike Hommeye929cd22009-06-06 16:43:53 +08002008 fclose(result);
2009
Junio C Hamanocb5add52015-08-07 14:40:24 -07002010 if (ret == HTTP_OK && finalize_object_file(tmpfile.buf, filename))
Mike Hommeye929cd22009-06-06 16:43:53 +08002011 ret = HTTP_ERROR;
2012cleanup:
2013 strbuf_release(&tmpfile);
2014 return ret;
2015}
2016
Daniel Barkalowc13b2632008-04-26 15:53:09 -04002017int http_fetch_ref(const char *base, struct ref *ref)
Mike Hommeyd7e92802007-12-11 00:08:25 +01002018{
Jeff King1bbcc222013-09-28 04:31:23 -04002019 struct http_get_options options = {0};
Mike Hommeyd7e92802007-12-11 00:08:25 +01002020 char *url;
2021 struct strbuf buffer = STRBUF_INIT;
Mike Hommey0d5896e2009-06-06 16:43:55 +08002022 int ret = -1;
Mike Hommeyd7e92802007-12-11 00:08:25 +01002023
Jeff King1bbcc222013-09-28 04:31:23 -04002024 options.no_cache = 1;
2025
Daniel Barkalowc13b2632008-04-26 15:53:09 -04002026 url = quote_ref_url(base, ref->name);
Jeff King1bbcc222013-09-28 04:31:23 -04002027 if (http_get_strbuf(url, &buffer, &options) == HTTP_OK) {
Mike Hommey0d5896e2009-06-06 16:43:55 +08002028 strbuf_rtrim(&buffer);
brian m. carlsonae041a02019-02-19 00:05:13 +00002029 if (buffer.len == the_hash_algo->hexsz)
brian m. carlsonf4e54d02015-11-10 02:22:20 +00002030 ret = get_oid_hex(buffer.buf, &ref->old_oid);
Christian Couder59556542013-11-30 21:55:40 +01002031 else if (starts_with(buffer.buf, "ref: ")) {
Mike Hommey0d5896e2009-06-06 16:43:55 +08002032 ref->symref = xstrdup(buffer.buf + 5);
2033 ret = 0;
Mike Hommeyd7e92802007-12-11 00:08:25 +01002034 }
Mike Hommeyd7e92802007-12-11 00:08:25 +01002035 }
2036
2037 strbuf_release(&buffer);
2038 free(url);
2039 return ret;
2040}
Tay Ray Chuanb8caac22009-06-06 16:43:59 +08002041
2042/* Helpers for fetching packs */
brian m. carlson05dfc7c2019-02-19 00:05:15 +00002043static char *fetch_pack_index(unsigned char *hash, const char *base_url)
Tay Ray Chuanb8caac22009-06-06 16:43:59 +08002044{
Shawn O. Pearce750ef422010-04-19 07:23:10 -07002045 char *url, *tmp;
Tay Ray Chuanb8caac22009-06-06 16:43:59 +08002046 struct strbuf buf = STRBUF_INIT;
Tay Ray Chuanb8caac22009-06-06 16:43:59 +08002047
Tay Ray Chuanb8caac22009-06-06 16:43:59 +08002048 if (http_is_verbose)
brian m. carlson05dfc7c2019-02-19 00:05:15 +00002049 fprintf(stderr, "Getting index for pack %s\n", hash_to_hex(hash));
Tay Ray Chuanb8caac22009-06-06 16:43:59 +08002050
2051 end_url_with_slash(&buf, base_url);
brian m. carlson05dfc7c2019-02-19 00:05:15 +00002052 strbuf_addf(&buf, "objects/pack/pack-%s.idx", hash_to_hex(hash));
Tay Ray Chuanb8caac22009-06-06 16:43:59 +08002053 url = strbuf_detach(&buf, NULL);
2054
brian m. carlson05dfc7c2019-02-19 00:05:15 +00002055 strbuf_addf(&buf, "%s.temp", sha1_pack_index_name(hash));
Shawn O. Pearce750ef422010-04-19 07:23:10 -07002056 tmp = strbuf_detach(&buf, NULL);
Tay Ray Chuanb8caac22009-06-06 16:43:59 +08002057
Ramsay Jones70900ed2013-10-24 21:17:19 +01002058 if (http_get_file(url, tmp, NULL) != HTTP_OK) {
Pete Wyckoff82247e92012-04-29 20:28:45 -04002059 error("Unable to get pack index %s", url);
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +00002060 FREE_AND_NULL(tmp);
Shawn O. Pearce750ef422010-04-19 07:23:10 -07002061 }
2062
Tay Ray Chuanb8caac22009-06-06 16:43:59 +08002063 free(url);
Shawn O. Pearce750ef422010-04-19 07:23:10 -07002064 return tmp;
Tay Ray Chuanb8caac22009-06-06 16:43:59 +08002065}
2066
2067static int fetch_and_setup_pack_index(struct packed_git **packs_head,
2068 unsigned char *sha1, const char *base_url)
2069{
2070 struct packed_git *new_pack;
Shawn O. Pearce750ef422010-04-19 07:23:10 -07002071 char *tmp_idx = NULL;
2072 int ret;
Tay Ray Chuanb8caac22009-06-06 16:43:59 +08002073
Shawn O. Pearce750ef422010-04-19 07:23:10 -07002074 if (has_pack_index(sha1)) {
Jeff King8b9c2dd2015-01-27 15:02:27 -05002075 new_pack = parse_pack_index(sha1, sha1_pack_index_name(sha1));
Shawn O. Pearce750ef422010-04-19 07:23:10 -07002076 if (!new_pack)
2077 return -1; /* parse_pack_index() already issued error message */
2078 goto add_pack;
2079 }
2080
2081 tmp_idx = fetch_pack_index(sha1, base_url);
2082 if (!tmp_idx)
Tay Ray Chuanb8caac22009-06-06 16:43:59 +08002083 return -1;
2084
Shawn O. Pearce750ef422010-04-19 07:23:10 -07002085 new_pack = parse_pack_index(sha1, tmp_idx);
2086 if (!new_pack) {
2087 unlink(tmp_idx);
2088 free(tmp_idx);
2089
Tay Ray Chuanb8caac22009-06-06 16:43:59 +08002090 return -1; /* parse_pack_index() already issued error message */
Shawn O. Pearce750ef422010-04-19 07:23:10 -07002091 }
2092
2093 ret = verify_pack_index(new_pack);
2094 if (!ret) {
2095 close_pack_index(new_pack);
Junio C Hamanocb5add52015-08-07 14:40:24 -07002096 ret = finalize_object_file(tmp_idx, sha1_pack_index_name(sha1));
Shawn O. Pearce750ef422010-04-19 07:23:10 -07002097 }
2098 free(tmp_idx);
2099 if (ret)
2100 return -1;
2101
2102add_pack:
Tay Ray Chuanb8caac22009-06-06 16:43:59 +08002103 new_pack->next = *packs_head;
2104 *packs_head = new_pack;
2105 return 0;
2106}
2107
2108int http_get_info_packs(const char *base_url, struct packed_git **packs_head)
2109{
Jeff King1bbcc222013-09-28 04:31:23 -04002110 struct http_get_options options = {0};
Jeff Kingddc56d42019-04-05 14:12:55 -04002111 int ret = 0;
2112 char *url;
2113 const char *data;
Tay Ray Chuanb8caac22009-06-06 16:43:59 +08002114 struct strbuf buf = STRBUF_INIT;
Jeff Kingddc56d42019-04-05 14:12:55 -04002115 struct object_id oid;
Tay Ray Chuanb8caac22009-06-06 16:43:59 +08002116
2117 end_url_with_slash(&buf, base_url);
2118 strbuf_addstr(&buf, "objects/info/packs");
2119 url = strbuf_detach(&buf, NULL);
2120
Jeff King1bbcc222013-09-28 04:31:23 -04002121 options.no_cache = 1;
2122 ret = http_get_strbuf(url, &buf, &options);
Tay Ray Chuanb8caac22009-06-06 16:43:59 +08002123 if (ret != HTTP_OK)
2124 goto cleanup;
2125
2126 data = buf.buf;
Jeff Kingddc56d42019-04-05 14:12:55 -04002127 while (*data) {
2128 if (skip_prefix(data, "P pack-", &data) &&
2129 !parse_oid_hex(data, &oid, &data) &&
2130 skip_prefix(data, ".pack", &data) &&
2131 (*data == '\n' || *data == '\0')) {
2132 fetch_and_setup_pack_index(packs_head, oid.hash, base_url);
2133 } else {
2134 data = strchrnul(data, '\n');
Tay Ray Chuanb8caac22009-06-06 16:43:59 +08002135 }
Jeff Kingddc56d42019-04-05 14:12:55 -04002136 if (*data)
2137 data++; /* skip past newline */
Tay Ray Chuanb8caac22009-06-06 16:43:59 +08002138 }
2139
2140cleanup:
2141 free(url);
2142 return ret;
2143}
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +08002144
2145void release_http_pack_request(struct http_pack_request *preq)
2146{
Junio C Hamanoafe8a902022-05-02 09:50:37 -07002147 if (preq->packfile) {
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +08002148 fclose(preq->packfile);
2149 preq->packfile = NULL;
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +08002150 }
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +08002151 preq->slot = NULL;
Jeff King390c6cb2018-05-18 18:56:37 -07002152 strbuf_release(&preq->tmpfile);
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +08002153 free(preq->url);
Stefan Beller826aed52015-03-20 17:28:06 -07002154 free(preq);
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +08002155}
2156
Jonathan Tan726b25a2021-02-22 11:20:06 -08002157static const char *default_index_pack_args[] =
2158 {"index-pack", "--stdin", NULL};
2159
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +08002160int finish_http_pack_request(struct http_pack_request *preq)
2161{
René Scharfed3180272014-08-19 21:09:35 +02002162 struct child_process ip = CHILD_PROCESS_INIT;
Jonathan Tan9cb3cab2020-06-10 13:57:15 -07002163 int tmpfile_fd;
2164 int ret = 0;
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +08002165
Shawn O. Pearce30652742010-04-17 13:07:37 -07002166 fclose(preq->packfile);
2167 preq->packfile = NULL;
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +08002168
Jonathan Tan9cb3cab2020-06-10 13:57:15 -07002169 tmpfile_fd = xopen(preq->tmpfile.buf, O_RDONLY);
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +08002170
Shawn O. Pearcefe72d422010-04-19 07:23:09 -07002171 ip.git_cmd = 1;
Jonathan Tan9cb3cab2020-06-10 13:57:15 -07002172 ip.in = tmpfile_fd;
Ævar Arnfjörð Bjarmason6def0ff2021-11-25 23:52:18 +01002173 strvec_pushv(&ip.args, preq->index_pack_args ?
2174 preq->index_pack_args :
2175 default_index_pack_args);
Jonathan Tan726b25a2021-02-22 11:20:06 -08002176
2177 if (preq->preserve_index_pack_stdout)
Jonathan Tan8d5d2a32020-06-10 13:57:18 -07002178 ip.out = 0;
Jonathan Tan726b25a2021-02-22 11:20:06 -08002179 else
Jonathan Tan8d5d2a32020-06-10 13:57:18 -07002180 ip.no_stdout = 1;
Shawn O. Pearcefe72d422010-04-19 07:23:09 -07002181
2182 if (run_command(&ip)) {
Jonathan Tan9cb3cab2020-06-10 13:57:15 -07002183 ret = -1;
2184 goto cleanup;
Shawn O. Pearcefe72d422010-04-19 07:23:09 -07002185 }
2186
Jonathan Tan9cb3cab2020-06-10 13:57:15 -07002187cleanup:
2188 close(tmpfile_fd);
2189 unlink(preq->tmpfile.buf);
2190 return ret;
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +08002191}
2192
Jonathan Taneb053492020-06-10 13:57:16 -07002193void http_install_packfile(struct packed_git *p,
2194 struct packed_git **list_to_remove_from)
2195{
2196 struct packed_git **lst = list_to_remove_from;
2197
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +08002198 while (*lst != p)
2199 lst = &((*lst)->next);
2200 *lst = (*lst)->next;
2201
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +08002202 install_packed_git(the_repository, p);
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +08002203}
2204
2205struct http_pack_request *new_http_pack_request(
Jonathan Tan8d5d2a32020-06-10 13:57:18 -07002206 const unsigned char *packed_git_hash, const char *base_url) {
2207
2208 struct strbuf buf = STRBUF_INIT;
2209
2210 end_url_with_slash(&buf, base_url);
2211 strbuf_addf(&buf, "objects/pack/pack-%s.pack",
2212 hash_to_hex(packed_git_hash));
2213 return new_direct_http_pack_request(packed_git_hash,
2214 strbuf_detach(&buf, NULL));
2215}
2216
2217struct http_pack_request *new_direct_http_pack_request(
2218 const unsigned char *packed_git_hash, char *url)
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +08002219{
Jeff Kingf8117f52015-11-02 17:10:27 -05002220 off_t prev_posn = 0;
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +08002221 struct http_pack_request *preq;
2222
René Scharfeca56dad2021-03-13 17:17:22 +01002223 CALLOC_ARRAY(preq, 1);
Jeff King390c6cb2018-05-18 18:56:37 -07002224 strbuf_init(&preq->tmpfile, 0);
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +08002225
Jonathan Tan8d5d2a32020-06-10 13:57:18 -07002226 preq->url = url;
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +08002227
Jonathan Taneb053492020-06-10 13:57:16 -07002228 strbuf_addf(&preq->tmpfile, "%s.temp", sha1_pack_name(packed_git_hash));
Jeff King390c6cb2018-05-18 18:56:37 -07002229 preq->packfile = fopen(preq->tmpfile.buf, "a");
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +08002230 if (!preq->packfile) {
2231 error("Unable to open local file %s for pack",
Jeff King390c6cb2018-05-18 18:56:37 -07002232 preq->tmpfile.buf);
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +08002233 goto abort;
2234 }
2235
2236 preq->slot = get_active_slot();
Ævar Arnfjörð Bjarmason8dda4cb2021-07-30 19:59:46 +02002237 curl_easy_setopt(preq->slot->curl, CURLOPT_WRITEDATA, preq->packfile);
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +08002238 curl_easy_setopt(preq->slot->curl, CURLOPT_WRITEFUNCTION, fwrite);
Tay Ray Chuanbb991902009-08-10 23:59:55 +08002239 curl_easy_setopt(preq->slot->curl, CURLOPT_URL, preq->url);
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +08002240 curl_easy_setopt(preq->slot->curl, CURLOPT_HTTPHEADER,
2241 no_pragma_header);
2242
2243 /*
2244 * If there is data present from a previous transfer attempt,
2245 * resume where it left off
2246 */
Jeff Kingf8117f52015-11-02 17:10:27 -05002247 prev_posn = ftello(preq->packfile);
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +08002248 if (prev_posn>0) {
2249 if (http_is_verbose)
2250 fprintf(stderr,
Ramsay Jones838ecf02015-11-12 00:07:42 +00002251 "Resuming fetch of pack %s at byte %"PRIuMAX"\n",
Jonathan Taneb053492020-06-10 13:57:16 -07002252 hash_to_hex(packed_git_hash),
brian m. carlson538b1522019-02-19 00:05:03 +00002253 (uintmax_t)prev_posn);
David Turner835c4d32015-11-02 16:39:58 -05002254 http_opt_request_remainder(preq->slot->curl, prev_posn);
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +08002255 }
2256
2257 return preq;
2258
2259abort:
Jeff King390c6cb2018-05-18 18:56:37 -07002260 strbuf_release(&preq->tmpfile);
Tay Ray Chuanbb991902009-08-10 23:59:55 +08002261 free(preq->url);
Tay Ray Chuan5ae9ebf2009-08-10 23:55:48 +08002262 free(preq);
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +08002263 return NULL;
2264}
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002265
2266/* Helpers for fetching objects (loose) */
Dan McGeea04ff3e2011-05-03 23:47:27 +08002267static size_t fwrite_sha1_file(char *ptr, size_t eltsize, size_t nmemb,
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002268 void *data)
2269{
2270 unsigned char expn[4096];
2271 size_t size = eltsize * nmemb;
2272 int posn = 0;
Eric Wong17966c02016-07-11 20:51:30 +00002273 struct http_object_request *freq = data;
2274 struct active_request_slot *slot = freq->slot;
2275
2276 if (slot) {
2277 CURLcode c = curl_easy_getinfo(slot->curl, CURLINFO_HTTP_CODE,
2278 &slot->http_code);
2279 if (c != CURLE_OK)
Johannes Schindelin033abf92018-05-02 11:38:39 +02002280 BUG("curl_easy_getinfo for HTTP code failed: %s",
Eric Wong17966c02016-07-11 20:51:30 +00002281 curl_easy_strerror(c));
Jeff King3680f162016-12-06 13:25:39 -05002282 if (slot->http_code >= 300)
Mike Hommey5c3d5a32019-05-08 08:03:54 +09002283 return nmemb;
Eric Wong17966c02016-07-11 20:51:30 +00002284 }
2285
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002286 do {
2287 ssize_t retval = xwrite(freq->localfile,
2288 (char *) ptr + posn, size - posn);
2289 if (retval < 0)
Mike Hommey5c3d5a32019-05-08 08:03:54 +09002290 return posn / eltsize;
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002291 posn += retval;
2292 } while (posn < size);
2293
2294 freq->stream.avail_in = size;
Dan McGeea04ff3e2011-05-03 23:47:27 +08002295 freq->stream.next_in = (void *)ptr;
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002296 do {
2297 freq->stream.next_out = expn;
2298 freq->stream.avail_out = sizeof(expn);
2299 freq->zret = git_inflate(&freq->stream, Z_SYNC_FLUSH);
brian m. carlsoneed0e602019-02-19 00:05:14 +00002300 the_hash_algo->update_fn(&freq->c, expn,
2301 sizeof(expn) - freq->stream.avail_out);
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002302 } while (freq->stream.avail_in && freq->zret == Z_OK);
Mike Hommey5c3d5a32019-05-08 08:03:54 +09002303 return nmemb;
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002304}
2305
2306struct http_object_request *new_http_object_request(const char *base_url,
Jeff Kingf0be0db2019-01-07 03:34:40 -05002307 const struct object_id *oid)
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002308{
Jeff Kingf0be0db2019-01-07 03:34:40 -05002309 char *hex = oid_to_hex(oid);
Christian Couderea657732018-01-17 18:54:54 +01002310 struct strbuf filename = STRBUF_INIT;
Jeff King390c6cb2018-05-18 18:56:37 -07002311 struct strbuf prevfile = STRBUF_INIT;
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002312 int prevlocal;
Dan McGeea04ff3e2011-05-03 23:47:27 +08002313 char prev_buf[PREV_BUF_SIZE];
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002314 ssize_t prev_read = 0;
Jeff Kingf8117f52015-11-02 17:10:27 -05002315 off_t prev_posn = 0;
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002316 struct http_object_request *freq;
2317
René Scharfeca56dad2021-03-13 17:17:22 +01002318 CALLOC_ARRAY(freq, 1);
Jeff King390c6cb2018-05-18 18:56:37 -07002319 strbuf_init(&freq->tmpfile, 0);
Jeff Kingf0be0db2019-01-07 03:34:40 -05002320 oidcpy(&freq->oid, oid);
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002321 freq->localfile = -1;
2322
Jeff King514c5fd2019-01-07 03:35:42 -05002323 loose_object_path(the_repository, &filename, oid);
Jeff King390c6cb2018-05-18 18:56:37 -07002324 strbuf_addf(&freq->tmpfile, "%s.temp", filename.buf);
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002325
Jeff King390c6cb2018-05-18 18:56:37 -07002326 strbuf_addf(&prevfile, "%s.prev", filename.buf);
2327 unlink_or_warn(prevfile.buf);
2328 rename(freq->tmpfile.buf, prevfile.buf);
2329 unlink_or_warn(freq->tmpfile.buf);
Christian Couderea657732018-01-17 18:54:54 +01002330 strbuf_release(&filename);
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002331
2332 if (freq->localfile != -1)
2333 error("fd leakage in start: %d", freq->localfile);
Jeff King390c6cb2018-05-18 18:56:37 -07002334 freq->localfile = open(freq->tmpfile.buf,
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002335 O_WRONLY | O_CREAT | O_EXCL, 0666);
2336 /*
2337 * This could have failed due to the "lazy directory creation";
2338 * try to mkdir the last path component.
2339 */
2340 if (freq->localfile < 0 && errno == ENOENT) {
Jeff King390c6cb2018-05-18 18:56:37 -07002341 char *dir = strrchr(freq->tmpfile.buf, '/');
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002342 if (dir) {
2343 *dir = 0;
Jeff King390c6cb2018-05-18 18:56:37 -07002344 mkdir(freq->tmpfile.buf, 0777);
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002345 *dir = '/';
2346 }
Jeff King390c6cb2018-05-18 18:56:37 -07002347 freq->localfile = open(freq->tmpfile.buf,
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002348 O_WRONLY | O_CREAT | O_EXCL, 0666);
2349 }
2350
2351 if (freq->localfile < 0) {
Jeff King390c6cb2018-05-18 18:56:37 -07002352 error_errno("Couldn't create temporary file %s",
2353 freq->tmpfile.buf);
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002354 goto abort;
2355 }
2356
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002357 git_inflate_init(&freq->stream);
2358
brian m. carlsoneed0e602019-02-19 00:05:14 +00002359 the_hash_algo->init_fn(&freq->c);
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002360
Tay Ray Chuanbb991902009-08-10 23:59:55 +08002361 freq->url = get_remote_object_url(base_url, hex, 0);
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002362
2363 /*
2364 * If a previous temp file is present, process what was already
2365 * fetched.
2366 */
Jeff King390c6cb2018-05-18 18:56:37 -07002367 prevlocal = open(prevfile.buf, O_RDONLY);
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002368 if (prevlocal != -1) {
2369 do {
2370 prev_read = xread(prevlocal, prev_buf, PREV_BUF_SIZE);
2371 if (prev_read>0) {
2372 if (fwrite_sha1_file(prev_buf,
2373 1,
2374 prev_read,
2375 freq) == prev_read) {
2376 prev_posn += prev_read;
2377 } else {
2378 prev_read = -1;
2379 }
2380 }
2381 } while (prev_read > 0);
2382 close(prevlocal);
2383 }
Jeff King390c6cb2018-05-18 18:56:37 -07002384 unlink_or_warn(prevfile.buf);
2385 strbuf_release(&prevfile);
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002386
2387 /*
2388 * Reset inflate/SHA1 if there was an error reading the previous temp
2389 * file; also rewind to the beginning of the local file.
2390 */
2391 if (prev_read == -1) {
2392 memset(&freq->stream, 0, sizeof(freq->stream));
2393 git_inflate_init(&freq->stream);
brian m. carlsoneed0e602019-02-19 00:05:14 +00002394 the_hash_algo->init_fn(&freq->c);
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002395 if (prev_posn>0) {
2396 prev_posn = 0;
2397 lseek(freq->localfile, 0, SEEK_SET);
Jeff Lasslett0c4f21e2009-08-11 00:05:06 +08002398 if (ftruncate(freq->localfile, 0) < 0) {
Nguyễn Thái Ngọc Duyd2e255e2016-05-08 16:47:48 +07002399 error_errno("Couldn't truncate temporary file %s",
Jeff King390c6cb2018-05-18 18:56:37 -07002400 freq->tmpfile.buf);
Jeff Lasslett0c4f21e2009-08-11 00:05:06 +08002401 goto abort;
2402 }
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002403 }
2404 }
2405
2406 freq->slot = get_active_slot();
2407
Ævar Arnfjörð Bjarmason8dda4cb2021-07-30 19:59:46 +02002408 curl_easy_setopt(freq->slot->curl, CURLOPT_WRITEDATA, freq);
Eric Wong17966c02016-07-11 20:51:30 +00002409 curl_easy_setopt(freq->slot->curl, CURLOPT_FAILONERROR, 0);
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002410 curl_easy_setopt(freq->slot->curl, CURLOPT_WRITEFUNCTION, fwrite_sha1_file);
2411 curl_easy_setopt(freq->slot->curl, CURLOPT_ERRORBUFFER, freq->errorstr);
Tay Ray Chuanbb991902009-08-10 23:59:55 +08002412 curl_easy_setopt(freq->slot->curl, CURLOPT_URL, freq->url);
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002413 curl_easy_setopt(freq->slot->curl, CURLOPT_HTTPHEADER, no_pragma_header);
2414
2415 /*
2416 * If we have successfully processed data from a previous fetch
2417 * attempt, only fetch the data we don't already have.
2418 */
2419 if (prev_posn>0) {
2420 if (http_is_verbose)
2421 fprintf(stderr,
Ramsay Jones838ecf02015-11-12 00:07:42 +00002422 "Resuming fetch of object %s at byte %"PRIuMAX"\n",
2423 hex, (uintmax_t)prev_posn);
David Turner835c4d32015-11-02 16:39:58 -05002424 http_opt_request_remainder(freq->slot->curl, prev_posn);
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002425 }
2426
2427 return freq;
2428
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002429abort:
Jeff King390c6cb2018-05-18 18:56:37 -07002430 strbuf_release(&prevfile);
Tay Ray Chuanbb991902009-08-10 23:59:55 +08002431 free(freq->url);
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002432 free(freq);
2433 return NULL;
2434}
2435
2436void process_http_object_request(struct http_object_request *freq)
2437{
Junio C Hamanoafe8a902022-05-02 09:50:37 -07002438 if (!freq->slot)
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002439 return;
2440 freq->curl_result = freq->slot->curl_result;
2441 freq->http_code = freq->slot->http_code;
2442 freq->slot = NULL;
2443}
2444
2445int finish_http_object_request(struct http_object_request *freq)
2446{
2447 struct stat st;
Christian Couderea657732018-01-17 18:54:54 +01002448 struct strbuf filename = STRBUF_INIT;
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002449
2450 close(freq->localfile);
2451 freq->localfile = -1;
2452
2453 process_http_object_request(freq);
2454
2455 if (freq->http_code == 416) {
Thiago Farinabd757c12010-01-03 11:20:30 -05002456 warning("requested range invalid; we may already have all the data.");
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002457 } else if (freq->curl_result != CURLE_OK) {
Jeff King390c6cb2018-05-18 18:56:37 -07002458 if (stat(freq->tmpfile.buf, &st) == 0)
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002459 if (st.st_size == 0)
Jeff King390c6cb2018-05-18 18:56:37 -07002460 unlink_or_warn(freq->tmpfile.buf);
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002461 return -1;
2462 }
2463
2464 git_inflate_end(&freq->stream);
brian m. carlson5951bf42021-04-26 01:02:53 +00002465 the_hash_algo->final_oid_fn(&freq->real_oid, &freq->c);
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002466 if (freq->zret != Z_STREAM_END) {
Jeff King390c6cb2018-05-18 18:56:37 -07002467 unlink_or_warn(freq->tmpfile.buf);
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002468 return -1;
2469 }
Jeff Kingf0be0db2019-01-07 03:34:40 -05002470 if (!oideq(&freq->oid, &freq->real_oid)) {
Jeff King390c6cb2018-05-18 18:56:37 -07002471 unlink_or_warn(freq->tmpfile.buf);
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002472 return -1;
2473 }
Jeff King514c5fd2019-01-07 03:35:42 -05002474 loose_object_path(the_repository, &filename, &freq->oid);
Jeff King390c6cb2018-05-18 18:56:37 -07002475 freq->rename = finalize_object_file(freq->tmpfile.buf, filename.buf);
Christian Couderea657732018-01-17 18:54:54 +01002476 strbuf_release(&filename);
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002477
2478 return freq->rename;
2479}
2480
2481void abort_http_object_request(struct http_object_request *freq)
2482{
Jeff King390c6cb2018-05-18 18:56:37 -07002483 unlink_or_warn(freq->tmpfile.buf);
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002484
2485 release_http_object_request(freq);
2486}
2487
2488void release_http_object_request(struct http_object_request *freq)
2489{
2490 if (freq->localfile != -1) {
2491 close(freq->localfile);
2492 freq->localfile = -1;
2493 }
Ævar Arnfjörð Bjarmasonce528de2018-08-17 13:02:50 +00002494 FREE_AND_NULL(freq->url);
Junio C Hamanoafe8a902022-05-02 09:50:37 -07002495 if (freq->slot) {
Tay Ray Chuan4b9fa0e2009-08-26 20:20:53 +08002496 freq->slot->callback_func = NULL;
2497 freq->slot->callback_data = NULL;
2498 release_active_slot(freq->slot);
2499 freq->slot = NULL;
2500 }
Jeff King390c6cb2018-05-18 18:56:37 -07002501 strbuf_release(&freq->tmpfile);
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002502}