remote-curl.c 26.7 KB
Newer Older
1 2 3 4 5
#include "cache.h"
#include "remote.h"
#include "strbuf.h"
#include "walker.h"
#include "http.h"
6
#include "exec_cmd.h"
7
#include "run-command.h"
8
#include "pkt-line.h"
9
#include "string-list.h"
10
#include "sideband.h"
J
Junio C Hamano 已提交
11
#include "argv-array.h"
12
#include "credential.h"
13
#include "sha1-array.h"
14
#include "send-pack.h"
15

16
static struct remote *remote;
J
Jeff King 已提交
17 18
/* always ends with a trailing slash */
static struct strbuf url = STRBUF_INIT;
19

20 21 22
struct options {
	int verbosity;
	unsigned long depth;
23
	char *deepen_since;
24
	struct string_list deepen_not;
25
	unsigned progress : 1,
26
		check_self_contained_and_connected : 1,
27 28
		cloning : 1,
		update_shallow : 1,
29
		followtags : 1,
30
		dry_run : 1,
31
		thin : 1,
32
		/* One of the SEND_PACK_PUSH_CERT_* constants. */
33 34
		push_cert : 2,
		deepen_relative : 1;
35 36
};
static struct options options;
37
static struct string_list cas_options = STRING_LIST_INIT_DUP;
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55

static int set_option(const char *name, const char *value)
{
	if (!strcmp(name, "verbosity")) {
		char *end;
		int v = strtol(value, &end, 10);
		if (value == end || *end)
			return -1;
		options.verbosity = v;
		return 0;
	}
	else if (!strcmp(name, "progress")) {
		if (!strcmp(value, "true"))
			options.progress = 1;
		else if (!strcmp(value, "false"))
			options.progress = 0;
		else
			return -1;
56
		return 0;
57 58 59 60 61 62 63
	}
	else if (!strcmp(name, "depth")) {
		char *end;
		unsigned long v = strtoul(value, &end, 10);
		if (value == end || *end)
			return -1;
		options.depth = v;
64
		return 0;
65
	}
66 67 68 69
	else if (!strcmp(name, "deepen-since")) {
		options.deepen_since = xstrdup(value);
		return 0;
	}
70 71 72 73
	else if (!strcmp(name, "deepen-not")) {
		string_list_append(&options.deepen_not, value);
		return 0;
	}
74 75 76 77 78 79 80 81 82
	else if (!strcmp(name, "deepen-relative")) {
		if (!strcmp(value, "true"))
			options.deepen_relative = 1;
		else if (!strcmp(value, "false"))
			options.deepen_relative = 0;
		else
			return -1;
		return 0;
	}
83 84 85 86 87 88 89
	else if (!strcmp(name, "followtags")) {
		if (!strcmp(value, "true"))
			options.followtags = 1;
		else if (!strcmp(value, "false"))
			options.followtags = 0;
		else
			return -1;
90
		return 0;
91
	}
92 93 94 95 96 97 98 99 100
	else if (!strcmp(name, "dry-run")) {
		if (!strcmp(value, "true"))
			options.dry_run = 1;
		else if (!strcmp(value, "false"))
			options.dry_run = 0;
		else
			return -1;
		return 0;
	}
101 102 103 104 105 106 107 108 109
	else if (!strcmp(name, "check-connectivity")) {
		if (!strcmp(value, "true"))
			options.check_self_contained_and_connected = 1;
		else if (!strcmp(value, "false"))
			options.check_self_contained_and_connected = 0;
		else
			return -1;
		return 0;
	}
110 111 112 113 114 115
	else if (!strcmp(name, "cas")) {
		struct strbuf val = STRBUF_INIT;
		strbuf_addf(&val, "--" CAS_OPT_NAME "=%s", value);
		string_list_append(&cas_options, val.buf);
		strbuf_release(&val);
		return 0;
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
	} else if (!strcmp(name, "cloning")) {
		if (!strcmp(value, "true"))
			options.cloning = 1;
		else if (!strcmp(value, "false"))
			options.cloning = 0;
		else
			return -1;
		return 0;
	} else if (!strcmp(name, "update-shallow")) {
		if (!strcmp(value, "true"))
			options.update_shallow = 1;
		else if (!strcmp(value, "false"))
			options.update_shallow = 0;
		else
			return -1;
		return 0;
132 133
	} else if (!strcmp(name, "pushcert")) {
		if (!strcmp(value, "true"))
134
			options.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
135
		else if (!strcmp(value, "false"))
136 137 138
			options.push_cert = SEND_PACK_PUSH_CERT_NEVER;
		else if (!strcmp(value, "if-asked"))
			options.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
139 140 141
		else
			return -1;
		return 0;
142 143 144 145 146 147 148 149 150 151 152 153 154

#if LIBCURL_VERSION_NUM >= 0x070a08
	} else if (!strcmp(name, "family")) {
		if (!strcmp(value, "ipv4"))
			git_curl_ipresolve = CURL_IPRESOLVE_V4;
		else if (!strcmp(value, "ipv6"))
			git_curl_ipresolve = CURL_IPRESOLVE_V6;
		else if (!strcmp(value, "all"))
			git_curl_ipresolve = CURL_IPRESOLVE_WHATEVER;
		else
			return -1;
		return 0;
#endif /* LIBCURL_VERSION_NUM >= 0x070a08 */
155
	} else {
156 157 158 159
		return 1 /* unsupported */;
	}
}

