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