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