]> git.ipfire.org Git - thirdparty/git.git/blame - remote-curl.c
bundle: detect hash algorithm when reading refs
[thirdparty/git.git] / remote-curl.c
CommitLineData
a2d725b7 1#include "cache.h"
b2141fc1 2#include "config.h"
a2d725b7 3#include "remote.h"
ad6ac124 4#include "connect.h"
a2d725b7
DB
5#include "strbuf.h"
6#include "walker.h"
7#include "http.h"
d807c4a0 8#include "exec-cmd.h"
ae4efe19 9#include "run-command.h"
97cc7bc4 10#include "pkt-line.h"
05c1eb10 11#include "string-list.h"
de1a2fdd 12#include "sideband.h"
222b1212 13#include "argv-array.h"
2501aff8 14#include "credential.h"
fe299ec5 15#include "oid-array.h"
30261094 16#include "send-pack.h"
ad6ac124 17#include "protocol.h"
90dce21e 18#include "quote.h"
c1284b21 19#include "transport.h"
a2d725b7 20
37a8768f 21static struct remote *remote;
b227bbc4
JK
22/* always ends with a trailing slash */
23static struct strbuf url = STRBUF_INIT;
37a8768f 24
ef08ef9e
SP
25struct options {
26 int verbosity;
27 unsigned long depth;
508ea882 28 char *deepen_since;
a45a2600 29 struct string_list deepen_not;
511155db 30 struct string_list push_options;
acb0c572 31 char *filter;
ef08ef9e 32 unsigned progress : 1,
9ba38048 33 check_self_contained_and_connected : 1,
16094885
NTND
34 cloning : 1,
35 update_shallow : 1,
ae4efe19 36 followtags : 1,
de1a2fdd 37 dry_run : 1,
0ea47f9d 38 thin : 1,
30261094 39 /* One of the SEND_PACK_PUSH_CERT_* constants. */
cccf74e2 40 push_cert : 2,
88e2f9ed
JT
41 deepen_relative : 1,
42 from_promisor : 1,
6f119424 43 no_dependents : 1,
7f605017 44 atomic : 1,
45 object_format : 1;
46 const struct git_hash_algo *hash_algo;
ef08ef9e
SP
47};
48static struct options options;
05c1eb10 49static struct string_list cas_options = STRING_LIST_INIT_DUP;
ef08ef9e 50
ef08ef9e
SP
51static int set_option(const char *name, const char *value)
52{
53 if (!strcmp(name, "verbosity")) {
54 char *end;
55 int v = strtol(value, &end, 10);
56 if (value == end || *end)
57 return -1;
58 options.verbosity = v;
59 return 0;
60 }
61 else if (!strcmp(name, "progress")) {
62 if (!strcmp(value, "true"))
63 options.progress = 1;
64 else if (!strcmp(value, "false"))
65 options.progress = 0;
66 else
67 return -1;
249b2004 68 return 0;
ef08ef9e
SP
69 }
70 else if (!strcmp(name, "depth")) {
71 char *end;
72 unsigned long v = strtoul(value, &end, 10);
73 if (value == end || *end)
74 return -1;
75 options.depth = v;
249b2004 76 return 0;
ef08ef9e 77 }
508ea882
NTND
78 else if (!strcmp(name, "deepen-since")) {
79 options.deepen_since = xstrdup(value);
80 return 0;
81 }
a45a2600
NTND
82 else if (!strcmp(name, "deepen-not")) {
83 string_list_append(&options.deepen_not, value);
84 return 0;
85 }
cccf74e2
NTND
86 else if (!strcmp(name, "deepen-relative")) {
87 if (!strcmp(value, "true"))
88 options.deepen_relative = 1;
89 else if (!strcmp(value, "false"))
90 options.deepen_relative = 0;
91 else
92 return -1;
93 return 0;
94 }
ef08ef9e
SP
95 else if (!strcmp(name, "followtags")) {
96 if (!strcmp(value, "true"))
97 options.followtags = 1;
98 else if (!strcmp(value, "false"))
99 options.followtags = 0;
100 else
101 return -1;
249b2004 102 return 0;
ef08ef9e 103 }
ae4efe19
SP
104 else if (!strcmp(name, "dry-run")) {
105 if (!strcmp(value, "true"))
106 options.dry_run = 1;
107 else if (!strcmp(value, "false"))
108 options.dry_run = 0;
109 else
110 return -1;
111 return 0;
112 }
9ba38048
NTND
113 else if (!strcmp(name, "check-connectivity")) {
114 if (!strcmp(value, "true"))
115 options.check_self_contained_and_connected = 1;
116 else if (!strcmp(value, "false"))
117 options.check_self_contained_and_connected = 0;
118 else
119 return -1;
120 return 0;
121 }
05c1eb10
JH
122 else if (!strcmp(name, "cas")) {
123 struct strbuf val = STRBUF_INIT;
124 strbuf_addf(&val, "--" CAS_OPT_NAME "=%s", value);
125 string_list_append(&cas_options, val.buf);
126 strbuf_release(&val);
127 return 0;
16094885
NTND
128 } else if (!strcmp(name, "cloning")) {
129 if (!strcmp(value, "true"))
130 options.cloning = 1;
131 else if (!strcmp(value, "false"))
132 options.cloning = 0;
133 else
134 return -1;
135 return 0;
136 } else if (!strcmp(name, "update-shallow")) {
137 if (!strcmp(value, "true"))
138 options.update_shallow = 1;
139 else if (!strcmp(value, "false"))
140 options.update_shallow = 0;
141 else
142 return -1;
143 return 0;
0ea47f9d
JH
144 } else if (!strcmp(name, "pushcert")) {
145 if (!strcmp(value, "true"))
30261094 146 options.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
0ea47f9d 147 else if (!strcmp(value, "false"))
30261094
DB
148 options.push_cert = SEND_PACK_PUSH_CERT_NEVER;
149 else if (!strcmp(value, "if-asked"))
150 options.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
0ea47f9d
JH
151 else
152 return -1;
153 return 0;
6f119424 154 } else if (!strcmp(name, "atomic")) {
155 if (!strcmp(value, "true"))
156 options.atomic = 1;
157 else if (!strcmp(value, "false"))
158 options.atomic = 0;
159 else
160 return -1;
161 return 0;
511155db 162 } else if (!strcmp(name, "push-option")) {
90dce21e
JK
163 if (*value != '"')
164 string_list_append(&options.push_options, value);
165 else {
166 struct strbuf unquoted = STRBUF_INIT;
167 if (unquote_c_style(&unquoted, value, NULL) < 0)
ed8b4132 168 die(_("invalid quoting in push-option value: '%s'"), value);
90dce21e
JK
169 string_list_append_nodup(&options.push_options,
170 strbuf_detach(&unquoted, NULL));
171 }
511155db 172 return 0;
c915f11e
EW
173
174#if LIBCURL_VERSION_NUM >= 0x070a08
175 } else if (!strcmp(name, "family")) {
176 if (!strcmp(value, "ipv4"))
177 git_curl_ipresolve = CURL_IPRESOLVE_V4;
178 else if (!strcmp(value, "ipv6"))
179 git_curl_ipresolve = CURL_IPRESOLVE_V6;
180 else if (!strcmp(value, "all"))
181 git_curl_ipresolve = CURL_IPRESOLVE_WHATEVER;
182 else
183 return -1;
184 return 0;
185#endif /* LIBCURL_VERSION_NUM >= 0x070a08 */
88e2f9ed
JT
186 } else if (!strcmp(name, "from-promisor")) {
187 options.from_promisor = 1;
188 return 0;
189 } else if (!strcmp(name, "no-dependents")) {
190 options.no_dependents = 1;
191 return 0;
acb0c572 192 } else if (!strcmp(name, "filter")) {
c3b9bc94 193 options.filter = xstrdup(value);
acb0c572 194 return 0;
7f605017 195 } else if (!strcmp(name, "object-format")) {
196 int algo;
197 options.object_format = 1;
198 if (strcmp(value, "true")) {
199 algo = hash_algo_by_name(value);
200 if (algo == GIT_HASH_UNKNOWN)
201 die("unknown object format '%s'", value);
202 options.hash_algo = &hash_algos[algo];
203 }
204 return 0;
16094885 205 } else {
ef08ef9e
SP
206 return 1 /* unsupported */;
207 }
208}
209
97cc7bc4 210struct discovery {
f08a5d42 211 char *service;
97cc7bc4
SP
212 char *buf_alloc;
213 char *buf;
214 size_t len;
2a455202 215 struct ref *refs;
910650d2 216 struct oid_array shallow;
49e85e95 217 enum protocol_version version;
97cc7bc4
SP
218 unsigned proto_git : 1;
219};
220static struct discovery *last_discovery;
221
b8054bbe
JK
222static struct ref *parse_git_refs(struct discovery *heads, int for_push)
223{
224 struct ref *list = NULL;
ad6ac124
BW
225 struct packet_reader reader;
226
227 packet_reader_init(&reader, -1, heads->buf, heads->len,
228 PACKET_READ_CHOMP_NEWLINE |
2d103c31
MS
229 PACKET_READ_GENTLE_ON_EOF |
230 PACKET_READ_DIE_ON_ERR_PACKET);
ad6ac124 231
49e85e95
BW
232 heads->version = discover_version(&reader);
233 switch (heads->version) {
8f6982b4 234 case protocol_v2:
0f1dc53f
BW
235 /*
236 * Do nothing. This isn't a list of refs but rather a
237 * capability advertisement. Client would have run
238 * 'stateless-connect' so we'll dump this capability listing
239 * and let them request the refs themselves.
240 */
8f6982b4 241 break;
ad6ac124
BW
242 case protocol_v1:
243 case protocol_v0:
244 get_remote_heads(&reader, &list, for_push ? REF_NORMAL : 0,
245 NULL, &heads->shallow);
7f605017 246 options.hash_algo = reader.hash_algo;
ad6ac124
BW
247 break;
248 case protocol_unknown_version:
249 BUG("unknown protocol version");
250 }
251
b8054bbe
JK
252 return list;
253}
254
ac093d07 255static const struct git_hash_algo *detect_hash_algo(struct discovery *heads)
256{
257 const char *p = memchr(heads->buf, '\t', heads->len);
258 int algo;
259 if (!p)
260 return the_hash_algo;
261
262 algo = hash_algo_by_length((p - heads->buf) / 2);
263 if (algo == GIT_HASH_UNKNOWN)
264 return NULL;
265 return &hash_algos[algo];
266}
267
b8054bbe
JK
268static struct ref *parse_info_refs(struct discovery *heads)
269{
270 char *data, *start, *mid;
271 char *ref_name;
272 int i = 0;
273
274 struct ref *refs = NULL;
275 struct ref *ref = NULL;
276 struct ref *last_ref = NULL;
277
ac093d07 278 options.hash_algo = detect_hash_algo(heads);
279 if (!options.hash_algo)
280 die("%sinfo/refs not valid: could not determine hash algorithm; "
281 "is this a git repository?",
282 transport_anonymize_url(url.buf));
283
b8054bbe
JK
284 data = heads->buf;
285 start = NULL;
286 mid = data;
287 while (i < heads->len) {
288 if (!start) {
289 start = &data[i];
290 }
291 if (data[i] == '\t')
292 mid = &data[i];
293 if (data[i] == '\n') {
ac093d07 294 if (mid - start != options.hash_algo->hexsz)
ed8b4132 295 die(_("%sinfo/refs not valid: is this a git repository?"),
c1284b21 296 transport_anonymize_url(url.buf));
b8054bbe
JK
297 data[i] = 0;
298 ref_name = mid + 1;
6f687c21 299 ref = alloc_ref(ref_name);
ac093d07 300 get_oid_hex_algop(start, &ref->old_oid, options.hash_algo);
b8054bbe
JK
301 if (!refs)
302 refs = ref;
303 if (last_ref)
304 last_ref->next = ref;
305 last_ref = ref;
306 start = NULL;
307 }
308 i++;
309 }
310
311 ref = alloc_ref("HEAD");
b227bbc4 312 if (!http_fetch_ref(url.buf, ref) &&
b8054bbe
JK
313 !resolve_remote_symref(ref, refs)) {
314 ref->next = refs;
315 refs = ref;
316 } else {
317 free(ref);
318 }
319
320 return refs;
321}
322
97cc7bc4
SP
323static void free_discovery(struct discovery *d)
324{
325 if (d) {
326 if (d == last_discovery)
327 last_discovery = NULL;
ee3051bd 328 free(d->shallow.oid);
97cc7bc4 329 free(d->buf_alloc);
2a455202 330 free_refs(d->refs);
f08a5d42 331 free(d->service);
97cc7bc4
SP
332 free(d);
333 }
334}
335
fc1b774c
JK
336static int show_http_message(struct strbuf *type, struct strbuf *charset,
337 struct strbuf *msg)
426e70d4
JK
338{
339 const char *p, *eol;
340
341 /*
342 * We only show text/plain parts, as other types are likely
343 * to be ugly to look at on the user's terminal.
426e70d4 344 */
bf197fd7 345 if (strcmp(type->buf, "text/plain"))
426e70d4 346 return -1;
fc1b774c
JK
347 if (charset->len)
348 strbuf_reencode(msg, charset->buf, get_log_output_encoding());
426e70d4
JK
349
350 strbuf_trim(msg);
351 if (!msg->len)
352 return -1;
353
354 p = msg->buf;
355 do {
356 eol = strchrnul(p, '\n');
357 fprintf(stderr, "remote: %.*s\n", (int)(eol - p), p);
358 p = eol + 1;
359 } while(*eol);
360 return 0;
361}
362
884e586f
BW
363static int get_protocol_http_header(enum protocol_version version,
364 struct strbuf *header)
365{
366 if (version > 0) {
367 strbuf_addf(header, GIT_PROTOCOL_HEADER ": version=%d",
368 version);
369
370 return 1;
371 }
372
373 return 0;
374}
375
8ee3e120
JK
376static void check_smart_http(struct discovery *d, const char *service,
377 struct strbuf *type)
378{
379 const char *p;
380 struct packet_reader reader;
381
382 /*
383 * If we don't see x-$service-advertisement, then it's not smart-http.
384 * But once we do, we commit to it and assume any other protocol
385 * violations are hard errors.
386 */
387 if (!skip_prefix(type->buf, "application/x-", &p) ||
388 !skip_prefix(p, service, &p) ||
389 strcmp(p, "-advertisement"))
390 return;
391
392 packet_reader_init(&reader, -1, d->buf, d->len,
393 PACKET_READ_CHOMP_NEWLINE |
394 PACKET_READ_DIE_ON_ERR_PACKET);
395 if (packet_reader_read(&reader) != PACKET_READ_NORMAL)
aa1edf14 396 die(_("invalid server response; expected service, got flush packet"));
8ee3e120
JK
397
398 if (skip_prefix(reader.line, "# service=", &p) && !strcmp(p, service)) {
399 /*
400 * The header can include additional metadata lines, up
401 * until a packet flush marker. Ignore these now, but
402 * in the future we might start to scan them.
403 */
404 for (;;) {
405 packet_reader_read(&reader);
406 if (reader.pktlen <= 0) {
407 break;
408 }
409 }
410
411 /*
412 * v0 smart http; callers expect us to soak up the
413 * service and header packets
414 */
415 d->buf = reader.src_buffer;
416 d->len = reader.src_len;
417 d->proto_git = 1;
418
cbdb8d14 419 } else if (!strcmp(reader.line, "version 2")) {
8ee3e120
JK
420 /*
421 * v2 smart http; do not consume version packet, which will
422 * be handled elsewhere.
423 */
424 d->proto_git = 1;
425
426 } else {
aa1edf14 427 die(_("invalid server response; got '%s'"), reader.line);
8ee3e120
JK
428 }
429}
430
24d36f14 431static struct discovery *discover_refs(const char *service, int for_push)
a2d725b7 432{
4656bf47 433 struct strbuf type = STRBUF_INIT;
fc1b774c 434 struct strbuf charset = STRBUF_INIT;
a2d725b7 435 struct strbuf buffer = STRBUF_INIT;
c65d5692 436 struct strbuf refs_url = STRBUF_INIT;
050ef365 437 struct strbuf effective_url = STRBUF_INIT;
884e586f
BW
438 struct strbuf protocol_header = STRBUF_INIT;
439 struct string_list extra_headers = STRING_LIST_INIT_DUP;
97cc7bc4 440 struct discovery *last = last_discovery;
243c329c 441 int http_ret, maybe_smart = 0;
fcaa6e64 442 struct http_get_options http_options;
a4d78ce2 443 enum protocol_version version = get_protocol_version_config();
a2d725b7 444
97cc7bc4
SP
445 if (last && !strcmp(service, last->service))
446 return last;
447 free_discovery(last);
a2d725b7 448
b227bbc4 449 strbuf_addf(&refs_url, "%sinfo/refs", url.buf);
59556548 450 if ((starts_with(url.buf, "http://") || starts_with(url.buf, "https://")) &&
02572c2e 451 git_env_bool("GIT_SMART_HTTP", 1)) {
243c329c 452 maybe_smart = 1;
b227bbc4 453 if (!strchr(url.buf, '?'))
c65d5692 454 strbuf_addch(&refs_url, '?');
97cc7bc4 455 else
c65d5692
JK
456 strbuf_addch(&refs_url, '&');
457 strbuf_addf(&refs_url, "service=%s", service);
97cc7bc4 458 }
a2d725b7 459
a4d78ce2
BW
460 /*
461 * NEEDSWORK: If we are trying to use protocol v2 and we are planning
462 * to perform a push, then fallback to v0 since the client doesn't know
463 * how to push yet using v2.
464 */
465 if (version == protocol_v2 && !strcmp("git-receive-pack", service))
466 version = protocol_v0;
467
884e586f 468 /* Add the extra Git-Protocol header */
a4d78ce2 469 if (get_protocol_http_header(version, &protocol_header))
884e586f
BW
470 string_list_append(&extra_headers, protocol_header.buf);
471
fcaa6e64
JK
472 memset(&http_options, 0, sizeof(http_options));
473 http_options.content_type = &type;
474 http_options.charset = &charset;
475 http_options.effective_url = &effective_url;
476 http_options.base_url = &url;
884e586f 477 http_options.extra_headers = &extra_headers;
50d34137 478 http_options.initial_request = 1;
fcaa6e64 479 http_options.no_cache = 1;
1bbcc224 480
fcaa6e64 481 http_ret = http_get_strbuf(refs_url.buf, &buffer, &http_options);
a2d725b7
DB
482 switch (http_ret) {
483 case HTTP_OK:
484 break;
485 case HTTP_MISSING_TARGET:
fc1b774c 486 show_http_message(&type, &charset, &buffer);
ed8b4132 487 die(_("repository '%s' not found"),
c1284b21 488 transport_anonymize_url(url.buf));
42653c09 489 case HTTP_NOAUTH:
fc1b774c 490 show_http_message(&type, &charset, &buffer);
ed8b4132 491 die(_("Authentication failed for '%s'"),
c1284b21 492 transport_anonymize_url(url.buf));
a2d725b7 493 default:
fc1b774c 494 show_http_message(&type, &charset, &buffer);
ed8b4132 495 die(_("unable to access '%s': %s"),
c1284b21 496 transport_anonymize_url(url.buf), curl_errorstr);
a2d725b7
DB
497 }
498
c1284b21
JS
499 if (options.verbosity && !starts_with(refs_url.buf, url.buf)) {
500 char *u = transport_anonymize_url(url.buf);
501 warning(_("redirecting to %s"), u);
502 free(u);
503 }
50d34137 504
97cc7bc4 505 last= xcalloc(1, sizeof(*last_discovery));
f08a5d42 506 last->service = xstrdup(service);
97cc7bc4
SP
507 last->buf_alloc = strbuf_detach(&buffer, &last->len);
508 last->buf = last->buf_alloc;
509
8ee3e120
JK
510 if (maybe_smart)
511 check_smart_http(last, service, &type);
97cc7bc4 512
2a455202
JK
513 if (last->proto_git)
514 last->refs = parse_git_refs(last, for_push);
515 else
516 last->refs = parse_info_refs(last);
517
c65d5692 518 strbuf_release(&refs_url);
4656bf47 519 strbuf_release(&type);
fc1b774c 520 strbuf_release(&charset);
050ef365 521 strbuf_release(&effective_url);
97cc7bc4 522 strbuf_release(&buffer);
884e586f
BW
523 strbuf_release(&protocol_header);
524 string_list_clear(&extra_headers, 0);
97cc7bc4
SP
525 last_discovery = last;
526 return last;
527}
528
97cc7bc4
SP
529static struct ref *get_refs(int for_push)
530{
531 struct discovery *heads;
532
533 if (for_push)
2a455202 534 heads = discover_refs("git-receive-pack", for_push);
97cc7bc4 535 else
2a455202 536 heads = discover_refs("git-upload-pack", for_push);
97cc7bc4 537
2a455202 538 return heads->refs;
97cc7bc4
SP
539}
540
ae4efe19
SP
541static void output_refs(struct ref *refs)
542{
543 struct ref *posn;
7f605017 544 if (options.object_format && options.hash_algo) {
545 printf(":object-format %s\n", options.hash_algo->name);
546 }
ae4efe19
SP
547 for (posn = refs; posn; posn = posn->next) {
548 if (posn->symref)
549 printf("@%s %s\n", posn->symref, posn->name);
550 else
97997e6a 551 printf("%s %s\n", hash_to_hex_algop(posn->old_oid.hash,
552 options.hash_algo),
553 posn->name);
ae4efe19
SP
554 }
555 printf("\n");
556 fflush(stdout);
ae4efe19
SP
557}
558
de1a2fdd
SP
559struct rpc_state {
560 const char *service_name;
de1a2fdd
SP
561 char *service_url;
562 char *hdr_content_type;
563 char *hdr_accept;
884e586f 564 char *protocol_header;
de1a2fdd
SP
565 char *buf;
566 size_t alloc;
567 size_t len;
568 size_t pos;
569 int in;
570 int out;
296b847c 571 int any_written;
b8538603 572 unsigned gzip_request : 1;
6c81a990 573 unsigned initial_buffer : 1;
a97d0079
JT
574
575 /*
576 * Whenever a pkt-line is read into buf, append the 4 characters
577 * denoting its length before appending the payload.
578 */
579 unsigned write_line_lengths : 1;
580
581 /*
582 * Used by rpc_out; initialize to 0. This is true if a flush has been
583 * read, but the corresponding line length (if write_line_lengths is
584 * true) and EOF have not been sent to libcurl. Since each flush marks
585 * the end of a request, each flush must be completely sent before any
586 * further reading occurs.
587 */
588 unsigned flush_read_but_not_sent : 1;
de1a2fdd
SP
589};
590
78ad9172
JT
591/*
592 * Appends the result of reading from rpc->out to the string represented by
593 * rpc->buf and rpc->len if there is enough space. Returns 1 if there was
594 * enough space, 0 otherwise.
595 *
a97d0079
JT
596 * If rpc->write_line_lengths is true, appends the line length as a 4-byte
597 * hexadecimal string before appending the result described above.
598 *
599 * Writes the total number of bytes appended into appended.
78ad9172 600 */
a97d0079
JT
601static int rpc_read_from_out(struct rpc_state *rpc, int options,
602 size_t *appended,
603 enum packet_read_status *status) {
604 size_t left;
605 char *buf;
606 int pktlen_raw;
607
608 if (rpc->write_line_lengths) {
609 left = rpc->alloc - rpc->len - 4;
610 buf = rpc->buf + rpc->len + 4;
611 } else {
612 left = rpc->alloc - rpc->len;
613 buf = rpc->buf + rpc->len;
614 }
78ad9172
JT
615
616 if (left < LARGE_PACKET_MAX)
617 return 0;
618
a97d0079
JT
619 *status = packet_read_with_status(rpc->out, NULL, NULL, buf,
620 left, &pktlen_raw, options);
621 if (*status != PACKET_READ_EOF) {
622 *appended = pktlen_raw + (rpc->write_line_lengths ? 4 : 0);
623 rpc->len += *appended;
624 }
625
626 if (rpc->write_line_lengths) {
627 switch (*status) {
628 case PACKET_READ_EOF:
629 if (!(options & PACKET_READ_GENTLE_ON_EOF))
aa1edf14 630 die(_("shouldn't have EOF when not gentle on EOF"));
a97d0079
JT
631 break;
632 case PACKET_READ_NORMAL:
633 set_packet_header(buf - 4, *appended);
634 break;
635 case PACKET_READ_DELIM:
636 memcpy(buf - 4, "0001", 4);
637 break;
638 case PACKET_READ_FLUSH:
639 memcpy(buf - 4, "0000", 4);
640 break;
641 }
642 }
643
78ad9172
JT
644 return 1;
645}
646
de1a2fdd
SP
647static size_t rpc_out(void *ptr, size_t eltsize,
648 size_t nmemb, void *buffer_)
649{
650 size_t max = eltsize * nmemb;
651 struct rpc_state *rpc = buffer_;
652 size_t avail = rpc->len - rpc->pos;
a97d0079 653 enum packet_read_status status;
de1a2fdd
SP
654
655 if (!avail) {
6c81a990 656 rpc->initial_buffer = 0;
78ad9172 657 rpc->len = 0;
de1a2fdd 658 rpc->pos = 0;
a97d0079
JT
659 if (!rpc->flush_read_but_not_sent) {
660 if (!rpc_read_from_out(rpc, 0, &avail, &status))
661 BUG("The entire rpc->buf should be larger than LARGE_PACKET_MAX");
662 if (status == PACKET_READ_FLUSH)
663 rpc->flush_read_but_not_sent = 1;
664 }
665 /*
666 * If flush_read_but_not_sent is true, we have already read one
667 * full request but have not fully sent it + EOF, which is why
668 * we need to refrain from reading.
669 */
670 }
671 if (rpc->flush_read_but_not_sent) {
672 if (!avail) {
673 /*
674 * The line length either does not need to be sent at
675 * all or has already been completely sent. Now we can
676 * return 0, indicating EOF, meaning that the flush has
677 * been fully sent.
678 */
679 rpc->flush_read_but_not_sent = 0;
680 return 0;
681 }
682 /*
683 * If avail is non-zerp, the line length for the flush still
684 * hasn't been fully sent. Proceed with sending the line
685 * length.
686 */
de1a2fdd
SP
687 }
688
48310608 689 if (max < avail)
de1a2fdd
SP
690 avail = max;
691 memcpy(ptr, rpc->buf + rpc->pos, avail);
692 rpc->pos += avail;
693 return avail;
694}
695
6c81a990 696#ifndef NO_CURL_IOCTL
5092d3ec 697static curlioerr rpc_ioctl(CURL *handle, int cmd, void *clientp)
6c81a990
MS
698{
699 struct rpc_state *rpc = clientp;
700
701 switch (cmd) {
702 case CURLIOCMD_NOP:
703 return CURLIOE_OK;
704
705 case CURLIOCMD_RESTARTREAD:
706 if (rpc->initial_buffer) {
707 rpc->pos = 0;
708 return CURLIOE_OK;
709 }
ed8b4132 710 error(_("unable to rewind rpc post data - try increasing http.postBuffer"));
6c81a990
MS
711 return CURLIOE_FAILRESTART;
712
713 default:
714 return CURLIOE_UNKNOWNCMD;
715 }
716}
717#endif
718
cf2fb92b
MS
719struct rpc_in_data {
720 struct rpc_state *rpc;
b79bdd8c 721 struct active_request_slot *slot;
cf2fb92b
MS
722};
723
724/*
725 * A callback for CURLOPT_WRITEFUNCTION. The return value is the bytes consumed
726 * from ptr.
727 */
a04ff3ec 728static size_t rpc_in(char *ptr, size_t eltsize,
de1a2fdd
SP
729 size_t nmemb, void *buffer_)
730{
731 size_t size = eltsize * nmemb;
cf2fb92b 732 struct rpc_in_data *data = buffer_;
b79bdd8c
MS
733 long response_code;
734
735 if (curl_easy_getinfo(data->slot->curl, CURLINFO_RESPONSE_CODE,
736 &response_code) != CURLE_OK)
737 return size;
738 if (response_code >= 300)
739 return size;
296b847c 740 if (size)
cf2fb92b
MS
741 data->rpc->any_written = 1;
742 write_or_die(data->rpc->in, ptr, size);
de1a2fdd
SP
743 return size;
744}
745
3a347ed7
JK
746static int run_slot(struct active_request_slot *slot,
747 struct slot_results *results)
206b099d 748{
b81401c1 749 int err;
3a347ed7 750 struct slot_results results_buf;
206b099d 751
3a347ed7
JK
752 if (!results)
753 results = &results_buf;
754
beed336c 755 err = run_one_slot(slot, results);
206b099d 756
b81401c1 757 if (err != HTTP_OK && err != HTTP_REAUTH) {
00540458
SP
758 struct strbuf msg = STRBUF_INIT;
759 if (results->http_code && results->http_code != 200)
760 strbuf_addf(&msg, "HTTP %ld", results->http_code);
761 if (results->curl_result != CURLE_OK) {
762 if (msg.len)
763 strbuf_addch(&msg, ' ');
764 strbuf_addf(&msg, "curl %d", results->curl_result);
765 if (curl_errorstr[0]) {
766 strbuf_addch(&msg, ' ');
767 strbuf_addstr(&msg, curl_errorstr);
768 }
769 }
ed8b4132 770 error(_("RPC failed; %s"), msg.buf);
00540458 771 strbuf_release(&msg);
206b099d
SP
772 }
773
774 return err;
775}
776
3a347ed7 777static int probe_rpc(struct rpc_state *rpc, struct slot_results *results)
206b099d
SP
778{
779 struct active_request_slot *slot;
8cb01e2f 780 struct curl_slist *headers = http_copy_default_headers();
206b099d
SP
781 struct strbuf buf = STRBUF_INIT;
782 int err;
783
784 slot = get_active_slot();
785
786 headers = curl_slist_append(headers, rpc->hdr_content_type);
787 headers = curl_slist_append(headers, rpc->hdr_accept);
788
789 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
790 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
791 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
aa90b969 792 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, NULL);
206b099d
SP
793 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, "0000");
794 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, 4);
795 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
796 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
797 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buf);
798
3a347ed7 799 err = run_slot(slot, results);
206b099d
SP
800
801 curl_slist_free_all(headers);
802 strbuf_release(&buf);
803 return err;
804}
805
3b335762
NTND
806static curl_off_t xcurl_off_t(size_t len)
807{
cb8010bb
TB
808 uintmax_t size = len;
809 if (size > maximum_signed_value_of_type(curl_off_t))
ed8b4132 810 die(_("cannot handle pushes this big"));
cb8010bb 811 return (curl_off_t)size;
37ee680d
DT
812}
813
a97d0079
JT
814/*
815 * If flush_received is true, do not attempt to read any more; just use what's
816 * in rpc->buf.
817 */
818static int post_rpc(struct rpc_state *rpc, int flush_received)
de1a2fdd
SP
819{
820 struct active_request_slot *slot;
8cb01e2f 821 struct curl_slist *headers = http_copy_default_headers();
b8538603
SP
822 int use_gzip = rpc->gzip_request;
823 char *gzip_body = NULL;
37711549 824 size_t gzip_size = 0;
206b099d 825 int err, large_request = 0;
c80d96ca 826 int needs_100_continue = 0;
cf2fb92b 827 struct rpc_in_data rpc_in_data;
de1a2fdd
SP
828
829 /* Try to load the entire request, if we can fit it into the
830 * allocated buffer space we can use HTTP/1.0 and avoid the
831 * chunked encoding mess.
832 */
a97d0079
JT
833 if (!flush_received) {
834 while (1) {
835 size_t n;
836 enum packet_read_status status;
837
838 if (!rpc_read_from_out(rpc, 0, &n, &status)) {
839 large_request = 1;
840 use_gzip = 0;
841 break;
842 }
843 if (status == PACKET_READ_FLUSH)
844 break;
de1a2fdd 845 }
de1a2fdd
SP
846 }
847
206b099d 848 if (large_request) {
c80d96ca 849 struct slot_results results;
850
b81401c1 851 do {
c80d96ca 852 err = probe_rpc(rpc, &results);
2501aff8
JK
853 if (err == HTTP_REAUTH)
854 credential_fill(&http_auth);
b81401c1
JK
855 } while (err == HTTP_REAUTH);
856 if (err != HTTP_OK)
857 return -1;
c80d96ca 858
859 if (results.auth_avail & CURLAUTH_GSSNEGOTIATE)
860 needs_100_continue = 1;
206b099d
SP
861 }
862
abf8df86
JK
863 headers = curl_slist_append(headers, rpc->hdr_content_type);
864 headers = curl_slist_append(headers, rpc->hdr_accept);
c80d96ca 865 headers = curl_slist_append(headers, needs_100_continue ?
866 "Expect: 100-continue" : "Expect:");
abf8df86 867
884e586f
BW
868 /* Add the extra Git-Protocol header */
869 if (rpc->protocol_header)
870 headers = curl_slist_append(headers, rpc->protocol_header);
871
abf8df86 872retry:
de1a2fdd 873 slot = get_active_slot();
de1a2fdd 874
de1a2fdd 875 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
d21f9794 876 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
de1a2fdd 877 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
1a53e692 878 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "");
de1a2fdd 879
de1a2fdd
SP
880 if (large_request) {
881 /* The request body is large and the size cannot be predicted.
882 * We must use chunked encoding to send it.
883 */
de1a2fdd 884 headers = curl_slist_append(headers, "Transfer-Encoding: chunked");
6c81a990 885 rpc->initial_buffer = 1;
de1a2fdd
SP
886 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, rpc_out);
887 curl_easy_setopt(slot->curl, CURLOPT_INFILE, rpc);
6c81a990
MS
888#ifndef NO_CURL_IOCTL
889 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, rpc_ioctl);
890 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, rpc);
891#endif
de1a2fdd
SP
892 if (options.verbosity > 1) {
893 fprintf(stderr, "POST %s (chunked)\n", rpc->service_name);
894 fflush(stderr);
895 }
896
2e736fd5
JK
897 } else if (gzip_body) {
898 /*
899 * If we are looping to retry authentication, then the previous
900 * run will have set up the headers and gzip buffer already,
901 * and we just need to send it.
902 */
903 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
37ee680d 904 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(gzip_size));
2e736fd5 905
b8538603
SP
906 } else if (use_gzip && 1024 < rpc->len) {
907 /* The client backend isn't giving us compressed data so
a8132410 908 * we can try to deflate it ourselves, this may save on
b8538603
SP
909 * the transfer time.
910 */
ef49a7a0 911 git_zstream stream;
b8538603
SP
912 int ret;
913
55bb5c91 914 git_deflate_init_gzip(&stream, Z_BEST_COMPRESSION);
df126e10
JK
915 gzip_size = git_deflate_bound(&stream, rpc->len);
916 gzip_body = xmalloc(gzip_size);
b8538603
SP
917
918 stream.next_in = (unsigned char *)rpc->buf;
919 stream.avail_in = rpc->len;
920 stream.next_out = (unsigned char *)gzip_body;
df126e10 921 stream.avail_out = gzip_size;
b8538603 922
55bb5c91 923 ret = git_deflate(&stream, Z_FINISH);
b8538603 924 if (ret != Z_STREAM_END)
ed8b4132 925 die(_("cannot deflate request; zlib deflate error %d"), ret);
b8538603 926
55bb5c91 927 ret = git_deflate_end_gently(&stream);
b8538603 928 if (ret != Z_OK)
ed8b4132 929 die(_("cannot deflate request; zlib end error %d"), ret);
b8538603 930
df126e10 931 gzip_size = stream.total_out;
b8538603
SP
932
933 headers = curl_slist_append(headers, "Content-Encoding: gzip");
934 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
37ee680d 935 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(gzip_size));
b8538603
SP
936
937 if (options.verbosity > 1) {
938 fprintf(stderr, "POST %s (gzip %lu to %lu bytes)\n",
939 rpc->service_name,
df126e10 940 (unsigned long)rpc->len, (unsigned long)gzip_size);
b8538603
SP
941 fflush(stderr);
942 }
de1a2fdd
SP
943 } else {
944 /* We know the complete request size in advance, use the
945 * more normal Content-Length approach.
946 */
947 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, rpc->buf);
37ee680d 948 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(rpc->len));
de1a2fdd
SP
949 if (options.verbosity > 1) {
950 fprintf(stderr, "POST %s (%lu bytes)\n",
951 rpc->service_name, (unsigned long)rpc->len);
952 fflush(stderr);
953 }
954 }
955
956 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
957 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, rpc_in);
cf2fb92b 958 rpc_in_data.rpc = rpc;
b79bdd8c 959 rpc_in_data.slot = slot;
cf2fb92b 960 curl_easy_setopt(slot->curl, CURLOPT_FILE, &rpc_in_data);
b79bdd8c 961 curl_easy_setopt(slot->curl, CURLOPT_FAILONERROR, 0);
de1a2fdd 962
296b847c
DT
963
964 rpc->any_written = 0;
3a347ed7 965 err = run_slot(slot, NULL);
2501aff8
JK
966 if (err == HTTP_REAUTH && !large_request) {
967 credential_fill(&http_auth);
abf8df86 968 goto retry;
2501aff8 969 }
b81401c1
JK
970 if (err != HTTP_OK)
971 err = -1;
de1a2fdd 972
296b847c
DT
973 if (!rpc->any_written)
974 err = -1;
975
de1a2fdd 976 curl_slist_free_all(headers);
b8538603 977 free(gzip_body);
de1a2fdd
SP
978 return err;
979}
980
7d50d34f 981static int rpc_service(struct rpc_state *rpc, struct discovery *heads,
b3590309
JT
982 const char **client_argv, const struct strbuf *preamble,
983 struct strbuf *rpc_result)
de1a2fdd
SP
984{
985 const char *svc = rpc->service_name;
986 struct strbuf buf = STRBUF_INIT;
d3180279 987 struct child_process client = CHILD_PROCESS_INIT;
de1a2fdd
SP
988 int err = 0;
989
de1a2fdd
SP
990 client.in = -1;
991 client.out = -1;
992 client.git_cmd = 1;
7d50d34f 993 client.argv = client_argv;
de1a2fdd
SP
994 if (start_command(&client))
995 exit(1);
5d916693 996 write_or_die(client.in, preamble->buf, preamble->len);
de1a2fdd
SP
997 if (heads)
998 write_or_die(client.in, heads->buf, heads->len);
999
1000 rpc->alloc = http_post_buffer;
1001 rpc->buf = xmalloc(rpc->alloc);
1002 rpc->in = client.in;
1003 rpc->out = client.out;
de1a2fdd 1004
b227bbc4 1005 strbuf_addf(&buf, "%s%s", url.buf, svc);
de1a2fdd
SP
1006 rpc->service_url = strbuf_detach(&buf, NULL);
1007
1008 strbuf_addf(&buf, "Content-Type: application/x-%s-request", svc);
1009 rpc->hdr_content_type = strbuf_detach(&buf, NULL);
1010
8efa5f62 1011 strbuf_addf(&buf, "Accept: application/x-%s-result", svc);
de1a2fdd
SP
1012 rpc->hdr_accept = strbuf_detach(&buf, NULL);
1013
884e586f
BW
1014 if (get_protocol_http_header(heads->version, &buf))
1015 rpc->protocol_header = strbuf_detach(&buf, NULL);
1016 else
1017 rpc->protocol_header = NULL;
1018
de1a2fdd 1019 while (!err) {
4981fe75 1020 int n = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
de1a2fdd
SP
1021 if (!n)
1022 break;
1023 rpc->pos = 0;
1024 rpc->len = n;
a97d0079 1025 err |= post_rpc(rpc, 0);
de1a2fdd 1026 }
de1a2fdd
SP
1027
1028 close(client.in);
de1a2fdd 1029 client.in = -1;
6cdf0223 1030 if (!err) {
b3590309 1031 strbuf_read(rpc_result, client.out, 0);
6cdf0223
SP
1032 } else {
1033 char buf[4096];
1034 for (;;)
1035 if (xread(client.out, buf, sizeof(buf)) <= 0)
1036 break;
1037 }
b4ee10f6
SP
1038
1039 close(client.out);
de1a2fdd
SP
1040 client.out = -1;
1041
1042 err |= finish_command(&client);
1043 free(rpc->service_url);
1044 free(rpc->hdr_content_type);
1045 free(rpc->hdr_accept);
884e586f 1046 free(rpc->protocol_header);
de1a2fdd
SP
1047 free(rpc->buf);
1048 strbuf_release(&buf);
1049 return err;
1050}
1051
292ce46b
SP
1052static int fetch_dumb(int nr_heads, struct ref **to_fetch)
1053{
26e1e0b2 1054 struct walker *walker;
b32fa95f 1055 char **targets;
292ce46b
SP
1056 int ret, i;
1057
b32fa95f 1058 ALLOC_ARRAY(targets, nr_heads);
508ea882 1059 if (options.depth || options.deepen_since)
ed8b4132 1060 die(_("dumb http transport does not support shallow capabilities"));
292ce46b 1061 for (i = 0; i < nr_heads; i++)
f4e54d02 1062 targets[i] = xstrdup(oid_to_hex(&to_fetch[i]->old_oid));
292ce46b 1063
b227bbc4 1064 walker = get_http_walker(url.buf);
ef08ef9e 1065 walker->get_verbosely = options.verbosity >= 3;
7655b411 1066 walker->get_progress = options.progress;
292ce46b
SP
1067 walker->get_recover = 0;
1068 ret = walker_fetch(walker, nr_heads, targets, NULL, NULL);
26e1e0b2 1069 walker_free(walker);
292ce46b
SP
1070
1071 for (i = 0; i < nr_heads; i++)
1072 free(targets[i]);
1073 free(targets);
1074
ed8b4132 1075 return ret ? error(_("fetch failed.")) : 0;
292ce46b
SP
1076}
1077
249b2004
SP
1078static int fetch_git(struct discovery *heads,
1079 int nr_heads, struct ref **to_fetch)
1080{
1081 struct rpc_state rpc;
8150749d 1082 struct strbuf preamble = STRBUF_INIT;
b5f62ebe
NTND
1083 int i, err;
1084 struct argv_array args = ARGV_ARRAY_INIT;
b3590309 1085 struct strbuf rpc_result = STRBUF_INIT;
b5f62ebe
NTND
1086
1087 argv_array_pushl(&args, "fetch-pack", "--stateless-rpc",
1088 "--stdin", "--lock-pack", NULL);
249b2004 1089 if (options.followtags)
b5f62ebe 1090 argv_array_push(&args, "--include-tag");
249b2004 1091 if (options.thin)
b5f62ebe
NTND
1092 argv_array_push(&args, "--thin");
1093 if (options.verbosity >= 3)
1094 argv_array_pushl(&args, "-v", "-v", NULL);
9ba38048 1095 if (options.check_self_contained_and_connected)
b5f62ebe 1096 argv_array_push(&args, "--check-self-contained-and-connected");
16094885 1097 if (options.cloning)
b5f62ebe 1098 argv_array_push(&args, "--cloning");
16094885 1099 if (options.update_shallow)
b5f62ebe 1100 argv_array_push(&args, "--update-shallow");
249b2004 1101 if (!options.progress)
b5f62ebe
NTND
1102 argv_array_push(&args, "--no-progress");
1103 if (options.depth)
1104 argv_array_pushf(&args, "--depth=%lu", options.depth);
508ea882
NTND
1105 if (options.deepen_since)
1106 argv_array_pushf(&args, "--shallow-since=%s", options.deepen_since);
a45a2600
NTND
1107 for (i = 0; i < options.deepen_not.nr; i++)
1108 argv_array_pushf(&args, "--shallow-exclude=%s",
1109 options.deepen_not.items[i].string);
cccf74e2
NTND
1110 if (options.deepen_relative && options.depth)
1111 argv_array_push(&args, "--deepen-relative");
88e2f9ed
JT
1112 if (options.from_promisor)
1113 argv_array_push(&args, "--from-promisor");
1114 if (options.no_dependents)
1115 argv_array_push(&args, "--no-dependents");
acb0c572
JH
1116 if (options.filter)
1117 argv_array_pushf(&args, "--filter=%s", options.filter);
b5f62ebe 1118 argv_array_push(&args, url.buf);
8150749d 1119
249b2004
SP
1120 for (i = 0; i < nr_heads; i++) {
1121 struct ref *ref = to_fetch[i];
94ee8e2c 1122 if (!*ref->name)
ed8b4132 1123 die(_("cannot fetch by sha1 over smart http"));
58f2ed05 1124 packet_buf_write(&preamble, "%s %s\n",
f4e54d02 1125 oid_to_hex(&ref->old_oid), ref->name);
249b2004 1126 }
8150749d 1127 packet_buf_flush(&preamble);
249b2004
SP
1128
1129 memset(&rpc, 0, sizeof(rpc));
1130 rpc.service_name = "git-upload-pack",
b8538603 1131 rpc.gzip_request = 1;
249b2004 1132
b3590309
JT
1133 err = rpc_service(&rpc, heads, args.argv, &preamble, &rpc_result);
1134 if (rpc_result.len)
1135 write_or_die(1, rpc_result.buf, rpc_result.len);
1136 strbuf_release(&rpc_result);
8150749d 1137 strbuf_release(&preamble);
b5f62ebe 1138 argv_array_clear(&args);
249b2004
SP
1139 return err;
1140}
1141
1142static int fetch(int nr_heads, struct ref **to_fetch)
1143{
2a455202 1144 struct discovery *d = discover_refs("git-upload-pack", 0);
249b2004
SP
1145 if (d->proto_git)
1146 return fetch_git(d, nr_heads, to_fetch);
1147 else
1148 return fetch_dumb(nr_heads, to_fetch);
1149}
1150
292ce46b
SP
1151static void parse_fetch(struct strbuf *buf)
1152{
1153 struct ref **to_fetch = NULL;
1154 struct ref *list_head = NULL;
1155 struct ref **list = &list_head;
1156 int alloc_heads = 0, nr_heads = 0;
1157
1158 do {
95b567c7
JK
1159 const char *p;
1160 if (skip_prefix(buf->buf, "fetch ", &p)) {
1161 const char *name;
292ce46b 1162 struct ref *ref;
8338c911 1163 struct object_id old_oid;
9c9492e8 1164 const char *q;
292ce46b 1165
9c9492e8 1166 if (parse_oid_hex(p, &old_oid, &q))
8a1569d6 1167 die(_("protocol error: expected sha/ref, got '%s'"), p);
9c9492e8 1168 if (*q == ' ')
1169 name = q + 1;
1170 else if (!*q)
292ce46b
SP
1171 name = "";
1172 else
8a1569d6 1173 die(_("protocol error: expected sha/ref, got '%s'"), p);
292ce46b
SP
1174
1175 ref = alloc_ref(name);
8338c911 1176 oidcpy(&ref->old_oid, &old_oid);
292ce46b
SP
1177
1178 *list = ref;
1179 list = &ref->next;
1180
1181 ALLOC_GROW(to_fetch, nr_heads + 1, alloc_heads);
1182 to_fetch[nr_heads++] = ref;
1183 }
1184 else
ed8b4132 1185 die(_("http transport does not support %s"), buf->buf);
292ce46b
SP
1186
1187 strbuf_reset(buf);
8f309aeb 1188 if (strbuf_getline_lf(buf, stdin) == EOF)
292ce46b
SP
1189 return;
1190 if (!*buf->buf)
1191 break;
1192 } while (1);
1193
249b2004 1194 if (fetch(nr_heads, to_fetch))
292ce46b
SP
1195 exit(128); /* error already reported */
1196 free_refs(list_head);
1197 free(to_fetch);
1198
1199 printf("\n");
1200 fflush(stdout);
1201 strbuf_reset(buf);
1202}
1203
062a309d 1204static int push_dav(int nr_spec, const char **specs)
ae4efe19 1205{
850d2fec
JK
1206 struct child_process child = CHILD_PROCESS_INIT;
1207 size_t i;
ae4efe19 1208
850d2fec
JK
1209 child.git_cmd = 1;
1210 argv_array_push(&child.args, "http-push");
1211 argv_array_push(&child.args, "--helper-status");
ae4efe19 1212 if (options.dry_run)
850d2fec 1213 argv_array_push(&child.args, "--dry-run");
ae4efe19 1214 if (options.verbosity > 1)
850d2fec
JK
1215 argv_array_push(&child.args, "--verbose");
1216 argv_array_push(&child.args, url.buf);
ae4efe19 1217 for (i = 0; i < nr_spec; i++)
850d2fec 1218 argv_array_push(&child.args, specs[i]);
ae4efe19 1219
850d2fec 1220 if (run_command(&child))
ed8b4132 1221 die(_("git-http-push failed"));
ae4efe19
SP
1222 return 0;
1223}
1224
062a309d 1225static int push_git(struct discovery *heads, int nr_spec, const char **specs)
de1a2fdd
SP
1226{
1227 struct rpc_state rpc;
222b1212
JH
1228 int i, err;
1229 struct argv_array args;
05c1eb10 1230 struct string_list_item *cas_option;
26be19ba 1231 struct strbuf preamble = STRBUF_INIT;
b3590309 1232 struct strbuf rpc_result = STRBUF_INIT;
222b1212
JH
1233
1234 argv_array_init(&args);
1235 argv_array_pushl(&args, "send-pack", "--stateless-rpc", "--helper-status",
1236 NULL);
de1a2fdd 1237
de1a2fdd 1238 if (options.thin)
222b1212 1239 argv_array_push(&args, "--thin");
de1a2fdd 1240 if (options.dry_run)
222b1212 1241 argv_array_push(&args, "--dry-run");
30261094
DB
1242 if (options.push_cert == SEND_PACK_PUSH_CERT_ALWAYS)
1243 argv_array_push(&args, "--signed=yes");
1244 else if (options.push_cert == SEND_PACK_PUSH_CERT_IF_ASKED)
1245 argv_array_push(&args, "--signed=if-asked");
6f119424 1246 if (options.atomic)
1247 argv_array_push(&args, "--atomic");
c207e34f 1248 if (options.verbosity == 0)
222b1212 1249 argv_array_push(&args, "--quiet");
c207e34f 1250 else if (options.verbosity > 1)
222b1212 1251 argv_array_push(&args, "--verbose");
511155db
BW
1252 for (i = 0; i < options.push_options.nr; i++)
1253 argv_array_pushf(&args, "--push-option=%s",
1254 options.push_options.items[i].string);
222b1212 1255 argv_array_push(&args, options.progress ? "--progress" : "--no-progress");
05c1eb10 1256 for_each_string_list_item(cas_option, &cas_options)
2233ad45 1257 argv_array_push(&args, cas_option->string);
b227bbc4 1258 argv_array_push(&args, url.buf);
26be19ba
JK
1259
1260 argv_array_push(&args, "--stdin");
de1a2fdd 1261 for (i = 0; i < nr_spec; i++)
26be19ba
JK
1262 packet_buf_write(&preamble, "%s\n", specs[i]);
1263 packet_buf_flush(&preamble);
de1a2fdd
SP
1264
1265 memset(&rpc, 0, sizeof(rpc));
1266 rpc.service_name = "git-receive-pack",
de1a2fdd 1267
b3590309
JT
1268 err = rpc_service(&rpc, heads, args.argv, &preamble, &rpc_result);
1269 if (rpc_result.len)
1270 write_or_die(1, rpc_result.buf, rpc_result.len);
1271 strbuf_release(&rpc_result);
26be19ba 1272 strbuf_release(&preamble);
222b1212 1273 argv_array_clear(&args);
de1a2fdd
SP
1274 return err;
1275}
1276
062a309d 1277static int push(int nr_spec, const char **specs)
de1a2fdd 1278{
2a455202 1279 struct discovery *heads = discover_refs("git-receive-pack", 1);
de1a2fdd
SP
1280 int ret;
1281
1282 if (heads->proto_git)
1283 ret = push_git(heads, nr_spec, specs);
1284 else
1285 ret = push_dav(nr_spec, specs);
1286 free_discovery(heads);
1287 return ret;
1288}
1289
ae4efe19
SP
1290static void parse_push(struct strbuf *buf)
1291{
062a309d
RS
1292 struct argv_array specs = ARGV_ARRAY_INIT;
1293 int ret;
ae4efe19
SP
1294
1295 do {
145136a9
JH
1296 const char *arg;
1297 if (skip_prefix(buf->buf, "push ", &arg))
1298 argv_array_push(&specs, arg);
ae4efe19 1299 else
ed8b4132 1300 die(_("http transport does not support %s"), buf->buf);
ae4efe19
SP
1301
1302 strbuf_reset(buf);
8f309aeb 1303 if (strbuf_getline_lf(buf, stdin) == EOF)
dc4cd767 1304 goto free_specs;
ae4efe19
SP
1305 if (!*buf->buf)
1306 break;
1307 } while (1);
1308
062a309d 1309 ret = push(specs.argc, specs.argv);
ae4efe19
SP
1310 printf("\n");
1311 fflush(stdout);
dc4cd767 1312
5238cbf6
SP
1313 if (ret)
1314 exit(128); /* error already reported */
1315
dc4cd767 1316 free_specs:
062a309d 1317 argv_array_clear(&specs);
ae4efe19
SP
1318}
1319
0f1dc53f
BW
1320static int stateless_connect(const char *service_name)
1321{
1322 struct discovery *discover;
a97d0079
JT
1323 struct rpc_state rpc;
1324 struct strbuf buf = STRBUF_INIT;
0f1dc53f
BW
1325
1326 /*
1327 * Run the info/refs request and see if the server supports protocol
1328 * v2. If and only if the server supports v2 can we successfully
1329 * establish a stateless connection, otherwise we need to tell the
1330 * client to fallback to using other transport helper functions to
1331 * complete their request.
1332 */
1333 discover = discover_refs(service_name, 0);
1334 if (discover->version != protocol_v2) {
1335 printf("fallback\n");
1336 fflush(stdout);
1337 return -1;
1338 } else {
1339 /* Stateless Connection established */
1340 printf("\n");
1341 fflush(stdout);
1342 }
1343
a97d0079
JT
1344 rpc.service_name = service_name;
1345 rpc.service_url = xstrfmt("%s%s", url.buf, rpc.service_name);
1346 rpc.hdr_content_type = xstrfmt("Content-Type: application/x-%s-request", rpc.service_name);
1347 rpc.hdr_accept = xstrfmt("Accept: application/x-%s-result", rpc.service_name);
1348 if (get_protocol_http_header(discover->version, &buf)) {
1349 rpc.protocol_header = strbuf_detach(&buf, NULL);
1350 } else {
1351 rpc.protocol_header = NULL;
1352 strbuf_release(&buf);
1353 }
1354 rpc.buf = xmalloc(http_post_buffer);
1355 rpc.alloc = http_post_buffer;
1356 rpc.len = 0;
1357 rpc.pos = 0;
1358 rpc.in = 1;
1359 rpc.out = 0;
1360 rpc.any_written = 0;
1361 rpc.gzip_request = 1;
1362 rpc.initial_buffer = 0;
1363 rpc.write_line_lengths = 1;
1364 rpc.flush_read_but_not_sent = 0;
0f1dc53f
BW
1365
1366 /*
1367 * Dump the capability listing that we got from the server earlier
1368 * during the info/refs request.
1369 */
a97d0079
JT
1370 write_or_die(rpc.in, discover->buf, discover->len);
1371
1372 /* Until we see EOF keep sending POSTs */
1373 while (1) {
1374 size_t avail;
1375 enum packet_read_status status;
0f1dc53f 1376
a97d0079
JT
1377 if (!rpc_read_from_out(&rpc, PACKET_READ_GENTLE_ON_EOF, &avail,
1378 &status))
1379 BUG("The entire rpc->buf should be larger than LARGE_PACKET_MAX");
1380 if (status == PACKET_READ_EOF)
1381 break;
1382 if (post_rpc(&rpc, status == PACKET_READ_FLUSH))
0f1dc53f
BW
1383 /* We would have an err here */
1384 break;
a97d0079
JT
1385 /* Reset the buffer for next request */
1386 rpc.len = 0;
0f1dc53f
BW
1387 }
1388
a97d0079
JT
1389 free(rpc.service_url);
1390 free(rpc.hdr_content_type);
1391 free(rpc.hdr_accept);
1392 free(rpc.protocol_header);
1393 free(rpc.buf);
1394 strbuf_release(&buf);
1395
0f1dc53f
BW
1396 return 0;
1397}
1398
3f2e2297 1399int cmd_main(int argc, const char **argv)
a2d725b7 1400{
a2d725b7 1401 struct strbuf buf = STRBUF_INIT;
a45d3d7e 1402 int nongit;
a2d725b7 1403
a45d3d7e 1404 setup_git_directory_gently(&nongit);
a2d725b7 1405 if (argc < 2) {
ed8b4132 1406 error(_("remote-curl: usage: git remote-curl <remote> [<url>]"));
a2d725b7
DB
1407 return 1;
1408 }
1409
ef08ef9e
SP
1410 options.verbosity = 1;
1411 options.progress = !!isatty(2);
de1a2fdd 1412 options.thin = 1;
a45a2600 1413 string_list_init(&options.deepen_not, 1);
511155db 1414 string_list_init(&options.push_options, 1);
ef08ef9e 1415
ee4512ed
JH
1416 /*
1417 * Just report "remote-curl" here (folding all the various aliases
1418 * ("git-remote-http", "git-remote-https", and etc.) here since they
1419 * are all just copies of the same actual executable.
1420 */
1421 trace2_cmd_name("remote-curl");
1422
a2d725b7
DB
1423 remote = remote_get(argv[1]);
1424
1425 if (argc > 2) {
b227bbc4 1426 end_url_with_slash(&url, argv[2]);
a2d725b7 1427 } else {
b227bbc4 1428 end_url_with_slash(&url, remote->url[0]);
a2d725b7
DB
1429 }
1430
b227bbc4 1431 http_init(remote, url.buf, 0);
888692b7 1432
a2d725b7 1433 do {
95b567c7
JK
1434 const char *arg;
1435
8f309aeb 1436 if (strbuf_getline_lf(&buf, stdin) == EOF) {
1843f0ce 1437 if (ferror(stdin))
ed8b4132 1438 error(_("remote-curl: error reading command stream from git"));
1843f0ce
SR
1439 return 1;
1440 }
1441 if (buf.len == 0)
a2d725b7 1442 break;
59556548 1443 if (starts_with(buf.buf, "fetch ")) {
a45d3d7e 1444 if (nongit)
ed8b4132 1445 die(_("remote-curl: fetch attempted without a local repo"));
292ce46b
SP
1446 parse_fetch(&buf);
1447
59556548 1448 } else if (!strcmp(buf.buf, "list") || starts_with(buf.buf, "list ")) {
97cc7bc4
SP
1449 int for_push = !!strstr(buf.buf + 4, "for-push");
1450 output_refs(get_refs(for_push));
ae4efe19 1451
59556548 1452 } else if (starts_with(buf.buf, "push ")) {
ae4efe19
SP
1453 parse_push(&buf);
1454
95b567c7
JK
1455 } else if (skip_prefix(buf.buf, "option ", &arg)) {
1456 char *value = strchr(arg, ' ');
ef08ef9e
SP
1457 int result;
1458
1459 if (value)
1460 *value++ = '\0';
1461 else
1462 value = "true";
1463
95b567c7 1464 result = set_option(arg, value);
ef08ef9e
SP
1465 if (!result)
1466 printf("ok\n");
1467 else if (result < 0)
1468 printf("error invalid value\n");
1469 else
1470 printf("unsupported\n");
a2d725b7 1471 fflush(stdout);
ef08ef9e 1472
a2d725b7 1473 } else if (!strcmp(buf.buf, "capabilities")) {
0f1dc53f 1474 printf("stateless-connect\n");
a2d725b7 1475 printf("fetch\n");
ef08ef9e 1476 printf("option\n");
ae4efe19 1477 printf("push\n");
9ba38048 1478 printf("check-connectivity\n");
7f605017 1479 printf("object-format\n");
a2d725b7
DB
1480 printf("\n");
1481 fflush(stdout);
0f1dc53f
BW
1482 } else if (skip_prefix(buf.buf, "stateless-connect ", &arg)) {
1483 if (!stateless_connect(arg))
1484 break;
a2d725b7 1485 } else {
ed8b4132 1486 error(_("remote-curl: unknown command '%s' from git"), buf.buf);
a2d725b7
DB
1487 return 1;
1488 }
1489 strbuf_reset(&buf);
1490 } while (1);
888692b7
TRC
1491
1492 http_cleanup();
1493
a2d725b7
DB
1494 return 0;
1495}