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

19
static struct remote *remote;
J
Jeff King 已提交
20 21
/* always ends with a trailing slash */
static struct strbuf url = STRBUF_INIT;
22

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

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;
60
		return 0;
61 62 63 64 65 66 67
	}
	else if (!strcmp(name, "depth")) {
		char *end;
		unsigned long v = strtoul(value, &end, 10);
		if (value == end || *end)
			return -1;
		options.depth = v;
68
		return 0;
69
	}
70 71 72 73
	else if (!strcmp(name, "deepen-since")) {
		options.deepen_since = xstrdup(value);
		return 0;
	}
74 75 76 77
	else if (!strcmp(name, "deepen-not")) {
		string_list_append(&options.deepen_not, value);
		return 0;
	}
78 79 80 81 82 83 84 85 86
	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;
	}
87 88 89 90 91 92 93
	else if (!strcmp(name, "followtags")) {
		if (!strcmp(value, "true"))
			options.followtags = 1;
		else if (!strcmp(value, "false"))
			options.followtags = 0;
		else
			return -1;
94
		return 0;
95
	}
96 97 98 99 100 101 102 103 104
	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;
	}
105 106 107 108 109 110 111 112 113
	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;
	}
114 115 116 117 118 119
	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;
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
	} 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;
136 137
	} else if (!strcmp(name, "pushcert")) {
		if (!strcmp(value, "true"))
138
			options.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
139
		else if (!strcmp(value, "false"))
140 141 142
			options.push_cert = SEND_PACK_PUSH_CERT_NEVER;
		else if (!strcmp(value, "if-asked"))
			options.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
143 144 145
		else
			return -1;
		return 0;
146 147 148
	} else if (!strcmp(name, "push-option")) {
		string_list_append(&options.push_options, value);
		return 0;
149 150 151 152 153 154 155 156 157 158 159 160 161

#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 */
162
	} else {
163 164 165 166
		return 1 /* unsupported */;
	}
}

167
struct discovery {
168
	char *service;
169 170 171
	char *buf_alloc;
	char *buf;
	size_t len;
172
	struct ref *refs;
173
	struct oid_array shallow;
174 175 176 177
	unsigned proto_git : 1;
};
static struct discovery *last_discovery;

178 179 180
static struct ref *parse_git_refs(struct discovery *heads, int for_push)
{
	struct ref *list = NULL;
181 182 183 184 185 186 187
	struct packet_reader reader;

	packet_reader_init(&reader, -1, heads->buf, heads->len,
			   PACKET_READ_CHOMP_NEWLINE |
			   PACKET_READ_GENTLE_ON_EOF);

	switch (discover_version(&reader)) {
188 189 190
	case protocol_v2:
		die("support for protocol v2 not implemented yet");
		break;
191 192 193 194 195 196 197 198 199
	case protocol_v1:
	case protocol_v0:
		get_remote_heads(&reader, &list, for_push ? REF_NORMAL : 0,
				 NULL, &heads->shallow);
		break;
	case protocol_unknown_version:
		BUG("unknown protocol version");
	}

200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
	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 已提交
224 225
				die("%sinfo/refs not valid: is this a git repository?",
				    url.buf);
226 227
			data[i] = 0;
			ref_name = mid + 1;
228
			ref = alloc_ref(ref_name);
229
			get_oid_hex(start, &ref->old_oid);
230 231 232 233 234 235 236 237 238 239 240
			if (!refs)
				refs = ref;
			if (last_ref)
				last_ref->next = ref;
			last_ref = ref;
			start = NULL;
		}
		i++;
	}

	ref = alloc_ref("HEAD");
J
Jeff King 已提交
241
	if (!http_fetch_ref(url.buf, ref) &&
242 243 244 245 246 247 248 249 250 251
	    !resolve_remote_symref(ref, refs)) {
		ref->next = refs;
		refs = ref;
	} else {
		free(ref);
	}

	return refs;
}

252 253 254 255 256
static void free_discovery(struct discovery *d)
{
	if (d) {
		if (d == last_discovery)
			last_discovery = NULL;
257
		free(d->shallow.oid);
258
		free(d->buf_alloc);
259
		free_refs(d->refs);
260
		free(d->service);
261 262 263 264
		free(d);
	}
}

265 266
static int show_http_message(struct strbuf *type, struct strbuf *charset,
			     struct strbuf *msg)
