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