160 161 162 163 164
struct discovery {
	const char *service;
	char *buf_alloc;
	char *buf;
	size_t len;
165
	struct ref *refs;
166
	struct sha1_array shallow;
167 168 169 170
	unsigned proto_git : 1;
};
static struct discovery *last_discovery;

171 172 173 174
static struct ref *parse_git_refs(struct discovery *heads, int for_push)
{
	struct ref *list = NULL;
	get_remote_heads(-1, heads->buf, heads->len, &list,
175
			 for_push ? REF_NORMAL : 0, NULL, &heads->shallow);
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
	return list;
}

static struct ref *parse_info_refs(struct discovery *heads)
{
	char *data, *start, *mid;
	char *ref_name;
	int i = 0;

	struct ref *refs = NULL;
	struct ref *ref = NULL;
	struct ref *last_ref = NULL;

	data = heads->buf;
	start = NULL;
	mid = data;
	while (i < heads->len) {
		if (!start) {
			start = &data[i];
		}
		if (data[i] == '\t')
			mid = &data[i];
		if (data[i] == '\n') {
			if (mid - start != 40)
J
Jeff King 已提交
200 201
				die("%sinfo/refs not valid: is this a git repository?",
				    url.buf);
202 203
			data[i] = 0;
			ref_name = mid + 1;
204
			ref = alloc_ref(ref_name);
205
			get_oid_hex(start, &ref->old_oid);
206 207 208 209 210 211 212 213 214 215 216
			if (!refs)
				refs = ref;
			if (last_ref)
				last_ref->next = ref;
			last_ref = ref;
			start = NULL;
		}
		i++;
	}

	ref = alloc_ref("HEAD");
J
Jeff King 已提交
217
	if (!http_fetch_ref(url.buf, ref) &&
218 219 220 221 222 223 224 225 226 227
	    !resolve_remote_symref(ref, refs)) {
		ref->next = refs;
		refs = ref;
	} else {
		free(ref);
	}

	return refs;
}

228 229 230 231 232
static void free_discovery(struct discovery *d)
{
	if (d) {
		if (d == last_discovery)
			last_discovery = NULL;
233
		free(d->shallow.sha1);
234
		free(d->buf_alloc);
235
		free_refs(d->refs);
236 237 238 239
		free(d);
	}
}

240 241
static int show_http_message(struct strbuf *type, struct strbuf *charset,
			     struct strbuf *msg)
242 243 244 245 246 247 248
{
	const char *p, *eol;

	/*
	 * We only show text/plain parts, as other types are likely
	 * to be ugly to look at on the user's terminal.
	 */
249
	if (strcmp(type->buf, "text/plain"))
250
		return -1;
251 252
	if (charset->len)
		strbuf_reencode(msg, charset->buf, get_log_output_encoding());
253 254 255 256 257 258 259 260 261 262 263 264 265 266

	strbuf_trim(msg);
	if (!msg->len)
		return -1;

	p = msg->buf;
	do {
		eol = strchrnul(p, '\n');
		fprintf(stderr, "remote: %.*s\n", (int)(eol - p), p);
		p = eol + 1;
	} while(*eol);
	return 0;
}