267 268 269 270 271 272 273
{
	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.
	 */
274
	if (strcmp(type->buf, "text/plain"))
275
		return -1;
276 277
	if (charset->len)
		strbuf_reencode(msg, charset->buf, get_log_output_encoding());
278 279 280 281 282 283 284 285 286 287 288 289 290 291

	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;
}

292
static struct discovery *discover_refs(const char *service, int for_push)
293
{
294 295
	struct strbuf exp = STRBUF_INIT;
	struct strbuf type = STRBUF_INIT;
296
	struct strbuf charset = STRBUF_INIT;
297
	struct strbuf buffer = STRBUF_INIT;
J
Jeff King 已提交
298
	struct strbuf refs_url = STRBUF_INIT;
299
	struct strbuf effective_url = STRBUF_INIT;
300
	struct discovery *last = last_discovery;
J
Jeff King 已提交
301
	int http_ret, maybe_smart = 0;
302
	struct http_get_options http_options;
303

304 305 306
	if (last && !strcmp(service, last->service))
		return last;
	free_discovery(last);
307

J
Jeff King 已提交
308
	strbuf_addf(&refs_url, "%sinfo/refs", url.buf);
309
	if ((starts_with(url.buf, "http://") || starts_with(url.buf, "https://")) &&
310
	     git_env_bool("GIT_SMART_HTTP", 1)) {
J
Jeff King 已提交
311
		maybe_smart = 1;
J
Jeff King 已提交
312
		if (!strchr(url.buf, '?'))
J
Jeff King 已提交
313
			strbuf_addch(&refs_url, '?');
314
		else
J
Jeff King 已提交
315 316
			strbuf_addch(&refs_url, '&');
		strbuf_addf(&refs_url, "service=%s", service);
317
	}
318

319 320 321 322 323
	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 已提交
324
	http_options.initial_request = 1;
325 326
	http_options.no_cache = 1;
	http_options.keep_error = 1;
J
Jeff King 已提交
327

328
	http_ret = http_get_strbuf(refs_url.buf, &buffer, &http_options);
329 330 331 332
	switch (http_ret) {
	case HTTP_OK:
		break;
	case HTTP_MISSING_TARGET:
333
		show_http_message(&type, &charset, &buffer);
J
Jeff King 已提交
334
		die("repository '%s' not found", url.buf);
335
	case HTTP_NOAUTH:
336
		show_http_message(&type, &charset, &buffer);
J
Jeff King 已提交
337
		die("Authentication failed for '%s'", url.buf);
338
	default:
339
		show_http_message(&type, &charset, &buffer);
J
Jeff King 已提交
340
		die("unable to access '%s': %s", url.buf, curl_errorstr);
341 342
	}

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

346
	last= xcalloc(1, sizeof(*last_discovery));
347
	last->service = xstrdup(service);
348 349 350
	last->buf_alloc = strbuf_detach(&buffer, &last->len);
	last->buf = last->buf_alloc;

351 352 353 354
	strbuf_addf(&exp, "application/x-%s-advertisement", service);
	if (maybe_smart &&
	    (5 <= last->len && last->buf[4] == '#') &&
	    !strbuf_cmp(&exp, &type)) {
355 356
		char *line;

357 358
		/*
		 * smart HTTP response; validate that the service
359 360
		 * pkt-line matches our request.
		 */
361
		line = packet_read_line_buf(&last->buf, &last->len, NULL);
362

363
		strbuf_reset(&exp);
364
		strbuf_addf(&exp, "# service=%s", service);
365 366
		if (strcmp(line, exp.buf))
			die("invalid server response; got '%s'", line);
367 368 369 370 371 372
		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.
		 */
373 374
		while (packet_read_line_buf(&last->buf, &last->len, NULL))
			;
375 376 377 378

		last->proto_git = 1;
	}

379 380 381 382 383
	if (last->proto_git)
		last->refs = parse_git_refs(last, for_push);
	else
		last->refs = parse_info_refs(last);

J
Jeff King 已提交
384
	strbuf_release(&refs_url);
385 386
	strbuf_release(&exp);
	strbuf_release(&type);
387
	strbuf_release(&charset);
388
	strbuf_release(&effective_url);
389 390 391 392 393 394 395 396 397 398
	strbuf_release(&buffer);
	last_discovery = last;
	return last;
}

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

	if (for_push)
399
		heads = discover_refs("git-receive-pack", for_push);
