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