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