267
static struct discovery *discover_refs(const char *service, int for_push)
268
{
269 270
	struct strbuf exp = STRBUF_INIT;
	struct strbuf type = STRBUF_INIT;
271
	struct strbuf charset = STRBUF_INIT;
272
	struct strbuf buffer = STRBUF_INIT;
J
Jeff King 已提交
273
	struct strbuf refs_url = STRBUF_INIT;
274
	struct strbuf effective_url = STRBUF_INIT;
275
	struct discovery *last = last_discovery;
J
Jeff King 已提交
276
	int http_ret, maybe_smart = 0;
277
	struct http_get_options http_options;
278

279 280 281
	if (last && !strcmp(service, last->service))
		return last;
	free_discovery(last);
282

J
Jeff King 已提交
283
	strbuf_addf(&refs_url, "%sinfo/refs", url.buf);
284
	if ((starts_with(url.buf, "http://") || starts_with(url.buf, "https://")) &&
285
	     git_env_bool("GIT_SMART_HTTP", 1)) {
J
Jeff King 已提交
286
		maybe_smart = 1;
J
Jeff King 已提交
287
		if (!strchr(url.buf, '?'))
J
Jeff King 已提交
288
			strbuf_addch(&refs_url, '?');
289
		else
J
Jeff King 已提交
290 291
			strbuf_addch(&refs_url, '&');
		strbuf_addf(&refs_url, "service=%s", service);
292
	}
293

294 295 296 297 298
	memset(&http_options, 0, sizeof(http_options));
	http_options.content_type = &type;
	http_options.charset = &charset;
	http_options.effective_url = &effective_url;
	http_options.base_url = &url;
J
Jeff King 已提交
299
	http_options.initial_request = 1;
300 301
	http_options.no_cache = 1;
	http_options.keep_error = 1;
J
Jeff King 已提交
302

303
	http_ret = http_get_strbuf(refs_url.buf, &buffer, &http_options);
304 305 306 307
	switch (http_ret) {
	case HTTP_OK:
		break;
	case HTTP_MISSING_TARGET:
308
		show_http_message(&type, &charset, &buffer);
J
Jeff King 已提交
309
		die("repository '%s' not found", url.buf);
310
	case HTTP_NOAUTH:
311
		show_http_message(&type, &charset, &buffer);
J
Jeff King 已提交
312
		die("Authentication failed for '%s'", url.buf);
313
	default:
314
		show_http_message(&type, &charset, &buffer);
J
Jeff King 已提交
315
		die("unable to access '%s': %s", url.buf, curl_errorstr);
316 317
	}

J
Jeff King 已提交
318 319 320
	if (options.verbosity && !starts_with(refs_url.buf, url.buf))
		warning(_("redirecting to %s"), url.buf);

321 322 323 324 325
	last= xcalloc(1, sizeof(*last_discovery));
	last->service = service;
	last->buf_alloc = strbuf_detach(&buffer, &last->len);
	last->buf = last->buf_alloc;

326 327 328 329
	strbuf_addf(&exp, "application/x-%s-advertisement", service);
	if (maybe_smart &&
	    (5 <= last->len && last->buf[4] == '#') &&
	    !strbuf_cmp(&exp, &type)) {
330 331
		char *line;

332 333
		/*
		 * smart HTTP response; validate that the service
334 335
		 * pkt-line matches our request.
		 */
336
		line = packet_read_line_buf(&last->buf, &last->len, NULL);
337

338
		strbuf_reset(&exp);
339
		strbuf_addf(&exp, "# service=%s", service);
340 341
		if (strcmp(line, exp.buf))
			die("invalid server response; got '%s'", line);
342 343 344 345 346 347
		strbuf_release(&exp);

		/* The header can include additional metadata lines, up
		 * until a packet flush marker.  Ignore these now, but
		 * in the future we might start to scan them.
		 */
348 349
		while (packet_read_line_buf(&last->buf, &last->len, NULL))
			;
350 351 352 353

		last->proto_git = 1;
	}

354 355 356 357 358
	if (last->proto_git)
		last->refs = parse_git_refs(last, for_push);
	else
		last->refs = parse_info_refs(last);

J
Jeff King 已提交
359
	strbuf_release(&refs_url);
360 361
	strbuf_release(&exp);
	strbuf_release(&type);
362
	strbuf_release(&charset);
363
	strbuf_release(&effective_url);
364 365 366 367 368 369 370 371 372 373
	strbuf_release(&buffer);
	last_discovery = last;
	return last;
}

static struct ref *get_refs(int for_push)
{
	struct discovery *heads;

	if (for_push)
374
		heads = discover_refs("git-receive-pack", for_push);
375
	else
376
		heads = discover_refs("git-upload-pack", for_push);
377

378
	return heads->refs;
379 380
}

381 382 383 384 385 386 387
static void output_refs(struct ref *refs)
{
	struct ref *posn;
	for (posn = refs; posn; posn = posn->next) {
		if (posn->symref)
			printf("@%s %s\n", posn->symref, posn->name);
		else
388
			printf("%s %s\n", oid_to_hex(&posn->old_oid), posn->name);
389 390 391 392 393
	}
	printf("\n");
	fflush(stdout);
}

394 395 396
struct rpc_state {
	const char *service_name;
	const char **argv;
397
	struct strbuf *stdin_preamble;
398 399 400 401 402 403 404 405 406
	char *service_url;
	char *hdr_content_type;
	char *hdr_accept;
	char *buf;
	size_t alloc;
	size_t len;
	size_t pos;
	int in;
	int out;
407
	int any_written;
408
	struct strbuf result;
S
Shawn O. Pearce 已提交
409
	unsigned gzip_request : 1;
410
	unsigned initial_buffer : 1;
411 412 413 414 415 416 417 418 419 420
};

static size_t rpc_out(void *ptr, size_t eltsize,
		size_t nmemb, void *buffer_)
{
	size_t max = eltsize * nmemb;
	struct rpc_state *rpc = buffer_;
	size_t avail = rpc->len - rpc->pos;

	if (!avail) {
421
		rpc->initial_buffer = 0;
422
		avail = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
423 424 425 426 427 428
		if (!avail)
			return 0;
		rpc->pos = 0;
		rpc->len = avail;
	}

T
Tay Ray Chuan 已提交
429
	if (max < avail)
430 431 432 433 434 435
		avail = max;
	memcpy(ptr, rpc->buf + rpc->pos, avail);
	rpc->pos += avail;
	return avail;
}

