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