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