436
#ifndef NO_CURL_IOCTL
437
static curlioerr rpc_ioctl(CURL *handle, int cmd, void *clientp)
438 439 440 441 442 443 444 445 446 447 448 449
{
	struct rpc_state *rpc = clientp;

	switch (cmd) {
	case CURLIOCMD_NOP:
		return CURLIOE_OK;

	case CURLIOCMD_RESTARTREAD:
		if (rpc->initial_buffer) {
			rpc->pos = 0;
			return CURLIOE_OK;
		}
450
		error("unable to rewind rpc post data - try increasing http.postBuffer");
451 452 453 454 455 456 457 458
		return CURLIOE_FAILRESTART;

	default:
		return CURLIOE_UNKNOWNCMD;
	}
}
#endif

459
static size_t rpc_in(char *ptr, size_t eltsize,
460 461 462 463
		size_t nmemb, void *buffer_)
{
	size_t size = eltsize * nmemb;
	struct rpc_state *rpc = buffer_;
464 465
	if (size)
		rpc->any_written = 1;
466 467 468 469
	write_or_die(rpc->in, ptr, size);
	return size;
}

470 471
static int run_slot(struct active_request_slot *slot,
		    struct slot_results *results)
472
{
473
	int err;
474
	struct slot_results results_buf;
475

476 477 478
	if (!results)
		results = &results_buf;

J
Jeff King 已提交
479
	err = run_one_slot(slot, results);
480

481
	if (err != HTTP_OK && err != HTTP_REAUTH) {
482 483 484 485 486 487 488 489 490 491 492 493 494 495
		struct strbuf msg = STRBUF_INIT;
		if (results->http_code && results->http_code != 200)
			strbuf_addf(&msg, "HTTP %ld", results->http_code);
		if (results->curl_result != CURLE_OK) {
			if (msg.len)
				strbuf_addch(&msg, ' ');
			strbuf_addf(&msg, "curl %d", results->curl_result);
			if (curl_errorstr[0]) {
				strbuf_addch(&msg, ' ');
				strbuf_addstr(&msg, curl_errorstr);
			}
		}
		error("RPC failed; %s", msg.buf);
		strbuf_release(&msg);
496 497 498 499 500
	}

	return err;
}

501
static int probe_rpc(struct rpc_state *rpc, struct slot_results *results)
502 503
{
	struct active_request_slot *slot;
504
	struct curl_slist *headers = http_copy_default_headers();
505 506 507 508 509 510 511 512 513 514 515
	struct strbuf buf = STRBUF_INIT;
	int err;

	slot = get_active_slot();

	headers = curl_slist_append(headers, rpc->hdr_content_type);
	headers = curl_slist_append(headers, rpc->hdr_accept);

	curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
	curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
	curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
516
	curl_easy_setopt(slot->curl, CURLOPT_ENCODING, NULL);
517 518 519 520 521 522
	curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, "0000");
	curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, 4);
	curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
	curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
	curl_easy_setopt(slot->curl, CURLOPT_FILE, &buf);

523
	err = run_slot(slot, results);
524 525 526 527 528 529

	curl_slist_free_all(headers);
	strbuf_release(&buf);
	return err;
}

530 531 532
static int post_rpc(struct rpc_state *rpc)
{
	struct active_request_slot *slot;
533
	struct curl_slist *headers = http_copy_default_headers();
S
Shawn O. Pearce 已提交
534 535
	int use_gzip = rpc->gzip_request;
	char *gzip_body = NULL;
536
	size_t gzip_size = 0;
537
	int err, large_request = 0;
538
	int needs_100_continue = 0;
539 540 541 542 543 544 545 546 547 548 549 550

	/* Try to load the entire request, if we can fit it into the
	 * allocated buffer space we can use HTTP/1.0 and avoid the
	 * chunked encoding mess.
	 */
	while (1) {
		size_t left = rpc->alloc - rpc->len;
		char *buf = rpc->buf + rpc->len;
		int n;

		if (left < LARGE_PACKET_MAX) {
			large_request = 1;
S
Shawn O. Pearce 已提交
551
			use_gzip = 0;
552 553 554
			break;
		}

555
		n = packet_read(rpc->out, NULL, NULL, buf, left, 0);
556 557 558 559 560
		if (!n)
			break;
		rpc->len += n;
	}

561
	if (large_request) {
562 563
		struct slot_results results;

564
		do {
565
			err = probe_rpc(rpc, &results);
566 567
			if (err == HTTP_REAUTH)
				credential_fill(&http_auth);
568 569 570
		} while (err == HTTP_REAUTH);
		if (err != HTTP_OK)
			return -1;
571 572 573

		if (results.auth_avail & CURLAUTH_GSSNEGOTIATE)
			needs_100_continue = 1;
574 575
	}

576 577
	headers = curl_slist_append(headers, rpc->hdr_content_type);
	headers = curl_slist_append(headers, rpc->hdr_accept);
578 579
	headers = curl_slist_append(headers, needs_100_continue ?
		"Expect: 100-continue" : "Expect:");
580 581

retry:
582 583 584
	slot = get_active_slot();

	curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
585
	curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
586
	curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
587
	curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "gzip");
