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