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