588 589 590 591 592 593

	if (large_request) {
		/* The request body is large and the size cannot be predicted.
		 * We must use chunked encoding to send it.
		 */
		headers = curl_slist_append(headers, "Transfer-Encoding: chunked");
594
		rpc->initial_buffer = 1;
595 596
		curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, rpc_out);
		curl_easy_setopt(slot->curl, CURLOPT_INFILE, rpc);
597 598 599 600
#ifndef NO_CURL_IOCTL
		curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, rpc_ioctl);
		curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, rpc);
#endif
601 602 603 604 605
		if (options.verbosity > 1) {
			fprintf(stderr, "POST %s (chunked)\n", rpc->service_name);
			fflush(stderr);
		}

606 607 608 609 610 611 612 613 614
	} else if (gzip_body) {
		/*
		 * If we are looping to retry authentication, then the previous
		 * run will have set up the headers and gzip buffer already,
		 * and we just need to send it.
		 */
		curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
		curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, gzip_size);

S
Shawn O. Pearce 已提交
615 616 617 618 619
	} else if (use_gzip && 1024 < rpc->len) {
		/* The client backend isn't giving us compressed data so
		 * we can try to deflate it ourselves, this may save on.
		 * the transfer time.
		 */
620
		git_zstream stream;
S
Shawn O. Pearce 已提交
621 622
		int ret;

623
		git_deflate_init_gzip(&stream, Z_BEST_COMPRESSION);
624 625
		gzip_size = git_deflate_bound(&stream, rpc->len);
		gzip_body = xmalloc(gzip_size);
S
Shawn O. Pearce 已提交
626 627 628 629

		stream.next_in = (unsigned char *)rpc->buf;
		stream.avail_in = rpc->len;
		stream.next_out = (unsigned char *)gzip_body;
630
		stream.avail_out = gzip_size;
S
Shawn O. Pearce 已提交
631

632
		ret = git_deflate(&stream, Z_FINISH);
S
Shawn O. Pearce 已提交
633 634 635
		if (ret != Z_STREAM_END)
			die("cannot deflate request; zlib deflate error %d", ret);

636
		ret = git_deflate_end_gently(&stream);
S
Shawn O. Pearce 已提交
637 638 639
		if (ret != Z_OK)
			die("cannot deflate request; zlib end error %d", ret);

640
		gzip_size = stream.total_out;
S
Shawn O. Pearce 已提交
641 642 643

		headers = curl_slist_append(headers, "Content-Encoding: gzip");
		curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
644
		curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, gzip_size);
S
Shawn O. Pearce 已提交
645 646 647 648

		if (options.verbosity > 1) {
			fprintf(stderr, "POST %s (gzip %lu to %lu bytes)\n",
				rpc->service_name,
649
				(unsigned long)rpc->len, (unsigned long)gzip_size);
S
Shawn O. Pearce 已提交
650 651
			fflush(stderr);
		}
652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
	} else {
		/* We know the complete request size in advance, use the
		 * more normal Content-Length approach.
		 */
		curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, rpc->buf);
		curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, rpc->len);
		if (options.verbosity > 1) {
			fprintf(stderr, "POST %s (%lu bytes)\n",
				rpc->service_name, (unsigned long)rpc->len);
			fflush(stderr);
		}
	}

	curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
	curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, rpc_in);
	curl_easy_setopt(slot->curl, CURLOPT_FILE, rpc);

669 670

	rpc->any_written = 0;
671
	err = run_slot(slot, NULL);
672 673
	if (err == HTTP_REAUTH && !large_request) {
		credential_fill(&http_auth);
674
		goto retry;
675
	}
676 677
	if (err != HTTP_OK)
		err = -1;
678

679 680 681
	if (!rpc->any_written)
		err = -1;

682
	curl_slist_free_all(headers);
S
Shawn O. Pearce 已提交
683
	free(gzip_body);
684 685 686 687 688 689 690
	return err;
}

