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