400
	else
401
		heads = discover_refs("git-upload-pack", for_push);
402

403
	return heads->refs;
404 405
}

406 407 408 409 410 411 412
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
413
			printf("%s %s\n", oid_to_hex(&posn->old_oid), posn->name);
414 415 416 417 418
	}
	printf("\n");
	fflush(stdout);
}

419 420 421
struct rpc_state {
	const char *service_name;
	const char **argv;
422
	struct strbuf *stdin_preamble;
423 424 425 426 427 428 429 430 431
	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;
432
	int any_written;
433
	struct strbuf result;
S
Shawn O. Pearce 已提交
434
	unsigned gzip_request : 1;
435
	unsigned initial_buffer : 1;
436 437 438 439 440 441 442 443 444 445
};

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) {
446
		rpc->initial_buffer = 0;
447
		avail = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
448 449 450 451 452 453
		if (!avail)
			return 0;
		rpc->pos = 0;
		rpc->len = avail;
	}

T
Tay Ray Chuan 已提交
454
	if (max < avail)
455 456 457 458 459 460
		avail = max;
	memcpy(ptr, rpc->buf + rpc->pos, avail);
	rpc->pos += avail;
	return avail;
}

461
#ifndef NO_CURL_IOCTL
462
static curlioerr rpc_ioctl(CURL *handle, int cmd, void *clientp)
463 464 465 466 467 468 469 470 471 472 473 474
{
	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;
		}
475
		error("unable to rewind rpc post data - try increasing http.postBuffer");
476 477 478 479 480 481 482 483
		return CURLIOE_FAILRESTART;

	default:
		return CURLIOE_UNKNOWNCMD;
	}
}
#endif

484
static size_t rpc_in(char *ptr, size_t eltsize,
485 486 487 488
		size_t nmemb, void *buffer_)
{
	size_t size = eltsize * nmemb;
	struct rpc_state *rpc = buffer_;
489 490
	if (size)
		rpc->any_written = 1;
491 492 493 494
	write_or_die(rpc->in, ptr, size);
	return size;
}

495 496
static int run_slot(struct active_request_slot *slot,
		    struct slot_results *results)
497
{
498
	int err;
499
	struct slot_results results_buf;
500

501 502 503
	if (!results)
		results = &results_buf;

J
Jeff King 已提交
504
	err = run_one_slot(slot, results);
505

506
	if (err != HTTP_OK && err != HTTP_REAUTH) {
507 508 509 510 511 512 513 514 515 516 517 518 519 520
		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);
521 522 523 524 525
	}

	return err;
}

526
static int probe_rpc(struct rpc_state *rpc, struct slot_results *results)
527 528
{
	struct active_request_slot *slot;
529
	struct curl_slist *headers = http_copy_default_headers();
530 531 532 533 534 535 536 537 538 539 540
	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);
541
	curl_easy_setopt(slot->curl, CURLOPT_ENCODING, NULL);
542 543 544 545 546 547
	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);

548
	err = run_slot(slot, results);
549 550 551 552 553 554

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

555 556 557 558 559 560
static curl_off_t xcurl_off_t(ssize_t len) {
	if (len > maximum_signed_value_of_type(curl_off_t))
		die("cannot handle pushes this big");
	return (curl_off_t) len;
}

