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