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