]> git.ipfire.org Git - thirdparty/git.git/blame - remote-curl.c
notes-cache: convert to struct object_id
[thirdparty/git.git] / remote-curl.c
CommitLineData
a2d725b7
DB
1#include "cache.h"
2#include "remote.h"
3#include "strbuf.h"
4#include "walker.h"
5#include "http.h"
d01a8e32 6#include "exec_cmd.h"
ae4efe19 7#include "run-command.h"
97cc7bc4 8#include "pkt-line.h"
05c1eb10 9#include "string-list.h"
de1a2fdd 10#include "sideband.h"
222b1212 11#include "argv-array.h"
2501aff8 12#include "credential.h"
16094885 13#include "sha1-array.h"
30261094 14#include "send-pack.h"
a2d725b7 15
37a8768f 16static struct remote *remote;
b227bbc4
JK
17/* always ends with a trailing slash */
18static struct strbuf url = STRBUF_INIT;
37a8768f 19
ef08ef9e
SP
20struct options {
21 int verbosity;
22 unsigned long depth;
508ea882 23 char *deepen_since;
a45a2600 24 struct string_list deepen_not;
511155db 25 struct string_list push_options;
ef08ef9e 26 unsigned progress : 1,
9ba38048 27 check_self_contained_and_connected : 1,
16094885
NTND
28 cloning : 1,
29 update_shallow : 1,
ae4efe19 30 followtags : 1,
de1a2fdd 31 dry_run : 1,
0ea47f9d 32 thin : 1,
30261094 33 /* One of the SEND_PACK_PUSH_CERT_* constants. */
cccf74e2
NTND
34 push_cert : 2,
35 deepen_relative : 1;
ef08ef9e
SP
36};
37static struct options options;
05c1eb10 38static struct string_list cas_options = STRING_LIST_INIT_DUP;
ef08ef9e 39
ef08ef9e
SP
40static int set_option(const char *name, const char *value)
41{
42 if (!strcmp(name, "verbosity")) {
43 char *end;
44 int v = strtol(value, &end, 10);
45 if (value == end || *end)
46 return -1;
47 options.verbosity = v;
48 return 0;
49 }
50 else if (!strcmp(name, "progress")) {
51 if (!strcmp(value, "true"))
52 options.progress = 1;
53 else if (!strcmp(value, "false"))
54 options.progress = 0;
55 else
56 return -1;
249b2004 57 return 0;
ef08ef9e
SP
58 }
59 else if (!strcmp(name, "depth")) {
60 char *end;
61 unsigned long v = strtoul(value, &end, 10);
62 if (value == end || *end)
63 return -1;
64 options.depth = v;
249b2004 65 return 0;
ef08ef9e 66 }
508ea882
NTND
67 else if (!strcmp(name, "deepen-since")) {
68 options.deepen_since = xstrdup(value);
69 return 0;
70 }
a45a2600
NTND
71 else if (!strcmp(name, "deepen-not")) {
72 string_list_append(&options.deepen_not, value);
73 return 0;
74 }
cccf74e2
NTND
75 else if (!strcmp(name, "deepen-relative")) {
76 if (!strcmp(value, "true"))
77 options.deepen_relative = 1;
78 else if (!strcmp(value, "false"))
79 options.deepen_relative = 0;
80 else
81 return -1;
82 return 0;
83 }
ef08ef9e
SP
84 else if (!strcmp(name, "followtags")) {
85 if (!strcmp(value, "true"))
86 options.followtags = 1;
87 else if (!strcmp(value, "false"))
88 options.followtags = 0;
89 else
90 return -1;
249b2004 91 return 0;
ef08ef9e 92 }
ae4efe19
SP
93 else if (!strcmp(name, "dry-run")) {
94 if (!strcmp(value, "true"))
95 options.dry_run = 1;
96 else if (!strcmp(value, "false"))
97 options.dry_run = 0;
98 else
99 return -1;
100 return 0;
101 }
9ba38048
NTND
102 else if (!strcmp(name, "check-connectivity")) {
103 if (!strcmp(value, "true"))
104 options.check_self_contained_and_connected = 1;
105 else if (!strcmp(value, "false"))
106 options.check_self_contained_and_connected = 0;
107 else
108 return -1;
109 return 0;
110 }
05c1eb10
JH
111 else if (!strcmp(name, "cas")) {
112 struct strbuf val = STRBUF_INIT;
113 strbuf_addf(&val, "--" CAS_OPT_NAME "=%s", value);
114 string_list_append(&cas_options, val.buf);
115 strbuf_release(&val);
116 return 0;
16094885
NTND
117 } else if (!strcmp(name, "cloning")) {
118 if (!strcmp(value, "true"))
119 options.cloning = 1;
120 else if (!strcmp(value, "false"))
121 options.cloning = 0;
122 else
123 return -1;
124 return 0;
125 } else if (!strcmp(name, "update-shallow")) {
126 if (!strcmp(value, "true"))
127 options.update_shallow = 1;
128 else if (!strcmp(value, "false"))
129 options.update_shallow = 0;
130 else
131 return -1;
132 return 0;
0ea47f9d
JH
133 } else if (!strcmp(name, "pushcert")) {
134 if (!strcmp(value, "true"))
30261094 135 options.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
0ea47f9d 136 else if (!strcmp(value, "false"))
30261094
DB
137 options.push_cert = SEND_PACK_PUSH_CERT_NEVER;
138 else if (!strcmp(value, "if-asked"))
139 options.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
0ea47f9d
JH
140 else
141 return -1;
142 return 0;
511155db
BW
143 } else if (!strcmp(name, "push-option")) {
144 string_list_append(&options.push_options, value);
145 return 0;
c915f11e
EW
146
147#if LIBCURL_VERSION_NUM >= 0x070a08
148 } else if (!strcmp(name, "family")) {
149 if (!strcmp(value, "ipv4"))
150 git_curl_ipresolve = CURL_IPRESOLVE_V4;
151 else if (!strcmp(value, "ipv6"))
152 git_curl_ipresolve = CURL_IPRESOLVE_V6;
153 else if (!strcmp(value, "all"))
154 git_curl_ipresolve = CURL_IPRESOLVE_WHATEVER;
155 else
156 return -1;
157 return 0;
158#endif /* LIBCURL_VERSION_NUM >= 0x070a08 */
16094885 159 } else {
ef08ef9e
SP
160 return 1 /* unsupported */;
161 }
162}
163
97cc7bc4
SP
164struct discovery {
165 const char *service;
166 char *buf_alloc;
167 char *buf;
168 size_t len;
2a455202 169 struct ref *refs;
910650d2 170 struct oid_array shallow;
97cc7bc4
SP
171 unsigned proto_git : 1;
172};
173static struct discovery *last_discovery;
174
b8054bbe
JK
175static struct ref *parse_git_refs(struct discovery *heads, int for_push)
176{
177 struct ref *list = NULL;
178 get_remote_heads(-1, heads->buf, heads->len, &list,
16094885 179 for_push ? REF_NORMAL : 0, NULL, &heads->shallow);
b8054bbe
JK
180 return list;
181}
182
183static struct ref *parse_info_refs(struct discovery *heads)
184{
185 char *data, *start, *mid;
186 char *ref_name;
187 int i = 0;
188
189 struct ref *refs = NULL;
190 struct ref *ref = NULL;
191 struct ref *last_ref = NULL;
192
193 data = heads->buf;
194 start = NULL;
195 mid = data;
196 while (i < heads->len) {
197 if (!start) {
198 start = &data[i];
199 }
200 if (data[i] == '\t')
201 mid = &data[i];
202 if (data[i] == '\n') {
203 if (mid - start != 40)
b227bbc4
JK
204 die("%sinfo/refs not valid: is this a git repository?",
205 url.buf);
b8054bbe
JK
206 data[i] = 0;
207 ref_name = mid + 1;
6f687c21 208 ref = alloc_ref(ref_name);
f4e54d02 209 get_oid_hex(start, &ref->old_oid);
b8054bbe
JK
210 if (!refs)
211 refs = ref;
212 if (last_ref)
213 last_ref->next = ref;
214 last_ref = ref;
215 start = NULL;
216 }
217 i++;
218 }
219
220 ref = alloc_ref("HEAD");
b227bbc4 221 if (!http_fetch_ref(url.buf, ref) &&
b8054bbe
JK
222 !resolve_remote_symref(ref, refs)) {
223 ref->next = refs;
224 refs = ref;
225 } else {
226 free(ref);
227 }
228
229 return refs;
230}
231
97cc7bc4
SP
232static void free_discovery(struct discovery *d)
233{
234 if (d) {
235 if (d == last_discovery)
236 last_discovery = NULL;
ee3051bd 237 free(d->shallow.oid);
97cc7bc4 238 free(d->buf_alloc);
2a455202 239 free_refs(d->refs);
97cc7bc4
SP
240 free(d);
241 }
242}
243
fc1b774c
JK
244static int show_http_message(struct strbuf *type, struct strbuf *charset,
245 struct strbuf *msg)
426e70d4
JK
246{
247 const char *p, *eol;
248
249 /*
250 * We only show text/plain parts, as other types are likely
251 * to be ugly to look at on the user's terminal.
426e70d4 252 */
bf197fd7 253 if (strcmp(type->buf, "text/plain"))
426e70d4 254 return -1;
fc1b774c
JK
255 if (charset->len)
256 strbuf_reencode(msg, charset->buf, get_log_output_encoding());
426e70d4
JK
257
258 strbuf_trim(msg);
259 if (!msg->len)
260 return -1;
261
262 p = msg->buf;
263 do {
264 eol = strchrnul(p, '\n');
265 fprintf(stderr, "remote: %.*s\n", (int)(eol - p), p);
266 p = eol + 1;
267 } while(*eol);
268 return 0;
269}
270
24d36f14 271static struct discovery *discover_refs(const char *service, int for_push)
a2d725b7 272{
4656bf47
SP
273 struct strbuf exp = STRBUF_INIT;
274 struct strbuf type = STRBUF_INIT;
fc1b774c 275 struct strbuf charset = STRBUF_INIT;
a2d725b7 276 struct strbuf buffer = STRBUF_INIT;
c65d5692 277 struct strbuf refs_url = STRBUF_INIT;
050ef365 278 struct strbuf effective_url = STRBUF_INIT;
97cc7bc4 279 struct discovery *last = last_discovery;
243c329c 280 int http_ret, maybe_smart = 0;
fcaa6e64 281 struct http_get_options http_options;
a2d725b7 282
97cc7bc4
SP
283 if (last && !strcmp(service, last->service))
284 return last;
285 free_discovery(last);
a2d725b7 286
b227bbc4 287 strbuf_addf(&refs_url, "%sinfo/refs", url.buf);
59556548 288 if ((starts_with(url.buf, "http://") || starts_with(url.buf, "https://")) &&
02572c2e 289 git_env_bool("GIT_SMART_HTTP", 1)) {
243c329c 290 maybe_smart = 1;
b227bbc4 291 if (!strchr(url.buf, '?'))
c65d5692 292 strbuf_addch(&refs_url, '?');
97cc7bc4 293 else
c65d5692
JK
294 strbuf_addch(&refs_url, '&');
295 strbuf_addf(&refs_url, "service=%s", service);
97cc7bc4 296 }
a2d725b7 297
fcaa6e64
JK
298 memset(&http_options, 0, sizeof(http_options));
299 http_options.content_type = &type;
300 http_options.charset = &charset;
301 http_options.effective_url = &effective_url;
302 http_options.base_url = &url;
50d34137 303 http_options.initial_request = 1;
fcaa6e64
JK
304 http_options.no_cache = 1;
305 http_options.keep_error = 1;
1bbcc224 306
fcaa6e64 307 http_ret = http_get_strbuf(refs_url.buf, &buffer, &http_options);
a2d725b7
DB
308 switch (http_ret) {
309 case HTTP_OK:
310 break;
311 case HTTP_MISSING_TARGET:
fc1b774c 312 show_http_message(&type, &charset, &buffer);
b227bbc4 313 die("repository '%s' not found", url.buf);
42653c09 314 case HTTP_NOAUTH:
fc1b774c 315 show_http_message(&type, &charset, &buffer);
b227bbc4 316 die("Authentication failed for '%s'", url.buf);
a2d725b7 317 default:
fc1b774c 318 show_http_message(&type, &charset, &buffer);
b227bbc4 319 die("unable to access '%s': %s", url.buf, curl_errorstr);
a2d725b7
DB
320 }
321
50d34137
JK
322 if (options.verbosity && !starts_with(refs_url.buf, url.buf))
323 warning(_("redirecting to %s"), url.buf);
324
97cc7bc4
SP
325 last= xcalloc(1, sizeof(*last_discovery));
326 last->service = service;
327 last->buf_alloc = strbuf_detach(&buffer, &last->len);
328 last->buf = last->buf_alloc;
329
4656bf47
SP
330 strbuf_addf(&exp, "application/x-%s-advertisement", service);
331 if (maybe_smart &&
332 (5 <= last->len && last->buf[4] == '#') &&
333 !strbuf_cmp(&exp, &type)) {
4981fe75
JK
334 char *line;
335
4656bf47
SP
336 /*
337 * smart HTTP response; validate that the service
97cc7bc4
SP
338 * pkt-line matches our request.
339 */
4981fe75 340 line = packet_read_line_buf(&last->buf, &last->len, NULL);
97cc7bc4 341
4656bf47 342 strbuf_reset(&exp);
97cc7bc4 343 strbuf_addf(&exp, "# service=%s", service);
4981fe75
JK
344 if (strcmp(line, exp.buf))
345 die("invalid server response; got '%s'", line);
97cc7bc4
SP
346 strbuf_release(&exp);
347
348 /* The header can include additional metadata lines, up
349 * until a packet flush marker. Ignore these now, but
350 * in the future we might start to scan them.
351 */
4981fe75
JK
352 while (packet_read_line_buf(&last->buf, &last->len, NULL))
353 ;
97cc7bc4
SP
354
355 last->proto_git = 1;
356 }
357
2a455202
JK
358 if (last->proto_git)
359 last->refs = parse_git_refs(last, for_push);
360 else
361 last->refs = parse_info_refs(last);
362
c65d5692 363 strbuf_release(&refs_url);
4656bf47
SP
364 strbuf_release(&exp);
365 strbuf_release(&type);
fc1b774c 366 strbuf_release(&charset);
050ef365 367 strbuf_release(&effective_url);
97cc7bc4
SP
368 strbuf_release(&buffer);
369 last_discovery = last;
370 return last;
371}
372
97cc7bc4
SP
373static struct ref *get_refs(int for_push)
374{
375 struct discovery *heads;
376
377 if (for_push)
2a455202 378 heads = discover_refs("git-receive-pack", for_push);
97cc7bc4 379 else
2a455202 380 heads = discover_refs("git-upload-pack", for_push);
97cc7bc4 381
2a455202 382 return heads->refs;
97cc7bc4
SP
383}
384
ae4efe19
SP
385static void output_refs(struct ref *refs)
386{
387 struct ref *posn;
388 for (posn = refs; posn; posn = posn->next) {
389 if (posn->symref)
390 printf("@%s %s\n", posn->symref, posn->name);
391 else
f4e54d02 392 printf("%s %s\n", oid_to_hex(&posn->old_oid), posn->name);
ae4efe19
SP
393 }
394 printf("\n");
395 fflush(stdout);
ae4efe19
SP
396}
397
de1a2fdd
SP
398struct rpc_state {
399 const char *service_name;
400 const char **argv;
8150749d 401 struct strbuf *stdin_preamble;
de1a2fdd
SP
402 char *service_url;
403 char *hdr_content_type;
404 char *hdr_accept;
405 char *buf;
406 size_t alloc;
407 size_t len;
408 size_t pos;
409 int in;
410 int out;
296b847c 411 int any_written;
de1a2fdd 412 struct strbuf result;
b8538603 413 unsigned gzip_request : 1;
6c81a990 414 unsigned initial_buffer : 1;
de1a2fdd
SP
415};
416
417static size_t rpc_out(void *ptr, size_t eltsize,
418 size_t nmemb, void *buffer_)
419{
420 size_t max = eltsize * nmemb;
421 struct rpc_state *rpc = buffer_;
422 size_t avail = rpc->len - rpc->pos;
423
424 if (!avail) {
6c81a990 425 rpc->initial_buffer = 0;
4981fe75 426 avail = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
de1a2fdd
SP
427 if (!avail)
428 return 0;
429 rpc->pos = 0;
430 rpc->len = avail;
431 }
432
48310608 433 if (max < avail)
de1a2fdd
SP
434 avail = max;
435 memcpy(ptr, rpc->buf + rpc->pos, avail);
436 rpc->pos += avail;
437 return avail;
438}
439
6c81a990 440#ifndef NO_CURL_IOCTL
5092d3ec 441static curlioerr rpc_ioctl(CURL *handle, int cmd, void *clientp)
6c81a990
MS
442{
443 struct rpc_state *rpc = clientp;
444
445 switch (cmd) {
446 case CURLIOCMD_NOP:
447 return CURLIOE_OK;
448
449 case CURLIOCMD_RESTARTREAD:
450 if (rpc->initial_buffer) {
451 rpc->pos = 0;
452 return CURLIOE_OK;
453 }
b725b270 454 error("unable to rewind rpc post data - try increasing http.postBuffer");
6c81a990
MS
455 return CURLIOE_FAILRESTART;
456
457 default:
458 return CURLIOE_UNKNOWNCMD;
459 }
460}
461#endif
462
a04ff3ec 463static size_t rpc_in(char *ptr, size_t eltsize,
de1a2fdd
SP
464 size_t nmemb, void *buffer_)
465{
466 size_t size = eltsize * nmemb;
467 struct rpc_state *rpc = buffer_;
296b847c
DT
468 if (size)
469 rpc->any_written = 1;
de1a2fdd
SP
470 write_or_die(rpc->in, ptr, size);
471 return size;
472}
473
3a347ed7
JK
474static int run_slot(struct active_request_slot *slot,
475 struct slot_results *results)
206b099d 476{
b81401c1 477 int err;
3a347ed7 478 struct slot_results results_buf;
206b099d 479
3a347ed7
JK
480 if (!results)
481 results = &results_buf;
482
beed336c 483 err = run_one_slot(slot, results);
206b099d 484
b81401c1 485 if (err != HTTP_OK && err != HTTP_REAUTH) {
00540458
SP
486 struct strbuf msg = STRBUF_INIT;
487 if (results->http_code && results->http_code != 200)
488 strbuf_addf(&msg, "HTTP %ld", results->http_code);
489 if (results->curl_result != CURLE_OK) {
490 if (msg.len)
491 strbuf_addch(&msg, ' ');
492 strbuf_addf(&msg, "curl %d", results->curl_result);
493 if (curl_errorstr[0]) {
494 strbuf_addch(&msg, ' ');
495 strbuf_addstr(&msg, curl_errorstr);
496 }
497 }
498 error("RPC failed; %s", msg.buf);
499 strbuf_release(&msg);
206b099d
SP
500 }
501
502 return err;
503}
504
3a347ed7 505static int probe_rpc(struct rpc_state *rpc, struct slot_results *results)
206b099d
SP
506{
507 struct active_request_slot *slot;
8cb01e2f 508 struct curl_slist *headers = http_copy_default_headers();
206b099d
SP
509 struct strbuf buf = STRBUF_INIT;
510 int err;
511
512 slot = get_active_slot();
513
514 headers = curl_slist_append(headers, rpc->hdr_content_type);
515 headers = curl_slist_append(headers, rpc->hdr_accept);
516
517 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
518 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
519 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
aa90b969 520 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, NULL);
206b099d
SP
521 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, "0000");
522 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, 4);
523 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
524 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
525 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buf);
526
3a347ed7 527 err = run_slot(slot, results);
206b099d
SP
528
529 curl_slist_free_all(headers);
530 strbuf_release(&buf);
531 return err;
532}
533
37ee680d
DT
534static curl_off_t xcurl_off_t(ssize_t len) {
535 if (len > maximum_signed_value_of_type(curl_off_t))
536 die("cannot handle pushes this big");
537 return (curl_off_t) len;
538}
539
de1a2fdd
SP
540static int post_rpc(struct rpc_state *rpc)
541{
542 struct active_request_slot *slot;
8cb01e2f 543 struct curl_slist *headers = http_copy_default_headers();
b8538603
SP
544 int use_gzip = rpc->gzip_request;
545 char *gzip_body = NULL;
37711549 546 size_t gzip_size = 0;
206b099d 547 int err, large_request = 0;
c80d96ca 548 int needs_100_continue = 0;
de1a2fdd
SP
549
550 /* Try to load the entire request, if we can fit it into the
551 * allocated buffer space we can use HTTP/1.0 and avoid the
552 * chunked encoding mess.
553 */
554 while (1) {
555 size_t left = rpc->alloc - rpc->len;
556 char *buf = rpc->buf + rpc->len;
557 int n;
558
559 if (left < LARGE_PACKET_MAX) {
560 large_request = 1;
b8538603 561 use_gzip = 0;
de1a2fdd
SP
562 break;
563 }
564
4981fe75 565 n = packet_read(rpc->out, NULL, NULL, buf, left, 0);
de1a2fdd
SP
566 if (!n)
567 break;
568 rpc->len += n;
569 }
570
206b099d 571 if (large_request) {
c80d96ca 572 struct slot_results results;
573
b81401c1 574 do {
c80d96ca 575 err = probe_rpc(rpc, &results);
2501aff8
JK
576 if (err == HTTP_REAUTH)
577 credential_fill(&http_auth);
b81401c1
JK
578 } while (err == HTTP_REAUTH);
579 if (err != HTTP_OK)
580 return -1;
c80d96ca 581
582 if (results.auth_avail & CURLAUTH_GSSNEGOTIATE)
583 needs_100_continue = 1;
206b099d
SP
584 }
585
abf8df86
JK
586 headers = curl_slist_append(headers, rpc->hdr_content_type);
587 headers = curl_slist_append(headers, rpc->hdr_accept);
c80d96ca 588 headers = curl_slist_append(headers, needs_100_continue ?
589 "Expect: 100-continue" : "Expect:");
abf8df86
JK
590
591retry:
de1a2fdd 592 slot = get_active_slot();
de1a2fdd 593
de1a2fdd 594 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
d21f9794 595 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
de1a2fdd 596 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
aa90b969 597 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "gzip");
de1a2fdd 598
de1a2fdd
SP
599 if (large_request) {
600 /* The request body is large and the size cannot be predicted.
601 * We must use chunked encoding to send it.
602 */
de1a2fdd 603 headers = curl_slist_append(headers, "Transfer-Encoding: chunked");
6c81a990 604 rpc->initial_buffer = 1;
de1a2fdd
SP
605 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, rpc_out);
606 curl_easy_setopt(slot->curl, CURLOPT_INFILE, rpc);
6c81a990
MS
607#ifndef NO_CURL_IOCTL
608 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, rpc_ioctl);
609 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, rpc);
610#endif
de1a2fdd
SP
611 if (options.verbosity > 1) {
612 fprintf(stderr, "POST %s (chunked)\n", rpc->service_name);
613 fflush(stderr);
614 }
615
2e736fd5
JK
616 } else if (gzip_body) {
617 /*
618 * If we are looping to retry authentication, then the previous
619 * run will have set up the headers and gzip buffer already,
620 * and we just need to send it.
621 */
622 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
37ee680d 623 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(gzip_size));
2e736fd5 624
b8538603
SP
625 } else if (use_gzip && 1024 < rpc->len) {
626 /* The client backend isn't giving us compressed data so
627 * we can try to deflate it ourselves, this may save on.
628 * the transfer time.
629 */
ef49a7a0 630 git_zstream stream;
b8538603
SP
631 int ret;
632
55bb5c91 633 git_deflate_init_gzip(&stream, Z_BEST_COMPRESSION);
df126e10
JK
634 gzip_size = git_deflate_bound(&stream, rpc->len);
635 gzip_body = xmalloc(gzip_size);
b8538603
SP
636
637 stream.next_in = (unsigned char *)rpc->buf;
638 stream.avail_in = rpc->len;
639 stream.next_out = (unsigned char *)gzip_body;
df126e10 640 stream.avail_out = gzip_size;
b8538603 641
55bb5c91 642 ret = git_deflate(&stream, Z_FINISH);
b8538603
SP
643 if (ret != Z_STREAM_END)
644 die("cannot deflate request; zlib deflate error %d", ret);
645
55bb5c91 646 ret = git_deflate_end_gently(&stream);
b8538603
SP
647 if (ret != Z_OK)
648 die("cannot deflate request; zlib end error %d", ret);
649
df126e10 650 gzip_size = stream.total_out;
b8538603
SP
651
652 headers = curl_slist_append(headers, "Content-Encoding: gzip");
653 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
37ee680d 654 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(gzip_size));
b8538603
SP
655
656 if (options.verbosity > 1) {
657 fprintf(stderr, "POST %s (gzip %lu to %lu bytes)\n",
658 rpc->service_name,
df126e10 659 (unsigned long)rpc->len, (unsigned long)gzip_size);
b8538603
SP
660 fflush(stderr);
661 }
de1a2fdd
SP
662 } else {
663 /* We know the complete request size in advance, use the
664 * more normal Content-Length approach.
665 */
666 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, rpc->buf);
37ee680d 667 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(rpc->len));
de1a2fdd
SP
668 if (options.verbosity > 1) {
669 fprintf(stderr, "POST %s (%lu bytes)\n",
670 rpc->service_name, (unsigned long)rpc->len);
671 fflush(stderr);
672 }
673 }
674
675 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
676 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, rpc_in);
677 curl_easy_setopt(slot->curl, CURLOPT_FILE, rpc);
678
296b847c
DT
679
680 rpc->any_written = 0;
3a347ed7 681 err = run_slot(slot, NULL);
2501aff8
JK
682 if (err == HTTP_REAUTH && !large_request) {
683 credential_fill(&http_auth);
abf8df86 684 goto retry;
2501aff8 685 }
b81401c1
JK
686 if (err != HTTP_OK)
687 err = -1;
de1a2fdd 688
296b847c
DT
689 if (!rpc->any_written)
690 err = -1;
691
de1a2fdd 692 curl_slist_free_all(headers);
b8538603 693 free(gzip_body);
de1a2fdd
SP
694 return err;
695}
696
697static int rpc_service(struct rpc_state *rpc, struct discovery *heads)
698{
699 const char *svc = rpc->service_name;
700 struct strbuf buf = STRBUF_INIT;
8150749d 701 struct strbuf *preamble = rpc->stdin_preamble;
d3180279 702 struct child_process client = CHILD_PROCESS_INIT;
de1a2fdd
SP
703 int err = 0;
704
de1a2fdd
SP
705 client.in = -1;
706 client.out = -1;
707 client.git_cmd = 1;
708 client.argv = rpc->argv;
709 if (start_command(&client))
710 exit(1);
8150749d
IT
711 if (preamble)
712 write_or_die(client.in, preamble->buf, preamble->len);
de1a2fdd
SP
713 if (heads)
714 write_or_die(client.in, heads->buf, heads->len);
715
716 rpc->alloc = http_post_buffer;
717 rpc->buf = xmalloc(rpc->alloc);
718 rpc->in = client.in;
719 rpc->out = client.out;
720 strbuf_init(&rpc->result, 0);
721
b227bbc4 722 strbuf_addf(&buf, "%s%s", url.buf, svc);
de1a2fdd
SP
723 rpc->service_url = strbuf_detach(&buf, NULL);
724
725 strbuf_addf(&buf, "Content-Type: application/x-%s-request", svc);
726 rpc->hdr_content_type = strbuf_detach(&buf, NULL);
727
8efa5f62 728 strbuf_addf(&buf, "Accept: application/x-%s-result", svc);
de1a2fdd
SP
729 rpc->hdr_accept = strbuf_detach(&buf, NULL);
730
731 while (!err) {
4981fe75 732 int n = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
de1a2fdd
SP
733 if (!n)
734 break;
735 rpc->pos = 0;
736 rpc->len = n;
737 err |= post_rpc(rpc);
738 }
de1a2fdd
SP
739
740 close(client.in);
de1a2fdd 741 client.in = -1;
6cdf0223
SP
742 if (!err) {
743 strbuf_read(&rpc->result, client.out, 0);
744 } else {
745 char buf[4096];
746 for (;;)
747 if (xread(client.out, buf, sizeof(buf)) <= 0)
748 break;
749 }
b4ee10f6
SP
750
751 close(client.out);
de1a2fdd
SP
752 client.out = -1;
753
754 err |= finish_command(&client);
755 free(rpc->service_url);
756 free(rpc->hdr_content_type);
757 free(rpc->hdr_accept);
758 free(rpc->buf);
759 strbuf_release(&buf);
760 return err;
761}
762
292ce46b
SP
763static int fetch_dumb(int nr_heads, struct ref **to_fetch)
764{
26e1e0b2 765 struct walker *walker;
b32fa95f 766 char **targets;
292ce46b
SP
767 int ret, i;
768
b32fa95f 769 ALLOC_ARRAY(targets, nr_heads);
508ea882
NTND
770 if (options.depth || options.deepen_since)
771 die("dumb http transport does not support shallow capabilities");
292ce46b 772 for (i = 0; i < nr_heads; i++)
f4e54d02 773 targets[i] = xstrdup(oid_to_hex(&to_fetch[i]->old_oid));
292ce46b 774
b227bbc4 775 walker = get_http_walker(url.buf);
292ce46b
SP
776 walker->get_all = 1;
777 walker->get_tree = 1;
778 walker->get_history = 1;
ef08ef9e 779 walker->get_verbosely = options.verbosity >= 3;
292ce46b
SP
780 walker->get_recover = 0;
781 ret = walker_fetch(walker, nr_heads, targets, NULL, NULL);
26e1e0b2 782 walker_free(walker);
292ce46b
SP
783
784 for (i = 0; i < nr_heads; i++)
785 free(targets[i]);
786 free(targets);
787
b725b270 788 return ret ? error("fetch failed.") : 0;
292ce46b
SP
789}
790
249b2004
SP
791static int fetch_git(struct discovery *heads,
792 int nr_heads, struct ref **to_fetch)
793{
794 struct rpc_state rpc;
8150749d 795 struct strbuf preamble = STRBUF_INIT;
b5f62ebe
NTND
796 int i, err;
797 struct argv_array args = ARGV_ARRAY_INIT;
798
799 argv_array_pushl(&args, "fetch-pack", "--stateless-rpc",
800 "--stdin", "--lock-pack", NULL);
249b2004 801 if (options.followtags)
b5f62ebe 802 argv_array_push(&args, "--include-tag");
249b2004 803 if (options.thin)
b5f62ebe
NTND
804 argv_array_push(&args, "--thin");
805 if (options.verbosity >= 3)
806 argv_array_pushl(&args, "-v", "-v", NULL);
9ba38048 807 if (options.check_self_contained_and_connected)
b5f62ebe 808 argv_array_push(&args, "--check-self-contained-and-connected");
16094885 809 if (options.cloning)
b5f62ebe 810 argv_array_push(&args, "--cloning");
16094885 811 if (options.update_shallow)
b5f62ebe 812 argv_array_push(&args, "--update-shallow");
249b2004 813 if (!options.progress)
b5f62ebe
NTND
814 argv_array_push(&args, "--no-progress");
815 if (options.depth)
816 argv_array_pushf(&args, "--depth=%lu", options.depth);
508ea882
NTND
817 if (options.deepen_since)
818 argv_array_pushf(&args, "--shallow-since=%s", options.deepen_since);
a45a2600
NTND
819 for (i = 0; i < options.deepen_not.nr; i++)
820 argv_array_pushf(&args, "--shallow-exclude=%s",
821 options.deepen_not.items[i].string);
cccf74e2
NTND
822 if (options.deepen_relative && options.depth)
823 argv_array_push(&args, "--deepen-relative");
b5f62ebe 824 argv_array_push(&args, url.buf);
8150749d 825
249b2004
SP
826 for (i = 0; i < nr_heads; i++) {
827 struct ref *ref = to_fetch[i];
94ee8e2c 828 if (!*ref->name)
249b2004 829 die("cannot fetch by sha1 over smart http");
58f2ed05 830 packet_buf_write(&preamble, "%s %s\n",
f4e54d02 831 oid_to_hex(&ref->old_oid), ref->name);
249b2004 832 }
8150749d 833 packet_buf_flush(&preamble);
249b2004
SP
834
835 memset(&rpc, 0, sizeof(rpc));
836 rpc.service_name = "git-upload-pack",
b5f62ebe 837 rpc.argv = args.argv;
8150749d 838 rpc.stdin_preamble = &preamble;
b8538603 839 rpc.gzip_request = 1;
249b2004
SP
840
841 err = rpc_service(&rpc, heads);
842 if (rpc.result.len)
cdf4fb8e 843 write_or_die(1, rpc.result.buf, rpc.result.len);
249b2004 844 strbuf_release(&rpc.result);
8150749d 845 strbuf_release(&preamble);
b5f62ebe 846 argv_array_clear(&args);
249b2004
SP
847 return err;
848}
849
850static int fetch(int nr_heads, struct ref **to_fetch)
851{
2a455202 852 struct discovery *d = discover_refs("git-upload-pack", 0);
249b2004
SP
853 if (d->proto_git)
854 return fetch_git(d, nr_heads, to_fetch);
855 else
856 return fetch_dumb(nr_heads, to_fetch);
857}
858
292ce46b
SP
859static void parse_fetch(struct strbuf *buf)
860{
861 struct ref **to_fetch = NULL;
862 struct ref *list_head = NULL;
863 struct ref **list = &list_head;
864 int alloc_heads = 0, nr_heads = 0;
865
866 do {
95b567c7
JK
867 const char *p;
868 if (skip_prefix(buf->buf, "fetch ", &p)) {
869 const char *name;
292ce46b 870 struct ref *ref;
8338c911 871 struct object_id old_oid;
292ce46b 872
8338c911 873 if (get_oid_hex(p, &old_oid))
292ce46b 874 die("protocol error: expected sha/ref, got %s'", p);
8338c911 875 if (p[GIT_SHA1_HEXSZ] == ' ')
876 name = p + GIT_SHA1_HEXSZ + 1;
877 else if (!p[GIT_SHA1_HEXSZ])
292ce46b
SP
878 name = "";
879 else
880 die("protocol error: expected sha/ref, got %s'", p);
881
882 ref = alloc_ref(name);
8338c911 883 oidcpy(&ref->old_oid, &old_oid);
292ce46b
SP
884
885 *list = ref;
886 list = &ref->next;
887
888 ALLOC_GROW(to_fetch, nr_heads + 1, alloc_heads);
889 to_fetch[nr_heads++] = ref;
890 }
891 else
892 die("http transport does not support %s", buf->buf);
893
894 strbuf_reset(buf);
8f309aeb 895 if (strbuf_getline_lf(buf, stdin) == EOF)
292ce46b
SP
896 return;
897 if (!*buf->buf)
898 break;
899 } while (1);
900
249b2004 901 if (fetch(nr_heads, to_fetch))
292ce46b
SP
902 exit(128); /* error already reported */
903 free_refs(list_head);
904 free(to_fetch);
905
906 printf("\n");
907 fflush(stdout);
908 strbuf_reset(buf);
909}
910
ae4efe19
SP
911static int push_dav(int nr_spec, char **specs)
912{
850d2fec
JK
913 struct child_process child = CHILD_PROCESS_INIT;
914 size_t i;
ae4efe19 915
850d2fec
JK
916 child.git_cmd = 1;
917 argv_array_push(&child.args, "http-push");
918 argv_array_push(&child.args, "--helper-status");
ae4efe19 919 if (options.dry_run)
850d2fec 920 argv_array_push(&child.args, "--dry-run");
ae4efe19 921 if (options.verbosity > 1)
850d2fec
JK
922 argv_array_push(&child.args, "--verbose");
923 argv_array_push(&child.args, url.buf);
ae4efe19 924 for (i = 0; i < nr_spec; i++)
850d2fec 925 argv_array_push(&child.args, specs[i]);
ae4efe19 926
850d2fec
JK
927 if (run_command(&child))
928 die("git-http-push failed");
ae4efe19
SP
929 return 0;
930}
931
de1a2fdd
SP
932static int push_git(struct discovery *heads, int nr_spec, char **specs)
933{
934 struct rpc_state rpc;
222b1212
JH
935 int i, err;
936 struct argv_array args;
05c1eb10 937 struct string_list_item *cas_option;
26be19ba 938 struct strbuf preamble = STRBUF_INIT;
222b1212
JH
939
940 argv_array_init(&args);
941 argv_array_pushl(&args, "send-pack", "--stateless-rpc", "--helper-status",
942 NULL);
de1a2fdd 943
de1a2fdd 944 if (options.thin)
222b1212 945 argv_array_push(&args, "--thin");
de1a2fdd 946 if (options.dry_run)
222b1212 947 argv_array_push(&args, "--dry-run");
30261094
DB
948 if (options.push_cert == SEND_PACK_PUSH_CERT_ALWAYS)
949 argv_array_push(&args, "--signed=yes");
950 else if (options.push_cert == SEND_PACK_PUSH_CERT_IF_ASKED)
951 argv_array_push(&args, "--signed=if-asked");
c207e34f 952 if (options.verbosity == 0)
222b1212 953 argv_array_push(&args, "--quiet");
c207e34f 954 else if (options.verbosity > 1)
222b1212 955 argv_array_push(&args, "--verbose");
511155db
BW
956 for (i = 0; i < options.push_options.nr; i++)
957 argv_array_pushf(&args, "--push-option=%s",
958 options.push_options.items[i].string);
222b1212 959 argv_array_push(&args, options.progress ? "--progress" : "--no-progress");
05c1eb10 960 for_each_string_list_item(cas_option, &cas_options)
2233ad45 961 argv_array_push(&args, cas_option->string);
b227bbc4 962 argv_array_push(&args, url.buf);
26be19ba
JK
963
964 argv_array_push(&args, "--stdin");
de1a2fdd 965 for (i = 0; i < nr_spec; i++)
26be19ba
JK
966 packet_buf_write(&preamble, "%s\n", specs[i]);
967 packet_buf_flush(&preamble);
de1a2fdd
SP
968
969 memset(&rpc, 0, sizeof(rpc));
970 rpc.service_name = "git-receive-pack",
222b1212 971 rpc.argv = args.argv;
26be19ba 972 rpc.stdin_preamble = &preamble;
de1a2fdd
SP
973
974 err = rpc_service(&rpc, heads);
975 if (rpc.result.len)
cdf4fb8e 976 write_or_die(1, rpc.result.buf, rpc.result.len);
de1a2fdd 977 strbuf_release(&rpc.result);
26be19ba 978 strbuf_release(&preamble);
222b1212 979 argv_array_clear(&args);
de1a2fdd
SP
980 return err;
981}
982
983static int push(int nr_spec, char **specs)
984{
2a455202 985 struct discovery *heads = discover_refs("git-receive-pack", 1);
de1a2fdd
SP
986 int ret;
987
988 if (heads->proto_git)
989 ret = push_git(heads, nr_spec, specs);
990 else
991 ret = push_dav(nr_spec, specs);
992 free_discovery(heads);
993 return ret;
994}
995
ae4efe19
SP
996static void parse_push(struct strbuf *buf)
997{
998 char **specs = NULL;
5238cbf6 999 int alloc_spec = 0, nr_spec = 0, i, ret;
ae4efe19
SP
1000
1001 do {
59556548 1002 if (starts_with(buf->buf, "push ")) {
ae4efe19
SP
1003 ALLOC_GROW(specs, nr_spec + 1, alloc_spec);
1004 specs[nr_spec++] = xstrdup(buf->buf + 5);
1005 }
1006 else
1007 die("http transport does not support %s", buf->buf);
1008
1009 strbuf_reset(buf);
8f309aeb 1010 if (strbuf_getline_lf(buf, stdin) == EOF)
dc4cd767 1011 goto free_specs;
ae4efe19
SP
1012 if (!*buf->buf)
1013 break;
1014 } while (1);
1015
5238cbf6 1016 ret = push(nr_spec, specs);
ae4efe19
SP
1017 printf("\n");
1018 fflush(stdout);
dc4cd767 1019
5238cbf6
SP
1020 if (ret)
1021 exit(128); /* error already reported */
1022
dc4cd767
JM
1023 free_specs:
1024 for (i = 0; i < nr_spec; i++)
1025 free(specs[i]);
1026 free(specs);
ae4efe19
SP
1027}
1028
3f2e2297 1029int cmd_main(int argc, const char **argv)
a2d725b7 1030{
a2d725b7 1031 struct strbuf buf = STRBUF_INIT;
a45d3d7e 1032 int nongit;
a2d725b7 1033
a45d3d7e 1034 setup_git_directory_gently(&nongit);
a2d725b7 1035 if (argc < 2) {
cdaa4e98 1036 error("remote-curl: usage: git remote-curl <remote> [<url>]");
a2d725b7
DB
1037 return 1;
1038 }
1039
ef08ef9e
SP
1040 options.verbosity = 1;
1041 options.progress = !!isatty(2);
de1a2fdd 1042 options.thin = 1;
a45a2600 1043 string_list_init(&options.deepen_not, 1);
511155db 1044 string_list_init(&options.push_options, 1);
ef08ef9e 1045
a2d725b7
DB
1046 remote = remote_get(argv[1]);
1047
1048 if (argc > 2) {
b227bbc4 1049 end_url_with_slash(&url, argv[2]);
a2d725b7 1050 } else {
b227bbc4 1051 end_url_with_slash(&url, remote->url[0]);
a2d725b7
DB
1052 }
1053
b227bbc4 1054 http_init(remote, url.buf, 0);
888692b7 1055
a2d725b7 1056 do {
95b567c7
JK
1057 const char *arg;
1058
8f309aeb 1059 if (strbuf_getline_lf(&buf, stdin) == EOF) {
1843f0ce 1060 if (ferror(stdin))
cdaa4e98 1061 error("remote-curl: error reading command stream from git");
1843f0ce
SR
1062 return 1;
1063 }
1064 if (buf.len == 0)
a2d725b7 1065 break;
59556548 1066 if (starts_with(buf.buf, "fetch ")) {
a45d3d7e 1067 if (nongit)
cdaa4e98 1068 die("remote-curl: fetch attempted without a local repo");
292ce46b
SP
1069 parse_fetch(&buf);
1070
59556548 1071 } else if (!strcmp(buf.buf, "list") || starts_with(buf.buf, "list ")) {
97cc7bc4
SP
1072 int for_push = !!strstr(buf.buf + 4, "for-push");
1073 output_refs(get_refs(for_push));
ae4efe19 1074
59556548 1075 } else if (starts_with(buf.buf, "push ")) {
ae4efe19
SP
1076 parse_push(&buf);
1077
95b567c7
JK
1078 } else if (skip_prefix(buf.buf, "option ", &arg)) {
1079 char *value = strchr(arg, ' ');
ef08ef9e
SP
1080 int result;
1081
1082 if (value)
1083 *value++ = '\0';
1084 else
1085 value = "true";
1086
95b567c7 1087 result = set_option(arg, value);
ef08ef9e
SP
1088 if (!result)
1089 printf("ok\n");
1090 else if (result < 0)
1091 printf("error invalid value\n");
1092 else
1093 printf("unsupported\n");
a2d725b7 1094 fflush(stdout);
ef08ef9e 1095
a2d725b7
DB
1096 } else if (!strcmp(buf.buf, "capabilities")) {
1097 printf("fetch\n");
ef08ef9e 1098 printf("option\n");
ae4efe19 1099 printf("push\n");
9ba38048 1100 printf("check-connectivity\n");
a2d725b7
DB
1101 printf("\n");
1102 fflush(stdout);
1103 } else {
cdaa4e98 1104 error("remote-curl: unknown command '%s' from git", buf.buf);
a2d725b7
DB
1105 return 1;
1106 }
1107 strbuf_reset(&buf);
1108 } while (1);
888692b7
TRC
1109
1110 http_cleanup();
1111
a2d725b7
DB
1112 return 0;
1113}