upload-pack: add get_reachable_list()
[git/git.git] / remote-curl.c
CommitLineData
a2d725b7
DB
1#include "cache.h"
2#include "remote.h"
3#include "strbuf.h"
4#include "walker.h"
5#include "http.h"
d01a8e32 6#include "exec_cmd.h"
ae4efe19 7#include "run-command.h"
97cc7bc4 8#include "pkt-line.h"
05c1eb10 9#include "string-list.h"
de1a2fdd 10#include "sideband.h"
222b1212 11#include "argv-array.h"
2501aff8 12#include "credential.h"
16094885 13#include "sha1-array.h"
30261094 14#include "send-pack.h"
a2d725b7 15
37a8768f 16static struct remote *remote;
b227bbc4
JK
17/* always ends with a trailing slash */
18static struct strbuf url = STRBUF_INIT;
37a8768f 19
ef08ef9e
SP
20struct options {
21 int verbosity;
22 unsigned long depth;
508ea882 23 char *deepen_since;
a45a2600 24 struct string_list deepen_not;
ef08ef9e 25 unsigned progress : 1,
9ba38048 26 check_self_contained_and_connected : 1,
16094885
NTND
27 cloning : 1,
28 update_shallow : 1,
ae4efe19 29 followtags : 1,
de1a2fdd 30 dry_run : 1,
0ea47f9d 31 thin : 1,
30261094
DB
32 /* One of the SEND_PACK_PUSH_CERT_* constants. */
33 push_cert : 2;
ef08ef9e
SP
34};
35static struct options options;
05c1eb10 36static struct string_list cas_options = STRING_LIST_INIT_DUP;
ef08ef9e 37
ef08ef9e
SP
38static int set_option(const char *name, const char *value)
39{
40 if (!strcmp(name, "verbosity")) {
41 char *end;
42 int v = strtol(value, &end, 10);
43 if (value == end || *end)
44 return -1;
45 options.verbosity = v;
46 return 0;
47 }
48 else if (!strcmp(name, "progress")) {
49 if (!strcmp(value, "true"))
50 options.progress = 1;
51 else if (!strcmp(value, "false"))
52 options.progress = 0;
53 else
54 return -1;
249b2004 55 return 0;
ef08ef9e
SP
56 }
57 else if (!strcmp(name, "depth")) {
58 char *end;
59 unsigned long v = strtoul(value, &end, 10);
60 if (value == end || *end)
61 return -1;
62 options.depth = v;
249b2004 63 return 0;
ef08ef9e 64 }
508ea882
NTND
65 else if (!strcmp(name, "deepen-since")) {
66 options.deepen_since = xstrdup(value);
67 return 0;
68 }
a45a2600
NTND
69 else if (!strcmp(name, "deepen-not")) {
70 string_list_append(&options.deepen_not, value);
71 return 0;
72 }
ef08ef9e
SP
73 else if (!strcmp(name, "followtags")) {
74 if (!strcmp(value, "true"))
75 options.followtags = 1;
76 else if (!strcmp(value, "false"))
77 options.followtags = 0;
78 else
79 return -1;
249b2004 80 return 0;
ef08ef9e 81 }
ae4efe19
SP
82 else if (!strcmp(name, "dry-run")) {
83 if (!strcmp(value, "true"))
84 options.dry_run = 1;
85 else if (!strcmp(value, "false"))
86 options.dry_run = 0;
87 else
88 return -1;
89 return 0;
90 }
9ba38048
NTND
91 else if (!strcmp(name, "check-connectivity")) {
92 if (!strcmp(value, "true"))
93 options.check_self_contained_and_connected = 1;
94 else if (!strcmp(value, "false"))
95 options.check_self_contained_and_connected = 0;
96 else
97 return -1;
98 return 0;
99 }
05c1eb10
JH
100 else if (!strcmp(name, "cas")) {
101 struct strbuf val = STRBUF_INIT;
102 strbuf_addf(&val, "--" CAS_OPT_NAME "=%s", value);
103 string_list_append(&cas_options, val.buf);
104 strbuf_release(&val);
105 return 0;
16094885
NTND
106 } else if (!strcmp(name, "cloning")) {
107 if (!strcmp(value, "true"))
108 options.cloning = 1;
109 else if (!strcmp(value, "false"))
110 options.cloning = 0;
111 else
112 return -1;
113 return 0;
114 } else if (!strcmp(name, "update-shallow")) {
115 if (!strcmp(value, "true"))
116 options.update_shallow = 1;
117 else if (!strcmp(value, "false"))
118 options.update_shallow = 0;
119 else
120 return -1;
121 return 0;
0ea47f9d
JH
122 } else if (!strcmp(name, "pushcert")) {
123 if (!strcmp(value, "true"))
30261094 124 options.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
0ea47f9d 125 else if (!strcmp(value, "false"))
30261094
DB
126 options.push_cert = SEND_PACK_PUSH_CERT_NEVER;
127 else if (!strcmp(value, "if-asked"))
128 options.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
0ea47f9d
JH
129 else
130 return -1;
131 return 0;
16094885 132 } else {
ef08ef9e
SP
133 return 1 /* unsupported */;
134 }
135}
136
97cc7bc4
SP
137struct discovery {
138 const char *service;
139 char *buf_alloc;
140 char *buf;
141 size_t len;
2a455202 142 struct ref *refs;
16094885 143 struct sha1_array shallow;
97cc7bc4
SP
144 unsigned proto_git : 1;
145};
146static struct discovery *last_discovery;
147
b8054bbe
JK
148static struct ref *parse_git_refs(struct discovery *heads, int for_push)
149{
150 struct ref *list = NULL;
151 get_remote_heads(-1, heads->buf, heads->len, &list,
16094885 152 for_push ? REF_NORMAL : 0, NULL, &heads->shallow);
b8054bbe
JK
153 return list;
154}
155
156static struct ref *parse_info_refs(struct discovery *heads)
157{
158 char *data, *start, *mid;
159 char *ref_name;
160 int i = 0;
161
162 struct ref *refs = NULL;
163 struct ref *ref = NULL;
164 struct ref *last_ref = NULL;
165
166 data = heads->buf;
167 start = NULL;
168 mid = data;
169 while (i < heads->len) {
170 if (!start) {
171 start = &data[i];
172 }
173 if (data[i] == '\t')
174 mid = &data[i];
175 if (data[i] == '\n') {
176 if (mid - start != 40)
b227bbc4
JK
177 die("%sinfo/refs not valid: is this a git repository?",
178 url.buf);
b8054bbe
JK
179 data[i] = 0;
180 ref_name = mid + 1;
6f687c21 181 ref = alloc_ref(ref_name);
f4e54d02 182 get_oid_hex(start, &ref->old_oid);
b8054bbe
JK
183 if (!refs)
184 refs = ref;
185 if (last_ref)
186 last_ref->next = ref;
187 last_ref = ref;
188 start = NULL;
189 }
190 i++;
191 }
192
193 ref = alloc_ref("HEAD");
b227bbc4 194 if (!http_fetch_ref(url.buf, ref) &&
b8054bbe
JK
195 !resolve_remote_symref(ref, refs)) {
196 ref->next = refs;
197 refs = ref;
198 } else {
199 free(ref);
200 }
201
202 return refs;
203}
204
97cc7bc4
SP
205static void free_discovery(struct discovery *d)
206{
207 if (d) {
208 if (d == last_discovery)
209 last_discovery = NULL;
16094885 210 free(d->shallow.sha1);
97cc7bc4 211 free(d->buf_alloc);
2a455202 212 free_refs(d->refs);
97cc7bc4
SP
213 free(d);
214 }
215}
216
fc1b774c
JK
217static int show_http_message(struct strbuf *type, struct strbuf *charset,
218 struct strbuf *msg)
426e70d4
JK
219{
220 const char *p, *eol;
221
222 /*
223 * We only show text/plain parts, as other types are likely
224 * to be ugly to look at on the user's terminal.
426e70d4 225 */
bf197fd7 226 if (strcmp(type->buf, "text/plain"))
426e70d4 227 return -1;
fc1b774c
JK
228 if (charset->len)
229 strbuf_reencode(msg, charset->buf, get_log_output_encoding());
426e70d4
JK
230
231 strbuf_trim(msg);
232 if (!msg->len)
233 return -1;
234
235 p = msg->buf;
236 do {
237 eol = strchrnul(p, '\n');
238 fprintf(stderr, "remote: %.*s\n", (int)(eol - p), p);
239 p = eol + 1;
240 } while(*eol);
241 return 0;
242}
243
24d36f14 244static struct discovery *discover_refs(const char *service, int for_push)
a2d725b7 245{
4656bf47
SP
246 struct strbuf exp = STRBUF_INIT;
247 struct strbuf type = STRBUF_INIT;
fc1b774c 248 struct strbuf charset = STRBUF_INIT;
a2d725b7 249 struct strbuf buffer = STRBUF_INIT;
c65d5692 250 struct strbuf refs_url = STRBUF_INIT;
050ef365 251 struct strbuf effective_url = STRBUF_INIT;
97cc7bc4 252 struct discovery *last = last_discovery;
243c329c 253 int http_ret, maybe_smart = 0;
1bbcc224 254 struct http_get_options options;
a2d725b7 255
97cc7bc4
SP
256 if (last && !strcmp(service, last->service))
257 return last;
258 free_discovery(last);
a2d725b7 259
b227bbc4 260 strbuf_addf(&refs_url, "%sinfo/refs", url.buf);
59556548 261 if ((starts_with(url.buf, "http://") || starts_with(url.buf, "https://")) &&
02572c2e 262 git_env_bool("GIT_SMART_HTTP", 1)) {
243c329c 263 maybe_smart = 1;
b227bbc4 264 if (!strchr(url.buf, '?'))
c65d5692 265 strbuf_addch(&refs_url, '?');
97cc7bc4 266 else
c65d5692
JK
267 strbuf_addch(&refs_url, '&');
268 strbuf_addf(&refs_url, "service=%s", service);
97cc7bc4 269 }
a2d725b7 270
1bbcc224
JK
271 memset(&options, 0, sizeof(options));
272 options.content_type = &type;
fc1b774c 273 options.charset = &charset;
050ef365
JK
274 options.effective_url = &effective_url;
275 options.base_url = &url;
1bbcc224
JK
276 options.no_cache = 1;
277 options.keep_error = 1;
278
c65d5692 279 http_ret = http_get_strbuf(refs_url.buf, &buffer, &options);
a2d725b7
DB
280 switch (http_ret) {
281 case HTTP_OK:
282 break;
283 case HTTP_MISSING_TARGET:
fc1b774c 284 show_http_message(&type, &charset, &buffer);
b227bbc4 285 die("repository '%s' not found", url.buf);
42653c09 286 case HTTP_NOAUTH:
fc1b774c 287 show_http_message(&type, &charset, &buffer);
b227bbc4 288 die("Authentication failed for '%s'", url.buf);
a2d725b7 289 default:
fc1b774c 290 show_http_message(&type, &charset, &buffer);
b227bbc4 291 die("unable to access '%s': %s", url.buf, curl_errorstr);
a2d725b7
DB
292 }
293
97cc7bc4
SP
294 last= xcalloc(1, sizeof(*last_discovery));
295 last->service = service;
296 last->buf_alloc = strbuf_detach(&buffer, &last->len);
297 last->buf = last->buf_alloc;
298
4656bf47
SP
299 strbuf_addf(&exp, "application/x-%s-advertisement", service);
300 if (maybe_smart &&
301 (5 <= last->len && last->buf[4] == '#') &&
302 !strbuf_cmp(&exp, &type)) {
4981fe75
JK
303 char *line;
304
4656bf47
SP
305 /*
306 * smart HTTP response; validate that the service
97cc7bc4
SP
307 * pkt-line matches our request.
308 */
4981fe75 309 line = packet_read_line_buf(&last->buf, &last->len, NULL);
97cc7bc4 310
4656bf47 311 strbuf_reset(&exp);
97cc7bc4 312 strbuf_addf(&exp, "# service=%s", service);
4981fe75
JK
313 if (strcmp(line, exp.buf))
314 die("invalid server response; got '%s'", line);
97cc7bc4
SP
315 strbuf_release(&exp);
316
317 /* The header can include additional metadata lines, up
318 * until a packet flush marker. Ignore these now, but
319 * in the future we might start to scan them.
320 */
4981fe75
JK
321 while (packet_read_line_buf(&last->buf, &last->len, NULL))
322 ;
97cc7bc4
SP
323
324 last->proto_git = 1;
325 }
326
2a455202
JK
327 if (last->proto_git)
328 last->refs = parse_git_refs(last, for_push);
329 else
330 last->refs = parse_info_refs(last);
331
c65d5692 332 strbuf_release(&refs_url);
4656bf47
SP
333 strbuf_release(&exp);
334 strbuf_release(&type);
fc1b774c 335 strbuf_release(&charset);
050ef365 336 strbuf_release(&effective_url);
97cc7bc4
SP
337 strbuf_release(&buffer);
338 last_discovery = last;
339 return last;
340}
341
97cc7bc4
SP
342static struct ref *get_refs(int for_push)
343{
344 struct discovery *heads;
345
346 if (for_push)
2a455202 347 heads = discover_refs("git-receive-pack", for_push);
97cc7bc4 348 else
2a455202 349 heads = discover_refs("git-upload-pack", for_push);
97cc7bc4 350
2a455202 351 return heads->refs;
97cc7bc4
SP
352}
353
ae4efe19
SP
354static void output_refs(struct ref *refs)
355{
356 struct ref *posn;
357 for (posn = refs; posn; posn = posn->next) {
358 if (posn->symref)
359 printf("@%s %s\n", posn->symref, posn->name);
360 else
f4e54d02 361 printf("%s %s\n", oid_to_hex(&posn->old_oid), posn->name);
ae4efe19
SP
362 }
363 printf("\n");
364 fflush(stdout);
ae4efe19
SP
365}
366
de1a2fdd
SP
367struct rpc_state {
368 const char *service_name;
369 const char **argv;
8150749d 370 struct strbuf *stdin_preamble;
de1a2fdd
SP
371 char *service_url;
372 char *hdr_content_type;
373 char *hdr_accept;
374 char *buf;
375 size_t alloc;
376 size_t len;
377 size_t pos;
378 int in;
379 int out;
380 struct strbuf result;
b8538603 381 unsigned gzip_request : 1;
6c81a990 382 unsigned initial_buffer : 1;
de1a2fdd
SP
383};
384
385static size_t rpc_out(void *ptr, size_t eltsize,
386 size_t nmemb, void *buffer_)
387{
388 size_t max = eltsize * nmemb;
389 struct rpc_state *rpc = buffer_;
390 size_t avail = rpc->len - rpc->pos;
391
392 if (!avail) {
6c81a990 393 rpc->initial_buffer = 0;
4981fe75 394 avail = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
de1a2fdd
SP
395 if (!avail)
396 return 0;
397 rpc->pos = 0;
398 rpc->len = avail;
399 }
400
48310608 401 if (max < avail)
de1a2fdd
SP
402 avail = max;
403 memcpy(ptr, rpc->buf + rpc->pos, avail);
404 rpc->pos += avail;
405 return avail;
406}
407
6c81a990 408#ifndef NO_CURL_IOCTL
5092d3ec 409static curlioerr rpc_ioctl(CURL *handle, int cmd, void *clientp)
6c81a990
MS
410{
411 struct rpc_state *rpc = clientp;
412
413 switch (cmd) {
414 case CURLIOCMD_NOP:
415 return CURLIOE_OK;
416
417 case CURLIOCMD_RESTARTREAD:
418 if (rpc->initial_buffer) {
419 rpc->pos = 0;
420 return CURLIOE_OK;
421 }
b725b270 422 error("unable to rewind rpc post data - try increasing http.postBuffer");
6c81a990
MS
423 return CURLIOE_FAILRESTART;
424
425 default:
426 return CURLIOE_UNKNOWNCMD;
427 }
428}
429#endif
430
a04ff3ec 431static size_t rpc_in(char *ptr, size_t eltsize,
de1a2fdd
SP
432 size_t nmemb, void *buffer_)
433{
434 size_t size = eltsize * nmemb;
435 struct rpc_state *rpc = buffer_;
436 write_or_die(rpc->in, ptr, size);
437 return size;
438}
439
3a347ed7
JK
440static int run_slot(struct active_request_slot *slot,
441 struct slot_results *results)
206b099d 442{
b81401c1 443 int err;
3a347ed7 444 struct slot_results results_buf;
206b099d 445
3a347ed7
JK
446 if (!results)
447 results = &results_buf;
448
beed336c 449 err = run_one_slot(slot, results);
206b099d 450
b81401c1
JK
451 if (err != HTTP_OK && err != HTTP_REAUTH) {
452 error("RPC failed; result=%d, HTTP code = %ld",
3a347ed7 453 results->curl_result, results->http_code);
206b099d
SP
454 }
455
456 return err;
457}
458
3a347ed7 459static int probe_rpc(struct rpc_state *rpc, struct slot_results *results)
206b099d
SP
460{
461 struct active_request_slot *slot;
462 struct curl_slist *headers = NULL;
463 struct strbuf buf = STRBUF_INIT;
464 int err;
465
466 slot = get_active_slot();
467
468 headers = curl_slist_append(headers, rpc->hdr_content_type);
469 headers = curl_slist_append(headers, rpc->hdr_accept);
470
471 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
472 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
473 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
aa90b969 474 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, NULL);
206b099d
SP
475 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, "0000");
476 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, 4);
477 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
478 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
479 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buf);
480
3a347ed7 481 err = run_slot(slot, results);
206b099d
SP
482
483 curl_slist_free_all(headers);
484 strbuf_release(&buf);
485 return err;
486}
487
de1a2fdd
SP
488static int post_rpc(struct rpc_state *rpc)
489{
490 struct active_request_slot *slot;
de1a2fdd 491 struct curl_slist *headers = NULL;
b8538603
SP
492 int use_gzip = rpc->gzip_request;
493 char *gzip_body = NULL;
37711549 494 size_t gzip_size = 0;
206b099d 495 int err, large_request = 0;
c80d96ca 496 int needs_100_continue = 0;
de1a2fdd
SP
497
498 /* Try to load the entire request, if we can fit it into the
499 * allocated buffer space we can use HTTP/1.0 and avoid the
500 * chunked encoding mess.
501 */
502 while (1) {
503 size_t left = rpc->alloc - rpc->len;
504 char *buf = rpc->buf + rpc->len;
505 int n;
506
507 if (left < LARGE_PACKET_MAX) {
508 large_request = 1;
b8538603 509 use_gzip = 0;
de1a2fdd
SP
510 break;
511 }
512
4981fe75 513 n = packet_read(rpc->out, NULL, NULL, buf, left, 0);
de1a2fdd
SP
514 if (!n)
515 break;
516 rpc->len += n;
517 }
518
206b099d 519 if (large_request) {
c80d96ca 520 struct slot_results results;
521
b81401c1 522 do {
c80d96ca 523 err = probe_rpc(rpc, &results);
2501aff8
JK
524 if (err == HTTP_REAUTH)
525 credential_fill(&http_auth);
b81401c1
JK
526 } while (err == HTTP_REAUTH);
527 if (err != HTTP_OK)
528 return -1;
c80d96ca 529
530 if (results.auth_avail & CURLAUTH_GSSNEGOTIATE)
531 needs_100_continue = 1;
206b099d
SP
532 }
533
abf8df86
JK
534 headers = curl_slist_append(headers, rpc->hdr_content_type);
535 headers = curl_slist_append(headers, rpc->hdr_accept);
c80d96ca 536 headers = curl_slist_append(headers, needs_100_continue ?
537 "Expect: 100-continue" : "Expect:");
abf8df86
JK
538
539retry:
de1a2fdd 540 slot = get_active_slot();
de1a2fdd 541
de1a2fdd 542 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
d21f9794 543 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
de1a2fdd 544 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
aa90b969 545 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "gzip");
de1a2fdd 546
de1a2fdd
SP
547 if (large_request) {
548 /* The request body is large and the size cannot be predicted.
549 * We must use chunked encoding to send it.
550 */
de1a2fdd 551 headers = curl_slist_append(headers, "Transfer-Encoding: chunked");
6c81a990 552 rpc->initial_buffer = 1;
de1a2fdd
SP
553 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, rpc_out);
554 curl_easy_setopt(slot->curl, CURLOPT_INFILE, rpc);
6c81a990
MS
555#ifndef NO_CURL_IOCTL
556 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, rpc_ioctl);
557 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, rpc);
558#endif
de1a2fdd
SP
559 if (options.verbosity > 1) {
560 fprintf(stderr, "POST %s (chunked)\n", rpc->service_name);
561 fflush(stderr);
562 }
563
2e736fd5
JK
564 } else if (gzip_body) {
565 /*
566 * If we are looping to retry authentication, then the previous
567 * run will have set up the headers and gzip buffer already,
568 * and we just need to send it.
569 */
570 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
571 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, gzip_size);
572
b8538603
SP
573 } else if (use_gzip && 1024 < rpc->len) {
574 /* The client backend isn't giving us compressed data so
575 * we can try to deflate it ourselves, this may save on.
576 * the transfer time.
577 */
ef49a7a0 578 git_zstream stream;
b8538603
SP
579 int ret;
580
55bb5c91 581 git_deflate_init_gzip(&stream, Z_BEST_COMPRESSION);
df126e10
JK
582 gzip_size = git_deflate_bound(&stream, rpc->len);
583 gzip_body = xmalloc(gzip_size);
b8538603
SP
584
585 stream.next_in = (unsigned char *)rpc->buf;
586 stream.avail_in = rpc->len;
587 stream.next_out = (unsigned char *)gzip_body;
df126e10 588 stream.avail_out = gzip_size;
b8538603 589
55bb5c91 590 ret = git_deflate(&stream, Z_FINISH);
b8538603
SP
591 if (ret != Z_STREAM_END)
592 die("cannot deflate request; zlib deflate error %d", ret);
593
55bb5c91 594 ret = git_deflate_end_gently(&stream);
b8538603
SP
595 if (ret != Z_OK)
596 die("cannot deflate request; zlib end error %d", ret);
597
df126e10 598 gzip_size = stream.total_out;
b8538603
SP
599
600 headers = curl_slist_append(headers, "Content-Encoding: gzip");
601 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
df126e10 602 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, gzip_size);
b8538603
SP
603
604 if (options.verbosity > 1) {
605 fprintf(stderr, "POST %s (gzip %lu to %lu bytes)\n",
606 rpc->service_name,
df126e10 607 (unsigned long)rpc->len, (unsigned long)gzip_size);
b8538603
SP
608 fflush(stderr);
609 }
de1a2fdd
SP
610 } else {
611 /* We know the complete request size in advance, use the
612 * more normal Content-Length approach.
613 */
614 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, rpc->buf);
615 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, rpc->len);
616 if (options.verbosity > 1) {
617 fprintf(stderr, "POST %s (%lu bytes)\n",
618 rpc->service_name, (unsigned long)rpc->len);
619 fflush(stderr);
620 }
621 }
622
623 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
624 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, rpc_in);
625 curl_easy_setopt(slot->curl, CURLOPT_FILE, rpc);
626
3a347ed7 627 err = run_slot(slot, NULL);
2501aff8
JK
628 if (err == HTTP_REAUTH && !large_request) {
629 credential_fill(&http_auth);
abf8df86 630 goto retry;
2501aff8 631 }
b81401c1
JK
632 if (err != HTTP_OK)
633 err = -1;
de1a2fdd
SP
634
635 curl_slist_free_all(headers);
b8538603 636 free(gzip_body);
de1a2fdd
SP
637 return err;
638}
639
640static int rpc_service(struct rpc_state *rpc, struct discovery *heads)
641{
642 const char *svc = rpc->service_name;
643 struct strbuf buf = STRBUF_INIT;
8150749d 644 struct strbuf *preamble = rpc->stdin_preamble;
d3180279 645 struct child_process client = CHILD_PROCESS_INIT;
de1a2fdd
SP
646 int err = 0;
647
de1a2fdd
SP
648 client.in = -1;
649 client.out = -1;
650 client.git_cmd = 1;
651 client.argv = rpc->argv;
652 if (start_command(&client))
653 exit(1);
8150749d
IT
654 if (preamble)
655 write_or_die(client.in, preamble->buf, preamble->len);
de1a2fdd
SP
656 if (heads)
657 write_or_die(client.in, heads->buf, heads->len);
658
659 rpc->alloc = http_post_buffer;
660 rpc->buf = xmalloc(rpc->alloc);
661 rpc->in = client.in;
662 rpc->out = client.out;
663 strbuf_init(&rpc->result, 0);
664
b227bbc4 665 strbuf_addf(&buf, "%s%s", url.buf, svc);
de1a2fdd
SP
666 rpc->service_url = strbuf_detach(&buf, NULL);
667
668 strbuf_addf(&buf, "Content-Type: application/x-%s-request", svc);
669 rpc->hdr_content_type = strbuf_detach(&buf, NULL);
670
8efa5f62 671 strbuf_addf(&buf, "Accept: application/x-%s-result", svc);
de1a2fdd
SP
672 rpc->hdr_accept = strbuf_detach(&buf, NULL);
673
674 while (!err) {
4981fe75 675 int n = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
de1a2fdd
SP
676 if (!n)
677 break;
678 rpc->pos = 0;
679 rpc->len = n;
680 err |= post_rpc(rpc);
681 }
de1a2fdd
SP
682
683 close(client.in);
de1a2fdd 684 client.in = -1;
6cdf0223
SP
685 if (!err) {
686 strbuf_read(&rpc->result, client.out, 0);
687 } else {
688 char buf[4096];
689 for (;;)
690 if (xread(client.out, buf, sizeof(buf)) <= 0)
691 break;
692 }
b4ee10f6
SP
693
694 close(client.out);
de1a2fdd
SP
695 client.out = -1;
696
697 err |= finish_command(&client);
698 free(rpc->service_url);
699 free(rpc->hdr_content_type);
700 free(rpc->hdr_accept);
701 free(rpc->buf);
702 strbuf_release(&buf);
703 return err;
704}
705
292ce46b
SP
706static int fetch_dumb(int nr_heads, struct ref **to_fetch)
707{
26e1e0b2 708 struct walker *walker;
292ce46b
SP
709 char **targets = xmalloc(nr_heads * sizeof(char*));
710 int ret, i;
711
508ea882
NTND
712 if (options.depth || options.deepen_since)
713 die("dumb http transport does not support shallow capabilities");
292ce46b 714 for (i = 0; i < nr_heads; i++)
f4e54d02 715 targets[i] = xstrdup(oid_to_hex(&to_fetch[i]->old_oid));
292ce46b 716
b227bbc4 717 walker = get_http_walker(url.buf);
292ce46b
SP
718 walker->get_all = 1;
719 walker->get_tree = 1;
720 walker->get_history = 1;
ef08ef9e 721 walker->get_verbosely = options.verbosity >= 3;
292ce46b
SP
722 walker->get_recover = 0;
723 ret = walker_fetch(walker, nr_heads, targets, NULL, NULL);
26e1e0b2 724 walker_free(walker);
292ce46b
SP
725
726 for (i = 0; i < nr_heads; i++)
727 free(targets[i]);
728 free(targets);
729
b725b270 730 return ret ? error("fetch failed.") : 0;
292ce46b
SP
731}
732
249b2004
SP
733static int fetch_git(struct discovery *heads,
734 int nr_heads, struct ref **to_fetch)
735{
736 struct rpc_state rpc;
8150749d 737 struct strbuf preamble = STRBUF_INIT;
b5f62ebe
NTND
738 int i, err;
739 struct argv_array args = ARGV_ARRAY_INIT;
740
741 argv_array_pushl(&args, "fetch-pack", "--stateless-rpc",
742 "--stdin", "--lock-pack", NULL);
249b2004 743 if (options.followtags)
b5f62ebe 744 argv_array_push(&args, "--include-tag");
249b2004 745 if (options.thin)
b5f62ebe
NTND
746 argv_array_push(&args, "--thin");
747 if (options.verbosity >= 3)
748 argv_array_pushl(&args, "-v", "-v", NULL);
9ba38048 749 if (options.check_self_contained_and_connected)
b5f62ebe 750 argv_array_push(&args, "--check-self-contained-and-connected");
16094885 751 if (options.cloning)
b5f62ebe 752 argv_array_push(&args, "--cloning");
16094885 753 if (options.update_shallow)
b5f62ebe 754 argv_array_push(&args, "--update-shallow");
249b2004 755 if (!options.progress)
b5f62ebe
NTND
756 argv_array_push(&args, "--no-progress");
757 if (options.depth)
758 argv_array_pushf(&args, "--depth=%lu", options.depth);
508ea882
NTND
759 if (options.deepen_since)
760 argv_array_pushf(&args, "--shallow-since=%s", options.deepen_since);
a45a2600
NTND
761 for (i = 0; i < options.deepen_not.nr; i++)
762 argv_array_pushf(&args, "--shallow-exclude=%s",
763 options.deepen_not.items[i].string);
b5f62ebe 764 argv_array_push(&args, url.buf);
8150749d 765
249b2004
SP
766 for (i = 0; i < nr_heads; i++) {
767 struct ref *ref = to_fetch[i];
94ee8e2c 768 if (!*ref->name)
249b2004 769 die("cannot fetch by sha1 over smart http");
58f2ed05 770 packet_buf_write(&preamble, "%s %s\n",
f4e54d02 771 oid_to_hex(&ref->old_oid), ref->name);
249b2004 772 }
8150749d 773 packet_buf_flush(&preamble);
249b2004
SP
774
775 memset(&rpc, 0, sizeof(rpc));
776 rpc.service_name = "git-upload-pack",
b5f62ebe 777 rpc.argv = args.argv;
8150749d 778 rpc.stdin_preamble = &preamble;
b8538603 779 rpc.gzip_request = 1;
249b2004
SP
780
781 err = rpc_service(&rpc, heads);
782 if (rpc.result.len)
cdf4fb8e 783 write_or_die(1, rpc.result.buf, rpc.result.len);
249b2004 784 strbuf_release(&rpc.result);
8150749d 785 strbuf_release(&preamble);
b5f62ebe 786 argv_array_clear(&args);
249b2004
SP
787 return err;
788}
789
790static int fetch(int nr_heads, struct ref **to_fetch)
791{
2a455202 792 struct discovery *d = discover_refs("git-upload-pack", 0);
249b2004
SP
793 if (d->proto_git)
794 return fetch_git(d, nr_heads, to_fetch);
795 else
796 return fetch_dumb(nr_heads, to_fetch);
797}
798
292ce46b
SP
799static void parse_fetch(struct strbuf *buf)
800{
801 struct ref **to_fetch = NULL;
802 struct ref *list_head = NULL;
803 struct ref **list = &list_head;
804 int alloc_heads = 0, nr_heads = 0;
805
806 do {
95b567c7
JK
807 const char *p;
808 if (skip_prefix(buf->buf, "fetch ", &p)) {
809 const char *name;
292ce46b 810 struct ref *ref;
8338c911 811 struct object_id old_oid;
292ce46b 812
8338c911 813 if (get_oid_hex(p, &old_oid))
292ce46b 814 die("protocol error: expected sha/ref, got %s'", p);
8338c911 815 if (p[GIT_SHA1_HEXSZ] == ' ')
816 name = p + GIT_SHA1_HEXSZ + 1;
817 else if (!p[GIT_SHA1_HEXSZ])
292ce46b
SP
818 name = "";
819 else
820 die("protocol error: expected sha/ref, got %s'", p);
821
822 ref = alloc_ref(name);
8338c911 823 oidcpy(&ref->old_oid, &old_oid);
292ce46b
SP
824
825 *list = ref;
826 list = &ref->next;
827
828 ALLOC_GROW(to_fetch, nr_heads + 1, alloc_heads);
829 to_fetch[nr_heads++] = ref;
830 }
831 else
832 die("http transport does not support %s", buf->buf);
833
834 strbuf_reset(buf);
8f309aeb 835 if (strbuf_getline_lf(buf, stdin) == EOF)
292ce46b
SP
836 return;
837 if (!*buf->buf)
838 break;
839 } while (1);
840
249b2004 841 if (fetch(nr_heads, to_fetch))
292ce46b
SP
842 exit(128); /* error already reported */
843 free_refs(list_head);
844 free(to_fetch);
845
846 printf("\n");
847 fflush(stdout);
848 strbuf_reset(buf);
849}
850
ae4efe19
SP
851static int push_dav(int nr_spec, char **specs)
852{
853 const char **argv = xmalloc((10 + nr_spec) * sizeof(char*));
854 int argc = 0, i;
855
856 argv[argc++] = "http-push";
857 argv[argc++] = "--helper-status";
858 if (options.dry_run)
859 argv[argc++] = "--dry-run";
860 if (options.verbosity > 1)
861 argv[argc++] = "--verbose";
b227bbc4 862 argv[argc++] = url.buf;
ae4efe19
SP
863 for (i = 0; i < nr_spec; i++)
864 argv[argc++] = specs[i];
865 argv[argc++] = NULL;
866
867 if (run_command_v_opt(argv, RUN_GIT_CMD))
868 die("git-%s failed", argv[0]);
869 free(argv);
870 return 0;
871}
872
de1a2fdd
SP
873static int push_git(struct discovery *heads, int nr_spec, char **specs)
874{
875 struct rpc_state rpc;
222b1212
JH
876 int i, err;
877 struct argv_array args;
05c1eb10 878 struct string_list_item *cas_option;
26be19ba 879 struct strbuf preamble = STRBUF_INIT;
222b1212
JH
880
881 argv_array_init(&args);
882 argv_array_pushl(&args, "send-pack", "--stateless-rpc", "--helper-status",
883 NULL);
de1a2fdd 884
de1a2fdd 885 if (options.thin)
222b1212 886 argv_array_push(&args, "--thin");
de1a2fdd 887 if (options.dry_run)
222b1212 888 argv_array_push(&args, "--dry-run");
30261094
DB
889 if (options.push_cert == SEND_PACK_PUSH_CERT_ALWAYS)
890 argv_array_push(&args, "--signed=yes");
891 else if (options.push_cert == SEND_PACK_PUSH_CERT_IF_ASKED)
892 argv_array_push(&args, "--signed=if-asked");
c207e34f 893 if (options.verbosity == 0)
222b1212 894 argv_array_push(&args, "--quiet");
c207e34f 895 else if (options.verbosity > 1)
222b1212
JH
896 argv_array_push(&args, "--verbose");
897 argv_array_push(&args, options.progress ? "--progress" : "--no-progress");
05c1eb10 898 for_each_string_list_item(cas_option, &cas_options)
2233ad45 899 argv_array_push(&args, cas_option->string);
b227bbc4 900 argv_array_push(&args, url.buf);
26be19ba
JK
901
902 argv_array_push(&args, "--stdin");
de1a2fdd 903 for (i = 0; i < nr_spec; i++)
26be19ba
JK
904 packet_buf_write(&preamble, "%s\n", specs[i]);
905 packet_buf_flush(&preamble);
de1a2fdd
SP
906
907 memset(&rpc, 0, sizeof(rpc));
908 rpc.service_name = "git-receive-pack",
222b1212 909 rpc.argv = args.argv;
26be19ba 910 rpc.stdin_preamble = &preamble;
de1a2fdd
SP
911
912 err = rpc_service(&rpc, heads);
913 if (rpc.result.len)
cdf4fb8e 914 write_or_die(1, rpc.result.buf, rpc.result.len);
de1a2fdd 915 strbuf_release(&rpc.result);
26be19ba 916 strbuf_release(&preamble);
222b1212 917 argv_array_clear(&args);
de1a2fdd
SP
918 return err;
919}
920
921static int push(int nr_spec, char **specs)
922{
2a455202 923 struct discovery *heads = discover_refs("git-receive-pack", 1);
de1a2fdd
SP
924 int ret;
925
926 if (heads->proto_git)
927 ret = push_git(heads, nr_spec, specs);
928 else
929 ret = push_dav(nr_spec, specs);
930 free_discovery(heads);
931 return ret;
932}
933
ae4efe19
SP
934static void parse_push(struct strbuf *buf)
935{
936 char **specs = NULL;
5238cbf6 937 int alloc_spec = 0, nr_spec = 0, i, ret;
ae4efe19
SP
938
939 do {
59556548 940 if (starts_with(buf->buf, "push ")) {
ae4efe19
SP
941 ALLOC_GROW(specs, nr_spec + 1, alloc_spec);
942 specs[nr_spec++] = xstrdup(buf->buf + 5);
943 }
944 else
945 die("http transport does not support %s", buf->buf);
946
947 strbuf_reset(buf);
8f309aeb 948 if (strbuf_getline_lf(buf, stdin) == EOF)
dc4cd767 949 goto free_specs;
ae4efe19
SP
950 if (!*buf->buf)
951 break;
952 } while (1);
953
5238cbf6 954 ret = push(nr_spec, specs);
ae4efe19
SP
955 printf("\n");
956 fflush(stdout);
dc4cd767 957
5238cbf6
SP
958 if (ret)
959 exit(128); /* error already reported */
960
dc4cd767
JM
961 free_specs:
962 for (i = 0; i < nr_spec; i++)
963 free(specs[i]);
964 free(specs);
ae4efe19
SP
965}
966
a2d725b7
DB
967int main(int argc, const char **argv)
968{
a2d725b7 969 struct strbuf buf = STRBUF_INIT;
a45d3d7e 970 int nongit;
a2d725b7 971
f18604bb
YE
972 git_setup_gettext();
973
c6dfb399 974 git_extract_argv0_path(argv[0]);
a45d3d7e 975 setup_git_directory_gently(&nongit);
a2d725b7 976 if (argc < 2) {
cdaa4e98 977 error("remote-curl: usage: git remote-curl <remote> [<url>]");
a2d725b7
DB
978 return 1;
979 }
980
ef08ef9e
SP
981 options.verbosity = 1;
982 options.progress = !!isatty(2);
de1a2fdd 983 options.thin = 1;
a45a2600 984 string_list_init(&options.deepen_not, 1);
ef08ef9e 985
a2d725b7
DB
986 remote = remote_get(argv[1]);
987
988 if (argc > 2) {
b227bbc4 989 end_url_with_slash(&url, argv[2]);
a2d725b7 990 } else {
b227bbc4 991 end_url_with_slash(&url, remote->url[0]);
a2d725b7
DB
992 }
993
b227bbc4 994 http_init(remote, url.buf, 0);
888692b7 995
a2d725b7 996 do {
95b567c7
JK
997 const char *arg;
998
8f309aeb 999 if (strbuf_getline_lf(&buf, stdin) == EOF) {
1843f0ce 1000 if (ferror(stdin))
cdaa4e98 1001 error("remote-curl: error reading command stream from git");
1843f0ce
SR
1002 return 1;
1003 }
1004 if (buf.len == 0)
a2d725b7 1005 break;
59556548 1006 if (starts_with(buf.buf, "fetch ")) {
a45d3d7e 1007 if (nongit)
cdaa4e98 1008 die("remote-curl: fetch attempted without a local repo");
292ce46b
SP
1009 parse_fetch(&buf);
1010
59556548 1011 } else if (!strcmp(buf.buf, "list") || starts_with(buf.buf, "list ")) {
97cc7bc4
SP
1012 int for_push = !!strstr(buf.buf + 4, "for-push");
1013 output_refs(get_refs(for_push));
ae4efe19 1014
59556548 1015 } else if (starts_with(buf.buf, "push ")) {
ae4efe19
SP
1016 parse_push(&buf);
1017
95b567c7
JK
1018 } else if (skip_prefix(buf.buf, "option ", &arg)) {
1019 char *value = strchr(arg, ' ');
ef08ef9e
SP
1020 int result;
1021
1022 if (value)
1023 *value++ = '\0';
1024 else
1025 value = "true";
1026
95b567c7 1027 result = set_option(arg, value);
ef08ef9e
SP
1028 if (!result)
1029 printf("ok\n");
1030 else if (result < 0)
1031 printf("error invalid value\n");
1032 else
1033 printf("unsupported\n");
a2d725b7 1034 fflush(stdout);
ef08ef9e 1035
a2d725b7
DB
1036 } else if (!strcmp(buf.buf, "capabilities")) {
1037 printf("fetch\n");
ef08ef9e 1038 printf("option\n");
ae4efe19 1039 printf("push\n");
9ba38048 1040 printf("check-connectivity\n");
a2d725b7
DB
1041 printf("\n");
1042 fflush(stdout);
1043 } else {
cdaa4e98 1044 error("remote-curl: unknown command '%s' from git", buf.buf);
a2d725b7
DB
1045 return 1;
1046 }
1047 strbuf_reset(&buf);
1048 } while (1);
888692b7
TRC
1049
1050 http_cleanup();
1051
a2d725b7
DB
1052 return 0;
1053}