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