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