blob: b4bfbceaeb7e58e79cac17f8768a7e36cff3405e [file] [log] [blame]
David Aguilar1c4b6602014-09-14 00:40:45 -07001#include "git-compat-util.h"
Nick Hengeveld29508e12005-11-18 11:02:58 -08002#include "http.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07003#include "config.h"
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +08004#include "pack.h"
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -07005#include "sideband.h"
Shawn O. Pearcefe72d422010-04-19 07:23:09 -07006#include "run-command.h"
Gabriel Coronaf39f72d2010-11-14 02:51:15 +01007#include "url.h"
Kyle J. McKay6a569932013-08-05 13:20:36 -07008#include "urlmatch.h"
Jeff King148bb6a2011-12-10 05:31:21 -05009#include "credential.h"
Jeff King745c7c82012-06-02 15:03:08 -040010#include "version.h"
Jeff King047ec602013-02-20 15:02:45 -050011#include "pkt-line.h"
Jeff King93f7d912015-02-25 22:04:16 -050012#include "gettext.h"
Blake Burkhartf4113ca2015-09-22 18:06:04 -040013#include "transport.h"
Jonathan Tan4f39cd82017-08-18 15:20:16 -070014#include "packfile.h"
Brandon Williams19113a22017-10-16 10:55:29 -070015#include "protocol.h"
Jonathan Tan83411782018-01-18 16:28:01 -080016#include "string-list.h"
Stefan Bellera80d72d2018-03-23 18:20:59 +010017#include "object-store.h"
Nick Hengeveld29508e12005-11-18 11:02:58 -080018
Elia Pinto74c682d2016-05-23 13:44:02 +000019static struct trace_key trace_curl = TRACE_KEY_INIT(CURL);
Jonathan Tan8ba18e62018-01-18 16:28:02 -080020static int trace_curl_data = 1;
Jonathan Tan83411782018-01-18 16:28:01 -080021static struct string_list cookies_to_redact = STRING_LIST_INIT_DUP;
Eric Wongc915f112016-02-03 04:09:14 +000022#if LIBCURL_VERSION_NUM >= 0x070a08
23long int git_curl_ipresolve = CURL_IPRESOLVE_WHATEVER;
24#else
25long int git_curl_ipresolve;
26#endif
Junio C Hamano4251ccb2009-03-09 18:47:29 -070027int active_requests;
Tay Ray Chuane9176742009-06-06 16:43:41 +080028int http_is_verbose;
David Turner37ee6802017-04-11 14:13:57 -040029ssize_t http_post_buffer = 16 * LARGE_PACKET_MAX;
Nick Hengeveld29508e12005-11-18 11:02:58 -080030
Martin Storsjöb8ac9232009-11-27 23:43:08 +080031#if LIBCURL_VERSION_NUM >= 0x070a06
32#define LIBCURL_CAN_HANDLE_AUTH_ANY
33#endif
34
Tay Ray Chuanad75ebe2009-11-27 23:42:26 +080035static int min_curl_sessions = 1;
36static int curl_session_count;
Nick Hengeveld29508e12005-11-18 11:02:58 -080037#ifdef USE_CURL_MULTI
Mike Hommeycc3530e2007-12-09 18:04:57 +010038static int max_requests = -1;
39static CURLM *curlm;
Nick Hengeveld29508e12005-11-18 11:02:58 -080040#endif
41#ifndef NO_CURL_EASY_DUPHANDLE
Mike Hommeycc3530e2007-12-09 18:04:57 +010042static CURL *curl_default;
Nick Hengeveld29508e12005-11-18 11:02:58 -080043#endif
Tay Ray Chuan5424bc52009-06-06 16:44:02 +080044
45#define PREV_BUF_SIZE 4096
Tay Ray Chuan5424bc52009-06-06 16:44:02 +080046
Nick Hengeveld29508e12005-11-18 11:02:58 -080047char curl_errorstr[CURL_ERROR_SIZE];
48
Mike Hommeycc3530e2007-12-09 18:04:57 +010049static int curl_ssl_verify = -1;
Modestas Vainius4bc444e2013-04-07 22:10:39 +030050static int curl_ssl_try;
Junio C Hamano4251ccb2009-03-09 18:47:29 -070051static const char *ssl_cert;
Lars Kellogg-Stedmanf6f2a9e2015-05-08 09:22:15 -040052static const char *ssl_cipherlist;
Elia Pinto01861cb2015-08-14 21:37:43 +020053static const char *ssl_version;
54static struct {
55 const char *name;
56 long ssl_version;
57} sslversions[] = {
58 { "sslv2", CURL_SSLVERSION_SSLv2 },
59 { "sslv3", CURL_SSLVERSION_SSLv3 },
60 { "tlsv1", CURL_SSLVERSION_TLSv1 },
61#if LIBCURL_VERSION_NUM >= 0x072200
62 { "tlsv1.0", CURL_SSLVERSION_TLSv1_0 },
63 { "tlsv1.1", CURL_SSLVERSION_TLSv1_1 },
64 { "tlsv1.2", CURL_SSLVERSION_TLSv1_2 },
65#endif
Loganaden Velvindrond81b6512018-03-29 14:14:18 +040066#if LIBCURL_VERSION_NUM >= 0x073400
67 { "tlsv1.3", CURL_SSLVERSION_TLSv1_3 },
68#endif
Elia Pinto01861cb2015-08-14 21:37:43 +020069};
Mark Lodatoef52aaf2009-06-14 22:39:00 -040070#if LIBCURL_VERSION_NUM >= 0x070903
Junio C Hamano4251ccb2009-03-09 18:47:29 -070071static const char *ssl_key;
Nick Hengeveld29508e12005-11-18 11:02:58 -080072#endif
73#if LIBCURL_VERSION_NUM >= 0x070908
Junio C Hamano4251ccb2009-03-09 18:47:29 -070074static const char *ssl_capath;
Nick Hengeveld29508e12005-11-18 11:02:58 -080075#endif
Ramsay Jonesb8fd6002018-03-14 21:56:06 +000076#if LIBCURL_VERSION_NUM >= 0x071304
77static const char *curl_no_proxy;
78#endif
Christoph Eggeraeff8a62016-02-15 15:04:22 +010079#if LIBCURL_VERSION_NUM >= 0x072c00
80static const char *ssl_pinnedkey;
81#endif
Junio C Hamano4251ccb2009-03-09 18:47:29 -070082static const char *ssl_cainfo;
Mike Hommeycc3530e2007-12-09 18:04:57 +010083static long curl_low_speed_limit = -1;
84static long curl_low_speed_time = -1;
Junio C Hamano4251ccb2009-03-09 18:47:29 -070085static int curl_ftp_no_epsv;
86static const char *curl_http_proxy;
Knut Frankeef976392016-01-26 13:02:47 +000087static const char *http_proxy_authmethod;
88static struct {
89 const char *name;
90 long curlauth_param;
91} proxy_authmethods[] = {
92 { "basic", CURLAUTH_BASIC },
93 { "digest", CURLAUTH_DIGEST },
94 { "negotiate", CURLAUTH_GSSNEGOTIATE },
95 { "ntlm", CURLAUTH_NTLM },
96#ifdef LIBCURL_CAN_HANDLE_AUTH_ANY
97 { "anyauth", CURLAUTH_ANY },
98#endif
99 /*
100 * CURLAUTH_DIGEST_IE has no corresponding command-line option in
101 * curl(1) and is not included in CURLAUTH_ANY, so we leave it out
102 * here, too
103 */
104};
Tom G. Christensendd5df532017-08-11 18:37:34 +0200105#ifdef CURLGSSAPI_DELEGATION_FLAG
Petr Stodulka26a7b232016-09-28 20:01:34 +0200106static const char *curl_deleg;
107static struct {
108 const char *name;
109 long curl_deleg_param;
110} curl_deleg_levels[] = {
111 { "none", CURLGSSAPI_DELEGATION_NONE },
112 { "policy", CURLGSSAPI_DELEGATION_POLICY_FLAG },
113 { "always", CURLGSSAPI_DELEGATION_FLAG },
114};
115#endif
116
Knut Franke372370f2016-01-26 13:02:48 +0000117static struct credential proxy_auth = CREDENTIAL_INIT;
118static const char *curl_proxyuserpwd;
Duncan Brownbcfb95d2011-06-02 16:31:25 -0400119static const char *curl_cookie_file;
Dave Borowitz912b2ac2013-07-23 15:40:17 -0700120static int curl_save_cookies;
Jeff King2501aff2013-09-28 04:31:45 -0400121struct credential http_auth = CREDENTIAL_INIT;
Jeff Kinga4ddbc32011-12-13 19:11:56 -0500122static int http_proactive_auth;
Spencer E. Olsonb1d10582010-08-11 14:40:38 -0600123static const char *user_agent;
Jeff King40a18fc2017-02-25 14:18:31 -0500124static int curl_empty_auth = -1;
Nick Hengeveld29508e12005-11-18 11:02:58 -0800125
Jeff King50d34132016-12-06 13:24:41 -0500126enum http_follow_config http_follow_config = HTTP_FOLLOW_INITIAL;
127
Mark Lodato30dd9162009-05-27 23:16:02 -0400128#if LIBCURL_VERSION_NUM >= 0x071700
129/* Use CURLOPT_KEYPASSWD as is */
130#elif LIBCURL_VERSION_NUM >= 0x070903
131#define CURLOPT_KEYPASSWD CURLOPT_SSLKEYPASSWD
132#else
133#define CURLOPT_KEYPASSWD CURLOPT_SSLCERTPASSWD
134#endif
135
Jeff King148bb6a2011-12-10 05:31:21 -0500136static struct credential cert_auth = CREDENTIAL_INIT;
Mark Lodato30dd9162009-05-27 23:16:02 -0400137static int ssl_cert_password_required;
brian m. carlson4dbe6642015-01-08 00:29:20 +0000138#ifdef LIBCURL_CAN_HANDLE_AUTH_ANY
139static unsigned long http_auth_methods = CURLAUTH_ANY;
Jeff King40a18fc2017-02-25 14:18:31 -0500140static int http_auth_methods_restricted;
141/* Modes for which empty_auth cannot actually help us. */
142static unsigned long empty_auth_useless =
143 CURLAUTH_BASIC
144#ifdef CURLAUTH_DIGEST_IE
145 | CURLAUTH_DIGEST_IE
146#endif
147 | CURLAUTH_DIGEST;
brian m. carlson4dbe6642015-01-08 00:29:20 +0000148#endif
Mark Lodato30dd9162009-05-27 23:16:02 -0400149
Mike Hommeycc3530e2007-12-09 18:04:57 +0100150static struct curl_slist *pragma_header;
Tay Ray Chuan5424bc52009-06-06 16:44:02 +0800151static struct curl_slist *no_pragma_header;
Johannes Schindelin8cb01e22016-04-27 14:20:37 +0200152static struct curl_slist *extra_http_headers;
Tay Ray Chuane9176742009-06-06 16:43:41 +0800153
Junio C Hamano4251ccb2009-03-09 18:47:29 -0700154static struct active_request_slot *active_queue_head;
Nick Hengeveld29508e12005-11-18 11:02:58 -0800155
Yi EungJunf18604b2015-01-28 21:04:37 +0900156static char *cached_accept_language;
157
Dan McGeea04ff3e2011-05-03 23:47:27 +0800158size_t fread_buffer(char *ptr, size_t eltsize, size_t nmemb, void *buffer_)
Nick Hengeveld29508e12005-11-18 11:02:58 -0800159{
160 size_t size = eltsize * nmemb;
Junio C Hamanof444e522008-07-04 00:37:40 -0700161 struct buffer *buffer = buffer_;
162
Mike Hommey028c2972007-12-09 20:30:59 +0100163 if (size > buffer->buf.len - buffer->posn)
164 size = buffer->buf.len - buffer->posn;
165 memcpy(ptr, buffer->buf.buf + buffer->posn, size);
Nick Hengeveld29508e12005-11-18 11:02:58 -0800166 buffer->posn += size;
Mike Hommey028c2972007-12-09 20:30:59 +0100167
Nick Hengeveld29508e12005-11-18 11:02:58 -0800168 return size;
169}
170
Martin Storsjö3944ba02009-04-01 19:48:24 +0300171#ifndef NO_CURL_IOCTL
172curlioerr ioctl_buffer(CURL *handle, int cmd, void *clientp)
173{
174 struct buffer *buffer = clientp;
175
176 switch (cmd) {
177 case CURLIOCMD_NOP:
178 return CURLIOE_OK;
179
180 case CURLIOCMD_RESTARTREAD:
181 buffer->posn = 0;
182 return CURLIOE_OK;
183
184 default:
185 return CURLIOE_UNKNOWNCMD;
186 }
187}
188#endif
189
Dan McGeea04ff3e2011-05-03 23:47:27 +0800190size_t fwrite_buffer(char *ptr, size_t eltsize, size_t nmemb, void *buffer_)
Nick Hengeveld29508e12005-11-18 11:02:58 -0800191{
192 size_t size = eltsize * nmemb;
Junio C Hamanof444e522008-07-04 00:37:40 -0700193 struct strbuf *buffer = buffer_;
194
Mike Hommey028c2972007-12-09 20:30:59 +0100195 strbuf_add(buffer, ptr, size);
Nick Hengeveld29508e12005-11-18 11:02:58 -0800196 return size;
197}
198
Dan McGeea04ff3e2011-05-03 23:47:27 +0800199size_t fwrite_null(char *ptr, size_t eltsize, size_t nmemb, void *strbuf)
Nick Hengeveld29508e12005-11-18 11:02:58 -0800200{
Nick Hengeveld29508e12005-11-18 11:02:58 -0800201 return eltsize * nmemb;
202}
203
Junio C Hamanob90a3d72015-01-14 15:40:46 -0800204static void closedown_active_slot(struct active_request_slot *slot)
205{
206 active_requests--;
207 slot->in_use = 0;
208}
209
210static void finish_active_slot(struct active_request_slot *slot)
211{
212 closedown_active_slot(slot);
213 curl_easy_getinfo(slot->curl, CURLINFO_HTTP_CODE, &slot->http_code);
214
215 if (slot->finished != NULL)
216 (*slot->finished) = 1;
217
218 /* Store slot results so they can be read after the slot is reused */
219 if (slot->results != NULL) {
220 slot->results->curl_result = slot->curl_result;
221 slot->results->http_code = slot->http_code;
222#if LIBCURL_VERSION_NUM >= 0x070a08
223 curl_easy_getinfo(slot->curl, CURLINFO_HTTPAUTH_AVAIL,
224 &slot->results->auth_avail);
225#else
226 slot->results->auth_avail = 0;
227#endif
Knut Franke372370f2016-01-26 13:02:48 +0000228
229 curl_easy_getinfo(slot->curl, CURLINFO_HTTP_CONNECTCODE,
230 &slot->results->http_connectcode);
Junio C Hamanob90a3d72015-01-14 15:40:46 -0800231 }
232
233 /* Run callback if appropriate */
234 if (slot->callback_func != NULL)
235 slot->callback_func(slot->callback_data);
236}
237
Eric Wongd8b6b842016-09-13 00:25:56 +0000238static void xmulti_remove_handle(struct active_request_slot *slot)
239{
240#ifdef USE_CURL_MULTI
241 curl_multi_remove_handle(curlm, slot->curl);
242#endif
243}
244
Nick Hengeveld29508e12005-11-18 11:02:58 -0800245#ifdef USE_CURL_MULTI
246static void process_curl_messages(void)
247{
248 int num_messages;
249 struct active_request_slot *slot;
250 CURLMsg *curl_message = curl_multi_info_read(curlm, &num_messages);
251
252 while (curl_message != NULL) {
253 if (curl_message->msg == CURLMSG_DONE) {
254 int curl_result = curl_message->data.result;
255 slot = active_queue_head;
256 while (slot != NULL &&
257 slot->curl != curl_message->easy_handle)
258 slot = slot->next;
259 if (slot != NULL) {
Eric Wongd8b6b842016-09-13 00:25:56 +0000260 xmulti_remove_handle(slot);
Nick Hengeveld29508e12005-11-18 11:02:58 -0800261 slot->curl_result = curl_result;
262 finish_active_slot(slot);
263 } else {
264 fprintf(stderr, "Received DONE message for unknown request!\n");
265 }
266 } else {
267 fprintf(stderr, "Unknown CURL message received: %d\n",
268 (int)curl_message->msg);
269 }
270 curl_message = curl_multi_info_read(curlm, &num_messages);
271 }
272}
273#endif
274
Johannes Schindelinef90d6d2008-05-14 18:46:53 +0100275static int http_options(const char *var, const char *value, void *cb)
Nick Hengeveld29508e12005-11-18 11:02:58 -0800276{
277 if (!strcmp("http.sslverify", var)) {
Junio C Hamano7059cd92009-03-09 19:00:30 -0700278 curl_ssl_verify = git_config_bool(var, value);
Nick Hengeveld29508e12005-11-18 11:02:58 -0800279 return 0;
280 }
Lars Kellogg-Stedmanf6f2a9e2015-05-08 09:22:15 -0400281 if (!strcmp("http.sslcipherlist", var))
282 return git_config_string(&ssl_cipherlist, var, value);
Elia Pinto01861cb2015-08-14 21:37:43 +0200283 if (!strcmp("http.sslversion", var))
284 return git_config_string(&ssl_version, var, value);
Junio C Hamano7059cd92009-03-09 19:00:30 -0700285 if (!strcmp("http.sslcert", var))
Junio C Hamano8d154962017-07-20 13:30:52 -0700286 return git_config_pathname(&ssl_cert, var, value);
Mark Lodatoef52aaf2009-06-14 22:39:00 -0400287#if LIBCURL_VERSION_NUM >= 0x070903
Junio C Hamano7059cd92009-03-09 19:00:30 -0700288 if (!strcmp("http.sslkey", var))
Junio C Hamano8d154962017-07-20 13:30:52 -0700289 return git_config_pathname(&ssl_key, var, value);
Nick Hengeveld29508e12005-11-18 11:02:58 -0800290#endif
291#if LIBCURL_VERSION_NUM >= 0x070908
Junio C Hamano7059cd92009-03-09 19:00:30 -0700292 if (!strcmp("http.sslcapath", var))
Charles Baileybf9acba2015-11-23 12:02:40 +0000293 return git_config_pathname(&ssl_capath, var, value);
Nick Hengeveld29508e12005-11-18 11:02:58 -0800294#endif
Junio C Hamano7059cd92009-03-09 19:00:30 -0700295 if (!strcmp("http.sslcainfo", var))
Charles Baileybf9acba2015-11-23 12:02:40 +0000296 return git_config_pathname(&ssl_cainfo, var, value);
Mark Lodato754ae192009-05-27 23:16:03 -0400297 if (!strcmp("http.sslcertpasswordprotected", var)) {
Junio C Hamano3f4ccd22013-07-12 11:52:47 -0700298 ssl_cert_password_required = git_config_bool(var, value);
Mark Lodato754ae192009-05-27 23:16:03 -0400299 return 0;
300 }
Modestas Vainius4bc444e2013-04-07 22:10:39 +0300301 if (!strcmp("http.ssltry", var)) {
302 curl_ssl_try = git_config_bool(var, value);
303 return 0;
304 }
Tay Ray Chuanad75ebe2009-11-27 23:42:26 +0800305 if (!strcmp("http.minsessions", var)) {
306 min_curl_sessions = git_config_int(var, value);
307#ifndef USE_CURL_MULTI
308 if (min_curl_sessions > 1)
309 min_curl_sessions = 1;
310#endif
311 return 0;
312 }
Junio C Hamanoa6080a02007-06-07 00:04:01 -0700313#ifdef USE_CURL_MULTI
Nick Hengeveld29508e12005-11-18 11:02:58 -0800314 if (!strcmp("http.maxrequests", var)) {
Junio C Hamano7059cd92009-03-09 19:00:30 -0700315 max_requests = git_config_int(var, value);
Nick Hengeveld29508e12005-11-18 11:02:58 -0800316 return 0;
317 }
318#endif
Nick Hengeveld29508e12005-11-18 11:02:58 -0800319 if (!strcmp("http.lowspeedlimit", var)) {
Junio C Hamano7059cd92009-03-09 19:00:30 -0700320 curl_low_speed_limit = (long)git_config_int(var, value);
Nick Hengeveld29508e12005-11-18 11:02:58 -0800321 return 0;
322 }
323 if (!strcmp("http.lowspeedtime", var)) {
Junio C Hamano7059cd92009-03-09 19:00:30 -0700324 curl_low_speed_time = (long)git_config_int(var, value);
Nick Hengeveld29508e12005-11-18 11:02:58 -0800325 return 0;
326 }
327
Sasha Khapyorsky3ea099d2006-09-29 03:10:44 +0300328 if (!strcmp("http.noepsv", var)) {
329 curl_ftp_no_epsv = git_config_bool(var, value);
330 return 0;
331 }
Junio C Hamano7059cd92009-03-09 19:00:30 -0700332 if (!strcmp("http.proxy", var))
333 return git_config_string(&curl_http_proxy, var, value);
Sasha Khapyorsky3ea099d2006-09-29 03:10:44 +0300334
Knut Frankeef976392016-01-26 13:02:47 +0000335 if (!strcmp("http.proxyauthmethod", var))
336 return git_config_string(&http_proxy_authmethod, var, value);
337
Duncan Brownbcfb95d2011-06-02 16:31:25 -0400338 if (!strcmp("http.cookiefile", var))
Brian Norrise5a39ad2016-05-04 11:42:15 -0700339 return git_config_pathname(&curl_cookie_file, var, value);
Dave Borowitz912b2ac2013-07-23 15:40:17 -0700340 if (!strcmp("http.savecookies", var)) {
341 curl_save_cookies = git_config_bool(var, value);
342 return 0;
343 }
Duncan Brownbcfb95d2011-06-02 16:31:25 -0400344
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700345 if (!strcmp("http.postbuffer", var)) {
David Turner37ee6802017-04-11 14:13:57 -0400346 http_post_buffer = git_config_ssize_t(var, value);
347 if (http_post_buffer < 0)
348 warning(_("negative value for http.postbuffer; defaulting to %d"), LARGE_PACKET_MAX);
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700349 if (http_post_buffer < LARGE_PACKET_MAX)
350 http_post_buffer = LARGE_PACKET_MAX;
351 return 0;
352 }
353
Spencer E. Olsonb1d10582010-08-11 14:40:38 -0600354 if (!strcmp("http.useragent", var))
355 return git_config_string(&user_agent, var, value);
356
brian m. carlson121061f2016-02-15 18:44:46 +0000357 if (!strcmp("http.emptyauth", var)) {
Jeff King40a18fc2017-02-25 14:18:31 -0500358 if (value && !strcmp("auto", value))
359 curl_empty_auth = -1;
360 else
361 curl_empty_auth = git_config_bool(var, value);
brian m. carlson121061f2016-02-15 18:44:46 +0000362 return 0;
363 }
364
Petr Stodulka26a7b232016-09-28 20:01:34 +0200365 if (!strcmp("http.delegation", var)) {
Tom G. Christensendd5df532017-08-11 18:37:34 +0200366#ifdef CURLGSSAPI_DELEGATION_FLAG
Petr Stodulka26a7b232016-09-28 20:01:34 +0200367 return git_config_string(&curl_deleg, var, value);
368#else
369 warning(_("Delegation control is not supported with cURL < 7.22.0"));
370 return 0;
371#endif
372 }
373
Christoph Eggeraeff8a62016-02-15 15:04:22 +0100374 if (!strcmp("http.pinnedpubkey", var)) {
375#if LIBCURL_VERSION_NUM >= 0x072c00
376 return git_config_pathname(&ssl_pinnedkey, var, value);
377#else
378 warning(_("Public key pinning not supported with cURL < 7.44.0"));
379 return 0;
380#endif
381 }
Junio C Hamanoe79112d2016-02-24 13:25:58 -0800382
Johannes Schindelin8cb01e22016-04-27 14:20:37 +0200383 if (!strcmp("http.extraheader", var)) {
384 if (!value) {
385 return config_error_nonbool(var);
386 } else if (!*value) {
387 curl_slist_free_all(extra_http_headers);
388 extra_http_headers = NULL;
389 } else {
390 extra_http_headers =
391 curl_slist_append(extra_http_headers, value);
392 }
393 return 0;
394 }
395
Jeff King50d34132016-12-06 13:24:41 -0500396 if (!strcmp("http.followredirects", var)) {
397 if (value && !strcmp(value, "initial"))
398 http_follow_config = HTTP_FOLLOW_INITIAL;
399 else if (git_config_bool(var, value))
400 http_follow_config = HTTP_FOLLOW_ALWAYS;
401 else
402 http_follow_config = HTTP_FOLLOW_NONE;
403 return 0;
404 }
405
Nick Hengeveld29508e12005-11-18 11:02:58 -0800406 /* Fall back on the default ones */
Johannes Schindelinef90d6d2008-05-14 18:46:53 +0100407 return git_default_config(var, value, cb);
Nick Hengeveld29508e12005-11-18 11:02:58 -0800408}
409
Jeff King40a18fc2017-02-25 14:18:31 -0500410static int curl_empty_auth_enabled(void)
411{
412 if (curl_empty_auth >= 0)
413 return curl_empty_auth;
414
415#ifndef LIBCURL_CAN_HANDLE_AUTH_ANY
416 /*
417 * Our libcurl is too old to do AUTH_ANY in the first place;
418 * just default to turning the feature off.
419 */
420#else
421 /*
422 * In the automatic case, kick in the empty-auth
423 * hack as long as we would potentially try some
424 * method more exotic than "Basic" or "Digest".
425 *
426 * But only do this when this is our second or
427 * subsequent request, as by then we know what
428 * methods are available.
429 */
430 if (http_auth_methods_restricted &&
431 (http_auth_methods & ~empty_auth_useless))
432 return 1;
433#endif
434 return 0;
435}
436
Junio C Hamanoc33976c2009-03-09 23:34:25 -0700437static void init_curl_http_auth(CURL *result)
438{
David Turner5275c302016-10-04 10:53:52 -0400439 if (!http_auth.username || !*http_auth.username) {
Jeff King40a18fc2017-02-25 14:18:31 -0500440 if (curl_empty_auth_enabled())
brian m. carlson121061f2016-02-15 18:44:46 +0000441 curl_easy_setopt(result, CURLOPT_USERPWD, ":");
Jeff King6f4c3472012-04-13 02:19:25 -0400442 return;
brian m. carlson121061f2016-02-15 18:44:46 +0000443 }
Jeff King6f4c3472012-04-13 02:19:25 -0400444
445 credential_fill(&http_auth);
446
447#if LIBCURL_VERSION_NUM >= 0x071301
448 curl_easy_setopt(result, CURLOPT_USERNAME, http_auth.username);
449 curl_easy_setopt(result, CURLOPT_PASSWORD, http_auth.password);
450#else
451 {
Jeff Kingaa0834a2012-04-13 02:18:35 -0400452 static struct strbuf up = STRBUF_INIT;
Brandon Caseya94cf2c2013-06-18 19:43:49 -0700453 /*
454 * Note that we assume we only ever have a single set of
455 * credentials in a given program run, so we do not have
456 * to worry about updating this buffer, only setting its
457 * initial value.
458 */
459 if (!up.len)
460 strbuf_addf(&up, "%s:%s",
461 http_auth.username, http_auth.password);
Jeff Kingaa0834a2012-04-13 02:18:35 -0400462 curl_easy_setopt(result, CURLOPT_USERPWD, up.buf);
Junio C Hamanoc33976c2009-03-09 23:34:25 -0700463 }
Jeff King6f4c3472012-04-13 02:19:25 -0400464#endif
Junio C Hamanoc33976c2009-03-09 23:34:25 -0700465}
466
Knut Frankeef976392016-01-26 13:02:47 +0000467/* *var must be free-able */
468static void var_override(const char **var, char *value)
469{
470 if (value) {
471 free((void *)*var);
472 *var = xstrdup(value);
473 }
474}
475
Knut Franke372370f2016-01-26 13:02:48 +0000476static void set_proxyauth_name_password(CURL *result)
477{
478#if LIBCURL_VERSION_NUM >= 0x071301
479 curl_easy_setopt(result, CURLOPT_PROXYUSERNAME,
480 proxy_auth.username);
481 curl_easy_setopt(result, CURLOPT_PROXYPASSWORD,
482 proxy_auth.password);
483#else
484 struct strbuf s = STRBUF_INIT;
485
486 strbuf_addstr_urlencode(&s, proxy_auth.username, 1);
487 strbuf_addch(&s, ':');
488 strbuf_addstr_urlencode(&s, proxy_auth.password, 1);
489 curl_proxyuserpwd = strbuf_detach(&s, NULL);
490 curl_easy_setopt(result, CURLOPT_PROXYUSERPWD, curl_proxyuserpwd);
491#endif
492}
493
Knut Frankeef976392016-01-26 13:02:47 +0000494static void init_curl_proxy_auth(CURL *result)
495{
Knut Franke372370f2016-01-26 13:02:48 +0000496 if (proxy_auth.username) {
497 if (!proxy_auth.password)
498 credential_fill(&proxy_auth);
499 set_proxyauth_name_password(result);
500 }
501
Knut Frankeef976392016-01-26 13:02:47 +0000502 var_override(&http_proxy_authmethod, getenv("GIT_HTTP_PROXY_AUTHMETHOD"));
503
504#if LIBCURL_VERSION_NUM >= 0x070a07 /* CURLOPT_PROXYAUTH and CURLAUTH_ANY */
505 if (http_proxy_authmethod) {
506 int i;
507 for (i = 0; i < ARRAY_SIZE(proxy_authmethods); i++) {
508 if (!strcmp(http_proxy_authmethod, proxy_authmethods[i].name)) {
509 curl_easy_setopt(result, CURLOPT_PROXYAUTH,
510 proxy_authmethods[i].curlauth_param);
511 break;
512 }
513 }
514 if (i == ARRAY_SIZE(proxy_authmethods)) {
515 warning("unsupported proxy authentication method %s: using anyauth",
516 http_proxy_authmethod);
517 curl_easy_setopt(result, CURLOPT_PROXYAUTH, CURLAUTH_ANY);
518 }
519 }
520 else
521 curl_easy_setopt(result, CURLOPT_PROXYAUTH, CURLAUTH_ANY);
522#endif
523}
524
Mark Lodato30dd9162009-05-27 23:16:02 -0400525static int has_cert_password(void)
526{
Mark Lodato30dd9162009-05-27 23:16:02 -0400527 if (ssl_cert == NULL || ssl_cert_password_required != 1)
528 return 0;
Jeff King148bb6a2011-12-10 05:31:21 -0500529 if (!cert_auth.password) {
530 cert_auth.protocol = xstrdup("cert");
Rene Bredlau75e9a402012-12-21 17:31:19 +0100531 cert_auth.username = xstrdup("");
Jeff King148bb6a2011-12-10 05:31:21 -0500532 cert_auth.path = xstrdup(ssl_cert);
533 credential_fill(&cert_auth);
534 }
535 return 1;
Mark Lodato30dd9162009-05-27 23:16:02 -0400536}
537
Jeff King47ce1152013-10-14 20:06:14 -0400538#if LIBCURL_VERSION_NUM >= 0x071900
539static void set_curl_keepalive(CURL *c)
540{
541 curl_easy_setopt(c, CURLOPT_TCP_KEEPALIVE, 1);
542}
543
544#elif LIBCURL_VERSION_NUM >= 0x071000
Eric Wonga15d0692013-10-12 22:29:40 +0000545static int sockopt_callback(void *client, curl_socket_t fd, curlsocktype type)
546{
547 int ka = 1;
548 int rc;
549 socklen_t len = (socklen_t)sizeof(ka);
550
551 if (type != CURLSOCKTYPE_IPCXN)
552 return 0;
553
554 rc = setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (void *)&ka, len);
555 if (rc < 0)
Nguyễn Thái Ngọc Duyd2e255e2016-05-08 16:47:48 +0700556 warning_errno("unable to set SO_KEEPALIVE on socket");
Eric Wonga15d0692013-10-12 22:29:40 +0000557
558 return 0; /* CURL_SOCKOPT_OK only exists since curl 7.21.5 */
559}
560
Jeff King47ce1152013-10-14 20:06:14 -0400561static void set_curl_keepalive(CURL *c)
562{
563 curl_easy_setopt(c, CURLOPT_SOCKOPTFUNCTION, sockopt_callback);
564}
565
566#else
567static void set_curl_keepalive(CURL *c)
568{
569 /* not supported on older curl versions */
570}
571#endif
572
Elia Pinto74c682d2016-05-23 13:44:02 +0000573static void redact_sensitive_header(struct strbuf *header)
574{
575 const char *sensitive_header;
576
577 if (skip_prefix(header->buf, "Authorization:", &sensitive_header) ||
578 skip_prefix(header->buf, "Proxy-Authorization:", &sensitive_header)) {
579 /* The first token is the type, which is OK to log */
580 while (isspace(*sensitive_header))
581 sensitive_header++;
582 while (*sensitive_header && !isspace(*sensitive_header))
583 sensitive_header++;
584 /* Everything else is opaque and possibly sensitive */
585 strbuf_setlen(header, sensitive_header - header->buf);
586 strbuf_addstr(header, " <redacted>");
Jonathan Tan83411782018-01-18 16:28:01 -0800587 } else if (cookies_to_redact.nr &&
588 skip_prefix(header->buf, "Cookie:", &sensitive_header)) {
589 struct strbuf redacted_header = STRBUF_INIT;
590 char *cookie;
591
592 while (isspace(*sensitive_header))
593 sensitive_header++;
594
595 /*
596 * The contents of header starting from sensitive_header will
597 * subsequently be overridden, so it is fine to mutate this
598 * string (hence the assignment to "char *").
599 */
600 cookie = (char *) sensitive_header;
601
602 while (cookie) {
603 char *equals;
604 char *semicolon = strstr(cookie, "; ");
605 if (semicolon)
606 *semicolon = 0;
607 equals = strchrnul(cookie, '=');
608 if (!equals) {
609 /* invalid cookie, just append and continue */
610 strbuf_addstr(&redacted_header, cookie);
611 continue;
612 }
613 *equals = 0; /* temporarily set to NUL for lookup */
614 if (string_list_lookup(&cookies_to_redact, cookie)) {
615 strbuf_addstr(&redacted_header, cookie);
616 strbuf_addstr(&redacted_header, "=<redacted>");
617 } else {
618 *equals = '=';
619 strbuf_addstr(&redacted_header, cookie);
620 }
621 if (semicolon) {
622 /*
623 * There are more cookies. (Or, for some
624 * reason, the input string ends in "; ".)
625 */
626 strbuf_addstr(&redacted_header, "; ");
627 cookie = semicolon + strlen("; ");
628 } else {
629 cookie = NULL;
630 }
631 }
632
633 strbuf_setlen(header, sensitive_header - header->buf);
634 strbuf_addbuf(header, &redacted_header);
Elia Pinto74c682d2016-05-23 13:44:02 +0000635 }
636}
637
638static void curl_dump_header(const char *text, unsigned char *ptr, size_t size, int hide_sensitive_header)
639{
640 struct strbuf out = STRBUF_INIT;
641 struct strbuf **headers, **header;
642
643 strbuf_addf(&out, "%s, %10.10ld bytes (0x%8.8lx)\n",
644 text, (long)size, (long)size);
645 trace_strbuf(&trace_curl, &out);
646 strbuf_reset(&out);
647 strbuf_add(&out, ptr, size);
648 headers = strbuf_split_max(&out, '\n', 0);
649
650 for (header = headers; *header; header++) {
651 if (hide_sensitive_header)
652 redact_sensitive_header(*header);
653 strbuf_insert((*header), 0, text, strlen(text));
654 strbuf_insert((*header), strlen(text), ": ", 2);
655 strbuf_rtrim((*header));
656 strbuf_addch((*header), '\n');
657 trace_strbuf(&trace_curl, (*header));
658 }
659 strbuf_list_free(headers);
660 strbuf_release(&out);
661}
662
663static void curl_dump_data(const char *text, unsigned char *ptr, size_t size)
664{
665 size_t i;
666 struct strbuf out = STRBUF_INIT;
667 unsigned int width = 60;
668
669 strbuf_addf(&out, "%s, %10.10ld bytes (0x%8.8lx)\n",
670 text, (long)size, (long)size);
671 trace_strbuf(&trace_curl, &out);
672
673 for (i = 0; i < size; i += width) {
674 size_t w;
675
676 strbuf_reset(&out);
677 strbuf_addf(&out, "%s: ", text);
678 for (w = 0; (w < width) && (i + w < size); w++) {
679 unsigned char ch = ptr[i + w];
680
681 strbuf_addch(&out,
682 (ch >= 0x20) && (ch < 0x80)
683 ? ch : '.');
684 }
685 strbuf_addch(&out, '\n');
686 trace_strbuf(&trace_curl, &out);
687 }
688 strbuf_release(&out);
689}
690
691static int curl_trace(CURL *handle, curl_infotype type, char *data, size_t size, void *userp)
692{
693 const char *text;
694 enum { NO_FILTER = 0, DO_FILTER = 1 };
695
696 switch (type) {
697 case CURLINFO_TEXT:
698 trace_printf_key(&trace_curl, "== Info: %s", data);
Jeff Kingd0e99832017-09-21 02:23:24 -0400699 break;
Elia Pinto74c682d2016-05-23 13:44:02 +0000700 case CURLINFO_HEADER_OUT:
701 text = "=> Send header";
702 curl_dump_header(text, (unsigned char *)data, size, DO_FILTER);
703 break;
704 case CURLINFO_DATA_OUT:
Jonathan Tan8ba18e62018-01-18 16:28:02 -0800705 if (trace_curl_data) {
706 text = "=> Send data";
707 curl_dump_data(text, (unsigned char *)data, size);
708 }
Elia Pinto74c682d2016-05-23 13:44:02 +0000709 break;
710 case CURLINFO_SSL_DATA_OUT:
Jonathan Tan8ba18e62018-01-18 16:28:02 -0800711 if (trace_curl_data) {
712 text = "=> Send SSL data";
713 curl_dump_data(text, (unsigned char *)data, size);
714 }
Elia Pinto74c682d2016-05-23 13:44:02 +0000715 break;
716 case CURLINFO_HEADER_IN:
717 text = "<= Recv header";
718 curl_dump_header(text, (unsigned char *)data, size, NO_FILTER);
719 break;
720 case CURLINFO_DATA_IN:
Jonathan Tan8ba18e62018-01-18 16:28:02 -0800721 if (trace_curl_data) {
722 text = "<= Recv data";
723 curl_dump_data(text, (unsigned char *)data, size);
724 }
Elia Pinto74c682d2016-05-23 13:44:02 +0000725 break;
726 case CURLINFO_SSL_DATA_IN:
Jonathan Tan8ba18e62018-01-18 16:28:02 -0800727 if (trace_curl_data) {
728 text = "<= Recv SSL data";
729 curl_dump_data(text, (unsigned char *)data, size);
730 }
Elia Pinto74c682d2016-05-23 13:44:02 +0000731 break;
Jeff Kingd0e99832017-09-21 02:23:24 -0400732
733 default: /* we ignore unknown types by default */
734 return 0;
Elia Pinto74c682d2016-05-23 13:44:02 +0000735 }
736 return 0;
737}
738
739void setup_curl_trace(CURL *handle)
740{
741 if (!trace_want(&trace_curl))
742 return;
743 curl_easy_setopt(handle, CURLOPT_VERBOSE, 1L);
744 curl_easy_setopt(handle, CURLOPT_DEBUGFUNCTION, curl_trace);
745 curl_easy_setopt(handle, CURLOPT_DEBUGDATA, NULL);
746}
747
Tom G. Christensenf18777b2017-08-11 18:37:33 +0200748#ifdef CURLPROTO_HTTP
Brandon Williamsa768a022016-12-14 14:39:54 -0800749static long get_curl_allowed_protocols(int from_user)
Brandon Williamsaeae4db2016-12-14 14:39:53 -0800750{
751 long allowed_protocols = 0;
752
Brandon Williamsa768a022016-12-14 14:39:54 -0800753 if (is_transport_allowed("http", from_user))
Brandon Williamsaeae4db2016-12-14 14:39:53 -0800754 allowed_protocols |= CURLPROTO_HTTP;
Brandon Williamsa768a022016-12-14 14:39:54 -0800755 if (is_transport_allowed("https", from_user))
Brandon Williamsaeae4db2016-12-14 14:39:53 -0800756 allowed_protocols |= CURLPROTO_HTTPS;
Brandon Williamsa768a022016-12-14 14:39:54 -0800757 if (is_transport_allowed("ftp", from_user))
Brandon Williamsaeae4db2016-12-14 14:39:53 -0800758 allowed_protocols |= CURLPROTO_FTP;
Brandon Williamsa768a022016-12-14 14:39:54 -0800759 if (is_transport_allowed("ftps", from_user))
Brandon Williamsaeae4db2016-12-14 14:39:53 -0800760 allowed_protocols |= CURLPROTO_FTPS;
761
762 return allowed_protocols;
763}
Tom G. Christensenf18777b2017-08-11 18:37:33 +0200764#endif
Elia Pinto74c682d2016-05-23 13:44:02 +0000765
Junio C Hamano4251ccb2009-03-09 18:47:29 -0700766static CURL *get_curl_handle(void)
Junio C Hamano11979b92005-11-18 17:06:46 -0800767{
Junio C Hamano4251ccb2009-03-09 18:47:29 -0700768 CURL *result = curl_easy_init();
Junio C Hamano11979b92005-11-18 17:06:46 -0800769
Bernhard Reiterfaa38072014-08-13 19:31:24 +0200770 if (!result)
771 die("curl_easy_init failed");
772
Junio C Hamanoa5ccc592008-02-21 15:10:37 -0800773 if (!curl_ssl_verify) {
774 curl_easy_setopt(result, CURLOPT_SSL_VERIFYPEER, 0);
775 curl_easy_setopt(result, CURLOPT_SSL_VERIFYHOST, 0);
776 } else {
777 /* Verify authenticity of the peer's certificate */
778 curl_easy_setopt(result, CURLOPT_SSL_VERIFYPEER, 1);
779 /* The name in the cert must match whom we tried to connect */
780 curl_easy_setopt(result, CURLOPT_SSL_VERIFYHOST, 2);
781 }
782
Junio C Hamano11979b92005-11-18 17:06:46 -0800783#if LIBCURL_VERSION_NUM >= 0x070907
784 curl_easy_setopt(result, CURLOPT_NETRC, CURL_NETRC_OPTIONAL);
785#endif
Martin Storsjöb8ac9232009-11-27 23:43:08 +0800786#ifdef LIBCURL_CAN_HANDLE_AUTH_ANY
Junio C Hamano525ecd22009-12-28 10:04:24 -0800787 curl_easy_setopt(result, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
Martin Storsjöb8ac9232009-11-27 23:43:08 +0800788#endif
Junio C Hamano11979b92005-11-18 17:06:46 -0800789
Tom G. Christensendd5df532017-08-11 18:37:34 +0200790#ifdef CURLGSSAPI_DELEGATION_FLAG
Petr Stodulka26a7b232016-09-28 20:01:34 +0200791 if (curl_deleg) {
792 int i;
793 for (i = 0; i < ARRAY_SIZE(curl_deleg_levels); i++) {
794 if (!strcmp(curl_deleg, curl_deleg_levels[i].name)) {
795 curl_easy_setopt(result, CURLOPT_GSSAPI_DELEGATION,
796 curl_deleg_levels[i].curl_deleg_param);
797 break;
798 }
799 }
800 if (i == ARRAY_SIZE(curl_deleg_levels))
801 warning("Unknown delegation method '%s': using default",
802 curl_deleg);
803 }
804#endif
805
Jeff Kinga4ddbc32011-12-13 19:11:56 -0500806 if (http_proactive_auth)
807 init_curl_http_auth(result);
808
Elia Pinto01861cb2015-08-14 21:37:43 +0200809 if (getenv("GIT_SSL_VERSION"))
810 ssl_version = getenv("GIT_SSL_VERSION");
811 if (ssl_version && *ssl_version) {
812 int i;
813 for (i = 0; i < ARRAY_SIZE(sslversions); i++) {
814 if (!strcmp(ssl_version, sslversions[i].name)) {
815 curl_easy_setopt(result, CURLOPT_SSLVERSION,
816 sslversions[i].ssl_version);
817 break;
818 }
819 }
820 if (i == ARRAY_SIZE(sslversions))
821 warning("unsupported ssl version %s: using default",
822 ssl_version);
823 }
824
Lars Kellogg-Stedmanf6f2a9e2015-05-08 09:22:15 -0400825 if (getenv("GIT_SSL_CIPHER_LIST"))
826 ssl_cipherlist = getenv("GIT_SSL_CIPHER_LIST");
Lars Kellogg-Stedmanf6f2a9e2015-05-08 09:22:15 -0400827 if (ssl_cipherlist != NULL && *ssl_cipherlist)
828 curl_easy_setopt(result, CURLOPT_SSL_CIPHER_LIST,
829 ssl_cipherlist);
830
Junio C Hamano11979b92005-11-18 17:06:46 -0800831 if (ssl_cert != NULL)
832 curl_easy_setopt(result, CURLOPT_SSLCERT, ssl_cert);
Mark Lodato30dd9162009-05-27 23:16:02 -0400833 if (has_cert_password())
Jeff King148bb6a2011-12-10 05:31:21 -0500834 curl_easy_setopt(result, CURLOPT_KEYPASSWD, cert_auth.password);
Mark Lodatoef52aaf2009-06-14 22:39:00 -0400835#if LIBCURL_VERSION_NUM >= 0x070903
Junio C Hamano11979b92005-11-18 17:06:46 -0800836 if (ssl_key != NULL)
837 curl_easy_setopt(result, CURLOPT_SSLKEY, ssl_key);
838#endif
839#if LIBCURL_VERSION_NUM >= 0x070908
840 if (ssl_capath != NULL)
841 curl_easy_setopt(result, CURLOPT_CAPATH, ssl_capath);
842#endif
Christoph Eggeraeff8a62016-02-15 15:04:22 +0100843#if LIBCURL_VERSION_NUM >= 0x072c00
844 if (ssl_pinnedkey != NULL)
845 curl_easy_setopt(result, CURLOPT_PINNEDPUBLICKEY, ssl_pinnedkey);
846#endif
Junio C Hamano11979b92005-11-18 17:06:46 -0800847 if (ssl_cainfo != NULL)
848 curl_easy_setopt(result, CURLOPT_CAINFO, ssl_cainfo);
Junio C Hamano11979b92005-11-18 17:06:46 -0800849
850 if (curl_low_speed_limit > 0 && curl_low_speed_time > 0) {
851 curl_easy_setopt(result, CURLOPT_LOW_SPEED_LIMIT,
852 curl_low_speed_limit);
853 curl_easy_setopt(result, CURLOPT_LOW_SPEED_TIME,
854 curl_low_speed_time);
855 }
856
Blake Burkhartb2581162015-09-22 18:06:20 -0400857 curl_easy_setopt(result, CURLOPT_MAXREDIRS, 20);
Tay Ray Chuan311e2ea2010-09-25 12:20:35 +0800858#if LIBCURL_VERSION_NUM >= 0x071301
859 curl_easy_setopt(result, CURLOPT_POSTREDIR, CURL_REDIR_POST_ALL);
860#elif LIBCURL_VERSION_NUM >= 0x071101
861 curl_easy_setopt(result, CURLOPT_POST301, 1);
862#endif
Tom G. Christensenf18777b2017-08-11 18:37:33 +0200863#ifdef CURLPROTO_HTTP
Brandon Williamsaeae4db2016-12-14 14:39:53 -0800864 curl_easy_setopt(result, CURLOPT_REDIR_PROTOCOLS,
Brandon Williamsa768a022016-12-14 14:39:54 -0800865 get_curl_allowed_protocols(0));
Brandon Williamsaeae4db2016-12-14 14:39:53 -0800866 curl_easy_setopt(result, CURLOPT_PROTOCOLS,
Brandon Williamsa768a022016-12-14 14:39:54 -0800867 get_curl_allowed_protocols(-1));
Blake Burkhartf4113ca2015-09-22 18:06:04 -0400868#else
Brandon Williamsf962ddf2016-12-14 14:39:51 -0800869 warning("protocol restrictions not applied to curl redirects because\n"
870 "your curl version is too old (>= 7.19.4)");
Blake Burkhartf4113ca2015-09-22 18:06:04 -0400871#endif
Mark Wooding7982d742006-02-01 11:44:37 +0000872 if (getenv("GIT_CURL_VERBOSE"))
Elia Pinto74c682d2016-05-23 13:44:02 +0000873 curl_easy_setopt(result, CURLOPT_VERBOSE, 1L);
874 setup_curl_trace(result);
Jonathan Tan8ba18e62018-01-18 16:28:02 -0800875 if (getenv("GIT_TRACE_CURL_NO_DATA"))
876 trace_curl_data = 0;
Jonathan Tan83411782018-01-18 16:28:01 -0800877 if (getenv("GIT_REDACT_COOKIES")) {
878 string_list_split(&cookies_to_redact,
879 getenv("GIT_REDACT_COOKIES"), ',', -1);
880 string_list_sort(&cookies_to_redact);
881 }
Mark Wooding7982d742006-02-01 11:44:37 +0000882
Spencer E. Olsonb1d10582010-08-11 14:40:38 -0600883 curl_easy_setopt(result, CURLOPT_USERAGENT,
Jeff King745c7c82012-06-02 15:03:08 -0400884 user_agent ? user_agent : git_user_agent());
Nick Hengeveld20fc9bc2006-04-04 10:11:29 -0700885
Sasha Khapyorsky3ea099d2006-09-29 03:10:44 +0300886 if (curl_ftp_no_epsv)
887 curl_easy_setopt(result, CURLOPT_FTP_USE_EPSV, 0);
888
Modestas Vainius4bc444e2013-04-07 22:10:39 +0300889#ifdef CURLOPT_USE_SSL
890 if (curl_ssl_try)
891 curl_easy_setopt(result, CURLOPT_USE_SSL, CURLUSESSL_TRY);
892#endif
893
Knut Franke372370f2016-01-26 13:02:48 +0000894 /*
895 * CURL also examines these variables as a fallback; but we need to query
896 * them here in order to decide whether to prompt for missing password (cf.
897 * init_curl_proxy_auth()).
898 *
899 * Unlike many other common environment variables, these are historically
900 * lowercase only. It appears that CURL did not know this and implemented
901 * only uppercase variants, which was later corrected to take both - with
902 * the exception of http_proxy, which is lowercase only also in CURL. As
903 * the lowercase versions are the historical quasi-standard, they take
904 * precedence here, as in CURL.
905 */
906 if (!curl_http_proxy) {
Jeff Kingd63ed6e2016-09-07 16:06:42 -0400907 if (http_auth.protocol && !strcmp(http_auth.protocol, "https")) {
Knut Franke372370f2016-01-26 13:02:48 +0000908 var_override(&curl_http_proxy, getenv("HTTPS_PROXY"));
909 var_override(&curl_http_proxy, getenv("https_proxy"));
910 } else {
911 var_override(&curl_http_proxy, getenv("http_proxy"));
912 }
913 if (!curl_http_proxy) {
914 var_override(&curl_http_proxy, getenv("ALL_PROXY"));
915 var_override(&curl_http_proxy, getenv("all_proxy"));
916 }
917 }
918
Sergey Ryazanov57415082017-04-11 23:22:18 +0300919 if (curl_http_proxy && curl_http_proxy[0] == '\0') {
920 /*
921 * Handle case with the empty http.proxy value here to keep
922 * common code clean.
923 * NB: empty option disables proxying at all.
924 */
925 curl_easy_setopt(result, CURLOPT_PROXY, "");
926 } else if (curl_http_proxy) {
Pat Thoyts6d7afe02015-10-26 14:15:07 +0100927#if LIBCURL_VERSION_NUM >= 0x071800
Junio C Hamano87f8a0b2016-04-08 12:16:06 -0700928 if (starts_with(curl_http_proxy, "socks5h"))
929 curl_easy_setopt(result,
930 CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5_HOSTNAME);
931 else if (starts_with(curl_http_proxy, "socks5"))
Pat Thoyts6d7afe02015-10-26 14:15:07 +0100932 curl_easy_setopt(result,
933 CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
934 else if (starts_with(curl_http_proxy, "socks4a"))
935 curl_easy_setopt(result,
936 CURLOPT_PROXYTYPE, CURLPROXY_SOCKS4A);
937 else if (starts_with(curl_http_proxy, "socks"))
938 curl_easy_setopt(result,
939 CURLOPT_PROXYTYPE, CURLPROXY_SOCKS4);
940#endif
Wei Shuyu82b68032017-12-20 01:24:01 +0800941#if LIBCURL_VERSION_NUM >= 0x073400
942 else if (starts_with(curl_http_proxy, "https"))
943 curl_easy_setopt(result,
944 CURLOPT_PROXYTYPE, CURLPROXY_HTTPS);
945#endif
Knut Franke372370f2016-01-26 13:02:48 +0000946 if (strstr(curl_http_proxy, "://"))
947 credential_from_url(&proxy_auth, curl_http_proxy);
948 else {
949 struct strbuf url = STRBUF_INIT;
950 strbuf_addf(&url, "http://%s", curl_http_proxy);
951 credential_from_url(&proxy_auth, url.buf);
952 strbuf_release(&url);
953 }
954
Sergey Ryazanovae51d912017-04-11 23:22:19 +0300955 if (!proxy_auth.host)
956 die("Invalid proxy URL '%s'", curl_http_proxy);
957
Knut Franke372370f2016-01-26 13:02:48 +0000958 curl_easy_setopt(result, CURLOPT_PROXY, proxy_auth.host);
Jiang Xind445fda2016-02-29 23:16:57 +0800959#if LIBCURL_VERSION_NUM >= 0x071304
960 var_override(&curl_no_proxy, getenv("NO_PROXY"));
961 var_override(&curl_no_proxy, getenv("no_proxy"));
962 curl_easy_setopt(result, CURLOPT_NOPROXY, curl_no_proxy);
963#endif
Nelson Benitez Leondd613992012-03-02 14:55:57 +0100964 }
Knut Frankeef976392016-01-26 13:02:47 +0000965 init_curl_proxy_auth(result);
Sam Vilain9c5665a2007-11-23 13:07:00 +1300966
Jeff King47ce1152013-10-14 20:06:14 -0400967 set_curl_keepalive(result);
Eric Wonga15d0692013-10-12 22:29:40 +0000968
Junio C Hamano11979b92005-11-18 17:06:46 -0800969 return result;
970}
971
Junio C Hamano7059cd92009-03-09 19:00:30 -0700972static void set_from_env(const char **var, const char *envname)
973{
974 const char *val = getenv(envname);
975 if (val)
976 *var = val;
977}
978
Jeff Kinga4ddbc32011-12-13 19:11:56 -0500979void http_init(struct remote *remote, const char *url, int proactive_auth)
Nick Hengeveld29508e12005-11-18 11:02:58 -0800980{
981 char *low_speed_limit;
982 char *low_speed_time;
Kyle J. McKay6a569932013-08-05 13:20:36 -0700983 char *normalized_url;
984 struct urlmatch_config config = { STRING_LIST_INIT_DUP };
985
986 config.section = "http";
987 config.key = NULL;
988 config.collect_fn = http_options;
989 config.cascade_fn = git_default_config;
990 config.cb = NULL;
Nick Hengeveld29508e12005-11-18 11:02:58 -0800991
Tay Ray Chuane9176742009-06-06 16:43:41 +0800992 http_is_verbose = 0;
Kyle J. McKay6a569932013-08-05 13:20:36 -0700993 normalized_url = url_normalize(url, &config.url);
Tay Ray Chuane9176742009-06-06 16:43:41 +0800994
Kyle J. McKay6a569932013-08-05 13:20:36 -0700995 git_config(urlmatch_config_entry, &config);
996 free(normalized_url);
Junio C Hamano7059cd92009-03-09 19:00:30 -0700997
Bernhard Reiterfaa38072014-08-13 19:31:24 +0200998 if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK)
999 die("curl_global_init failed");
Nick Hengeveld29508e12005-11-18 11:02:58 -08001000
Jeff Kinga4ddbc32011-12-13 19:11:56 -05001001 http_proactive_auth = proactive_auth;
1002
Mike Hommey9fc64402008-02-27 21:35:50 +01001003 if (remote && remote->http_proxy)
1004 curl_http_proxy = xstrdup(remote->http_proxy);
1005
Knut Frankeef976392016-01-26 13:02:47 +00001006 if (remote)
1007 var_override(&http_proxy_authmethod, remote->http_proxy_authmethod);
1008
Johannes Schindelin8cb01e22016-04-27 14:20:37 +02001009 pragma_header = curl_slist_append(http_copy_default_headers(),
1010 "Pragma: no-cache");
1011 no_pragma_header = curl_slist_append(http_copy_default_headers(),
1012 "Pragma:");
Nick Hengeveld29508e12005-11-18 11:02:58 -08001013
1014#ifdef USE_CURL_MULTI
1015 {
1016 char *http_max_requests = getenv("GIT_HTTP_MAX_REQUESTS");
1017 if (http_max_requests != NULL)
1018 max_requests = atoi(http_max_requests);
1019 }
1020
1021 curlm = curl_multi_init();
Jeff King8837eb42014-08-17 03:35:53 -04001022 if (!curlm)
1023 die("curl_multi_init failed");
Nick Hengeveld29508e12005-11-18 11:02:58 -08001024#endif
1025
1026 if (getenv("GIT_SSL_NO_VERIFY"))
1027 curl_ssl_verify = 0;
1028
Junio C Hamano7059cd92009-03-09 19:00:30 -07001029 set_from_env(&ssl_cert, "GIT_SSL_CERT");
Mark Lodatoef52aaf2009-06-14 22:39:00 -04001030#if LIBCURL_VERSION_NUM >= 0x070903
Junio C Hamano7059cd92009-03-09 19:00:30 -07001031 set_from_env(&ssl_key, "GIT_SSL_KEY");
Nick Hengeveld29508e12005-11-18 11:02:58 -08001032#endif
1033#if LIBCURL_VERSION_NUM >= 0x070908
Junio C Hamano7059cd92009-03-09 19:00:30 -07001034 set_from_env(&ssl_capath, "GIT_SSL_CAPATH");
Nick Hengeveld29508e12005-11-18 11:02:58 -08001035#endif
Junio C Hamano7059cd92009-03-09 19:00:30 -07001036 set_from_env(&ssl_cainfo, "GIT_SSL_CAINFO");
Nick Hengeveld29508e12005-11-18 11:02:58 -08001037
Spencer E. Olsonb1d10582010-08-11 14:40:38 -06001038 set_from_env(&user_agent, "GIT_HTTP_USER_AGENT");
1039
Nick Hengeveld29508e12005-11-18 11:02:58 -08001040 low_speed_limit = getenv("GIT_HTTP_LOW_SPEED_LIMIT");
1041 if (low_speed_limit != NULL)
1042 curl_low_speed_limit = strtol(low_speed_limit, NULL, 10);
1043 low_speed_time = getenv("GIT_HTTP_LOW_SPEED_TIME");
1044 if (low_speed_time != NULL)
1045 curl_low_speed_time = strtol(low_speed_time, NULL, 10);
1046
Nick Hengeveld29508e12005-11-18 11:02:58 -08001047 if (curl_ssl_verify == -1)
1048 curl_ssl_verify = 1;
1049
Tay Ray Chuanad75ebe2009-11-27 23:42:26 +08001050 curl_session_count = 0;
Nick Hengeveld29508e12005-11-18 11:02:58 -08001051#ifdef USE_CURL_MULTI
1052 if (max_requests < 1)
1053 max_requests = DEFAULT_MAX_REQUESTS;
1054#endif
1055
Sasha Khapyorsky3ea099d2006-09-29 03:10:44 +03001056 if (getenv("GIT_CURL_FTP_NO_EPSV"))
1057 curl_ftp_no_epsv = 1;
1058
Jeff Kingdeba4932011-10-14 09:40:40 +02001059 if (url) {
Jeff King148bb6a2011-12-10 05:31:21 -05001060 credential_from_url(&http_auth, url);
Mark Lodato754ae192009-05-27 23:16:03 -04001061 if (!ssl_cert_password_required &&
1062 getenv("GIT_SSL_CERT_PASSWORD_PROTECTED") &&
Christian Couder59556542013-11-30 21:55:40 +01001063 starts_with(url, "https://"))
Mark Lodato30dd9162009-05-27 23:16:02 -04001064 ssl_cert_password_required = 1;
1065 }
Junio C Hamanoc33976c2009-03-09 23:34:25 -07001066
Nick Hengeveld29508e12005-11-18 11:02:58 -08001067#ifndef NO_CURL_EASY_DUPHANDLE
1068 curl_default = get_curl_handle();
1069#endif
1070}
1071
1072void http_cleanup(void)
1073{
1074 struct active_request_slot *slot = active_queue_head;
Nick Hengeveld29508e12005-11-18 11:02:58 -08001075
1076 while (slot != NULL) {
Shawn O. Pearce3278cd02007-09-15 03:23:00 -04001077 struct active_request_slot *next = slot->next;
Mike Hommeyf23d1f72008-03-03 20:30:16 +01001078 if (slot->curl != NULL) {
Eric Wongd8b6b842016-09-13 00:25:56 +00001079 xmulti_remove_handle(slot);
Nick Hengeveld29508e12005-11-18 11:02:58 -08001080 curl_easy_cleanup(slot->curl);
Mike Hommeyf23d1f72008-03-03 20:30:16 +01001081 }
Shawn O. Pearce3278cd02007-09-15 03:23:00 -04001082 free(slot);
1083 slot = next;
Nick Hengeveld29508e12005-11-18 11:02:58 -08001084 }
Shawn O. Pearce3278cd02007-09-15 03:23:00 -04001085 active_queue_head = NULL;
Nick Hengeveld29508e12005-11-18 11:02:58 -08001086
1087#ifndef NO_CURL_EASY_DUPHANDLE
1088 curl_easy_cleanup(curl_default);
1089#endif
1090
1091#ifdef USE_CURL_MULTI
1092 curl_multi_cleanup(curlm);
1093#endif
1094 curl_global_cleanup();
Nick Hengeveldb3ca4e4e2006-06-06 09:41:32 -07001095
Johannes Schindelin8cb01e22016-04-27 14:20:37 +02001096 curl_slist_free_all(extra_http_headers);
1097 extra_http_headers = NULL;
1098
Nick Hengeveldb3ca4e4e2006-06-06 09:41:32 -07001099 curl_slist_free_all(pragma_header);
Shawn O. Pearce3278cd02007-09-15 03:23:00 -04001100 pragma_header = NULL;
Mike Hommey9fc64402008-02-27 21:35:50 +01001101
Tay Ray Chuane9176742009-06-06 16:43:41 +08001102 curl_slist_free_all(no_pragma_header);
1103 no_pragma_header = NULL;
1104
Mike Hommey9fc64402008-02-27 21:35:50 +01001105 if (curl_http_proxy) {
Miklos Vajnae4a80ec2008-12-07 01:45:37 +01001106 free((void *)curl_http_proxy);
Mike Hommey9fc64402008-02-27 21:35:50 +01001107 curl_http_proxy = NULL;
1108 }
Mark Lodato30dd9162009-05-27 23:16:02 -04001109
Knut Franke372370f2016-01-26 13:02:48 +00001110 if (proxy_auth.password) {
1111 memset(proxy_auth.password, 0, strlen(proxy_auth.password));
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +00001112 FREE_AND_NULL(proxy_auth.password);
Knut Franke372370f2016-01-26 13:02:48 +00001113 }
1114
1115 free((void *)curl_proxyuserpwd);
1116 curl_proxyuserpwd = NULL;
1117
Knut Frankeef976392016-01-26 13:02:47 +00001118 free((void *)http_proxy_authmethod);
1119 http_proxy_authmethod = NULL;
1120
Jeff King148bb6a2011-12-10 05:31:21 -05001121 if (cert_auth.password != NULL) {
1122 memset(cert_auth.password, 0, strlen(cert_auth.password));
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +00001123 FREE_AND_NULL(cert_auth.password);
Mark Lodato30dd9162009-05-27 23:16:02 -04001124 }
1125 ssl_cert_password_required = 0;
Yi EungJunf18604b2015-01-28 21:04:37 +09001126
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +00001127 FREE_AND_NULL(cached_accept_language);
Nick Hengeveld29508e12005-11-18 11:02:58 -08001128}
1129
Nick Hengeveld29508e12005-11-18 11:02:58 -08001130struct active_request_slot *get_active_slot(void)
1131{
1132 struct active_request_slot *slot = active_queue_head;
1133 struct active_request_slot *newslot;
1134
1135#ifdef USE_CURL_MULTI
1136 int num_transfers;
1137
1138 /* Wait for a slot to open up if the queue is full */
1139 while (active_requests >= max_requests) {
1140 curl_multi_perform(curlm, &num_transfers);
Junio C Hamano4251ccb2009-03-09 18:47:29 -07001141 if (num_transfers < active_requests)
Nick Hengeveld29508e12005-11-18 11:02:58 -08001142 process_curl_messages();
Nick Hengeveld29508e12005-11-18 11:02:58 -08001143 }
1144#endif
1145
Junio C Hamano4251ccb2009-03-09 18:47:29 -07001146 while (slot != NULL && slot->in_use)
Nick Hengeveld29508e12005-11-18 11:02:58 -08001147 slot = slot->next;
Junio C Hamano4251ccb2009-03-09 18:47:29 -07001148
Nick Hengeveld29508e12005-11-18 11:02:58 -08001149 if (slot == NULL) {
1150 newslot = xmalloc(sizeof(*newslot));
1151 newslot->curl = NULL;
1152 newslot->in_use = 0;
1153 newslot->next = NULL;
1154
1155 slot = active_queue_head;
1156 if (slot == NULL) {
1157 active_queue_head = newslot;
1158 } else {
Junio C Hamano4251ccb2009-03-09 18:47:29 -07001159 while (slot->next != NULL)
Nick Hengeveld29508e12005-11-18 11:02:58 -08001160 slot = slot->next;
Nick Hengeveld29508e12005-11-18 11:02:58 -08001161 slot->next = newslot;
1162 }
1163 slot = newslot;
1164 }
1165
1166 if (slot->curl == NULL) {
1167#ifdef NO_CURL_EASY_DUPHANDLE
1168 slot->curl = get_curl_handle();
1169#else
1170 slot->curl = curl_easy_duphandle(curl_default);
1171#endif
Tay Ray Chuanad75ebe2009-11-27 23:42:26 +08001172 curl_session_count++;
Nick Hengeveld29508e12005-11-18 11:02:58 -08001173 }
1174
1175 active_requests++;
1176 slot->in_use = 1;
Nick Hengeveldc8568e12006-01-31 11:06:55 -08001177 slot->results = NULL;
Nick Hengeveldbaa7b672006-03-10 20:18:01 -08001178 slot->finished = NULL;
Nick Hengeveld29508e12005-11-18 11:02:58 -08001179 slot->callback_data = NULL;
1180 slot->callback_func = NULL;
Duncan Brownbcfb95d2011-06-02 16:31:25 -04001181 curl_easy_setopt(slot->curl, CURLOPT_COOKIEFILE, curl_cookie_file);
Dave Borowitz912b2ac2013-07-23 15:40:17 -07001182 if (curl_save_cookies)
1183 curl_easy_setopt(slot->curl, CURLOPT_COOKIEJAR, curl_cookie_file);
Nick Hengeveld29508e12005-11-18 11:02:58 -08001184 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, pragma_header);
Nick Hengeveld29508e12005-11-18 11:02:58 -08001185 curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, curl_errorstr);
Nick Hengeveld90949502006-05-31 16:25:03 -07001186 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, NULL);
1187 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, NULL);
1188 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, NULL);
Junio C Hamano1e418272011-04-26 08:04:49 -07001189 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, NULL);
Nick Hengeveld90949502006-05-31 16:25:03 -07001190 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 0);
1191 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
Jeff Kingb793acf2013-04-15 20:30:38 -04001192 curl_easy_setopt(slot->curl, CURLOPT_FAILONERROR, 1);
David Turner835c4d32015-11-02 16:39:58 -05001193 curl_easy_setopt(slot->curl, CURLOPT_RANGE, NULL);
Eric Wongc915f112016-02-03 04:09:14 +00001194
Jeff King50d34132016-12-06 13:24:41 -05001195 /*
1196 * Default following to off unless "ALWAYS" is configured; this gives
1197 * callers a sane starting point, and they can tweak for individual
1198 * HTTP_FOLLOW_* cases themselves.
1199 */
1200 if (http_follow_config == HTTP_FOLLOW_ALWAYS)
1201 curl_easy_setopt(slot->curl, CURLOPT_FOLLOWLOCATION, 1);
1202 else
1203 curl_easy_setopt(slot->curl, CURLOPT_FOLLOWLOCATION, 0);
1204
Eric Wongc915f112016-02-03 04:09:14 +00001205#if LIBCURL_VERSION_NUM >= 0x070a08
1206 curl_easy_setopt(slot->curl, CURLOPT_IPRESOLVE, git_curl_ipresolve);
1207#endif
brian m. carlson4dbe6642015-01-08 00:29:20 +00001208#ifdef LIBCURL_CAN_HANDLE_AUTH_ANY
1209 curl_easy_setopt(slot->curl, CURLOPT_HTTPAUTH, http_auth_methods);
1210#endif
Jeff King40a18fc2017-02-25 14:18:31 -05001211 if (http_auth.password || curl_empty_auth_enabled())
Jeff Kingdfa17252012-04-10 11:53:40 +02001212 init_curl_http_auth(slot->curl);
Nick Hengeveld29508e12005-11-18 11:02:58 -08001213
1214 return slot;
1215}
1216
1217int start_active_slot(struct active_request_slot *slot)
1218{
1219#ifdef USE_CURL_MULTI
1220 CURLMcode curlm_result = curl_multi_add_handle(curlm, slot->curl);
Daniel Barkalow45c17412007-09-10 23:02:28 -04001221 int num_transfers;
Nick Hengeveld29508e12005-11-18 11:02:58 -08001222
1223 if (curlm_result != CURLM_OK &&
1224 curlm_result != CURLM_CALL_MULTI_PERFORM) {
Eric Wong9f1b5882016-09-13 00:25:55 +00001225 warning("curl_multi_add_handle failed: %s",
1226 curl_multi_strerror(curlm_result));
Nick Hengeveld29508e12005-11-18 11:02:58 -08001227 active_requests--;
1228 slot->in_use = 0;
1229 return 0;
1230 }
Daniel Barkalow45c17412007-09-10 23:02:28 -04001231
1232 /*
1233 * We know there must be something to do, since we just added
1234 * something.
1235 */
1236 curl_multi_perform(curlm, &num_transfers);
Nick Hengeveld29508e12005-11-18 11:02:58 -08001237#endif
1238 return 1;
1239}
1240
1241#ifdef USE_CURL_MULTI
Daniel Barkalowfc57b6a2007-09-10 23:02:34 -04001242struct fill_chain {
1243 void *data;
1244 int (*fill)(void *);
1245 struct fill_chain *next;
1246};
1247
Junio C Hamano4251ccb2009-03-09 18:47:29 -07001248static struct fill_chain *fill_cfg;
Daniel Barkalowfc57b6a2007-09-10 23:02:34 -04001249
1250void add_fill_function(void *data, int (*fill)(void *))
1251{
Brandon Williamsee6e0652018-02-14 10:59:42 -08001252 struct fill_chain *new_fill = xmalloc(sizeof(*new_fill));
Daniel Barkalowfc57b6a2007-09-10 23:02:34 -04001253 struct fill_chain **linkp = &fill_cfg;
Brandon Williamsee6e0652018-02-14 10:59:42 -08001254 new_fill->data = data;
1255 new_fill->fill = fill;
1256 new_fill->next = NULL;
Daniel Barkalowfc57b6a2007-09-10 23:02:34 -04001257 while (*linkp)
1258 linkp = &(*linkp)->next;
Brandon Williamsee6e0652018-02-14 10:59:42 -08001259 *linkp = new_fill;
Daniel Barkalowfc57b6a2007-09-10 23:02:34 -04001260}
1261
Daniel Barkalow45c17412007-09-10 23:02:28 -04001262void fill_active_slots(void)
1263{
1264 struct active_request_slot *slot = active_queue_head;
1265
Daniel Barkalowfc57b6a2007-09-10 23:02:34 -04001266 while (active_requests < max_requests) {
1267 struct fill_chain *fill;
1268 for (fill = fill_cfg; fill; fill = fill->next)
1269 if (fill->fill(fill->data))
1270 break;
1271
1272 if (!fill)
Daniel Barkalow45c17412007-09-10 23:02:28 -04001273 break;
Daniel Barkalowfc57b6a2007-09-10 23:02:34 -04001274 }
Daniel Barkalow45c17412007-09-10 23:02:28 -04001275
1276 while (slot != NULL) {
Tay Ray Chuanad75ebe2009-11-27 23:42:26 +08001277 if (!slot->in_use && slot->curl != NULL
1278 && curl_session_count > min_curl_sessions) {
Daniel Barkalow45c17412007-09-10 23:02:28 -04001279 curl_easy_cleanup(slot->curl);
1280 slot->curl = NULL;
Tay Ray Chuanad75ebe2009-11-27 23:42:26 +08001281 curl_session_count--;
Daniel Barkalow45c17412007-09-10 23:02:28 -04001282 }
1283 slot = slot->next;
1284 }
1285}
1286
Nick Hengeveld29508e12005-11-18 11:02:58 -08001287void step_active_slots(void)
1288{
1289 int num_transfers;
1290 CURLMcode curlm_result;
1291
1292 do {
1293 curlm_result = curl_multi_perform(curlm, &num_transfers);
1294 } while (curlm_result == CURLM_CALL_MULTI_PERFORM);
1295 if (num_transfers < active_requests) {
1296 process_curl_messages();
1297 fill_active_slots();
1298 }
1299}
1300#endif
1301
1302void run_active_slot(struct active_request_slot *slot)
1303{
1304#ifdef USE_CURL_MULTI
Nick Hengeveld29508e12005-11-18 11:02:58 -08001305 fd_set readfds;
1306 fd_set writefds;
1307 fd_set excfds;
1308 int max_fd;
1309 struct timeval select_timeout;
Nick Hengeveldbaa7b672006-03-10 20:18:01 -08001310 int finished = 0;
Nick Hengeveld29508e12005-11-18 11:02:58 -08001311
Nick Hengeveldbaa7b672006-03-10 20:18:01 -08001312 slot->finished = &finished;
1313 while (!finished) {
Nick Hengeveld29508e12005-11-18 11:02:58 -08001314 step_active_slots();
1315
Mika Fischerdf26c472011-11-04 15:19:27 +01001316 if (slot->in_use) {
Mika Fischereb56c822011-11-04 15:19:26 +01001317#if LIBCURL_VERSION_NUM >= 0x070f04
1318 long curl_timeout;
1319 curl_multi_timeout(curlm, &curl_timeout);
1320 if (curl_timeout == 0) {
1321 continue;
1322 } else if (curl_timeout == -1) {
1323 select_timeout.tv_sec = 0;
1324 select_timeout.tv_usec = 50000;
1325 } else {
1326 select_timeout.tv_sec = curl_timeout / 1000;
1327 select_timeout.tv_usec = (curl_timeout % 1000) * 1000;
1328 }
1329#else
1330 select_timeout.tv_sec = 0;
1331 select_timeout.tv_usec = 50000;
1332#endif
Nick Hengeveld29508e12005-11-18 11:02:58 -08001333
Mika Fischer6f9dd672011-11-04 15:19:25 +01001334 max_fd = -1;
Nick Hengeveld29508e12005-11-18 11:02:58 -08001335 FD_ZERO(&readfds);
1336 FD_ZERO(&writefds);
1337 FD_ZERO(&excfds);
Mika Fischer6f9dd672011-11-04 15:19:25 +01001338 curl_multi_fdset(curlm, &readfds, &writefds, &excfds, &max_fd);
Mika Fischereb56c822011-11-04 15:19:26 +01001339
Stefan Zager7202b812012-10-19 14:04:20 -07001340 /*
1341 * It can happen that curl_multi_timeout returns a pathologically
1342 * long timeout when curl_multi_fdset returns no file descriptors
1343 * to read. See commit message for more details.
1344 */
1345 if (max_fd < 0 &&
1346 (select_timeout.tv_sec > 0 ||
1347 select_timeout.tv_usec > 50000)) {
1348 select_timeout.tv_sec = 0;
1349 select_timeout.tv_usec = 50000;
1350 }
1351
Mika Fischer6f9dd672011-11-04 15:19:25 +01001352 select(max_fd+1, &readfds, &writefds, &excfds, &select_timeout);
Nick Hengeveld29508e12005-11-18 11:02:58 -08001353 }
1354 }
1355#else
1356 while (slot->in_use) {
1357 slot->curl_result = curl_easy_perform(slot->curl);
1358 finish_active_slot(slot);
1359 }
1360#endif
1361}
1362
Junio C Hamano83e41e22010-01-11 22:26:08 -08001363static void release_active_slot(struct active_request_slot *slot)
Mark Wooding53f31382006-02-07 10:07:39 +00001364{
1365 closedown_active_slot(slot);
Eric Wong2abc8482016-09-13 00:25:57 +00001366 if (slot->curl) {
Eric Wongd8b6b842016-09-13 00:25:56 +00001367 xmulti_remove_handle(slot);
Eric Wong2abc8482016-09-13 00:25:57 +00001368 if (curl_session_count > min_curl_sessions) {
1369 curl_easy_cleanup(slot->curl);
1370 slot->curl = NULL;
1371 curl_session_count--;
1372 }
Mark Wooding53f31382006-02-07 10:07:39 +00001373 }
Nick Hengeveldb3ca4e4e2006-06-06 09:41:32 -07001374#ifdef USE_CURL_MULTI
Mark Wooding53f31382006-02-07 10:07:39 +00001375 fill_active_slots();
Nick Hengeveldb3ca4e4e2006-06-06 09:41:32 -07001376#endif
Mark Wooding53f31382006-02-07 10:07:39 +00001377}
1378
Nick Hengeveld29508e12005-11-18 11:02:58 -08001379void finish_all_active_slots(void)
1380{
1381 struct active_request_slot *slot = active_queue_head;
1382
1383 while (slot != NULL)
1384 if (slot->in_use) {
1385 run_active_slot(slot);
1386 slot = active_queue_head;
1387 } else {
1388 slot = slot->next;
1389 }
1390}
Mike Hommeyd7e92802007-12-11 00:08:25 +01001391
Tay Ray Chuan5ace9942009-06-06 16:43:43 +08001392/* Helpers for modifying and creating URLs */
Mike Hommeyd7e92802007-12-11 00:08:25 +01001393static inline int needs_quote(int ch)
1394{
1395 if (((ch >= 'A') && (ch <= 'Z'))
1396 || ((ch >= 'a') && (ch <= 'z'))
1397 || ((ch >= '0') && (ch <= '9'))
1398 || (ch == '/')
1399 || (ch == '-')
1400 || (ch == '.'))
1401 return 0;
1402 return 1;
1403}
1404
Mike Hommeyd7e92802007-12-11 00:08:25 +01001405static char *quote_ref_url(const char *base, const char *ref)
1406{
Tay Ray Chuan113106e2009-03-08 00:47:21 +08001407 struct strbuf buf = STRBUF_INIT;
Mike Hommeyd7e92802007-12-11 00:08:25 +01001408 const char *cp;
Tay Ray Chuan113106e2009-03-08 00:47:21 +08001409 int ch;
Mike Hommeyd7e92802007-12-11 00:08:25 +01001410
Tay Ray Chuan5ace9942009-06-06 16:43:43 +08001411 end_url_with_slash(&buf, base);
Tay Ray Chuan113106e2009-03-08 00:47:21 +08001412
1413 for (cp = ref; (ch = *cp) != 0; cp++)
Mike Hommeyd7e92802007-12-11 00:08:25 +01001414 if (needs_quote(ch))
Tay Ray Chuan113106e2009-03-08 00:47:21 +08001415 strbuf_addf(&buf, "%%%02x", ch);
Mike Hommeyd7e92802007-12-11 00:08:25 +01001416 else
Tay Ray Chuan113106e2009-03-08 00:47:21 +08001417 strbuf_addch(&buf, *cp);
Mike Hommeyd7e92802007-12-11 00:08:25 +01001418
Tay Ray Chuan113106e2009-03-08 00:47:21 +08001419 return strbuf_detach(&buf, NULL);
Mike Hommeyd7e92802007-12-11 00:08:25 +01001420}
1421
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08001422void append_remote_object_url(struct strbuf *buf, const char *url,
1423 const char *hex,
1424 int only_two_digit_prefix)
1425{
Tay Ray Chuan800324c2009-08-17 17:09:43 +08001426 end_url_with_slash(buf, url);
1427
1428 strbuf_addf(buf, "objects/%.*s/", 2, hex);
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08001429 if (!only_two_digit_prefix)
René Scharfebc57b9c2016-08-05 22:37:11 +02001430 strbuf_addstr(buf, hex + 2);
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08001431}
1432
1433char *get_remote_object_url(const char *url, const char *hex,
1434 int only_two_digit_prefix)
1435{
1436 struct strbuf buf = STRBUF_INIT;
1437 append_remote_object_url(&buf, url, hex, only_two_digit_prefix);
1438 return strbuf_detach(&buf, NULL);
1439}
1440
Junio C Hamanob90a3d72015-01-14 15:40:46 -08001441static int handle_curl_result(struct slot_results *results)
Jeff King88097032012-08-27 09:26:04 -04001442{
Jeff King6d052d72013-04-05 18:14:06 -04001443 /*
1444 * If we see a failing http code with CURLE_OK, we have turned off
1445 * FAILONERROR (to keep the server's custom error response), and should
1446 * translate the code into failure here.
Jeff King50d34132016-12-06 13:24:41 -05001447 *
1448 * Likewise, if we see a redirect (30x code), that means we turned off
1449 * redirect-following, and we should treat the result as an error.
Jeff King6d052d72013-04-05 18:14:06 -04001450 */
1451 if (results->curl_result == CURLE_OK &&
Jeff King50d34132016-12-06 13:24:41 -05001452 results->http_code >= 300) {
Jeff King6d052d72013-04-05 18:14:06 -04001453 results->curl_result = CURLE_HTTP_RETURNED_ERROR;
1454 /*
1455 * Normally curl will already have put the "reason phrase"
1456 * from the server into curl_errorstr; unfortunately without
1457 * FAILONERROR it is lost, so we can give only the numeric
1458 * status code.
1459 */
Jeff King1a168e52017-03-28 15:46:56 -04001460 xsnprintf(curl_errorstr, sizeof(curl_errorstr),
1461 "The requested URL returned error: %ld",
1462 results->http_code);
Jeff King6d052d72013-04-05 18:14:06 -04001463 }
1464
Jeff King88097032012-08-27 09:26:04 -04001465 if (results->curl_result == CURLE_OK) {
1466 credential_approve(&http_auth);
Knut Franke372370f2016-01-26 13:02:48 +00001467 if (proxy_auth.password)
1468 credential_approve(&proxy_auth);
Jeff King88097032012-08-27 09:26:04 -04001469 return HTTP_OK;
1470 } else if (missing_target(results))
1471 return HTTP_MISSING_TARGET;
1472 else if (results->http_code == 401) {
1473 if (http_auth.username && http_auth.password) {
1474 credential_reject(&http_auth);
1475 return HTTP_NOAUTH;
1476 } else {
brian m. carlson4dbe6642015-01-08 00:29:20 +00001477#ifdef LIBCURL_CAN_HANDLE_AUTH_ANY
1478 http_auth_methods &= ~CURLAUTH_GSSNEGOTIATE;
Jeff King40a18fc2017-02-25 14:18:31 -05001479 if (results->auth_avail) {
Jeff King840398f2017-02-22 18:34:37 -05001480 http_auth_methods &= results->auth_avail;
Jeff King40a18fc2017-02-25 14:18:31 -05001481 http_auth_methods_restricted = 1;
1482 }
brian m. carlson4dbe6642015-01-08 00:29:20 +00001483#endif
Jeff King88097032012-08-27 09:26:04 -04001484 return HTTP_REAUTH;
1485 }
1486 } else {
Knut Franke372370f2016-01-26 13:02:48 +00001487 if (results->http_connectcode == 407)
1488 credential_reject(&proxy_auth);
Junio C Hamano3503e9a2012-09-12 14:08:05 -07001489#if LIBCURL_VERSION_NUM >= 0x070c00
Jeff King88097032012-08-27 09:26:04 -04001490 if (!curl_errorstr[0])
1491 strlcpy(curl_errorstr,
1492 curl_easy_strerror(results->curl_result),
1493 sizeof(curl_errorstr));
Junio C Hamano3503e9a2012-09-12 14:08:05 -07001494#endif
Jeff King88097032012-08-27 09:26:04 -04001495 return HTTP_ERROR;
1496 }
1497}
1498
Jeff Kingbeed3362014-02-18 05:34:20 -05001499int run_one_slot(struct active_request_slot *slot,
1500 struct slot_results *results)
1501{
1502 slot->results = results;
1503 if (!start_active_slot(slot)) {
Jeff King1a168e52017-03-28 15:46:56 -04001504 xsnprintf(curl_errorstr, sizeof(curl_errorstr),
1505 "failed to start HTTP request");
Jeff Kingbeed3362014-02-18 05:34:20 -05001506 return HTTP_START_FAILED;
1507 }
1508
1509 run_active_slot(slot);
1510 return handle_curl_result(results);
1511}
1512
Johannes Schindelin8cb01e22016-04-27 14:20:37 +02001513struct curl_slist *http_copy_default_headers(void)
1514{
1515 struct curl_slist *headers = NULL, *h;
1516
1517 for (h = extra_http_headers; h; h = h->next)
1518 headers = curl_slist_append(headers, h->data);
1519
1520 return headers;
1521}
1522
Jeff King132b70a2013-09-28 04:31:11 -04001523static CURLcode curlinfo_strbuf(CURL *curl, CURLINFO info, struct strbuf *buf)
1524{
1525 char *ptr;
1526 CURLcode ret;
1527
1528 strbuf_reset(buf);
1529 ret = curl_easy_getinfo(curl, info, &ptr);
1530 if (!ret && ptr)
1531 strbuf_addstr(buf, ptr);
1532 return ret;
1533}
1534
Jeff Kingbf197fd2014-05-22 05:29:47 -04001535/*
Jeff Kinge3131622014-05-22 05:30:05 -04001536 * Check for and extract a content-type parameter. "raw"
1537 * should be positioned at the start of the potential
1538 * parameter, with any whitespace already removed.
1539 *
1540 * "name" is the name of the parameter. The value is appended
1541 * to "out".
1542 */
1543static int extract_param(const char *raw, const char *name,
1544 struct strbuf *out)
1545{
1546 size_t len = strlen(name);
1547
1548 if (strncasecmp(raw, name, len))
1549 return -1;
1550 raw += len;
1551
1552 if (*raw != '=')
1553 return -1;
1554 raw++;
1555
Yi EungJunf34a6552014-06-18 07:11:53 +09001556 while (*raw && !isspace(*raw) && *raw != ';')
Jeff Kinge3131622014-05-22 05:30:05 -04001557 strbuf_addch(out, *raw++);
1558 return 0;
1559}
1560
1561/*
Jeff Kingbf197fd2014-05-22 05:29:47 -04001562 * Extract a normalized version of the content type, with any
1563 * spaces suppressed, all letters lowercased, and no trailing ";"
1564 * or parameters.
1565 *
1566 * Note that we will silently remove even invalid whitespace. For
1567 * example, "text / plain" is specifically forbidden by RFC 2616,
1568 * but "text/plain" is the only reasonable output, and this keeps
1569 * our code simple.
1570 *
Jeff Kinge3131622014-05-22 05:30:05 -04001571 * If the "charset" argument is not NULL, store the value of any
1572 * charset parameter there.
1573 *
Jeff Kingbf197fd2014-05-22 05:29:47 -04001574 * Example:
Jeff Kinge3131622014-05-22 05:30:05 -04001575 * "TEXT/PLAIN; charset=utf-8" -> "text/plain", "utf-8"
Jeff Kingbf197fd2014-05-22 05:29:47 -04001576 * "text / plain" -> "text/plain"
1577 */
Jeff Kinge3131622014-05-22 05:30:05 -04001578static void extract_content_type(struct strbuf *raw, struct strbuf *type,
1579 struct strbuf *charset)
Jeff Kingbf197fd2014-05-22 05:29:47 -04001580{
1581 const char *p;
1582
1583 strbuf_reset(type);
1584 strbuf_grow(type, raw->len);
1585 for (p = raw->buf; *p; p++) {
1586 if (isspace(*p))
1587 continue;
Jeff Kinge3131622014-05-22 05:30:05 -04001588 if (*p == ';') {
1589 p++;
Jeff Kingbf197fd2014-05-22 05:29:47 -04001590 break;
Jeff Kinge3131622014-05-22 05:30:05 -04001591 }
Jeff Kingbf197fd2014-05-22 05:29:47 -04001592 strbuf_addch(type, tolower(*p));
1593 }
Jeff Kinge3131622014-05-22 05:30:05 -04001594
1595 if (!charset)
1596 return;
1597
1598 strbuf_reset(charset);
1599 while (*p) {
Yi EungJunf34a6552014-06-18 07:11:53 +09001600 while (isspace(*p) || *p == ';')
Jeff Kinge3131622014-05-22 05:30:05 -04001601 p++;
1602 if (!extract_param(p, "charset", charset))
1603 return;
1604 while (*p && !isspace(*p))
1605 p++;
1606 }
Jeff Kingc553fd12014-05-22 05:36:12 -04001607
1608 if (!charset->len && starts_with(type->buf, "text/"))
1609 strbuf_addstr(charset, "ISO-8859-1");
Jeff Kingbf197fd2014-05-22 05:29:47 -04001610}
1611
Yi EungJunf18604b2015-01-28 21:04:37 +09001612static void write_accept_language(struct strbuf *buf)
1613{
1614 /*
1615 * MAX_DECIMAL_PLACES must not be larger than 3. If it is larger than
1616 * that, q-value will be smaller than 0.001, the minimum q-value the
1617 * HTTP specification allows. See
1618 * http://tools.ietf.org/html/rfc7231#section-5.3.1 for q-value.
1619 */
1620 const int MAX_DECIMAL_PLACES = 3;
1621 const int MAX_LANGUAGE_TAGS = 1000;
1622 const int MAX_ACCEPT_LANGUAGE_HEADER_SIZE = 4000;
1623 char **language_tags = NULL;
1624 int num_langs = 0;
1625 const char *s = get_preferred_languages();
1626 int i;
1627 struct strbuf tag = STRBUF_INIT;
1628
1629 /* Don't add Accept-Language header if no language is preferred. */
1630 if (!s)
1631 return;
1632
1633 /*
1634 * Split the colon-separated string of preferred languages into
1635 * language_tags array.
1636 */
1637 do {
1638 /* collect language tag */
1639 for (; *s && (isalnum(*s) || *s == '_'); s++)
1640 strbuf_addch(&tag, *s == '_' ? '-' : *s);
1641
1642 /* skip .codeset, @modifier and any other unnecessary parts */
1643 while (*s && *s != ':')
1644 s++;
1645
1646 if (tag.len) {
1647 num_langs++;
1648 REALLOC_ARRAY(language_tags, num_langs);
1649 language_tags[num_langs - 1] = strbuf_detach(&tag, NULL);
1650 if (num_langs >= MAX_LANGUAGE_TAGS - 1) /* -1 for '*' */
1651 break;
1652 }
1653 } while (*s++);
1654
1655 /* write Accept-Language header into buf */
1656 if (num_langs) {
1657 int last_buf_len = 0;
1658 int max_q;
1659 int decimal_places;
1660 char q_format[32];
1661
1662 /* add '*' */
1663 REALLOC_ARRAY(language_tags, num_langs + 1);
1664 language_tags[num_langs++] = "*"; /* it's OK; this won't be freed */
1665
1666 /* compute decimal_places */
1667 for (max_q = 1, decimal_places = 0;
1668 max_q < num_langs && decimal_places <= MAX_DECIMAL_PLACES;
1669 decimal_places++, max_q *= 10)
1670 ;
1671
Jeff King5096d492015-09-24 17:06:08 -04001672 xsnprintf(q_format, sizeof(q_format), ";q=0.%%0%dd", decimal_places);
Yi EungJunf18604b2015-01-28 21:04:37 +09001673
1674 strbuf_addstr(buf, "Accept-Language: ");
1675
1676 for (i = 0; i < num_langs; i++) {
1677 if (i > 0)
1678 strbuf_addstr(buf, ", ");
1679
1680 strbuf_addstr(buf, language_tags[i]);
1681
1682 if (i > 0)
1683 strbuf_addf(buf, q_format, max_q - i);
1684
1685 if (buf->len > MAX_ACCEPT_LANGUAGE_HEADER_SIZE) {
1686 strbuf_remove(buf, last_buf_len, buf->len - last_buf_len);
1687 break;
1688 }
1689
1690 last_buf_len = buf->len;
1691 }
1692 }
1693
1694 /* free language tags -- last one is a static '*' */
1695 for (i = 0; i < num_langs - 1; i++)
1696 free(language_tags[i]);
1697 free(language_tags);
1698}
1699
1700/*
1701 * Get an Accept-Language header which indicates user's preferred languages.
1702 *
1703 * Examples:
1704 * LANGUAGE= -> ""
1705 * LANGUAGE=ko:en -> "Accept-Language: ko, en; q=0.9, *; q=0.1"
1706 * LANGUAGE=ko_KR.UTF-8:sr@latin -> "Accept-Language: ko-KR, sr; q=0.9, *; q=0.1"
1707 * LANGUAGE=ko LANG=en_US.UTF-8 -> "Accept-Language: ko, *; q=0.1"
1708 * LANGUAGE= LANG=en_US.UTF-8 -> "Accept-Language: en-US, *; q=0.1"
1709 * LANGUAGE= LANG=C -> ""
1710 */
1711static const char *get_accept_language(void)
1712{
1713 if (!cached_accept_language) {
1714 struct strbuf buf = STRBUF_INIT;
1715 write_accept_language(&buf);
1716 if (buf.len > 0)
1717 cached_accept_language = strbuf_detach(&buf, NULL);
1718 }
1719
1720 return cached_accept_language;
1721}
1722
David Turner835c4d32015-11-02 16:39:58 -05001723static void http_opt_request_remainder(CURL *curl, off_t pos)
1724{
1725 char buf[128];
1726 xsnprintf(buf, sizeof(buf), "%"PRIuMAX"-", (uintmax_t)pos);
1727 curl_easy_setopt(curl, CURLOPT_RANGE, buf);
1728}
1729
Mike Hommeye929cd22009-06-06 16:43:53 +08001730/* http_request() targets */
1731#define HTTP_REQUEST_STRBUF 0
1732#define HTTP_REQUEST_FILE 1
1733
Jeff King1bbcc222013-09-28 04:31:23 -04001734static int http_request(const char *url,
1735 void *result, int target,
1736 const struct http_get_options *options)
Mike Hommeye929cd22009-06-06 16:43:53 +08001737{
1738 struct active_request_slot *slot;
1739 struct slot_results results;
Johannes Schindelin8cb01e22016-04-27 14:20:37 +02001740 struct curl_slist *headers = http_copy_default_headers();
Mike Hommeye929cd22009-06-06 16:43:53 +08001741 struct strbuf buf = STRBUF_INIT;
Yi EungJunf18604b2015-01-28 21:04:37 +09001742 const char *accept_language;
Mike Hommeye929cd22009-06-06 16:43:53 +08001743 int ret;
1744
1745 slot = get_active_slot();
Mike Hommeye929cd22009-06-06 16:43:53 +08001746 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
1747
1748 if (result == NULL) {
1749 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 1);
1750 } else {
1751 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
1752 curl_easy_setopt(slot->curl, CURLOPT_FILE, result);
1753
1754 if (target == HTTP_REQUEST_FILE) {
Jeff Kingf8117f52015-11-02 17:10:27 -05001755 off_t posn = ftello(result);
Mike Hommeye929cd22009-06-06 16:43:53 +08001756 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION,
1757 fwrite);
David Turner835c4d32015-11-02 16:39:58 -05001758 if (posn > 0)
1759 http_opt_request_remainder(slot->curl, posn);
Mike Hommeye929cd22009-06-06 16:43:53 +08001760 } else
1761 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION,
1762 fwrite_buffer);
1763 }
1764
Yi EungJunf18604b2015-01-28 21:04:37 +09001765 accept_language = get_accept_language();
1766
1767 if (accept_language)
1768 headers = curl_slist_append(headers, accept_language);
1769
Mike Hommeye929cd22009-06-06 16:43:53 +08001770 strbuf_addstr(&buf, "Pragma:");
Jeff King1bbcc222013-09-28 04:31:23 -04001771 if (options && options->no_cache)
Mike Hommeye929cd22009-06-06 16:43:53 +08001772 strbuf_addstr(&buf, " no-cache");
Jeff King1bbcc222013-09-28 04:31:23 -04001773 if (options && options->keep_error)
Jeff King6d052d72013-04-05 18:14:06 -04001774 curl_easy_setopt(slot->curl, CURLOPT_FAILONERROR, 0);
Jeff King50d34132016-12-06 13:24:41 -05001775 if (options && options->initial_request &&
1776 http_follow_config == HTTP_FOLLOW_INITIAL)
1777 curl_easy_setopt(slot->curl, CURLOPT_FOLLOWLOCATION, 1);
Mike Hommeye929cd22009-06-06 16:43:53 +08001778
1779 headers = curl_slist_append(headers, buf.buf);
1780
Brandon Williams8ff14ed2018-03-15 10:31:38 -07001781 /* Add additional headers here */
1782 if (options && options->extra_headers) {
1783 const struct string_list_item *item;
1784 for_each_string_list_item(item, options->extra_headers) {
1785 headers = curl_slist_append(headers, item->string);
1786 }
1787 }
1788
Mike Hommeye929cd22009-06-06 16:43:53 +08001789 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
1790 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
Brandon Williams1a53e692018-05-22 11:42:03 -07001791 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "");
Mike Hommeye929cd22009-06-06 16:43:53 +08001792
Jeff Kingbeed3362014-02-18 05:34:20 -05001793 ret = run_one_slot(slot, &results);
Mike Hommeye929cd22009-06-06 16:43:53 +08001794
Jeff Kingbf197fd2014-05-22 05:29:47 -04001795 if (options && options->content_type) {
1796 struct strbuf raw = STRBUF_INIT;
1797 curlinfo_strbuf(slot->curl, CURLINFO_CONTENT_TYPE, &raw);
Jeff Kinge3131622014-05-22 05:30:05 -04001798 extract_content_type(&raw, options->content_type,
1799 options->charset);
Jeff Kingbf197fd2014-05-22 05:29:47 -04001800 strbuf_release(&raw);
1801 }
Shawn Pearce4656bf42013-01-31 13:02:07 -08001802
Jeff King78868962013-09-28 04:32:02 -04001803 if (options && options->effective_url)
1804 curlinfo_strbuf(slot->curl, CURLINFO_EFFECTIVE_URL,
1805 options->effective_url);
Mike Hommeye929cd22009-06-06 16:43:53 +08001806
Mike Hommeye929cd22009-06-06 16:43:53 +08001807 curl_slist_free_all(headers);
1808 strbuf_release(&buf);
1809
1810 return ret;
1811}
1812
Jeff Kingc93c92f2013-09-28 04:34:05 -04001813/*
1814 * Update the "base" url to a more appropriate value, as deduced by
1815 * redirects seen when requesting a URL starting with "url".
1816 *
1817 * The "asked" parameter is a URL that we asked curl to access, and must begin
1818 * with "base".
1819 *
1820 * The "got" parameter is the URL that curl reported to us as where we ended
1821 * up.
1822 *
1823 * Returns 1 if we updated the base url, 0 otherwise.
1824 *
1825 * Our basic strategy is to compare "base" and "asked" to find the bits
1826 * specific to our request. We then strip those bits off of "got" to yield the
1827 * new base. So for example, if our base is "http://example.com/foo.git",
1828 * and we ask for "http://example.com/foo.git/info/refs", we might end up
1829 * with "https://other.example.com/foo.git/info/refs". We would want the
1830 * new URL to become "https://other.example.com/foo.git".
1831 *
1832 * Note that this assumes a sane redirect scheme. It's entirely possible
1833 * in the example above to end up at a URL that does not even end in
Jeff King6628eb42016-12-06 13:24:35 -05001834 * "info/refs". In such a case we die. There's not much we can do, such a
1835 * scheme is unlikely to represent a real git repository, and failing to
1836 * rewrite the base opens options for malicious redirects to do funny things.
Jeff Kingc93c92f2013-09-28 04:34:05 -04001837 */
1838static int update_url_from_redirect(struct strbuf *base,
1839 const char *asked,
1840 const struct strbuf *got)
Jeff King8d677ed2011-07-18 03:50:14 -04001841{
Jeff Kingc93c92f2013-09-28 04:34:05 -04001842 const char *tail;
Jeff King986d7f42016-12-06 13:24:29 -05001843 size_t new_len;
Jeff Kingc93c92f2013-09-28 04:34:05 -04001844
1845 if (!strcmp(asked, got->buf))
1846 return 0;
1847
Jeff Kingde8118e2014-06-18 15:57:17 -04001848 if (!skip_prefix(asked, base->buf, &tail))
Johannes Schindelin033abf92018-05-02 11:38:39 +02001849 BUG("update_url_from_redirect: %s is not a superset of %s",
Jeff Kingc93c92f2013-09-28 04:34:05 -04001850 asked, base->buf);
1851
Jeff King986d7f42016-12-06 13:24:29 -05001852 new_len = got->len;
1853 if (!strip_suffix_mem(got->buf, &new_len, tail))
Jeff King6628eb42016-12-06 13:24:35 -05001854 die(_("unable to update url base from redirection:\n"
1855 " asked for: %s\n"
1856 " redirect: %s"),
1857 asked, got->buf);
Jeff Kingc93c92f2013-09-28 04:34:05 -04001858
1859 strbuf_reset(base);
Jeff King986d7f42016-12-06 13:24:29 -05001860 strbuf_add(base, got->buf, new_len);
Jeff King6628eb42016-12-06 13:24:35 -05001861
Jeff Kingc93c92f2013-09-28 04:34:05 -04001862 return 1;
1863}
1864
Mike Hommeye929cd22009-06-06 16:43:53 +08001865static int http_request_reauth(const char *url,
Jeff King8d677ed2011-07-18 03:50:14 -04001866 void *result, int target,
Jeff King1bbcc222013-09-28 04:31:23 -04001867 struct http_get_options *options)
Jeff King8d677ed2011-07-18 03:50:14 -04001868{
Jeff King1bbcc222013-09-28 04:31:23 -04001869 int ret = http_request(url, result, target, options);
Jeff Kingc93c92f2013-09-28 04:34:05 -04001870
Jonathan Tan8e273912017-02-27 18:53:11 -08001871 if (ret != HTTP_OK && ret != HTTP_REAUTH)
1872 return ret;
1873
Jeff Kingc93c92f2013-09-28 04:34:05 -04001874 if (options && options->effective_url && options->base_url) {
1875 if (update_url_from_redirect(options->base_url,
1876 url, options->effective_url)) {
1877 credential_from_url(&http_auth, options->base_url->buf);
1878 url = options->effective_url->buf;
1879 }
1880 }
1881
Jeff King8d677ed2011-07-18 03:50:14 -04001882 if (ret != HTTP_REAUTH)
1883 return ret;
Jeff King6d052d72013-04-05 18:14:06 -04001884
1885 /*
1886 * If we are using KEEP_ERROR, the previous request may have
1887 * put cruft into our output stream; we should clear it out before
1888 * making our next request. We only know how to do this for
1889 * the strbuf case, but that is enough to satisfy current callers.
1890 */
Jeff King1bbcc222013-09-28 04:31:23 -04001891 if (options && options->keep_error) {
Jeff King6d052d72013-04-05 18:14:06 -04001892 switch (target) {
1893 case HTTP_REQUEST_STRBUF:
1894 strbuf_reset(result);
1895 break;
1896 default:
Johannes Schindelin033abf92018-05-02 11:38:39 +02001897 BUG("HTTP_KEEP_ERROR is only supported with strbufs");
Jeff King6d052d72013-04-05 18:14:06 -04001898 }
1899 }
Jeff King2501aff2013-09-28 04:31:45 -04001900
1901 credential_fill(&http_auth);
1902
Jeff King1bbcc222013-09-28 04:31:23 -04001903 return http_request(url, result, target, options);
Jeff King8d677ed2011-07-18 03:50:14 -04001904}
1905
Shawn Pearce4656bf42013-01-31 13:02:07 -08001906int http_get_strbuf(const char *url,
Jeff King1bbcc222013-09-28 04:31:23 -04001907 struct strbuf *result,
1908 struct http_get_options *options)
Mike Hommeye929cd22009-06-06 16:43:53 +08001909{
Jeff King1bbcc222013-09-28 04:31:23 -04001910 return http_request_reauth(url, result, HTTP_REQUEST_STRBUF, options);
Mike Hommeye929cd22009-06-06 16:43:53 +08001911}
1912
Junio C Hamano83e41e22010-01-11 22:26:08 -08001913/*
Jim Meyeringa7793a72012-03-28 10:41:54 +02001914 * Downloads a URL and stores the result in the given file.
Junio C Hamano83e41e22010-01-11 22:26:08 -08001915 *
1916 * If a previous interrupted download is detected (i.e. a previous temporary
1917 * file is still around) the download is resumed.
1918 */
Jeff King1bbcc222013-09-28 04:31:23 -04001919static int http_get_file(const char *url, const char *filename,
1920 struct http_get_options *options)
Mike Hommeye929cd22009-06-06 16:43:53 +08001921{
1922 int ret;
1923 struct strbuf tmpfile = STRBUF_INIT;
1924 FILE *result;
1925
1926 strbuf_addf(&tmpfile, "%s.temp", filename);
1927 result = fopen(tmpfile.buf, "a");
Jeff King3d1fb762013-09-28 04:31:00 -04001928 if (!result) {
Mike Hommeye929cd22009-06-06 16:43:53 +08001929 error("Unable to open local file %s", tmpfile.buf);
1930 ret = HTTP_ERROR;
1931 goto cleanup;
1932 }
1933
Jeff King1bbcc222013-09-28 04:31:23 -04001934 ret = http_request_reauth(url, result, HTTP_REQUEST_FILE, options);
Mike Hommeye929cd22009-06-06 16:43:53 +08001935 fclose(result);
1936
Junio C Hamanocb5add52015-08-07 14:40:24 -07001937 if (ret == HTTP_OK && finalize_object_file(tmpfile.buf, filename))
Mike Hommeye929cd22009-06-06 16:43:53 +08001938 ret = HTTP_ERROR;
1939cleanup:
1940 strbuf_release(&tmpfile);
1941 return ret;
1942}
1943
Daniel Barkalowc13b2632008-04-26 15:53:09 -04001944int http_fetch_ref(const char *base, struct ref *ref)
Mike Hommeyd7e92802007-12-11 00:08:25 +01001945{
Jeff King1bbcc222013-09-28 04:31:23 -04001946 struct http_get_options options = {0};
Mike Hommeyd7e92802007-12-11 00:08:25 +01001947 char *url;
1948 struct strbuf buffer = STRBUF_INIT;
Mike Hommey0d5896e2009-06-06 16:43:55 +08001949 int ret = -1;
Mike Hommeyd7e92802007-12-11 00:08:25 +01001950
Jeff King1bbcc222013-09-28 04:31:23 -04001951 options.no_cache = 1;
1952
Daniel Barkalowc13b2632008-04-26 15:53:09 -04001953 url = quote_ref_url(base, ref->name);
Jeff King1bbcc222013-09-28 04:31:23 -04001954 if (http_get_strbuf(url, &buffer, &options) == HTTP_OK) {
Mike Hommey0d5896e2009-06-06 16:43:55 +08001955 strbuf_rtrim(&buffer);
1956 if (buffer.len == 40)
brian m. carlsonf4e54d02015-11-10 02:22:20 +00001957 ret = get_oid_hex(buffer.buf, &ref->old_oid);
Christian Couder59556542013-11-30 21:55:40 +01001958 else if (starts_with(buffer.buf, "ref: ")) {
Mike Hommey0d5896e2009-06-06 16:43:55 +08001959 ref->symref = xstrdup(buffer.buf + 5);
1960 ret = 0;
Mike Hommeyd7e92802007-12-11 00:08:25 +01001961 }
Mike Hommeyd7e92802007-12-11 00:08:25 +01001962 }
1963
1964 strbuf_release(&buffer);
1965 free(url);
1966 return ret;
1967}
Tay Ray Chuanb8caac22009-06-06 16:43:59 +08001968
1969/* Helpers for fetching packs */
Shawn O. Pearce750ef422010-04-19 07:23:10 -07001970static char *fetch_pack_index(unsigned char *sha1, const char *base_url)
Tay Ray Chuanb8caac22009-06-06 16:43:59 +08001971{
Shawn O. Pearce750ef422010-04-19 07:23:10 -07001972 char *url, *tmp;
Tay Ray Chuanb8caac22009-06-06 16:43:59 +08001973 struct strbuf buf = STRBUF_INIT;
Tay Ray Chuanb8caac22009-06-06 16:43:59 +08001974
Tay Ray Chuanb8caac22009-06-06 16:43:59 +08001975 if (http_is_verbose)
Shawn O. Pearce162eb5f2010-04-19 07:23:05 -07001976 fprintf(stderr, "Getting index for pack %s\n", sha1_to_hex(sha1));
Tay Ray Chuanb8caac22009-06-06 16:43:59 +08001977
1978 end_url_with_slash(&buf, base_url);
Shawn O. Pearce162eb5f2010-04-19 07:23:05 -07001979 strbuf_addf(&buf, "objects/pack/pack-%s.idx", sha1_to_hex(sha1));
Tay Ray Chuanb8caac22009-06-06 16:43:59 +08001980 url = strbuf_detach(&buf, NULL);
1981
Shawn O. Pearce750ef422010-04-19 07:23:10 -07001982 strbuf_addf(&buf, "%s.temp", sha1_pack_index_name(sha1));
1983 tmp = strbuf_detach(&buf, NULL);
Tay Ray Chuanb8caac22009-06-06 16:43:59 +08001984
Ramsay Jones70900ed2013-10-24 21:17:19 +01001985 if (http_get_file(url, tmp, NULL) != HTTP_OK) {
Pete Wyckoff82247e92012-04-29 20:28:45 -04001986 error("Unable to get pack index %s", url);
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +00001987 FREE_AND_NULL(tmp);
Shawn O. Pearce750ef422010-04-19 07:23:10 -07001988 }
1989
Tay Ray Chuanb8caac22009-06-06 16:43:59 +08001990 free(url);
Shawn O. Pearce750ef422010-04-19 07:23:10 -07001991 return tmp;
Tay Ray Chuanb8caac22009-06-06 16:43:59 +08001992}
1993
1994static int fetch_and_setup_pack_index(struct packed_git **packs_head,
1995 unsigned char *sha1, const char *base_url)
1996{
1997 struct packed_git *new_pack;
Shawn O. Pearce750ef422010-04-19 07:23:10 -07001998 char *tmp_idx = NULL;
1999 int ret;
Tay Ray Chuanb8caac22009-06-06 16:43:59 +08002000
Shawn O. Pearce750ef422010-04-19 07:23:10 -07002001 if (has_pack_index(sha1)) {
Jeff King8b9c2dd2015-01-27 15:02:27 -05002002 new_pack = parse_pack_index(sha1, sha1_pack_index_name(sha1));
Shawn O. Pearce750ef422010-04-19 07:23:10 -07002003 if (!new_pack)
2004 return -1; /* parse_pack_index() already issued error message */
2005 goto add_pack;
2006 }
2007
2008 tmp_idx = fetch_pack_index(sha1, base_url);
2009 if (!tmp_idx)
Tay Ray Chuanb8caac22009-06-06 16:43:59 +08002010 return -1;
2011
Shawn O. Pearce750ef422010-04-19 07:23:10 -07002012 new_pack = parse_pack_index(sha1, tmp_idx);
2013 if (!new_pack) {
2014 unlink(tmp_idx);
2015 free(tmp_idx);
2016
Tay Ray Chuanb8caac22009-06-06 16:43:59 +08002017 return -1; /* parse_pack_index() already issued error message */
Shawn O. Pearce750ef422010-04-19 07:23:10 -07002018 }
2019
2020 ret = verify_pack_index(new_pack);
2021 if (!ret) {
2022 close_pack_index(new_pack);
Junio C Hamanocb5add52015-08-07 14:40:24 -07002023 ret = finalize_object_file(tmp_idx, sha1_pack_index_name(sha1));
Shawn O. Pearce750ef422010-04-19 07:23:10 -07002024 }
2025 free(tmp_idx);
2026 if (ret)
2027 return -1;
2028
2029add_pack:
Tay Ray Chuanb8caac22009-06-06 16:43:59 +08002030 new_pack->next = *packs_head;
2031 *packs_head = new_pack;
2032 return 0;
2033}
2034
2035int http_get_info_packs(const char *base_url, struct packed_git **packs_head)
2036{
Jeff King1bbcc222013-09-28 04:31:23 -04002037 struct http_get_options options = {0};
Tay Ray Chuanb8caac22009-06-06 16:43:59 +08002038 int ret = 0, i = 0;
2039 char *url, *data;
2040 struct strbuf buf = STRBUF_INIT;
brian m. carlsondd724bc2018-05-02 00:25:49 +00002041 unsigned char hash[GIT_MAX_RAWSZ];
2042 const unsigned hexsz = the_hash_algo->hexsz;
Tay Ray Chuanb8caac22009-06-06 16:43:59 +08002043
2044 end_url_with_slash(&buf, base_url);
2045 strbuf_addstr(&buf, "objects/info/packs");
2046 url = strbuf_detach(&buf, NULL);
2047
Jeff King1bbcc222013-09-28 04:31:23 -04002048 options.no_cache = 1;
2049 ret = http_get_strbuf(url, &buf, &options);
Tay Ray Chuanb8caac22009-06-06 16:43:59 +08002050 if (ret != HTTP_OK)
2051 goto cleanup;
2052
2053 data = buf.buf;
2054 while (i < buf.len) {
2055 switch (data[i]) {
2056 case 'P':
2057 i++;
brian m. carlsondd724bc2018-05-02 00:25:49 +00002058 if (i + hexsz + 12 <= buf.len &&
Christian Couder59556542013-11-30 21:55:40 +01002059 starts_with(data + i, " pack-") &&
brian m. carlsondd724bc2018-05-02 00:25:49 +00002060 starts_with(data + i + hexsz + 6, ".pack\n")) {
2061 get_sha1_hex(data + i + 6, hash);
2062 fetch_and_setup_pack_index(packs_head, hash,
Tay Ray Chuanb8caac22009-06-06 16:43:59 +08002063 base_url);
brian m. carlsondd724bc2018-05-02 00:25:49 +00002064 i += hexsz + 11;
Tay Ray Chuanb8caac22009-06-06 16:43:59 +08002065 break;
2066 }
2067 default:
2068 while (i < buf.len && data[i] != '\n')
2069 i++;
2070 }
2071 i++;
2072 }
2073
2074cleanup:
2075 free(url);
2076 return ret;
2077}
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +08002078
2079void release_http_pack_request(struct http_pack_request *preq)
2080{
2081 if (preq->packfile != NULL) {
2082 fclose(preq->packfile);
2083 preq->packfile = NULL;
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +08002084 }
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +08002085 preq->slot = NULL;
Jeff King390c6cb2018-05-18 18:56:37 -07002086 strbuf_release(&preq->tmpfile);
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +08002087 free(preq->url);
Stefan Beller826aed52015-03-20 17:28:06 -07002088 free(preq);
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +08002089}
2090
2091int finish_http_pack_request(struct http_pack_request *preq)
2092{
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +08002093 struct packed_git **lst;
Shawn O. Pearce021ab6f2010-04-17 13:07:36 -07002094 struct packed_git *p = preq->target;
Shawn O. Pearcefe72d422010-04-19 07:23:09 -07002095 char *tmp_idx;
Jeff King9ae97012015-09-24 17:07:09 -04002096 size_t len;
René Scharfed3180272014-08-19 21:09:35 +02002097 struct child_process ip = CHILD_PROCESS_INIT;
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +08002098
Shawn O. Pearcefe72d422010-04-19 07:23:09 -07002099 close_pack_index(p);
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +08002100
Shawn O. Pearce30652742010-04-17 13:07:37 -07002101 fclose(preq->packfile);
2102 preq->packfile = NULL;
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +08002103
2104 lst = preq->lst;
Shawn O. Pearce021ab6f2010-04-17 13:07:36 -07002105 while (*lst != p)
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +08002106 lst = &((*lst)->next);
2107 *lst = (*lst)->next;
2108
Jeff King390c6cb2018-05-18 18:56:37 -07002109 if (!strip_suffix(preq->tmpfile.buf, ".pack.temp", &len))
Johannes Schindelin033abf92018-05-02 11:38:39 +02002110 BUG("pack tmpfile does not end in .pack.temp?");
Jeff King390c6cb2018-05-18 18:56:37 -07002111 tmp_idx = xstrfmt("%.*s.idx.temp", (int)len, preq->tmpfile.buf);
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +08002112
René Scharfec7191fa2017-12-22 12:56:59 +01002113 argv_array_push(&ip.args, "index-pack");
2114 argv_array_pushl(&ip.args, "-o", tmp_idx, NULL);
Jeff King390c6cb2018-05-18 18:56:37 -07002115 argv_array_push(&ip.args, preq->tmpfile.buf);
Shawn O. Pearcefe72d422010-04-19 07:23:09 -07002116 ip.git_cmd = 1;
2117 ip.no_stdin = 1;
2118 ip.no_stdout = 1;
2119
2120 if (run_command(&ip)) {
Jeff King390c6cb2018-05-18 18:56:37 -07002121 unlink(preq->tmpfile.buf);
Shawn O. Pearcefe72d422010-04-19 07:23:09 -07002122 unlink(tmp_idx);
2123 free(tmp_idx);
2124 return -1;
2125 }
2126
2127 unlink(sha1_pack_index_name(p->sha1));
2128
Jeff King390c6cb2018-05-18 18:56:37 -07002129 if (finalize_object_file(preq->tmpfile.buf, sha1_pack_name(p->sha1))
Junio C Hamanocb5add52015-08-07 14:40:24 -07002130 || finalize_object_file(tmp_idx, sha1_pack_index_name(p->sha1))) {
Shawn O. Pearcefe72d422010-04-19 07:23:09 -07002131 free(tmp_idx);
2132 return -1;
2133 }
2134
Stefan Beller5babff12018-03-23 18:45:18 +01002135 install_packed_git(the_repository, p);
Shawn O. Pearcefe72d422010-04-19 07:23:09 -07002136 free(tmp_idx);
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +08002137 return 0;
2138}
2139
2140struct http_pack_request *new_http_pack_request(
2141 struct packed_git *target, const char *base_url)
2142{
Jeff Kingf8117f52015-11-02 17:10:27 -05002143 off_t prev_posn = 0;
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +08002144 struct strbuf buf = STRBUF_INIT;
2145 struct http_pack_request *preq;
2146
Tay Ray Chuanec99c9a2011-08-03 19:54:03 +08002147 preq = xcalloc(1, sizeof(*preq));
Jeff King390c6cb2018-05-18 18:56:37 -07002148 strbuf_init(&preq->tmpfile, 0);
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +08002149 preq->target = target;
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +08002150
2151 end_url_with_slash(&buf, base_url);
2152 strbuf_addf(&buf, "objects/pack/pack-%s.pack",
2153 sha1_to_hex(target->sha1));
Tay Ray Chuanbb991902009-08-10 23:59:55 +08002154 preq->url = strbuf_detach(&buf, NULL);
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +08002155
Jeff King390c6cb2018-05-18 18:56:37 -07002156 strbuf_addf(&preq->tmpfile, "%s.temp", sha1_pack_name(target->sha1));
2157 preq->packfile = fopen(preq->tmpfile.buf, "a");
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +08002158 if (!preq->packfile) {
2159 error("Unable to open local file %s for pack",
Jeff King390c6cb2018-05-18 18:56:37 -07002160 preq->tmpfile.buf);
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +08002161 goto abort;
2162 }
2163
2164 preq->slot = get_active_slot();
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +08002165 curl_easy_setopt(preq->slot->curl, CURLOPT_FILE, preq->packfile);
2166 curl_easy_setopt(preq->slot->curl, CURLOPT_WRITEFUNCTION, fwrite);
Tay Ray Chuanbb991902009-08-10 23:59:55 +08002167 curl_easy_setopt(preq->slot->curl, CURLOPT_URL, preq->url);
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +08002168 curl_easy_setopt(preq->slot->curl, CURLOPT_HTTPHEADER,
2169 no_pragma_header);
2170
2171 /*
2172 * If there is data present from a previous transfer attempt,
2173 * resume where it left off
2174 */
Jeff Kingf8117f52015-11-02 17:10:27 -05002175 prev_posn = ftello(preq->packfile);
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +08002176 if (prev_posn>0) {
2177 if (http_is_verbose)
2178 fprintf(stderr,
Ramsay Jones838ecf02015-11-12 00:07:42 +00002179 "Resuming fetch of pack %s at byte %"PRIuMAX"\n",
2180 sha1_to_hex(target->sha1), (uintmax_t)prev_posn);
David Turner835c4d32015-11-02 16:39:58 -05002181 http_opt_request_remainder(preq->slot->curl, prev_posn);
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +08002182 }
2183
2184 return preq;
2185
2186abort:
Jeff King390c6cb2018-05-18 18:56:37 -07002187 strbuf_release(&preq->tmpfile);
Tay Ray Chuanbb991902009-08-10 23:59:55 +08002188 free(preq->url);
Tay Ray Chuan5ae9ebf2009-08-10 23:55:48 +08002189 free(preq);
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +08002190 return NULL;
2191}
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002192
2193/* Helpers for fetching objects (loose) */
Dan McGeea04ff3e2011-05-03 23:47:27 +08002194static size_t fwrite_sha1_file(char *ptr, size_t eltsize, size_t nmemb,
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002195 void *data)
2196{
2197 unsigned char expn[4096];
2198 size_t size = eltsize * nmemb;
2199 int posn = 0;
Eric Wong17966c02016-07-11 20:51:30 +00002200 struct http_object_request *freq = data;
2201 struct active_request_slot *slot = freq->slot;
2202
2203 if (slot) {
2204 CURLcode c = curl_easy_getinfo(slot->curl, CURLINFO_HTTP_CODE,
2205 &slot->http_code);
2206 if (c != CURLE_OK)
Johannes Schindelin033abf92018-05-02 11:38:39 +02002207 BUG("curl_easy_getinfo for HTTP code failed: %s",
Eric Wong17966c02016-07-11 20:51:30 +00002208 curl_easy_strerror(c));
Jeff King3680f162016-12-06 13:25:39 -05002209 if (slot->http_code >= 300)
Eric Wong17966c02016-07-11 20:51:30 +00002210 return size;
2211 }
2212
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002213 do {
2214 ssize_t retval = xwrite(freq->localfile,
2215 (char *) ptr + posn, size - posn);
2216 if (retval < 0)
2217 return posn;
2218 posn += retval;
2219 } while (posn < size);
2220
2221 freq->stream.avail_in = size;
Dan McGeea04ff3e2011-05-03 23:47:27 +08002222 freq->stream.next_in = (void *)ptr;
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002223 do {
2224 freq->stream.next_out = expn;
2225 freq->stream.avail_out = sizeof(expn);
2226 freq->zret = git_inflate(&freq->stream, Z_SYNC_FLUSH);
2227 git_SHA1_Update(&freq->c, expn,
2228 sizeof(expn) - freq->stream.avail_out);
2229 } while (freq->stream.avail_in && freq->zret == Z_OK);
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002230 return size;
2231}
2232
2233struct http_object_request *new_http_object_request(const char *base_url,
2234 unsigned char *sha1)
2235{
2236 char *hex = sha1_to_hex(sha1);
Christian Couderea657732018-01-17 18:54:54 +01002237 struct strbuf filename = STRBUF_INIT;
Jeff King390c6cb2018-05-18 18:56:37 -07002238 struct strbuf prevfile = STRBUF_INIT;
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002239 int prevlocal;
Dan McGeea04ff3e2011-05-03 23:47:27 +08002240 char prev_buf[PREV_BUF_SIZE];
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002241 ssize_t prev_read = 0;
Jeff Kingf8117f52015-11-02 17:10:27 -05002242 off_t prev_posn = 0;
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002243 struct http_object_request *freq;
2244
Tay Ray Chuanec99c9a2011-08-03 19:54:03 +08002245 freq = xcalloc(1, sizeof(*freq));
Jeff King390c6cb2018-05-18 18:56:37 -07002246 strbuf_init(&freq->tmpfile, 0);
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002247 hashcpy(freq->sha1, sha1);
2248 freq->localfile = -1;
2249
Stefan Bellercf78ae42018-03-23 18:21:10 +01002250 sha1_file_name(the_repository, &filename, sha1);
Jeff King390c6cb2018-05-18 18:56:37 -07002251 strbuf_addf(&freq->tmpfile, "%s.temp", filename.buf);
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002252
Jeff King390c6cb2018-05-18 18:56:37 -07002253 strbuf_addf(&prevfile, "%s.prev", filename.buf);
2254 unlink_or_warn(prevfile.buf);
2255 rename(freq->tmpfile.buf, prevfile.buf);
2256 unlink_or_warn(freq->tmpfile.buf);
Christian Couderea657732018-01-17 18:54:54 +01002257 strbuf_release(&filename);
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002258
2259 if (freq->localfile != -1)
2260 error("fd leakage in start: %d", freq->localfile);
Jeff King390c6cb2018-05-18 18:56:37 -07002261 freq->localfile = open(freq->tmpfile.buf,
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002262 O_WRONLY | O_CREAT | O_EXCL, 0666);
2263 /*
2264 * This could have failed due to the "lazy directory creation";
2265 * try to mkdir the last path component.
2266 */
2267 if (freq->localfile < 0 && errno == ENOENT) {
Jeff King390c6cb2018-05-18 18:56:37 -07002268 char *dir = strrchr(freq->tmpfile.buf, '/');
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002269 if (dir) {
2270 *dir = 0;
Jeff King390c6cb2018-05-18 18:56:37 -07002271 mkdir(freq->tmpfile.buf, 0777);
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002272 *dir = '/';
2273 }
Jeff King390c6cb2018-05-18 18:56:37 -07002274 freq->localfile = open(freq->tmpfile.buf,
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002275 O_WRONLY | O_CREAT | O_EXCL, 0666);
2276 }
2277
2278 if (freq->localfile < 0) {
Jeff King390c6cb2018-05-18 18:56:37 -07002279 error_errno("Couldn't create temporary file %s",
2280 freq->tmpfile.buf);
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002281 goto abort;
2282 }
2283
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002284 git_inflate_init(&freq->stream);
2285
2286 git_SHA1_Init(&freq->c);
2287
Tay Ray Chuanbb991902009-08-10 23:59:55 +08002288 freq->url = get_remote_object_url(base_url, hex, 0);
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002289
2290 /*
2291 * If a previous temp file is present, process what was already
2292 * fetched.
2293 */
Jeff King390c6cb2018-05-18 18:56:37 -07002294 prevlocal = open(prevfile.buf, O_RDONLY);
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002295 if (prevlocal != -1) {
2296 do {
2297 prev_read = xread(prevlocal, prev_buf, PREV_BUF_SIZE);
2298 if (prev_read>0) {
2299 if (fwrite_sha1_file(prev_buf,
2300 1,
2301 prev_read,
2302 freq) == prev_read) {
2303 prev_posn += prev_read;
2304 } else {
2305 prev_read = -1;
2306 }
2307 }
2308 } while (prev_read > 0);
2309 close(prevlocal);
2310 }
Jeff King390c6cb2018-05-18 18:56:37 -07002311 unlink_or_warn(prevfile.buf);
2312 strbuf_release(&prevfile);
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002313
2314 /*
2315 * Reset inflate/SHA1 if there was an error reading the previous temp
2316 * file; also rewind to the beginning of the local file.
2317 */
2318 if (prev_read == -1) {
2319 memset(&freq->stream, 0, sizeof(freq->stream));
2320 git_inflate_init(&freq->stream);
2321 git_SHA1_Init(&freq->c);
2322 if (prev_posn>0) {
2323 prev_posn = 0;
2324 lseek(freq->localfile, 0, SEEK_SET);
Jeff Lasslett0c4f21e2009-08-11 00:05:06 +08002325 if (ftruncate(freq->localfile, 0) < 0) {
Nguyễn Thái Ngọc Duyd2e255e2016-05-08 16:47:48 +07002326 error_errno("Couldn't truncate temporary file %s",
Jeff King390c6cb2018-05-18 18:56:37 -07002327 freq->tmpfile.buf);
Jeff Lasslett0c4f21e2009-08-11 00:05:06 +08002328 goto abort;
2329 }
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002330 }
2331 }
2332
2333 freq->slot = get_active_slot();
2334
2335 curl_easy_setopt(freq->slot->curl, CURLOPT_FILE, freq);
Eric Wong17966c02016-07-11 20:51:30 +00002336 curl_easy_setopt(freq->slot->curl, CURLOPT_FAILONERROR, 0);
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002337 curl_easy_setopt(freq->slot->curl, CURLOPT_WRITEFUNCTION, fwrite_sha1_file);
2338 curl_easy_setopt(freq->slot->curl, CURLOPT_ERRORBUFFER, freq->errorstr);
Tay Ray Chuanbb991902009-08-10 23:59:55 +08002339 curl_easy_setopt(freq->slot->curl, CURLOPT_URL, freq->url);
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002340 curl_easy_setopt(freq->slot->curl, CURLOPT_HTTPHEADER, no_pragma_header);
2341
2342 /*
2343 * If we have successfully processed data from a previous fetch
2344 * attempt, only fetch the data we don't already have.
2345 */
2346 if (prev_posn>0) {
2347 if (http_is_verbose)
2348 fprintf(stderr,
Ramsay Jones838ecf02015-11-12 00:07:42 +00002349 "Resuming fetch of object %s at byte %"PRIuMAX"\n",
2350 hex, (uintmax_t)prev_posn);
David Turner835c4d32015-11-02 16:39:58 -05002351 http_opt_request_remainder(freq->slot->curl, prev_posn);
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002352 }
2353
2354 return freq;
2355
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002356abort:
Jeff King390c6cb2018-05-18 18:56:37 -07002357 strbuf_release(&prevfile);
Tay Ray Chuanbb991902009-08-10 23:59:55 +08002358 free(freq->url);
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002359 free(freq);
2360 return NULL;
2361}
2362
2363void process_http_object_request(struct http_object_request *freq)
2364{
2365 if (freq->slot == NULL)
2366 return;
2367 freq->curl_result = freq->slot->curl_result;
2368 freq->http_code = freq->slot->http_code;
2369 freq->slot = NULL;
2370}
2371
2372int finish_http_object_request(struct http_object_request *freq)
2373{
2374 struct stat st;
Christian Couderea657732018-01-17 18:54:54 +01002375 struct strbuf filename = STRBUF_INIT;
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002376
2377 close(freq->localfile);
2378 freq->localfile = -1;
2379
2380 process_http_object_request(freq);
2381
2382 if (freq->http_code == 416) {
Thiago Farinabd757c12010-01-03 11:20:30 -05002383 warning("requested range invalid; we may already have all the data.");
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002384 } else if (freq->curl_result != CURLE_OK) {
Jeff King390c6cb2018-05-18 18:56:37 -07002385 if (stat(freq->tmpfile.buf, &st) == 0)
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002386 if (st.st_size == 0)
Jeff King390c6cb2018-05-18 18:56:37 -07002387 unlink_or_warn(freq->tmpfile.buf);
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002388 return -1;
2389 }
2390
2391 git_inflate_end(&freq->stream);
2392 git_SHA1_Final(freq->real_sha1, &freq->c);
2393 if (freq->zret != Z_STREAM_END) {
Jeff King390c6cb2018-05-18 18:56:37 -07002394 unlink_or_warn(freq->tmpfile.buf);
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002395 return -1;
2396 }
2397 if (hashcmp(freq->sha1, freq->real_sha1)) {
Jeff King390c6cb2018-05-18 18:56:37 -07002398 unlink_or_warn(freq->tmpfile.buf);
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002399 return -1;
2400 }
Stefan Bellercf78ae42018-03-23 18:21:10 +01002401 sha1_file_name(the_repository, &filename, freq->sha1);
Jeff King390c6cb2018-05-18 18:56:37 -07002402 freq->rename = finalize_object_file(freq->tmpfile.buf, filename.buf);
Christian Couderea657732018-01-17 18:54:54 +01002403 strbuf_release(&filename);
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002404
2405 return freq->rename;
2406}
2407
2408void abort_http_object_request(struct http_object_request *freq)
2409{
Jeff King390c6cb2018-05-18 18:56:37 -07002410 unlink_or_warn(freq->tmpfile.buf);
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002411
2412 release_http_object_request(freq);
2413}
2414
2415void release_http_object_request(struct http_object_request *freq)
2416{
2417 if (freq->localfile != -1) {
2418 close(freq->localfile);
2419 freq->localfile = -1;
2420 }
2421 if (freq->url != NULL) {
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +00002422 FREE_AND_NULL(freq->url);
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002423 }
Tay Ray Chuan4b9fa0e2009-08-26 20:20:53 +08002424 if (freq->slot != NULL) {
2425 freq->slot->callback_func = NULL;
2426 freq->slot->callback_data = NULL;
2427 release_active_slot(freq->slot);
2428 freq->slot = NULL;
2429 }
Jeff King390c6cb2018-05-18 18:56:37 -07002430 strbuf_release(&freq->tmpfile);
Tay Ray Chuan5424bc52009-06-06 16:44:02 +08002431}