561 562 563
static int post_rpc(struct rpc_state *rpc)
{
	struct active_request_slot *slot;
564
	struct curl_slist *headers = http_copy_default_headers();
S
Shawn O. Pearce 已提交
565 566
	int use_gzip = rpc->gzip_request;
	char *gzip_body = NULL;
567
	size_t gzip_size = 0;
568
	int err, large_request = 0;
569
	int needs_100_continue = 0;
570 571 572 573 574 575 576 577 578 579 580 581

	/* 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 已提交
582
			use_gzip = 0;
583 584 585
			break;
		}

586
		n = packet_read(rpc->out, NULL, NULL, buf, left, 0);
587 588 589 590 591
		if (!n)
			break;
		rpc->len += n;
	}

592
	if (large_request) {
593 594
		struct slot_results results;

595
		do {
596
			err = probe_rpc(rpc, &results);
597 598
			if (err == HTTP_REAUTH)
				credential_fill(&http_auth);
599 600 601
		} while (err == HTTP_REAUTH);
		if (err != HTTP_OK)
			return -1;
602 603 604

		if (results.auth_avail & CURLAUTH_GSSNEGOTIATE)
			needs_100_continue = 1;
605 606
	}

607 608
	headers = curl_slist_append(headers, rpc->hdr_content_type);
	headers = curl_slist_append(headers, rpc->hdr_accept);
609 610
	headers = curl_slist_append(headers, needs_100_continue ?
		"Expect: 100-continue" : "Expect:");
611 612

retry:
613 614 615
	slot = get_active_slot();

	curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
616
	curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
617
	curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
618
	curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "gzip");
619 620 621 622 623 624

	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");
625
		rpc->initial_buffer = 1;
626 627
		curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, rpc_out);
		curl_easy_setopt(slot->curl, CURLOPT_INFILE, rpc);
628 629 630 631
#ifndef NO_CURL_IOCTL
		curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, rpc_ioctl);
		curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, rpc);
#endif
632 633 634 635 636
		if (options.verbosity > 1) {
			fprintf(stderr, "POST %s (chunked)\n", rpc->service_name);
			fflush(stderr);
		}

637 638 639 640 641 642 643
	} 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);
644
		curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(gzip_size));
645

S
Shawn O. Pearce 已提交
646 647 648 649 650
	} 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.
		 */
651
		git_zstream stream;
S
Shawn O. Pearce 已提交
652 653
		int ret;

654
		git_deflate_init_gzip(&stream, Z_BEST_COMPRESSION);
655 656
		gzip_size = git_deflate_bound(&stream, rpc->len);
		gzip_body = xmalloc(gzip_size);
S
Shawn O. Pearce 已提交
657 658 659 660

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

663
		ret = git_deflate(&stream, Z_FINISH);
S
Shawn O. Pearce 已提交
664 665 666
		if (ret != Z_STREAM_END)
			die("cannot deflate request; zlib deflate error %d", ret);

667
		ret = git_deflate_end_gently(&stream);
S
Shawn O. Pearce 已提交
668 669 670
		if (ret != Z_OK)
			die("cannot deflate request; zlib end error %d", ret);

671
		gzip_size = stream.total_out;
S
Shawn O. Pearce 已提交
672 673 674

		headers = curl_slist_append(headers, "Content-Encoding: gzip");
		curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
675
		curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(gzip_size));
S
Shawn O. Pearce 已提交
676 677 678 679

		if (options.verbosity > 1) {
			fprintf(stderr, "POST %s (gzip %lu to %lu bytes)\n",
				rpc->service_name,
680
				(unsigned long)rpc->len, (unsigned long)gzip_size);
S
Shawn O. Pearce 已提交
681 682
			fflush(stderr);
		}
683 684 685 686 687
	} 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);
688
		curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(rpc->len));
689 690 691 692 693 694 695 696 697 698 699
		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);

700 701

	rpc->any_written = 0;
702
	err = run_slot(slot, NULL);
703 704
	if (err == HTTP_REAUTH && !large_request) {
		credential_fill(&http_auth);
705
		goto retry;
706
	}
707 708
	if (err != HTTP_OK)
		err = -1;
709

710 711 712
	if (!rpc->any_written)
		err = -1;

713
	curl_slist_free_all(headers);
S
Shawn O. Pearce 已提交
714
	free(gzip_body);
715 716 717 718 719 720 721
	return err;
}

static int rpc_service(struct rpc_state *rpc, struct discovery *heads)
{
	const char *svc = rpc->service_name;
	struct strbuf buf = STRBUF_INIT;
722
	struct strbuf *preamble = rpc->stdin_preamble;
723
	struct child_process client = CHILD_PROCESS_INIT;
724 725 726 727 728 729 730 731
	int err = 0;

	client.in = -1;
	client.out = -1;
	client.git_cmd = 1;
	client.argv = rpc->argv;
	if (start_command(&client))
		exit(1);
732 733
	if (preamble)
		write_or_die(client.in, preamble->buf, preamble->len);
734 735 736 737 738 739 740 741 742
	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 已提交
743
	strbuf_addf(&buf, "%s%s", url.buf, svc);
744 745 746 747 748
	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);

749
	strbuf_addf(&buf, "Accept: application/x-%s-result", svc);