static int rpc_service(struct rpc_state *rpc, struct discovery *heads)
{
	const char *svc = rpc->service_name;
	struct strbuf buf = STRBUF_INIT;
691
	struct strbuf *preamble = rpc->stdin_preamble;
692
	struct child_process client = CHILD_PROCESS_INIT;
693 694 695 696 697 698 699 700
	int err = 0;

	client.in = -1;
	client.out = -1;
	client.git_cmd = 1;
	client.argv = rpc->argv;
	if (start_command(&client))
		exit(1);
701 702
	if (preamble)
		write_or_die(client.in, preamble->buf, preamble->len);
703 704 705 706 707 708 709 710 711
	if (heads)
		write_or_die(client.in, heads->buf, heads->len);

	rpc->alloc = http_post_buffer;
	rpc->buf = xmalloc(rpc->alloc);
	rpc->in = client.in;
	rpc->out = client.out;
	strbuf_init(&rpc->result, 0);

J
Jeff King 已提交
712
	strbuf_addf(&buf, "%s%s", url.buf, svc);
713 714 715 716 717
	rpc->service_url = strbuf_detach(&buf, NULL);

	strbuf_addf(&buf, "Content-Type: application/x-%s-request", svc);
	rpc->hdr_content_type = strbuf_detach(&buf, NULL);

718
	strbuf_addf(&buf, "Accept: application/x-%s-result", svc);
719 720 721
	rpc->hdr_accept = strbuf_detach(&buf, NULL);

	while (!err) {
722
		int n = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
723 724 725 726 727 728 729 730 731
		if (!n)
			break;
		rpc->pos = 0;
		rpc->len = n;
		err |= post_rpc(rpc);
	}

	close(client.in);
	client.in = -1;
732 733 734 735 736 737 738 739
	if (!err) {
		strbuf_read(&rpc->result, client.out, 0);
	} else {
		char buf[4096];
		for (;;)
			if (xread(client.out, buf, sizeof(buf)) <= 0)
				break;
	}
740 741

	close(client.out);
742 743 744 745 746 747 748 749 750 751 752
	client.out = -1;

	err |= finish_command(&client);
	free(rpc->service_url);
	free(rpc->hdr_content_type);
	free(rpc->hdr_accept);
	free(rpc->buf);
	strbuf_release(&buf);
	return err;
}

753 754
static int fetch_dumb(int nr_heads, struct ref **to_fetch)
{
755
	struct walker *walker;
J
Jeff King 已提交
756
	char **targets;
757 758
	int ret, i;

J
Jeff King 已提交
759
	ALLOC_ARRAY(targets, nr_heads);
760 761
	if (options.depth || options.deepen_since)
		die("dumb http transport does not support shallow capabilities");
762
	for (i = 0; i < nr_heads; i++)
763
		targets[i] = xstrdup(oid_to_hex(&to_fetch[i]->old_oid));
764

J
Jeff King 已提交
765
	walker = get_http_walker(url.buf);
766 767 768
	walker->get_all = 1;
	walker->get_tree = 1;
	walker->get_history = 1;
769
	walker->get_verbosely = options.verbosity >= 3;
770 771
	walker->get_recover = 0;
	ret = walker_fetch(walker, nr_heads, targets, NULL, NULL);
772
	walker_free(walker);
773 774 775 776 777

	for (i = 0; i < nr_heads; i++)
		free(targets[i]);
	free(targets);

778
	return ret ? error("fetch failed.") : 0;
779 780
}

781 782 783 784
static int fetch_git(struct discovery *heads,
	int nr_heads, struct ref **to_fetch)
{
	struct rpc_state rpc;
785
	struct strbuf preamble = STRBUF_INIT;
786 787 788 789 790
	int i, err;
	struct argv_array args = ARGV_ARRAY_INIT;

	argv_array_pushl(&args, "fetch-pack", "--stateless-rpc",
			 "--stdin", "--lock-pack", NULL);
791
	if (options.followtags)
792
		argv_array_push(&args, "--include-tag");
793
	if (options.thin)
794 795 796
		argv_array_push(&args, "--thin");
	if (options.verbosity >= 3)
		argv_array_pushl(&args, "-v", "-v", NULL);
797
	if (options.check_self_contained_and_connected)
798
		argv_array_push(&args, "--check-self-contained-and-connected");
799
	if (options.cloning)
800
		argv_array_push(&args, "--cloning");
801
	if (options.update_shallow)
802
		argv_array_push(&args, "--update-shallow");
803
	if (!options.progress)
804 805 806
		argv_array_push(&args, "--no-progress");
	if (options.depth)
		argv_array_pushf(&args, "--depth=%lu", options.depth);
807 808
	if (options.deepen_since)
		argv_array_pushf(&args, "--shallow-since=%s", options.deepen_since);
809 810 811
	for (i = 0; i < options.deepen_not.nr; i++)
		argv_array_pushf(&args, "--shallow-exclude=%s",
				 options.deepen_not.items[i].string);
812 813
	if (options.deepen_relative && options.depth)
		argv_array_push(&args, "--deepen-relative");
814
	argv_array_push(&args, url.buf);
815

816 817
	for (i = 0; i < nr_heads; i++) {
		struct ref *ref = to_fetch[i];
818
		if (!*ref->name)
819
			die("cannot fetch by sha1 over smart http");
820
		packet_buf_write(&preamble, "%s %s\n",
821
				 oid_to_hex(&ref->old_oid), ref->name);
822
	}
823
	packet_buf_flush(&preamble);
824 825 826

	memset(&rpc, 0, sizeof(rpc));
	rpc.service_name = "git-upload-pack",
827
	rpc.argv = args.argv;
828
	rpc.stdin_preamble = &preamble;
S
Shawn O. Pearce 已提交
829
	rpc.gzip_request = 1;
830 831 832

	err = rpc_service(&rpc, heads);
	if (rpc.result.len)
J
Jeff King 已提交
833
		write_or_die(1, rpc.result.buf, rpc.result.len);
834
	strbuf_release(&rpc.result);
835
	strbuf_release(&preamble);
836
	argv_array_clear(&args);
837 838 839 840 841
	return err;
}

