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