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