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