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