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