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