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