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