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