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