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