750 751 752
	rpc->hdr_accept = strbuf_detach(&buf, NULL);

	while (!err) {
753
		int n = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
754 755 756 757 758 759 760 761 762
		if (!n)
			break;
		rpc->pos = 0;
		rpc->len = n;
		err |= post_rpc(rpc);
	}

	close(client.in);
	client.in = -1;
763 764 765 766 767 768 769 770
	if (!err) {
		strbuf_read(&rpc->result, client.out, 0);
	} else {
		char buf[4096];
		for (;;)
			if (xread(client.out, buf, sizeof(buf)) <= 0)
				break;
	}
771 772

	close(client.out);
773 774 775 776 777 778 779 780 781 782 783
	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;
}

784 785
static int fetch_dumb(int nr_heads, struct ref **to_fetch)
{
786
	struct walker *walker;
J
Jeff King 已提交
787
	char **targets;
788 789
	int ret, i;

J
Jeff King 已提交
790
	ALLOC_ARRAY(targets, nr_heads);
791 792
	if (options.depth || options.deepen_since)
		die("dumb http transport does not support shallow capabilities");
793
	for (i = 0; i < nr_heads; i++)
794
		targets[i] = xstrdup(oid_to_hex(&to_fetch[i]->old_oid));
795

J
Jeff King 已提交
796
	walker = get_http_walker(url.buf);
797 798 799
	walker->get_all = 1;
	walker->get_tree = 1;
	walker->get_history = 1;
800
	walker->get_verbosely = options.verbosity >= 3;
801 802
	walker->get_recover = 0;
	ret = walker_fetch(walker, nr_heads, targets, NULL, NULL);
803
	walker_free(walker);
804 805 806 807 808

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

809
	return ret ? error("fetch failed.") : 0;
810 811
}

812 813 814 815
static int fetch_git(struct discovery *heads,
	int nr_heads, struct ref **to_fetch)
{
	struct rpc_state rpc;
816
	struct strbuf preamble = STRBUF_INIT;
817 818 819 820 821
	int i, err;
	struct argv_array args = ARGV_ARRAY_INIT;

	argv_array_pushl(&args, "fetch-pack", "--stateless-rpc",
			 "--stdin", "--lock-pack", NULL);
822
	if (options.followtags)
823
		argv_array_push(&args, "--include-tag");
824
	if (options.thin)
825 826 827
		argv_array_push(&args, "--thin");
	if (options.verbosity >= 3)
		argv_array_pushl(&args, "-v", "-v", NULL);
828
	if (options.check_self_contained_and_connected)
829
		argv_array_push(&args, "--check-self-contained-and-connected");
830
	if (options.cloning)
831
		argv_array_push(&args, "--cloning");
832
	if (options.update_shallow)
833
		argv_array_push(&args, "--update-shallow");
834
	if (!options.progress)
835 836 837
		argv_array_push(&args, "--no-progress");
	if (options.depth)
		argv_array_pushf(&args, "--depth=%lu", options.depth);
838 839
	if (options.deepen_since)
		argv_array_pushf(&args, "--shallow-since=%s", options.deepen_since);
840 841 842
	for (i = 0; i < options.deepen_not.nr; i++)
		argv_array_pushf(&args, "--shallow-exclude=%s",
				 options.deepen_not.items[i].string);
843 844
	if (options.deepen_relative && options.depth)
		argv_array_push(&args, "--deepen-relative");
845
	argv_array_push(&args, url.buf);
846

847 848
	for (i = 0; i < nr_heads; i++) {
		struct ref *ref = to_fetch[i];
849
		if (!*ref->name)
850
			die("cannot fetch by sha1 over smart http");
851
		packet_buf_write(&preamble, "%s %s\n",
852
				 oid_to_hex(&ref->old_oid), ref->name);
853
	}
854
	packet_buf_flush(&preamble);
855 856 857

	memset(&rpc, 0, sizeof(rpc));
	rpc.service_name = "git-upload-pack",
858
	rpc.argv = args.argv;
859
	rpc.stdin_preamble = &preamble;
S
Shawn O. Pearce 已提交
860
	rpc.gzip_request = 1;
861 862 863

	err = rpc_service(&rpc, heads);
	if (rpc.result.len)
J
Jeff King 已提交
864
		write_or_die(1, rpc.result.buf, rpc.result.len);
865
	strbuf_release(&rpc.result);
866
	strbuf_release(&preamble);
867
	argv_array_clear(&args);
868 869 870 871 872
	return err;
}

