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