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