static int fetch(int nr_heads, struct ref **to_fetch)
{
842
	struct discovery *d = discover_refs("git-upload-pack", 0);
843 844 845 846 847 848
	if (d->proto_git)
		return fetch_git(d, nr_heads, to_fetch);
	else
		return fetch_dumb(nr_heads, to_fetch);
}

849 850 851 852 853 854 855 856
static void parse_fetch(struct strbuf *buf)
{
	struct ref **to_fetch = NULL;
	struct ref *list_head = NULL;
	struct ref **list = &list_head;
	int alloc_heads = 0, nr_heads = 0;

	do {
857 858 859
		const char *p;
		if (skip_prefix(buf->buf, "fetch ", &p)) {
			const char *name;
860
			struct ref *ref;
861
			struct object_id old_oid;
862

863
			if (get_oid_hex(p, &old_oid))
864
				die("protocol error: expected sha/ref, got %s'", p);
865 866 867
			if (p[GIT_SHA1_HEXSZ] == ' ')
				name = p + GIT_SHA1_HEXSZ + 1;
			else if (!p[GIT_SHA1_HEXSZ])
868 869 870 871 872
				name = "";
			else
				die("protocol error: expected sha/ref, got %s'", p);

			ref = alloc_ref(name);
873
			oidcpy(&ref->old_oid, &old_oid);
874 875 876 877 878 879 880 881 882 883 884

			*list = ref;
			list = &ref->next;

			ALLOC_GROW(to_fetch, nr_heads + 1, alloc_heads);
			to_fetch[nr_heads++] = ref;
		}
		else
			die("http transport does not support %s", buf->buf);

		strbuf_reset(buf);
885
		if (strbuf_getline_lf(buf, stdin) == EOF)
886 887 888 889 890
			return;
		if (!*buf->buf)
			break;
	} while (1);

891
	if (fetch(nr_heads, to_fetch))
892 893 894 895 896 897 898 899 900
		exit(128); /* error already reported */
	free_refs(list_head);
	free(to_fetch);

	printf("\n");
	fflush(stdout);
	strbuf_reset(buf);
}

901 902
static int push_dav(int nr_spec, char **specs)
{
903 904
	struct child_process child = CHILD_PROCESS_INIT;
	size_t i;
905

906 907 908
	child.git_cmd = 1;
	argv_array_push(&child.args, "http-push");
	argv_array_push(&child.args, "--helper-status");
909
	if (options.dry_run)
910
		argv_array_push(&child.args, "--dry-run");
911
	if (options.verbosity > 1)
912 913
		argv_array_push(&child.args, "--verbose");
	argv_array_push(&child.args, url.buf);
914
	for (i = 0; i < nr_spec; i++)
915
		argv_array_push(&child.args, specs[i]);
916

917 918
	if (run_command(&child))
		die("git-http-push failed");
919 920 921
	return 0;
}

922 923 924
static int push_git(struct discovery *heads, int nr_spec, char **specs)
{
	struct rpc_state rpc;
J
Junio C Hamano 已提交
925 926
	int i, err;
	struct argv_array args;
927
	struct string_list_item *cas_option;
J
Jeff King 已提交
928
	struct strbuf preamble = STRBUF_INIT;
J
Junio C Hamano 已提交
929 930 931 932

	argv_array_init(&args);
	argv_array_pushl(&args, "send-pack", "--stateless-rpc", "--helper-status",
			 NULL);
933 934

	if (options.thin)
J
Junio C Hamano 已提交
935
		argv_array_push(&args, "--thin");
936
	if (options.dry_run)
J
Junio C Hamano 已提交
937
		argv_array_push(&args, "--dry-run");
938 939 940 941
	if (options.push_cert == SEND_PACK_PUSH_CERT_ALWAYS)
		argv_array_push(&args, "--signed=yes");
	else if (options.push_cert == SEND_PACK_PUSH_CERT_IF_ASKED)
		argv_array_push(&args, "--signed=if-asked");
942
	if (options.verbosity == 0)
J
Junio C Hamano 已提交
943
		argv_array_push(&args, "--quiet");
944
	else if (options.verbosity > 1)
J
Junio C Hamano 已提交
945 946
		argv_array_push(&args, "--verbose");
	argv_array_push(&args, options.progress ? "--progress" : "--no-progress");
947
	for_each_string_list_item(cas_option, &cas_options)
J
Junio C Hamano 已提交
948
		argv_array_push(&args, cas_option->string);
J
Jeff King 已提交
949
	argv_array_push(&args, url.buf);
J
Jeff King 已提交
950 951

	argv_array_push(&args, "--stdin");
952
	for (i = 0; i < nr_spec; i++)
J
Jeff King 已提交
953 954
		packet_buf_write(&preamble, "%s\n", specs[i]);
	packet_buf_flush(&preamble);
955 956 957

	memset(&rpc, 0, sizeof(rpc));
	rpc.service_name = "git-receive-pack",
J
Junio C Hamano 已提交
958
	rpc.argv = args.argv;
J
Jeff King 已提交
959
	rpc.stdin_preamble = &preamble;
960 961 962

	err = rpc_service(&rpc, heads);
	if (rpc.result.len)
J
Jeff King 已提交
963
		write_or_die(1, rpc.result.buf, rpc.result.len);
964
	strbuf_release(&rpc.result);
J
Jeff King 已提交
965
	strbuf_release(&preamble);
J
Junio C Hamano 已提交
966
	argv_array_clear(&args);
967 968 969 970 971
	return err;
}

