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