static int fetch(int nr_heads, struct ref **to_fetch)
{
873
	struct discovery *d = discover_refs("git-upload-pack", 0);
874 875 876 877 878 879
	if (d->proto_git)
		return fetch_git(d, nr_heads, to_fetch);
	else
		return fetch_dumb(nr_heads, to_fetch);
}

880 881 882 883 884 885 886 887
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 {
888 889 890
		const char *p;
		if (skip_prefix(buf->buf, "fetch ", &p)) {
			const char *name;
891
			struct ref *ref;
892
			struct object_id old_oid;
893

894
			if (get_oid_hex(p, &old_oid))
895
				die("protocol error: expected sha/ref, got %s'", p);
896 897 898
			if (p[GIT_SHA1_HEXSZ] == ' ')
				name = p + GIT_SHA1_HEXSZ + 1;
			else if (!p[GIT_SHA1_HEXSZ])
899 900 901 902 903
				name = "";
			else
				die("protocol error: expected sha/ref, got %s'", p);

			ref = alloc_ref(name);
904
			oidcpy(&ref->old_oid, &old_oid);
905 906 907 908 909 910 911 912 913 914 915

			*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);
916
		if (strbuf_getline_lf(buf, stdin) == EOF)
917 918 919 920 921
			return;
		if (!*buf->buf)
			break;
	} while (1);

922
	if (fetch(nr_heads, to_fetch))
923 924 925 926 927 928 929 930 931
		exit(128); /* error already reported */
	free_refs(list_head);
	free(to_fetch);

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

932 933
static int push_dav(int nr_spec, char **specs)
{
934 935
	struct child_process child = CHILD_PROCESS_INIT;
	size_t i;
936

937 938 939
	child.git_cmd = 1;
	argv_array_push(&child.args, "http-push");
	argv_array_push(&child.args, "--helper-status");
940
	if (options.dry_run)
941
		argv_array_push(&child.args, "--dry-run");
942
	if (options.verbosity > 1)
943 944
		argv_array_push(&child.args, "--verbose");
	argv_array_push(&child.args, url.buf);
945
	for (i = 0; i < nr_spec; i++)
946
		argv_array_push(&child.args, specs[i]);
947

948 949
	if (run_command(&child))
		die("git-http-push failed");
950 951 952
	return 0;
}

953 954 955
static int push_git(struct discovery *heads, int nr_spec, char **specs)
{
	struct rpc_state rpc;
J
Junio C Hamano 已提交
956 957
	int i, err;
	struct argv_array args;
958
	struct string_list_item *cas_option;
J
Jeff King 已提交
959
	struct strbuf preamble = STRBUF_INIT;
J
Junio C Hamano 已提交
960 961 962 963

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

	if (options.thin)
J
Junio C Hamano 已提交
966
		argv_array_push(&args, "--thin");
967
	if (options.dry_run)
J
Junio C Hamano 已提交
968
		argv_array_push(&args, "--dry-run");
969 970 971 972
	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");
973
	if (options.verbosity == 0)
J
Junio C Hamano 已提交
974
		argv_array_push(&args, "--quiet");
975
	else if (options.verbosity > 1)
J
Junio C Hamano 已提交
976
		argv_array_push(&args, "--verbose");
977 978 979
	for (i = 0; i < options.push_options.nr; i++)
		argv_array_pushf(&args, "--push-option=%s",
				 options.push_options.items[i].string);
J
Junio C Hamano 已提交
980
	argv_array_push(&args, options.progress ? "--progress" : "--no-progress");
981
	for_each_string_list_item(cas_option, &cas_options)
J
Junio C Hamano 已提交
982
		argv_array_push(&args, cas_option->string);
J
Jeff King 已提交
983
	argv_array_push(&args, url.buf);
J
Jeff King 已提交
984 985

	argv_array_push(&args, "--stdin");
986
	for (i = 0; i < nr_spec; i++)
J
Jeff King 已提交
987 988
		packet_buf_write(&preamble, "%s\n", specs[i]);
	packet_buf_flush(&preamble);
989 990 991

	memset(&rpc, 0, sizeof(rpc));
	rpc.service_name = "git-receive-pack",
J
Junio C Hamano 已提交
992
	rpc.argv = args.argv;
J
Jeff King 已提交
993
	rpc.stdin_preamble = &preamble;
994 995 996

	err = rpc_service(&rpc, heads);
	if (rpc.result.len)
J
Jeff King 已提交
997
		write_or_die(1, rpc.result.buf, rpc.result.len);
998
	strbuf_release(&rpc.result);
J
Jeff King 已提交
999
	strbuf_release(&preamble);
J
Junio C Hamano 已提交
1000
	argv_array_clear(&args);
1001 1002 1003 1004 1005
	return err;
}

