]>
Commit | Line | Data |
---|---|---|
29508e1e | 1 | #include "http.h" |
2264dfa5 | 2 | #include "pack.h" |
de1a2fdd | 3 | #include "sideband.h" |
fe72d420 | 4 | #include "run-command.h" |
f39f72d8 | 5 | #include "url.h" |
148bb6a7 | 6 | #include "credential.h" |
29508e1e | 7 | |
4251ccbd | 8 | int active_requests; |
e9176745 | 9 | int http_is_verbose; |
de1a2fdd | 10 | size_t http_post_buffer = 16 * LARGE_PACKET_MAX; |
29508e1e | 11 | |
b8ac9230 MS |
12 | #if LIBCURL_VERSION_NUM >= 0x070a06 |
13 | #define LIBCURL_CAN_HANDLE_AUTH_ANY | |
14 | #endif | |
15 | ||
ad75ebe5 TRC |
16 | static int min_curl_sessions = 1; |
17 | static int curl_session_count; | |
29508e1e | 18 | #ifdef USE_CURL_MULTI |
cc3530e8 MH |
19 | static int max_requests = -1; |
20 | static CURLM *curlm; | |
29508e1e NH |
21 | #endif |
22 | #ifndef NO_CURL_EASY_DUPHANDLE | |
cc3530e8 | 23 | static CURL *curl_default; |
29508e1e | 24 | #endif |
5424bc55 TRC |
25 | |
26 | #define PREV_BUF_SIZE 4096 | |
27 | #define RANGE_HEADER_SIZE 30 | |
28 | ||
29508e1e NH |
29 | char curl_errorstr[CURL_ERROR_SIZE]; |
30 | ||
cc3530e8 | 31 | static int curl_ssl_verify = -1; |
4251ccbd | 32 | static const char *ssl_cert; |
ef52aafa | 33 | #if LIBCURL_VERSION_NUM >= 0x070903 |
4251ccbd | 34 | static const char *ssl_key; |
29508e1e NH |
35 | #endif |
36 | #if LIBCURL_VERSION_NUM >= 0x070908 | |
4251ccbd | 37 | static const char *ssl_capath; |
29508e1e | 38 | #endif |
4251ccbd | 39 | static const char *ssl_cainfo; |
cc3530e8 MH |
40 | static long curl_low_speed_limit = -1; |
41 | static long curl_low_speed_time = -1; | |
4251ccbd JH |
42 | static int curl_ftp_no_epsv; |
43 | static const char *curl_http_proxy; | |
bcfb95dd | 44 | static const char *curl_cookie_file; |
148bb6a7 | 45 | static struct credential http_auth = CREDENTIAL_INIT; |
a4ddbc33 | 46 | static int http_proactive_auth; |
b1d1058c | 47 | static const char *user_agent; |
29508e1e | 48 | |
30dd9163 ML |
49 | #if LIBCURL_VERSION_NUM >= 0x071700 |
50 | /* Use CURLOPT_KEYPASSWD as is */ | |
51 | #elif LIBCURL_VERSION_NUM >= 0x070903 | |
52 | #define CURLOPT_KEYPASSWD CURLOPT_SSLKEYPASSWD | |
53 | #else | |
54 | #define CURLOPT_KEYPASSWD CURLOPT_SSLCERTPASSWD | |
55 | #endif | |
56 | ||
148bb6a7 | 57 | static struct credential cert_auth = CREDENTIAL_INIT; |
30dd9163 ML |
58 | static int ssl_cert_password_required; |
59 | ||
cc3530e8 | 60 | static struct curl_slist *pragma_header; |
5424bc55 | 61 | static struct curl_slist *no_pragma_header; |
e9176745 | 62 | |
4251ccbd | 63 | static struct active_request_slot *active_queue_head; |
29508e1e | 64 | |
a04ff3ec | 65 | size_t fread_buffer(char *ptr, size_t eltsize, size_t nmemb, void *buffer_) |
29508e1e NH |
66 | { |
67 | size_t size = eltsize * nmemb; | |
f444e528 JH |
68 | struct buffer *buffer = buffer_; |
69 | ||
028c2976 MH |
70 | if (size > buffer->buf.len - buffer->posn) |
71 | size = buffer->buf.len - buffer->posn; | |
72 | memcpy(ptr, buffer->buf.buf + buffer->posn, size); | |
29508e1e | 73 | buffer->posn += size; |
028c2976 | 74 | |
29508e1e NH |
75 | return size; |
76 | } | |
77 | ||
3944ba0c MS |
78 | #ifndef NO_CURL_IOCTL |
79 | curlioerr ioctl_buffer(CURL *handle, int cmd, void *clientp) | |
80 | { | |
81 | struct buffer *buffer = clientp; | |
82 | ||
83 | switch (cmd) { | |
84 | case CURLIOCMD_NOP: | |
85 | return CURLIOE_OK; | |
86 | ||
87 | case CURLIOCMD_RESTARTREAD: | |
88 | buffer->posn = 0; | |
89 | return CURLIOE_OK; | |
90 | ||
91 | default: | |
92 | return CURLIOE_UNKNOWNCMD; | |
93 | } | |
94 | } | |
95 | #endif | |
96 | ||
a04ff3ec | 97 | size_t fwrite_buffer(char *ptr, size_t eltsize, size_t nmemb, void *buffer_) |
29508e1e NH |
98 | { |
99 | size_t size = eltsize * nmemb; | |
f444e528 JH |
100 | struct strbuf *buffer = buffer_; |
101 | ||
028c2976 | 102 | strbuf_add(buffer, ptr, size); |
29508e1e NH |
103 | return size; |
104 | } | |
105 | ||
a04ff3ec | 106 | size_t fwrite_null(char *ptr, size_t eltsize, size_t nmemb, void *strbuf) |
29508e1e | 107 | { |
29508e1e NH |
108 | return eltsize * nmemb; |
109 | } | |
110 | ||
29508e1e NH |
111 | #ifdef USE_CURL_MULTI |
112 | static void process_curl_messages(void) | |
113 | { | |
114 | int num_messages; | |
115 | struct active_request_slot *slot; | |
116 | CURLMsg *curl_message = curl_multi_info_read(curlm, &num_messages); | |
117 | ||
118 | while (curl_message != NULL) { | |
119 | if (curl_message->msg == CURLMSG_DONE) { | |
120 | int curl_result = curl_message->data.result; | |
121 | slot = active_queue_head; | |
122 | while (slot != NULL && | |
123 | slot->curl != curl_message->easy_handle) | |
124 | slot = slot->next; | |
125 | if (slot != NULL) { | |
126 | curl_multi_remove_handle(curlm, slot->curl); | |
127 | slot->curl_result = curl_result; | |
128 | finish_active_slot(slot); | |
129 | } else { | |
130 | fprintf(stderr, "Received DONE message for unknown request!\n"); | |
131 | } | |
132 | } else { | |
133 | fprintf(stderr, "Unknown CURL message received: %d\n", | |
134 | (int)curl_message->msg); | |
135 | } | |
136 | curl_message = curl_multi_info_read(curlm, &num_messages); | |
137 | } | |
138 | } | |
139 | #endif | |
140 | ||
ef90d6d4 | 141 | static int http_options(const char *var, const char *value, void *cb) |
29508e1e NH |
142 | { |
143 | if (!strcmp("http.sslverify", var)) { | |
7059cd99 | 144 | curl_ssl_verify = git_config_bool(var, value); |
29508e1e NH |
145 | return 0; |
146 | } | |
7059cd99 JH |
147 | if (!strcmp("http.sslcert", var)) |
148 | return git_config_string(&ssl_cert, var, value); | |
ef52aafa | 149 | #if LIBCURL_VERSION_NUM >= 0x070903 |
7059cd99 JH |
150 | if (!strcmp("http.sslkey", var)) |
151 | return git_config_string(&ssl_key, var, value); | |
29508e1e NH |
152 | #endif |
153 | #if LIBCURL_VERSION_NUM >= 0x070908 | |
7059cd99 JH |
154 | if (!strcmp("http.sslcapath", var)) |
155 | return git_config_string(&ssl_capath, var, value); | |
29508e1e | 156 | #endif |
7059cd99 JH |
157 | if (!strcmp("http.sslcainfo", var)) |
158 | return git_config_string(&ssl_cainfo, var, value); | |
754ae192 ML |
159 | if (!strcmp("http.sslcertpasswordprotected", var)) { |
160 | if (git_config_bool(var, value)) | |
161 | ssl_cert_password_required = 1; | |
162 | return 0; | |
163 | } | |
ad75ebe5 TRC |
164 | if (!strcmp("http.minsessions", var)) { |
165 | min_curl_sessions = git_config_int(var, value); | |
166 | #ifndef USE_CURL_MULTI | |
167 | if (min_curl_sessions > 1) | |
168 | min_curl_sessions = 1; | |
169 | #endif | |
170 | return 0; | |
171 | } | |
a6080a0a | 172 | #ifdef USE_CURL_MULTI |
29508e1e | 173 | if (!strcmp("http.maxrequests", var)) { |
7059cd99 | 174 | max_requests = git_config_int(var, value); |
29508e1e NH |
175 | return 0; |
176 | } | |
177 | #endif | |
29508e1e | 178 | if (!strcmp("http.lowspeedlimit", var)) { |
7059cd99 | 179 | curl_low_speed_limit = (long)git_config_int(var, value); |
29508e1e NH |
180 | return 0; |
181 | } | |
182 | if (!strcmp("http.lowspeedtime", var)) { | |
7059cd99 | 183 | curl_low_speed_time = (long)git_config_int(var, value); |
29508e1e NH |
184 | return 0; |
185 | } | |
186 | ||
3ea099d4 SK |
187 | if (!strcmp("http.noepsv", var)) { |
188 | curl_ftp_no_epsv = git_config_bool(var, value); | |
189 | return 0; | |
190 | } | |
7059cd99 JH |
191 | if (!strcmp("http.proxy", var)) |
192 | return git_config_string(&curl_http_proxy, var, value); | |
3ea099d4 | 193 | |
bcfb95dd DB |
194 | if (!strcmp("http.cookiefile", var)) |
195 | return git_config_string(&curl_cookie_file, var, value); | |
196 | ||
de1a2fdd SP |
197 | if (!strcmp("http.postbuffer", var)) { |
198 | http_post_buffer = git_config_int(var, value); | |
199 | if (http_post_buffer < LARGE_PACKET_MAX) | |
200 | http_post_buffer = LARGE_PACKET_MAX; | |
201 | return 0; | |
202 | } | |
203 | ||
b1d1058c SO |
204 | if (!strcmp("http.useragent", var)) |
205 | return git_config_string(&user_agent, var, value); | |
206 | ||
29508e1e | 207 | /* Fall back on the default ones */ |
ef90d6d4 | 208 | return git_default_config(var, value, cb); |
29508e1e NH |
209 | } |
210 | ||
c33976cb JH |
211 | static void init_curl_http_auth(CURL *result) |
212 | { | |
148bb6a7 | 213 | if (http_auth.username) { |
c33976cb | 214 | struct strbuf up = STRBUF_INIT; |
148bb6a7 JK |
215 | credential_fill(&http_auth); |
216 | strbuf_addf(&up, "%s:%s", | |
217 | http_auth.username, http_auth.password); | |
c33976cb JH |
218 | curl_easy_setopt(result, CURLOPT_USERPWD, |
219 | strbuf_detach(&up, NULL)); | |
220 | } | |
221 | } | |
222 | ||
30dd9163 ML |
223 | static int has_cert_password(void) |
224 | { | |
30dd9163 ML |
225 | if (ssl_cert == NULL || ssl_cert_password_required != 1) |
226 | return 0; | |
148bb6a7 JK |
227 | if (!cert_auth.password) { |
228 | cert_auth.protocol = xstrdup("cert"); | |
229 | cert_auth.path = xstrdup(ssl_cert); | |
230 | credential_fill(&cert_auth); | |
231 | } | |
232 | return 1; | |
30dd9163 ML |
233 | } |
234 | ||
4251ccbd | 235 | static CURL *get_curl_handle(void) |
11979b98 | 236 | { |
4251ccbd | 237 | CURL *result = curl_easy_init(); |
11979b98 | 238 | |
a5ccc597 JH |
239 | if (!curl_ssl_verify) { |
240 | curl_easy_setopt(result, CURLOPT_SSL_VERIFYPEER, 0); | |
241 | curl_easy_setopt(result, CURLOPT_SSL_VERIFYHOST, 0); | |
242 | } else { | |
243 | /* Verify authenticity of the peer's certificate */ | |
244 | curl_easy_setopt(result, CURLOPT_SSL_VERIFYPEER, 1); | |
245 | /* The name in the cert must match whom we tried to connect */ | |
246 | curl_easy_setopt(result, CURLOPT_SSL_VERIFYHOST, 2); | |
247 | } | |
248 | ||
11979b98 JH |
249 | #if LIBCURL_VERSION_NUM >= 0x070907 |
250 | curl_easy_setopt(result, CURLOPT_NETRC, CURL_NETRC_OPTIONAL); | |
251 | #endif | |
b8ac9230 | 252 | #ifdef LIBCURL_CAN_HANDLE_AUTH_ANY |
525ecd26 | 253 | curl_easy_setopt(result, CURLOPT_HTTPAUTH, CURLAUTH_ANY); |
b8ac9230 | 254 | #endif |
11979b98 | 255 | |
a4ddbc33 JK |
256 | if (http_proactive_auth) |
257 | init_curl_http_auth(result); | |
258 | ||
11979b98 JH |
259 | if (ssl_cert != NULL) |
260 | curl_easy_setopt(result, CURLOPT_SSLCERT, ssl_cert); | |
30dd9163 | 261 | if (has_cert_password()) |
148bb6a7 | 262 | curl_easy_setopt(result, CURLOPT_KEYPASSWD, cert_auth.password); |
ef52aafa | 263 | #if LIBCURL_VERSION_NUM >= 0x070903 |
11979b98 JH |
264 | if (ssl_key != NULL) |
265 | curl_easy_setopt(result, CURLOPT_SSLKEY, ssl_key); | |
266 | #endif | |
267 | #if LIBCURL_VERSION_NUM >= 0x070908 | |
268 | if (ssl_capath != NULL) | |
269 | curl_easy_setopt(result, CURLOPT_CAPATH, ssl_capath); | |
270 | #endif | |
271 | if (ssl_cainfo != NULL) | |
272 | curl_easy_setopt(result, CURLOPT_CAINFO, ssl_cainfo); | |
273 | curl_easy_setopt(result, CURLOPT_FAILONERROR, 1); | |
274 | ||
275 | if (curl_low_speed_limit > 0 && curl_low_speed_time > 0) { | |
276 | curl_easy_setopt(result, CURLOPT_LOW_SPEED_LIMIT, | |
277 | curl_low_speed_limit); | |
278 | curl_easy_setopt(result, CURLOPT_LOW_SPEED_TIME, | |
279 | curl_low_speed_time); | |
280 | } | |
281 | ||
282 | curl_easy_setopt(result, CURLOPT_FOLLOWLOCATION, 1); | |
311e2ea0 TRC |
283 | #if LIBCURL_VERSION_NUM >= 0x071301 |
284 | curl_easy_setopt(result, CURLOPT_POSTREDIR, CURL_REDIR_POST_ALL); | |
285 | #elif LIBCURL_VERSION_NUM >= 0x071101 | |
286 | curl_easy_setopt(result, CURLOPT_POST301, 1); | |
287 | #endif | |
11979b98 | 288 | |
7982d74e MW |
289 | if (getenv("GIT_CURL_VERBOSE")) |
290 | curl_easy_setopt(result, CURLOPT_VERBOSE, 1); | |
291 | ||
b1d1058c SO |
292 | curl_easy_setopt(result, CURLOPT_USERAGENT, |
293 | user_agent ? user_agent : GIT_HTTP_USER_AGENT); | |
20fc9bc5 | 294 | |
3ea099d4 SK |
295 | if (curl_ftp_no_epsv) |
296 | curl_easy_setopt(result, CURLOPT_FTP_USE_EPSV, 0); | |
297 | ||
9c5665aa SV |
298 | if (curl_http_proxy) |
299 | curl_easy_setopt(result, CURLOPT_PROXY, curl_http_proxy); | |
300 | ||
11979b98 JH |
301 | return result; |
302 | } | |
303 | ||
7059cd99 JH |
304 | static void set_from_env(const char **var, const char *envname) |
305 | { | |
306 | const char *val = getenv(envname); | |
307 | if (val) | |
308 | *var = val; | |
309 | } | |
310 | ||
a4ddbc33 | 311 | void http_init(struct remote *remote, const char *url, int proactive_auth) |
29508e1e NH |
312 | { |
313 | char *low_speed_limit; | |
314 | char *low_speed_time; | |
315 | ||
e9176745 TRC |
316 | http_is_verbose = 0; |
317 | ||
7059cd99 JH |
318 | git_config(http_options, NULL); |
319 | ||
29508e1e NH |
320 | curl_global_init(CURL_GLOBAL_ALL); |
321 | ||
a4ddbc33 JK |
322 | http_proactive_auth = proactive_auth; |
323 | ||
9fc6440d MH |
324 | if (remote && remote->http_proxy) |
325 | curl_http_proxy = xstrdup(remote->http_proxy); | |
326 | ||
29508e1e | 327 | pragma_header = curl_slist_append(pragma_header, "Pragma: no-cache"); |
e9176745 | 328 | no_pragma_header = curl_slist_append(no_pragma_header, "Pragma:"); |
29508e1e NH |
329 | |
330 | #ifdef USE_CURL_MULTI | |
331 | { | |
332 | char *http_max_requests = getenv("GIT_HTTP_MAX_REQUESTS"); | |
333 | if (http_max_requests != NULL) | |
334 | max_requests = atoi(http_max_requests); | |
335 | } | |
336 | ||
337 | curlm = curl_multi_init(); | |
338 | if (curlm == NULL) { | |
339 | fprintf(stderr, "Error creating curl multi handle.\n"); | |
340 | exit(1); | |
341 | } | |
342 | #endif | |
343 | ||
344 | if (getenv("GIT_SSL_NO_VERIFY")) | |
345 | curl_ssl_verify = 0; | |
346 | ||
7059cd99 | 347 | set_from_env(&ssl_cert, "GIT_SSL_CERT"); |
ef52aafa | 348 | #if LIBCURL_VERSION_NUM >= 0x070903 |
7059cd99 | 349 | set_from_env(&ssl_key, "GIT_SSL_KEY"); |
29508e1e NH |
350 | #endif |
351 | #if LIBCURL_VERSION_NUM >= 0x070908 | |
7059cd99 | 352 | set_from_env(&ssl_capath, "GIT_SSL_CAPATH"); |
29508e1e | 353 | #endif |
7059cd99 | 354 | set_from_env(&ssl_cainfo, "GIT_SSL_CAINFO"); |
29508e1e | 355 | |
b1d1058c SO |
356 | set_from_env(&user_agent, "GIT_HTTP_USER_AGENT"); |
357 | ||
29508e1e NH |
358 | low_speed_limit = getenv("GIT_HTTP_LOW_SPEED_LIMIT"); |
359 | if (low_speed_limit != NULL) | |
360 | curl_low_speed_limit = strtol(low_speed_limit, NULL, 10); | |
361 | low_speed_time = getenv("GIT_HTTP_LOW_SPEED_TIME"); | |
362 | if (low_speed_time != NULL) | |
363 | curl_low_speed_time = strtol(low_speed_time, NULL, 10); | |
364 | ||
29508e1e NH |
365 | if (curl_ssl_verify == -1) |
366 | curl_ssl_verify = 1; | |
367 | ||
ad75ebe5 | 368 | curl_session_count = 0; |
29508e1e NH |
369 | #ifdef USE_CURL_MULTI |
370 | if (max_requests < 1) | |
371 | max_requests = DEFAULT_MAX_REQUESTS; | |
372 | #endif | |
373 | ||
3ea099d4 SK |
374 | if (getenv("GIT_CURL_FTP_NO_EPSV")) |
375 | curl_ftp_no_epsv = 1; | |
376 | ||
deba4937 | 377 | if (url) { |
148bb6a7 | 378 | credential_from_url(&http_auth, url); |
754ae192 ML |
379 | if (!ssl_cert_password_required && |
380 | getenv("GIT_SSL_CERT_PASSWORD_PROTECTED") && | |
deba4937 | 381 | !prefixcmp(url, "https://")) |
30dd9163 ML |
382 | ssl_cert_password_required = 1; |
383 | } | |
c33976cb | 384 | |
29508e1e NH |
385 | #ifndef NO_CURL_EASY_DUPHANDLE |
386 | curl_default = get_curl_handle(); | |
387 | #endif | |
388 | } | |
389 | ||
390 | void http_cleanup(void) | |
391 | { | |
392 | struct active_request_slot *slot = active_queue_head; | |
29508e1e NH |
393 | |
394 | while (slot != NULL) { | |
3278cd0a | 395 | struct active_request_slot *next = slot->next; |
f23d1f76 | 396 | if (slot->curl != NULL) { |
29508e1e | 397 | #ifdef USE_CURL_MULTI |
f23d1f76 | 398 | curl_multi_remove_handle(curlm, slot->curl); |
29508e1e | 399 | #endif |
29508e1e | 400 | curl_easy_cleanup(slot->curl); |
f23d1f76 | 401 | } |
3278cd0a SP |
402 | free(slot); |
403 | slot = next; | |
29508e1e | 404 | } |
3278cd0a | 405 | active_queue_head = NULL; |
29508e1e NH |
406 | |
407 | #ifndef NO_CURL_EASY_DUPHANDLE | |
408 | curl_easy_cleanup(curl_default); | |
409 | #endif | |
410 | ||
411 | #ifdef USE_CURL_MULTI | |
412 | curl_multi_cleanup(curlm); | |
413 | #endif | |
414 | curl_global_cleanup(); | |
b3ca4e4e NH |
415 | |
416 | curl_slist_free_all(pragma_header); | |
3278cd0a | 417 | pragma_header = NULL; |
9fc6440d | 418 | |
e9176745 TRC |
419 | curl_slist_free_all(no_pragma_header); |
420 | no_pragma_header = NULL; | |
421 | ||
9fc6440d | 422 | if (curl_http_proxy) { |
e4a80ecf | 423 | free((void *)curl_http_proxy); |
9fc6440d MH |
424 | curl_http_proxy = NULL; |
425 | } | |
30dd9163 | 426 | |
148bb6a7 JK |
427 | if (cert_auth.password != NULL) { |
428 | memset(cert_auth.password, 0, strlen(cert_auth.password)); | |
429 | free(cert_auth.password); | |
430 | cert_auth.password = NULL; | |
30dd9163 ML |
431 | } |
432 | ssl_cert_password_required = 0; | |
29508e1e NH |
433 | } |
434 | ||
29508e1e NH |
435 | struct active_request_slot *get_active_slot(void) |
436 | { | |
437 | struct active_request_slot *slot = active_queue_head; | |
438 | struct active_request_slot *newslot; | |
439 | ||
440 | #ifdef USE_CURL_MULTI | |
441 | int num_transfers; | |
442 | ||
443 | /* Wait for a slot to open up if the queue is full */ | |
444 | while (active_requests >= max_requests) { | |
445 | curl_multi_perform(curlm, &num_transfers); | |
4251ccbd | 446 | if (num_transfers < active_requests) |
29508e1e | 447 | process_curl_messages(); |
29508e1e NH |
448 | } |
449 | #endif | |
450 | ||
4251ccbd | 451 | while (slot != NULL && slot->in_use) |
29508e1e | 452 | slot = slot->next; |
4251ccbd | 453 | |
29508e1e NH |
454 | if (slot == NULL) { |
455 | newslot = xmalloc(sizeof(*newslot)); | |
456 | newslot->curl = NULL; | |
457 | newslot->in_use = 0; | |
458 | newslot->next = NULL; | |
459 | ||
460 | slot = active_queue_head; | |
461 | if (slot == NULL) { | |
462 | active_queue_head = newslot; | |
463 | } else { | |
4251ccbd | 464 | while (slot->next != NULL) |
29508e1e | 465 | slot = slot->next; |
29508e1e NH |
466 | slot->next = newslot; |
467 | } | |
468 | slot = newslot; | |
469 | } | |
470 | ||
471 | if (slot->curl == NULL) { | |
472 | #ifdef NO_CURL_EASY_DUPHANDLE | |
473 | slot->curl = get_curl_handle(); | |
474 | #else | |
475 | slot->curl = curl_easy_duphandle(curl_default); | |
476 | #endif | |
ad75ebe5 | 477 | curl_session_count++; |
29508e1e NH |
478 | } |
479 | ||
480 | active_requests++; | |
481 | slot->in_use = 1; | |
c8568e13 | 482 | slot->results = NULL; |
baa7b67d | 483 | slot->finished = NULL; |
29508e1e NH |
484 | slot->callback_data = NULL; |
485 | slot->callback_func = NULL; | |
bcfb95dd | 486 | curl_easy_setopt(slot->curl, CURLOPT_COOKIEFILE, curl_cookie_file); |
29508e1e | 487 | curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, pragma_header); |
29508e1e | 488 | curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, curl_errorstr); |
9094950d NH |
489 | curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, NULL); |
490 | curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, NULL); | |
491 | curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, NULL); | |
1e41827d | 492 | curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, NULL); |
9094950d NH |
493 | curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 0); |
494 | curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1); | |
29508e1e NH |
495 | |
496 | return slot; | |
497 | } | |
498 | ||
499 | int start_active_slot(struct active_request_slot *slot) | |
500 | { | |
501 | #ifdef USE_CURL_MULTI | |
502 | CURLMcode curlm_result = curl_multi_add_handle(curlm, slot->curl); | |
45c17412 | 503 | int num_transfers; |
29508e1e NH |
504 | |
505 | if (curlm_result != CURLM_OK && | |
506 | curlm_result != CURLM_CALL_MULTI_PERFORM) { | |
507 | active_requests--; | |
508 | slot->in_use = 0; | |
509 | return 0; | |
510 | } | |
45c17412 DB |
511 | |
512 | /* | |
513 | * We know there must be something to do, since we just added | |
514 | * something. | |
515 | */ | |
516 | curl_multi_perform(curlm, &num_transfers); | |
29508e1e NH |
517 | #endif |
518 | return 1; | |
519 | } | |
520 | ||
521 | #ifdef USE_CURL_MULTI | |
fc57b6aa DB |
522 | struct fill_chain { |
523 | void *data; | |
524 | int (*fill)(void *); | |
525 | struct fill_chain *next; | |
526 | }; | |
527 | ||
4251ccbd | 528 | static struct fill_chain *fill_cfg; |
fc57b6aa DB |
529 | |
530 | void add_fill_function(void *data, int (*fill)(void *)) | |
531 | { | |
e8eec71d | 532 | struct fill_chain *new = xmalloc(sizeof(*new)); |
fc57b6aa DB |
533 | struct fill_chain **linkp = &fill_cfg; |
534 | new->data = data; | |
535 | new->fill = fill; | |
536 | new->next = NULL; | |
537 | while (*linkp) | |
538 | linkp = &(*linkp)->next; | |
539 | *linkp = new; | |
540 | } | |
541 | ||
45c17412 DB |
542 | void fill_active_slots(void) |
543 | { | |
544 | struct active_request_slot *slot = active_queue_head; | |
545 | ||
fc57b6aa DB |
546 | while (active_requests < max_requests) { |
547 | struct fill_chain *fill; | |
548 | for (fill = fill_cfg; fill; fill = fill->next) | |
549 | if (fill->fill(fill->data)) | |
550 | break; | |
551 | ||
552 | if (!fill) | |
45c17412 | 553 | break; |
fc57b6aa | 554 | } |
45c17412 DB |
555 | |
556 | while (slot != NULL) { | |
ad75ebe5 TRC |
557 | if (!slot->in_use && slot->curl != NULL |
558 | && curl_session_count > min_curl_sessions) { | |
45c17412 DB |
559 | curl_easy_cleanup(slot->curl); |
560 | slot->curl = NULL; | |
ad75ebe5 | 561 | curl_session_count--; |
45c17412 DB |
562 | } |
563 | slot = slot->next; | |
564 | } | |
565 | } | |
566 | ||
29508e1e NH |
567 | void step_active_slots(void) |
568 | { | |
569 | int num_transfers; | |
570 | CURLMcode curlm_result; | |
571 | ||
572 | do { | |
573 | curlm_result = curl_multi_perform(curlm, &num_transfers); | |
574 | } while (curlm_result == CURLM_CALL_MULTI_PERFORM); | |
575 | if (num_transfers < active_requests) { | |
576 | process_curl_messages(); | |
577 | fill_active_slots(); | |
578 | } | |
579 | } | |
580 | #endif | |
581 | ||
582 | void run_active_slot(struct active_request_slot *slot) | |
583 | { | |
584 | #ifdef USE_CURL_MULTI | |
29508e1e NH |
585 | fd_set readfds; |
586 | fd_set writefds; | |
587 | fd_set excfds; | |
588 | int max_fd; | |
589 | struct timeval select_timeout; | |
baa7b67d | 590 | int finished = 0; |
29508e1e | 591 | |
baa7b67d NH |
592 | slot->finished = &finished; |
593 | while (!finished) { | |
29508e1e NH |
594 | step_active_slots(); |
595 | ||
df26c471 | 596 | if (slot->in_use) { |
eb56c821 MF |
597 | #if LIBCURL_VERSION_NUM >= 0x070f04 |
598 | long curl_timeout; | |
599 | curl_multi_timeout(curlm, &curl_timeout); | |
600 | if (curl_timeout == 0) { | |
601 | continue; | |
602 | } else if (curl_timeout == -1) { | |
603 | select_timeout.tv_sec = 0; | |
604 | select_timeout.tv_usec = 50000; | |
605 | } else { | |
606 | select_timeout.tv_sec = curl_timeout / 1000; | |
607 | select_timeout.tv_usec = (curl_timeout % 1000) * 1000; | |
608 | } | |
609 | #else | |
610 | select_timeout.tv_sec = 0; | |
611 | select_timeout.tv_usec = 50000; | |
612 | #endif | |
29508e1e | 613 | |
6f9dd67f | 614 | max_fd = -1; |
29508e1e NH |
615 | FD_ZERO(&readfds); |
616 | FD_ZERO(&writefds); | |
617 | FD_ZERO(&excfds); | |
6f9dd67f | 618 | curl_multi_fdset(curlm, &readfds, &writefds, &excfds, &max_fd); |
eb56c821 | 619 | |
6f9dd67f | 620 | select(max_fd+1, &readfds, &writefds, &excfds, &select_timeout); |
29508e1e NH |
621 | } |
622 | } | |
623 | #else | |
624 | while (slot->in_use) { | |
625 | slot->curl_result = curl_easy_perform(slot->curl); | |
626 | finish_active_slot(slot); | |
627 | } | |
628 | #endif | |
629 | } | |
630 | ||
53f31389 | 631 | static void closedown_active_slot(struct active_request_slot *slot) |
29508e1e | 632 | { |
028c2976 MH |
633 | active_requests--; |
634 | slot->in_use = 0; | |
53f31389 MW |
635 | } |
636 | ||
83e41e2e | 637 | static void release_active_slot(struct active_request_slot *slot) |
53f31389 MW |
638 | { |
639 | closedown_active_slot(slot); | |
ad75ebe5 | 640 | if (slot->curl && curl_session_count > min_curl_sessions) { |
b3ca4e4e | 641 | #ifdef USE_CURL_MULTI |
53f31389 | 642 | curl_multi_remove_handle(curlm, slot->curl); |
b3ca4e4e | 643 | #endif |
53f31389 MW |
644 | curl_easy_cleanup(slot->curl); |
645 | slot->curl = NULL; | |
ad75ebe5 | 646 | curl_session_count--; |
53f31389 | 647 | } |
b3ca4e4e | 648 | #ifdef USE_CURL_MULTI |
53f31389 | 649 | fill_active_slots(); |
b3ca4e4e | 650 | #endif |
53f31389 MW |
651 | } |
652 | ||
de1a2fdd | 653 | void finish_active_slot(struct active_request_slot *slot) |
53f31389 MW |
654 | { |
655 | closedown_active_slot(slot); | |
028c2976 | 656 | curl_easy_getinfo(slot->curl, CURLINFO_HTTP_CODE, &slot->http_code); |
c8568e13 | 657 | |
baa7b67d NH |
658 | if (slot->finished != NULL) |
659 | (*slot->finished) = 1; | |
660 | ||
c8568e13 NH |
661 | /* Store slot results so they can be read after the slot is reused */ |
662 | if (slot->results != NULL) { | |
663 | slot->results->curl_result = slot->curl_result; | |
664 | slot->results->http_code = slot->http_code; | |
665 | } | |
666 | ||
028c2976 | 667 | /* Run callback if appropriate */ |
4251ccbd | 668 | if (slot->callback_func != NULL) |
028c2976 | 669 | slot->callback_func(slot->callback_data); |
29508e1e NH |
670 | } |
671 | ||
672 | void finish_all_active_slots(void) | |
673 | { | |
674 | struct active_request_slot *slot = active_queue_head; | |
675 | ||
676 | while (slot != NULL) | |
677 | if (slot->in_use) { | |
678 | run_active_slot(slot); | |
679 | slot = active_queue_head; | |
680 | } else { | |
681 | slot = slot->next; | |
682 | } | |
683 | } | |
d7e92806 | 684 | |
5ace994f | 685 | /* Helpers for modifying and creating URLs */ |
d7e92806 MH |
686 | static inline int needs_quote(int ch) |
687 | { | |
688 | if (((ch >= 'A') && (ch <= 'Z')) | |
689 | || ((ch >= 'a') && (ch <= 'z')) | |
690 | || ((ch >= '0') && (ch <= '9')) | |
691 | || (ch == '/') | |
692 | || (ch == '-') | |
693 | || (ch == '.')) | |
694 | return 0; | |
695 | return 1; | |
696 | } | |
697 | ||
d7e92806 MH |
698 | static char *quote_ref_url(const char *base, const char *ref) |
699 | { | |
113106e0 | 700 | struct strbuf buf = STRBUF_INIT; |
d7e92806 | 701 | const char *cp; |
113106e0 | 702 | int ch; |
d7e92806 | 703 | |
5ace994f | 704 | end_url_with_slash(&buf, base); |
113106e0 TRC |
705 | |
706 | for (cp = ref; (ch = *cp) != 0; cp++) | |
d7e92806 | 707 | if (needs_quote(ch)) |
113106e0 | 708 | strbuf_addf(&buf, "%%%02x", ch); |
d7e92806 | 709 | else |
113106e0 | 710 | strbuf_addch(&buf, *cp); |
d7e92806 | 711 | |
113106e0 | 712 | return strbuf_detach(&buf, NULL); |
d7e92806 MH |
713 | } |
714 | ||
5424bc55 TRC |
715 | void append_remote_object_url(struct strbuf *buf, const char *url, |
716 | const char *hex, | |
717 | int only_two_digit_prefix) | |
718 | { | |
800324c3 TRC |
719 | end_url_with_slash(buf, url); |
720 | ||
721 | strbuf_addf(buf, "objects/%.*s/", 2, hex); | |
5424bc55 TRC |
722 | if (!only_two_digit_prefix) |
723 | strbuf_addf(buf, "%s", hex+2); | |
724 | } | |
725 | ||
726 | char *get_remote_object_url(const char *url, const char *hex, | |
727 | int only_two_digit_prefix) | |
728 | { | |
729 | struct strbuf buf = STRBUF_INIT; | |
730 | append_remote_object_url(&buf, url, hex, only_two_digit_prefix); | |
731 | return strbuf_detach(&buf, NULL); | |
732 | } | |
733 | ||
e929cd20 MH |
734 | /* http_request() targets */ |
735 | #define HTTP_REQUEST_STRBUF 0 | |
736 | #define HTTP_REQUEST_FILE 1 | |
737 | ||
738 | static int http_request(const char *url, void *result, int target, int options) | |
739 | { | |
740 | struct active_request_slot *slot; | |
741 | struct slot_results results; | |
742 | struct curl_slist *headers = NULL; | |
743 | struct strbuf buf = STRBUF_INIT; | |
744 | int ret; | |
745 | ||
746 | slot = get_active_slot(); | |
747 | slot->results = &results; | |
748 | curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1); | |
749 | ||
750 | if (result == NULL) { | |
751 | curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 1); | |
752 | } else { | |
753 | curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0); | |
754 | curl_easy_setopt(slot->curl, CURLOPT_FILE, result); | |
755 | ||
756 | if (target == HTTP_REQUEST_FILE) { | |
757 | long posn = ftell(result); | |
758 | curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, | |
759 | fwrite); | |
760 | if (posn > 0) { | |
761 | strbuf_addf(&buf, "Range: bytes=%ld-", posn); | |
762 | headers = curl_slist_append(headers, buf.buf); | |
763 | strbuf_reset(&buf); | |
764 | } | |
e929cd20 MH |
765 | } else |
766 | curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, | |
767 | fwrite_buffer); | |
768 | } | |
769 | ||
770 | strbuf_addstr(&buf, "Pragma:"); | |
771 | if (options & HTTP_NO_CACHE) | |
772 | strbuf_addstr(&buf, " no-cache"); | |
773 | ||
774 | headers = curl_slist_append(headers, buf.buf); | |
775 | ||
776 | curl_easy_setopt(slot->curl, CURLOPT_URL, url); | |
777 | curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers); | |
778 | ||
779 | if (start_active_slot(slot)) { | |
780 | run_active_slot(slot); | |
781 | if (results.curl_result == CURLE_OK) | |
782 | ret = HTTP_OK; | |
783 | else if (missing_target(&results)) | |
784 | ret = HTTP_MISSING_TARGET; | |
42653c09 | 785 | else if (results.http_code == 401) { |
148bb6a7 JK |
786 | if (http_auth.username && http_auth.password) { |
787 | credential_reject(&http_auth); | |
42653c09 SC |
788 | ret = HTTP_NOAUTH; |
789 | } else { | |
148bb6a7 | 790 | credential_fill(&http_auth); |
42653c09 SC |
791 | init_curl_http_auth(slot->curl); |
792 | ret = HTTP_REAUTH; | |
793 | } | |
be22d92e JN |
794 | } else { |
795 | if (!curl_errorstr[0]) | |
796 | strlcpy(curl_errorstr, | |
797 | curl_easy_strerror(results.curl_result), | |
798 | sizeof(curl_errorstr)); | |
e929cd20 | 799 | ret = HTTP_ERROR; |
be22d92e | 800 | } |
e929cd20 MH |
801 | } else { |
802 | error("Unable to start HTTP request for %s", url); | |
803 | ret = HTTP_START_FAILED; | |
804 | } | |
805 | ||
e929cd20 MH |
806 | curl_slist_free_all(headers); |
807 | strbuf_release(&buf); | |
808 | ||
148bb6a7 JK |
809 | if (ret == HTTP_OK) |
810 | credential_approve(&http_auth); | |
811 | ||
e929cd20 MH |
812 | return ret; |
813 | } | |
814 | ||
8d677edc JK |
815 | static int http_request_reauth(const char *url, void *result, int target, |
816 | int options) | |
817 | { | |
818 | int ret = http_request(url, result, target, options); | |
819 | if (ret != HTTP_REAUTH) | |
820 | return ret; | |
821 | return http_request(url, result, target, options); | |
822 | } | |
823 | ||
e929cd20 MH |
824 | int http_get_strbuf(const char *url, struct strbuf *result, int options) |
825 | { | |
8d677edc | 826 | return http_request_reauth(url, result, HTTP_REQUEST_STRBUF, options); |
e929cd20 MH |
827 | } |
828 | ||
83e41e2e JH |
829 | /* |
830 | * Downloads an url and stores the result in the given file. | |
831 | * | |
832 | * If a previous interrupted download is detected (i.e. a previous temporary | |
833 | * file is still around) the download is resumed. | |
834 | */ | |
835 | static int http_get_file(const char *url, const char *filename, int options) | |
e929cd20 MH |
836 | { |
837 | int ret; | |
838 | struct strbuf tmpfile = STRBUF_INIT; | |
839 | FILE *result; | |
840 | ||
841 | strbuf_addf(&tmpfile, "%s.temp", filename); | |
842 | result = fopen(tmpfile.buf, "a"); | |
843 | if (! result) { | |
844 | error("Unable to open local file %s", tmpfile.buf); | |
845 | ret = HTTP_ERROR; | |
846 | goto cleanup; | |
847 | } | |
848 | ||
8d677edc | 849 | ret = http_request_reauth(url, result, HTTP_REQUEST_FILE, options); |
e929cd20 MH |
850 | fclose(result); |
851 | ||
852 | if ((ret == HTTP_OK) && move_temp_to_file(tmpfile.buf, filename)) | |
853 | ret = HTTP_ERROR; | |
854 | cleanup: | |
855 | strbuf_release(&tmpfile); | |
856 | return ret; | |
857 | } | |
858 | ||
859 | int http_error(const char *url, int ret) | |
860 | { | |
861 | /* http_request has already handled HTTP_START_FAILED. */ | |
862 | if (ret != HTTP_START_FAILED) | |
8abc5082 | 863 | error("%s while accessing %s", curl_errorstr, url); |
e929cd20 MH |
864 | |
865 | return ret; | |
866 | } | |
867 | ||
c13b2633 | 868 | int http_fetch_ref(const char *base, struct ref *ref) |
d7e92806 MH |
869 | { |
870 | char *url; | |
871 | struct strbuf buffer = STRBUF_INIT; | |
0d5896e1 | 872 | int ret = -1; |
d7e92806 | 873 | |
c13b2633 | 874 | url = quote_ref_url(base, ref->name); |
0d5896e1 MH |
875 | if (http_get_strbuf(url, &buffer, HTTP_NO_CACHE) == HTTP_OK) { |
876 | strbuf_rtrim(&buffer); | |
877 | if (buffer.len == 40) | |
878 | ret = get_sha1_hex(buffer.buf, ref->old_sha1); | |
879 | else if (!prefixcmp(buffer.buf, "ref: ")) { | |
880 | ref->symref = xstrdup(buffer.buf + 5); | |
881 | ret = 0; | |
d7e92806 | 882 | } |
d7e92806 MH |
883 | } |
884 | ||
885 | strbuf_release(&buffer); | |
886 | free(url); | |
887 | return ret; | |
888 | } | |
b8caac2b TRC |
889 | |
890 | /* Helpers for fetching packs */ | |
750ef425 | 891 | static char *fetch_pack_index(unsigned char *sha1, const char *base_url) |
b8caac2b | 892 | { |
750ef425 | 893 | char *url, *tmp; |
b8caac2b | 894 | struct strbuf buf = STRBUF_INIT; |
b8caac2b | 895 | |
b8caac2b | 896 | if (http_is_verbose) |
162eb5f8 | 897 | fprintf(stderr, "Getting index for pack %s\n", sha1_to_hex(sha1)); |
b8caac2b TRC |
898 | |
899 | end_url_with_slash(&buf, base_url); | |
162eb5f8 | 900 | strbuf_addf(&buf, "objects/pack/pack-%s.idx", sha1_to_hex(sha1)); |
b8caac2b TRC |
901 | url = strbuf_detach(&buf, NULL); |
902 | ||
750ef425 SP |
903 | strbuf_addf(&buf, "%s.temp", sha1_pack_index_name(sha1)); |
904 | tmp = strbuf_detach(&buf, NULL); | |
905 | ||
906 | if (http_get_file(url, tmp, 0) != HTTP_OK) { | |
907 | error("Unable to get pack index %s\n", url); | |
908 | free(tmp); | |
909 | tmp = NULL; | |
910 | } | |
b8caac2b | 911 | |
b8caac2b | 912 | free(url); |
750ef425 | 913 | return tmp; |
b8caac2b TRC |
914 | } |
915 | ||
916 | static int fetch_and_setup_pack_index(struct packed_git **packs_head, | |
917 | unsigned char *sha1, const char *base_url) | |
918 | { | |
919 | struct packed_git *new_pack; | |
750ef425 SP |
920 | char *tmp_idx = NULL; |
921 | int ret; | |
b8caac2b | 922 | |
750ef425 SP |
923 | if (has_pack_index(sha1)) { |
924 | new_pack = parse_pack_index(sha1, NULL); | |
925 | if (!new_pack) | |
926 | return -1; /* parse_pack_index() already issued error message */ | |
927 | goto add_pack; | |
928 | } | |
929 | ||
930 | tmp_idx = fetch_pack_index(sha1, base_url); | |
931 | if (!tmp_idx) | |
b8caac2b TRC |
932 | return -1; |
933 | ||
750ef425 SP |
934 | new_pack = parse_pack_index(sha1, tmp_idx); |
935 | if (!new_pack) { | |
936 | unlink(tmp_idx); | |
937 | free(tmp_idx); | |
938 | ||
b8caac2b | 939 | return -1; /* parse_pack_index() already issued error message */ |
750ef425 SP |
940 | } |
941 | ||
942 | ret = verify_pack_index(new_pack); | |
943 | if (!ret) { | |
944 | close_pack_index(new_pack); | |
945 | ret = move_temp_to_file(tmp_idx, sha1_pack_index_name(sha1)); | |
946 | } | |
947 | free(tmp_idx); | |
948 | if (ret) | |
949 | return -1; | |
950 | ||
951 | add_pack: | |
b8caac2b TRC |
952 | new_pack->next = *packs_head; |
953 | *packs_head = new_pack; | |
954 | return 0; | |
955 | } | |
956 | ||
957 | int http_get_info_packs(const char *base_url, struct packed_git **packs_head) | |
958 | { | |
959 | int ret = 0, i = 0; | |
960 | char *url, *data; | |
961 | struct strbuf buf = STRBUF_INIT; | |
962 | unsigned char sha1[20]; | |
963 | ||
964 | end_url_with_slash(&buf, base_url); | |
965 | strbuf_addstr(&buf, "objects/info/packs"); | |
966 | url = strbuf_detach(&buf, NULL); | |
967 | ||
968 | ret = http_get_strbuf(url, &buf, HTTP_NO_CACHE); | |
969 | if (ret != HTTP_OK) | |
970 | goto cleanup; | |
971 | ||
972 | data = buf.buf; | |
973 | while (i < buf.len) { | |
974 | switch (data[i]) { | |
975 | case 'P': | |
976 | i++; | |
977 | if (i + 52 <= buf.len && | |
978 | !prefixcmp(data + i, " pack-") && | |
979 | !prefixcmp(data + i + 46, ".pack\n")) { | |
980 | get_sha1_hex(data + i + 6, sha1); | |
981 | fetch_and_setup_pack_index(packs_head, sha1, | |
982 | base_url); | |
983 | i += 51; | |
984 | break; | |
985 | } | |
986 | default: | |
987 | while (i < buf.len && data[i] != '\n') | |
988 | i++; | |
989 | } | |
990 | i++; | |
991 | } | |
992 | ||
993 | cleanup: | |
994 | free(url); | |
995 | return ret; | |
996 | } | |
2264dfa5 TRC |
997 | |
998 | void release_http_pack_request(struct http_pack_request *preq) | |
999 | { | |
1000 | if (preq->packfile != NULL) { | |
1001 | fclose(preq->packfile); | |
1002 | preq->packfile = NULL; | |
2264dfa5 TRC |
1003 | } |
1004 | if (preq->range_header != NULL) { | |
1005 | curl_slist_free_all(preq->range_header); | |
1006 | preq->range_header = NULL; | |
1007 | } | |
1008 | preq->slot = NULL; | |
1009 | free(preq->url); | |
1010 | } | |
1011 | ||
1012 | int finish_http_pack_request(struct http_pack_request *preq) | |
1013 | { | |
2264dfa5 | 1014 | struct packed_git **lst; |
021ab6f0 | 1015 | struct packed_git *p = preq->target; |
fe72d420 SP |
1016 | char *tmp_idx; |
1017 | struct child_process ip; | |
1018 | const char *ip_argv[8]; | |
2264dfa5 | 1019 | |
fe72d420 | 1020 | close_pack_index(p); |
2264dfa5 | 1021 | |
3065274c SP |
1022 | fclose(preq->packfile); |
1023 | preq->packfile = NULL; | |
2264dfa5 TRC |
1024 | |
1025 | lst = preq->lst; | |
021ab6f0 | 1026 | while (*lst != p) |
2264dfa5 TRC |
1027 | lst = &((*lst)->next); |
1028 | *lst = (*lst)->next; | |
1029 | ||
fe72d420 SP |
1030 | tmp_idx = xstrdup(preq->tmpfile); |
1031 | strcpy(tmp_idx + strlen(tmp_idx) - strlen(".pack.temp"), | |
1032 | ".idx.temp"); | |
1033 | ||
1034 | ip_argv[0] = "index-pack"; | |
1035 | ip_argv[1] = "-o"; | |
1036 | ip_argv[2] = tmp_idx; | |
1037 | ip_argv[3] = preq->tmpfile; | |
1038 | ip_argv[4] = NULL; | |
1039 | ||
1040 | memset(&ip, 0, sizeof(ip)); | |
1041 | ip.argv = ip_argv; | |
1042 | ip.git_cmd = 1; | |
1043 | ip.no_stdin = 1; | |
1044 | ip.no_stdout = 1; | |
1045 | ||
1046 | if (run_command(&ip)) { | |
1047 | unlink(preq->tmpfile); | |
1048 | unlink(tmp_idx); | |
1049 | free(tmp_idx); | |
2264dfa5 | 1050 | return -1; |
fe72d420 SP |
1051 | } |
1052 | ||
1053 | unlink(sha1_pack_index_name(p->sha1)); | |
2264dfa5 | 1054 | |
fe72d420 SP |
1055 | if (move_temp_to_file(preq->tmpfile, sha1_pack_name(p->sha1)) |
1056 | || move_temp_to_file(tmp_idx, sha1_pack_index_name(p->sha1))) { | |
1057 | free(tmp_idx); | |
2264dfa5 | 1058 | return -1; |
fe72d420 | 1059 | } |
2264dfa5 | 1060 | |
fe72d420 SP |
1061 | install_packed_git(p); |
1062 | free(tmp_idx); | |
2264dfa5 TRC |
1063 | return 0; |
1064 | } | |
1065 | ||
1066 | struct http_pack_request *new_http_pack_request( | |
1067 | struct packed_git *target, const char *base_url) | |
1068 | { | |
2264dfa5 TRC |
1069 | long prev_posn = 0; |
1070 | char range[RANGE_HEADER_SIZE]; | |
1071 | struct strbuf buf = STRBUF_INIT; | |
1072 | struct http_pack_request *preq; | |
1073 | ||
ec99c9a8 | 1074 | preq = xcalloc(1, sizeof(*preq)); |
2264dfa5 | 1075 | preq->target = target; |
2264dfa5 TRC |
1076 | |
1077 | end_url_with_slash(&buf, base_url); | |
1078 | strbuf_addf(&buf, "objects/pack/pack-%s.pack", | |
1079 | sha1_to_hex(target->sha1)); | |
bb99190e | 1080 | preq->url = strbuf_detach(&buf, NULL); |
2264dfa5 | 1081 | |
90d05713 TRC |
1082 | snprintf(preq->tmpfile, sizeof(preq->tmpfile), "%s.temp", |
1083 | sha1_pack_name(target->sha1)); | |
2264dfa5 TRC |
1084 | preq->packfile = fopen(preq->tmpfile, "a"); |
1085 | if (!preq->packfile) { | |
1086 | error("Unable to open local file %s for pack", | |
1087 | preq->tmpfile); | |
1088 | goto abort; | |
1089 | } | |
1090 | ||
1091 | preq->slot = get_active_slot(); | |
2264dfa5 TRC |
1092 | curl_easy_setopt(preq->slot->curl, CURLOPT_FILE, preq->packfile); |
1093 | curl_easy_setopt(preq->slot->curl, CURLOPT_WRITEFUNCTION, fwrite); | |
bb99190e | 1094 | curl_easy_setopt(preq->slot->curl, CURLOPT_URL, preq->url); |
2264dfa5 TRC |
1095 | curl_easy_setopt(preq->slot->curl, CURLOPT_HTTPHEADER, |
1096 | no_pragma_header); | |
1097 | ||
1098 | /* | |
1099 | * If there is data present from a previous transfer attempt, | |
1100 | * resume where it left off | |
1101 | */ | |
1102 | prev_posn = ftell(preq->packfile); | |
1103 | if (prev_posn>0) { | |
1104 | if (http_is_verbose) | |
1105 | fprintf(stderr, | |
1106 | "Resuming fetch of pack %s at byte %ld\n", | |
1107 | sha1_to_hex(target->sha1), prev_posn); | |
1108 | sprintf(range, "Range: bytes=%ld-", prev_posn); | |
1109 | preq->range_header = curl_slist_append(NULL, range); | |
1110 | curl_easy_setopt(preq->slot->curl, CURLOPT_HTTPHEADER, | |
1111 | preq->range_header); | |
1112 | } | |
1113 | ||
1114 | return preq; | |
1115 | ||
1116 | abort: | |
bb99190e | 1117 | free(preq->url); |
5ae9ebfd | 1118 | free(preq); |
2264dfa5 TRC |
1119 | return NULL; |
1120 | } | |
5424bc55 TRC |
1121 | |
1122 | /* Helpers for fetching objects (loose) */ | |
a04ff3ec | 1123 | static size_t fwrite_sha1_file(char *ptr, size_t eltsize, size_t nmemb, |
5424bc55 TRC |
1124 | void *data) |
1125 | { | |
1126 | unsigned char expn[4096]; | |
1127 | size_t size = eltsize * nmemb; | |
1128 | int posn = 0; | |
1129 | struct http_object_request *freq = | |
1130 | (struct http_object_request *)data; | |
1131 | do { | |
1132 | ssize_t retval = xwrite(freq->localfile, | |
1133 | (char *) ptr + posn, size - posn); | |
1134 | if (retval < 0) | |
1135 | return posn; | |
1136 | posn += retval; | |
1137 | } while (posn < size); | |
1138 | ||
1139 | freq->stream.avail_in = size; | |
a04ff3ec | 1140 | freq->stream.next_in = (void *)ptr; |
5424bc55 TRC |
1141 | do { |
1142 | freq->stream.next_out = expn; | |
1143 | freq->stream.avail_out = sizeof(expn); | |
1144 | freq->zret = git_inflate(&freq->stream, Z_SYNC_FLUSH); | |
1145 | git_SHA1_Update(&freq->c, expn, | |
1146 | sizeof(expn) - freq->stream.avail_out); | |
1147 | } while (freq->stream.avail_in && freq->zret == Z_OK); | |
5424bc55 TRC |
1148 | return size; |
1149 | } | |
1150 | ||
1151 | struct http_object_request *new_http_object_request(const char *base_url, | |
1152 | unsigned char *sha1) | |
1153 | { | |
1154 | char *hex = sha1_to_hex(sha1); | |
1155 | char *filename; | |
1156 | char prevfile[PATH_MAX]; | |
5424bc55 | 1157 | int prevlocal; |
a04ff3ec | 1158 | char prev_buf[PREV_BUF_SIZE]; |
5424bc55 TRC |
1159 | ssize_t prev_read = 0; |
1160 | long prev_posn = 0; | |
1161 | char range[RANGE_HEADER_SIZE]; | |
1162 | struct curl_slist *range_header = NULL; | |
1163 | struct http_object_request *freq; | |
1164 | ||
ec99c9a8 | 1165 | freq = xcalloc(1, sizeof(*freq)); |
5424bc55 TRC |
1166 | hashcpy(freq->sha1, sha1); |
1167 | freq->localfile = -1; | |
1168 | ||
1169 | filename = sha1_file_name(sha1); | |
5424bc55 TRC |
1170 | snprintf(freq->tmpfile, sizeof(freq->tmpfile), |
1171 | "%s.temp", filename); | |
1172 | ||
1173 | snprintf(prevfile, sizeof(prevfile), "%s.prev", filename); | |
1174 | unlink_or_warn(prevfile); | |
1175 | rename(freq->tmpfile, prevfile); | |
1176 | unlink_or_warn(freq->tmpfile); | |
1177 | ||
1178 | if (freq->localfile != -1) | |
1179 | error("fd leakage in start: %d", freq->localfile); | |
1180 | freq->localfile = open(freq->tmpfile, | |
1181 | O_WRONLY | O_CREAT | O_EXCL, 0666); | |
1182 | /* | |
1183 | * This could have failed due to the "lazy directory creation"; | |
1184 | * try to mkdir the last path component. | |
1185 | */ | |
1186 | if (freq->localfile < 0 && errno == ENOENT) { | |
1187 | char *dir = strrchr(freq->tmpfile, '/'); | |
1188 | if (dir) { | |
1189 | *dir = 0; | |
1190 | mkdir(freq->tmpfile, 0777); | |
1191 | *dir = '/'; | |
1192 | } | |
1193 | freq->localfile = open(freq->tmpfile, | |
1194 | O_WRONLY | O_CREAT | O_EXCL, 0666); | |
1195 | } | |
1196 | ||
1197 | if (freq->localfile < 0) { | |
0da8b2e7 SP |
1198 | error("Couldn't create temporary file %s: %s", |
1199 | freq->tmpfile, strerror(errno)); | |
5424bc55 TRC |
1200 | goto abort; |
1201 | } | |
1202 | ||
5424bc55 TRC |
1203 | git_inflate_init(&freq->stream); |
1204 | ||
1205 | git_SHA1_Init(&freq->c); | |
1206 | ||
bb99190e | 1207 | freq->url = get_remote_object_url(base_url, hex, 0); |
5424bc55 TRC |
1208 | |
1209 | /* | |
1210 | * If a previous temp file is present, process what was already | |
1211 | * fetched. | |
1212 | */ | |
1213 | prevlocal = open(prevfile, O_RDONLY); | |
1214 | if (prevlocal != -1) { | |
1215 | do { | |
1216 | prev_read = xread(prevlocal, prev_buf, PREV_BUF_SIZE); | |
1217 | if (prev_read>0) { | |
1218 | if (fwrite_sha1_file(prev_buf, | |
1219 | 1, | |
1220 | prev_read, | |
1221 | freq) == prev_read) { | |
1222 | prev_posn += prev_read; | |
1223 | } else { | |
1224 | prev_read = -1; | |
1225 | } | |
1226 | } | |
1227 | } while (prev_read > 0); | |
1228 | close(prevlocal); | |
1229 | } | |
1230 | unlink_or_warn(prevfile); | |
1231 | ||
1232 | /* | |
1233 | * Reset inflate/SHA1 if there was an error reading the previous temp | |
1234 | * file; also rewind to the beginning of the local file. | |
1235 | */ | |
1236 | if (prev_read == -1) { | |
1237 | memset(&freq->stream, 0, sizeof(freq->stream)); | |
1238 | git_inflate_init(&freq->stream); | |
1239 | git_SHA1_Init(&freq->c); | |
1240 | if (prev_posn>0) { | |
1241 | prev_posn = 0; | |
1242 | lseek(freq->localfile, 0, SEEK_SET); | |
0c4f21e4 | 1243 | if (ftruncate(freq->localfile, 0) < 0) { |
0da8b2e7 SP |
1244 | error("Couldn't truncate temporary file %s: %s", |
1245 | freq->tmpfile, strerror(errno)); | |
0c4f21e4 JL |
1246 | goto abort; |
1247 | } | |
5424bc55 TRC |
1248 | } |
1249 | } | |
1250 | ||
1251 | freq->slot = get_active_slot(); | |
1252 | ||
1253 | curl_easy_setopt(freq->slot->curl, CURLOPT_FILE, freq); | |
1254 | curl_easy_setopt(freq->slot->curl, CURLOPT_WRITEFUNCTION, fwrite_sha1_file); | |
1255 | curl_easy_setopt(freq->slot->curl, CURLOPT_ERRORBUFFER, freq->errorstr); | |
bb99190e | 1256 | curl_easy_setopt(freq->slot->curl, CURLOPT_URL, freq->url); |
5424bc55 TRC |
1257 | curl_easy_setopt(freq->slot->curl, CURLOPT_HTTPHEADER, no_pragma_header); |
1258 | ||
1259 | /* | |
1260 | * If we have successfully processed data from a previous fetch | |
1261 | * attempt, only fetch the data we don't already have. | |
1262 | */ | |
1263 | if (prev_posn>0) { | |
1264 | if (http_is_verbose) | |
1265 | fprintf(stderr, | |
1266 | "Resuming fetch of object %s at byte %ld\n", | |
1267 | hex, prev_posn); | |
1268 | sprintf(range, "Range: bytes=%ld-", prev_posn); | |
1269 | range_header = curl_slist_append(range_header, range); | |
1270 | curl_easy_setopt(freq->slot->curl, | |
1271 | CURLOPT_HTTPHEADER, range_header); | |
1272 | } | |
1273 | ||
1274 | return freq; | |
1275 | ||
5424bc55 | 1276 | abort: |
bb99190e | 1277 | free(freq->url); |
5424bc55 TRC |
1278 | free(freq); |
1279 | return NULL; | |
1280 | } | |
1281 | ||
1282 | void process_http_object_request(struct http_object_request *freq) | |
1283 | { | |
1284 | if (freq->slot == NULL) | |
1285 | return; | |
1286 | freq->curl_result = freq->slot->curl_result; | |
1287 | freq->http_code = freq->slot->http_code; | |
1288 | freq->slot = NULL; | |
1289 | } | |
1290 | ||
1291 | int finish_http_object_request(struct http_object_request *freq) | |
1292 | { | |
1293 | struct stat st; | |
1294 | ||
1295 | close(freq->localfile); | |
1296 | freq->localfile = -1; | |
1297 | ||
1298 | process_http_object_request(freq); | |
1299 | ||
1300 | if (freq->http_code == 416) { | |
bd757c18 | 1301 | warning("requested range invalid; we may already have all the data."); |
5424bc55 TRC |
1302 | } else if (freq->curl_result != CURLE_OK) { |
1303 | if (stat(freq->tmpfile, &st) == 0) | |
1304 | if (st.st_size == 0) | |
1305 | unlink_or_warn(freq->tmpfile); | |
1306 | return -1; | |
1307 | } | |
1308 | ||
1309 | git_inflate_end(&freq->stream); | |
1310 | git_SHA1_Final(freq->real_sha1, &freq->c); | |
1311 | if (freq->zret != Z_STREAM_END) { | |
1312 | unlink_or_warn(freq->tmpfile); | |
1313 | return -1; | |
1314 | } | |
1315 | if (hashcmp(freq->sha1, freq->real_sha1)) { | |
1316 | unlink_or_warn(freq->tmpfile); | |
1317 | return -1; | |
1318 | } | |
1319 | freq->rename = | |
0da8b2e7 | 1320 | move_temp_to_file(freq->tmpfile, sha1_file_name(freq->sha1)); |
5424bc55 TRC |
1321 | |
1322 | return freq->rename; | |
1323 | } | |
1324 | ||
1325 | void abort_http_object_request(struct http_object_request *freq) | |
1326 | { | |
1327 | unlink_or_warn(freq->tmpfile); | |
1328 | ||
1329 | release_http_object_request(freq); | |
1330 | } | |
1331 | ||
1332 | void release_http_object_request(struct http_object_request *freq) | |
1333 | { | |
1334 | if (freq->localfile != -1) { | |
1335 | close(freq->localfile); | |
1336 | freq->localfile = -1; | |
1337 | } | |
1338 | if (freq->url != NULL) { | |
1339 | free(freq->url); | |
1340 | freq->url = NULL; | |
1341 | } | |
4b9fa0e3 TRC |
1342 | if (freq->slot != NULL) { |
1343 | freq->slot->callback_func = NULL; | |
1344 | freq->slot->callback_data = NULL; | |
1345 | release_active_slot(freq->slot); | |
1346 | freq->slot = NULL; | |
1347 | } | |
5424bc55 | 1348 | } |