static int push(int nr_spec, char **specs)
{
972
	struct discovery *heads = discover_refs("git-receive-pack", 1);
973 974 975 976 977 978 979 980 981 982
	int ret;

	if (heads->proto_git)
		ret = push_git(heads, nr_spec, specs);
	else
		ret = push_dav(nr_spec, specs);
	free_discovery(heads);
	return ret;
}

983 984 985
static void parse_push(struct strbuf *buf)
{
	char **specs = NULL;
986
	int alloc_spec = 0, nr_spec = 0, i, ret;
987 988

	do {
989
		if (starts_with(buf->buf, "push ")) {
990 991 992 993 994 995 996
			ALLOC_GROW(specs, nr_spec + 1, alloc_spec);
			specs[nr_spec++] = xstrdup(buf->buf + 5);
		}
		else
			die("http transport does not support %s", buf->buf);

		strbuf_reset(buf);
997
		if (strbuf_getline_lf(buf, stdin) == EOF)
J
Jim Meyering 已提交
998
			goto free_specs;
999 1000 1001 1002
		if (!*buf->buf)
			break;
	} while (1);

1003
	ret = push(nr_spec, specs);
1004 1005
	printf("\n");
	fflush(stdout);
J
Jim Meyering 已提交
1006

1007 1008 1009
	if (ret)
		exit(128); /* error already reported */

J
Jim Meyering 已提交
1010 1011 1012 1013
 free_specs:
	for (i = 0; i < nr_spec; i++)
		free(specs[i]);
	free(specs);
1014 1015
}

1016
int cmd_main(int argc, const char **argv)
1017 1018
{
	struct strbuf buf = STRBUF_INIT;
1019
	int nongit;
1020

1021
	setup_git_directory_gently(&nongit);
1022
	if (argc < 2) {
1023
		error("remote-curl: usage: git remote-curl <remote> [<url>]");
1024 1025 1026
		return 1;
	}

1027 1028
	options.verbosity = 1;
	options.progress = !!isatty(2);
1029
	options.thin = 1;
1030
	string_list_init(&options.deepen_not, 1);
1031

1032 1033 1034
	remote = remote_get(argv[1]);

	if (argc > 2) {
J
Jeff King 已提交
1035
		end_url_with_slash(&url, argv[2]);
1036
	} else {
J
Jeff King 已提交
1037
		end_url_with_slash(&url, remote->url[0]);
1038 1039
	}

J
Jeff King 已提交
1040
	http_init(remote, url.buf, 0);
1041

1042
	do {
1043 1044
		const char *arg;

1045
		if (strbuf_getline_lf(&buf, stdin) == EOF) {
1046
			if (ferror(stdin))
1047
				error("remote-curl: error reading command stream from git");
1048 1049 1050
			return 1;
		}
		if (buf.len == 0)
1051
			break;
1052
		if (starts_with(buf.buf, "fetch ")) {
1053
			if (nongit)
1054
				die("remote-curl: fetch attempted without a local repo");
1055 1056
			parse_fetch(&buf);

1057
		} else if (!strcmp(buf.buf, "list") || starts_with(buf.buf, "list ")) {
1058 1059
			int for_push = !!strstr(buf.buf + 4, "for-push");
			output_refs(get_refs(for_push));
1060

1061
		} else if (starts_with(buf.buf, "push ")) {
1062 1063
			parse_push(&buf);

1064 1065
		} else if (skip_prefix(buf.buf, "option ", &arg)) {
			char *value = strchr(arg, ' ');
1066 1067 1068 1069 1070 1071 1072
			int result;

			if (value)
				*value++ = '\0';
			else
				value = "true";

1073
			result = set_option(arg, value);
1074 1075 1076 1077 1078 1079
			if (!result)
				printf("ok\n");
			else if (result < 0)
				printf("error invalid value\n");
			else
				printf("unsupported\n");
1080
			fflush(stdout);
1081

1082 1083
		} else if (!strcmp(buf.buf, "capabilities")) {
			printf("fetch\n");
1084
			printf("option\n");
1085
			printf("push\n");
1086
			printf("check-connectivity\n");
1087 1088 1089
			printf("\n");
			fflush(stdout);
		} else {
1090
			error("remote-curl: unknown command '%s' from git", buf.buf);
1091 1092 1093 1094
			return 1;
		}
		strbuf_reset(&buf);
	} while (1);
1095 1096 1097

	http_cleanup();

1098 1099
	return 0;
}