static int push(int nr_spec, char **specs)
{
1006
	struct discovery *heads = discover_refs("git-receive-pack", 1);
1007 1008 1009 1010 1011 1012 1013 1014 1015 1016
	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;
}

1017 1018 1019
static void parse_push(struct strbuf *buf)
{
	char **specs = NULL;
1020
	int alloc_spec = 0, nr_spec = 0, i, ret;
1021 1022

	do {
1023
		if (starts_with(buf->buf, "push ")) {
1024 1025 1026 1027 1028 1029 1030
			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);
1031
		if (strbuf_getline_lf(buf, stdin) == EOF)
J
Jim Meyering 已提交
1032
			goto free_specs;
1033 1034 1035 1036
		if (!*buf->buf)
			break;
	} while (1);

1037
	ret = push(nr_spec, specs);
1038 1039
	printf("\n");
	fflush(stdout);
J
Jim Meyering 已提交
1040

1041 1042 1043
	if (ret)
		exit(128); /* error already reported */

J
Jim Meyering 已提交
1044 1045 1046 1047
 free_specs:
	for (i = 0; i < nr_spec; i++)
		free(specs[i]);
	free(specs);
1048 1049
}

1050
int cmd_main(int argc, const char **argv)
1051 1052
{
	struct strbuf buf = STRBUF_INIT;
1053
	int nongit;
1054

1055
	setup_git_directory_gently(&nongit);
1056
	if (argc < 2) {
1057
		error("remote-curl: usage: git remote-curl <remote> [<url>]");
1058 1059 1060
		return 1;
	}

1061 1062
	options.verbosity = 1;
	options.progress = !!isatty(2);
1063
	options.thin = 1;
1064
	string_list_init(&options.deepen_not, 1);
1065
	string_list_init(&options.push_options, 1);
1066

1067 1068 1069
	remote = remote_get(argv[1]);

	if (argc > 2) {
J
Jeff King 已提交
1070
		end_url_with_slash(&url, argv[2]);
1071
	} else {
J
Jeff King 已提交
1072
		end_url_with_slash(&url, remote->url[0]);
1073 1074
	}

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

1077
	do {
1078 1079
		const char *arg;

1080
		if (strbuf_getline_lf(&buf, stdin) == EOF) {
1081
			if (ferror(stdin))
1082
				error("remote-curl: error reading command stream from git");
1083 1084 1085
			return 1;
		}
		if (buf.len == 0)
1086
			break;
1087
		if (starts_with(buf.buf, "fetch ")) {
1088
			if (nongit)
1089
				die("remote-curl: fetch attempted without a local repo");
1090 1091
			parse_fetch(&buf);

1092
		} else if (!strcmp(buf.buf, "list") || starts_with(buf.buf, "list ")) {
1093 1094
			int for_push = !!strstr(buf.buf + 4, "for-push");
			output_refs(get_refs(for_push));
1095

1096
		} else if (starts_with(buf.buf, "push ")) {
1097 1098
			parse_push(&buf);

1099 1100
		} else if (skip_prefix(buf.buf, "option ", &arg)) {
			char *value = strchr(arg, ' ');
1101 1102 1103 1104 1105 1106 1107
			int result;

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

1108
			result = set_option(arg, value);
1109 1110 1111 1112 1113 1114
			if (!result)
				printf("ok\n");
			else if (result < 0)
				printf("error invalid value\n");
			else
				printf("unsupported\n");
1115
			fflush(stdout);
1116

1117 1118
		} else if (!strcmp(buf.buf, "capabilities")) {
			printf("fetch\n");
1119
			printf("option\n");
1120
			printf("push\n");
1121
			printf("check-connectivity\n");
1122 1123 1124
			printf("\n");
			fflush(stdout);
		} else {
1125
			error("remote-curl: unknown command '%s' from git", buf.buf);
1126 1127 1128 1129
			return 1;
		}
		strbuf_reset(&buf);
	} while (1);
1130 1131 1132

	http_cleanup();

1133 1134
	return 0;
}