blob: 90d565c8c5421eee7f929059e8e465ddfbe78dc2 [file] [log] [blame]
Daniel Barkalowa2d725b2009-08-05 01:01:56 -04001#include "cache.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07002#include "config.h"
Daniel Barkalowa2d725b2009-08-05 01:01:56 -04003#include "remote.h"
Brandon Williamsad6ac122018-03-14 11:31:45 -07004#include "connect.h"
Daniel Barkalowa2d725b2009-08-05 01:01:56 -04005#include "strbuf.h"
6#include "walker.h"
7#include "http.h"
Stefan Bellerd807c4a2018-04-10 14:26:18 -07008#include "exec-cmd.h"
Shawn O. Pearceae4efe12009-10-30 17:47:30 -07009#include "run-command.h"
Shawn O. Pearce97cc7bc2009-10-30 17:47:40 -070010#include "pkt-line.h"
Junio C Hamano05c1eb12013-08-02 15:14:50 -070011#include "string-list.h"
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -070012#include "sideband.h"
Junio C Hamano222b1212013-07-08 22:16:31 -070013#include "argv-array.h"
Jeff King2501aff2013-09-28 04:31:45 -040014#include "credential.h"
Nguyễn Thái Ngọc Duy16094882013-12-05 20:02:50 +070015#include "sha1-array.h"
Dave Borowitz30261092015-08-19 11:26:46 -040016#include "send-pack.h"
Brandon Williamsad6ac122018-03-14 11:31:45 -070017#include "protocol.h"
Jeff King90dce212018-02-19 14:50:14 -050018#include "quote.h"
Daniel Barkalowa2d725b2009-08-05 01:01:56 -040019
Shawn O. Pearce37a87682009-10-30 17:47:26 -070020static struct remote *remote;
Jeff Kingb227bbc2013-09-28 04:35:25 -040021/* always ends with a trailing slash */
22static struct strbuf url = STRBUF_INIT;
Shawn O. Pearce37a87682009-10-30 17:47:26 -070023
Shawn O. Pearceef08ef92009-10-30 17:47:29 -070024struct options {
25 int verbosity;
26 unsigned long depth;
Nguyễn Thái Ngọc Duy508ea882016-06-12 17:53:59 +070027 char *deepen_since;
Nguyễn Thái Ngọc Duya45a2602016-06-12 17:54:04 +070028 struct string_list deepen_not;
Brandon Williams511155d2017-03-22 15:22:00 -070029 struct string_list push_options;
Jeff Hostetleracb0c572017-12-08 15:58:44 +000030 char *filter;
Shawn O. Pearceef08ef92009-10-30 17:47:29 -070031 unsigned progress : 1,
Nguyễn Thái Ngọc Duy9ba38042013-07-21 15:18:05 +070032 check_self_contained_and_connected : 1,
Nguyễn Thái Ngọc Duy16094882013-12-05 20:02:50 +070033 cloning : 1,
34 update_shallow : 1,
Shawn O. Pearceae4efe12009-10-30 17:47:30 -070035 followtags : 1,
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -070036 dry_run : 1,
Junio C Hamano0ea47f92014-09-15 14:59:00 -070037 thin : 1,
Dave Borowitz30261092015-08-19 11:26:46 -040038 /* One of the SEND_PACK_PUSH_CERT_* constants. */
Nguyễn Thái Ngọc Duycccf74e2016-06-12 17:54:09 +070039 push_cert : 2,
Jonathan Tan88e2f9e2017-12-05 16:58:49 +000040 deepen_relative : 1,
41 from_promisor : 1,
42 no_dependents : 1;
Shawn O. Pearceef08ef92009-10-30 17:47:29 -070043};
44static struct options options;
Junio C Hamano05c1eb12013-08-02 15:14:50 -070045static struct string_list cas_options = STRING_LIST_INIT_DUP;
Shawn O. Pearceef08ef92009-10-30 17:47:29 -070046
Shawn O. Pearceef08ef92009-10-30 17:47:29 -070047static 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;
Shawn O. Pearce249b2002009-10-30 17:47:42 -070064 return 0;
Shawn O. Pearceef08ef92009-10-30 17:47:29 -070065 }
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;
Shawn O. Pearce249b2002009-10-30 17:47:42 -070072 return 0;
Shawn O. Pearceef08ef92009-10-30 17:47:29 -070073 }
Nguyễn Thái Ngọc Duy508ea882016-06-12 17:53:59 +070074 else if (!strcmp(name, "deepen-since")) {
75 options.deepen_since = xstrdup(value);
76 return 0;
77 }
Nguyễn Thái Ngọc Duya45a2602016-06-12 17:54:04 +070078 else if (!strcmp(name, "deepen-not")) {
79 string_list_append(&options.deepen_not, value);
80 return 0;
81 }
Nguyễn Thái Ngọc Duycccf74e2016-06-12 17:54:09 +070082 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 }
Shawn O. Pearceef08ef92009-10-30 17:47:29 -070091 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;
Shawn O. Pearce249b2002009-10-30 17:47:42 -070098 return 0;
Shawn O. Pearceef08ef92009-10-30 17:47:29 -070099 }
Shawn O. Pearceae4efe12009-10-30 17:47:30 -0700100 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 }
Nguyễn Thái Ngọc Duy9ba38042013-07-21 15:18:05 +0700109 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 }
Junio C Hamano05c1eb12013-08-02 15:14:50 -0700118 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;
Nguyễn Thái Ngọc Duy16094882013-12-05 20:02:50 +0700124 } 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;
Junio C Hamano0ea47f92014-09-15 14:59:00 -0700140 } else if (!strcmp(name, "pushcert")) {
141 if (!strcmp(value, "true"))
Dave Borowitz30261092015-08-19 11:26:46 -0400142 options.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
Junio C Hamano0ea47f92014-09-15 14:59:00 -0700143 else if (!strcmp(value, "false"))
Dave Borowitz30261092015-08-19 11:26:46 -0400144 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;
Junio C Hamano0ea47f92014-09-15 14:59:00 -0700147 else
148 return -1;
149 return 0;
Brandon Williams511155d2017-03-22 15:22:00 -0700150 } else if (!strcmp(name, "push-option")) {
Jeff King90dce212018-02-19 14:50:14 -0500151 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 }
Brandon Williams511155d2017-03-22 15:22:00 -0700160 return 0;
Eric Wongc915f112016-02-03 04:09:14 +0000161
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 */
Jonathan Tan88e2f9e2017-12-05 16:58:49 +0000174 } 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;
Jeff Hostetleracb0c572017-12-08 15:58:44 +0000180 } else if (!strcmp(name, "filter")) {
Elijah Newrenc3b9bc92018-09-05 10:03:07 -0700181 options.filter = xstrdup(value);
Jeff Hostetleracb0c572017-12-08 15:58:44 +0000182 return 0;
Nguyễn Thái Ngọc Duy16094882013-12-05 20:02:50 +0700183 } else {
Shawn O. Pearceef08ef92009-10-30 17:47:29 -0700184 return 1 /* unsupported */;
185 }
186}
187
Shawn O. Pearce97cc7bc2009-10-30 17:47:40 -0700188struct discovery {
Brandon Williamsf08a5d42018-03-15 10:31:36 -0700189 char *service;
Shawn O. Pearce97cc7bc2009-10-30 17:47:40 -0700190 char *buf_alloc;
191 char *buf;
192 size_t len;
Jeff King2a455202013-02-20 15:07:19 -0500193 struct ref *refs;
brian m. carlson910650d2017-03-31 01:40:00 +0000194 struct oid_array shallow;
Brandon Williams49e85e92018-03-15 10:31:37 -0700195 enum protocol_version version;
Shawn O. Pearce97cc7bc2009-10-30 17:47:40 -0700196 unsigned proto_git : 1;
197};
198static struct discovery *last_discovery;
199
Jeff Kingb8054bb2013-02-20 15:07:11 -0500200static struct ref *parse_git_refs(struct discovery *heads, int for_push)
201{
202 struct ref *list = NULL;
Brandon Williamsad6ac122018-03-14 11:31:45 -0700203 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
Brandon Williams49e85e92018-03-15 10:31:37 -0700209 heads->version = discover_version(&reader);
210 switch (heads->version) {
Brandon Williams8f6982b2018-03-14 11:31:47 -0700211 case protocol_v2:
Brandon Williams0f1dc532018-03-15 10:31:41 -0700212 /*
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 */
Brandon Williams8f6982b2018-03-14 11:31:47 -0700218 break;
Brandon Williamsad6ac122018-03-14 11:31:45 -0700219 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
Jeff Kingb8054bb2013-02-20 15:07:11 -0500228 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)
Jeff Kingb227bbc2013-09-28 04:35:25 -0400252 die("%sinfo/refs not valid: is this a git repository?",
253 url.buf);
Jeff Kingb8054bb2013-02-20 15:07:11 -0500254 data[i] = 0;
255 ref_name = mid + 1;
Jeff King6f687c22015-09-24 17:08:09 -0400256 ref = alloc_ref(ref_name);
brian m. carlsonf4e54d02015-11-10 02:22:20 +0000257 get_oid_hex(start, &ref->old_oid);
Jeff Kingb8054bb2013-02-20 15:07:11 -0500258 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");
Jeff Kingb227bbc2013-09-28 04:35:25 -0400269 if (!http_fetch_ref(url.buf, ref) &&
Jeff Kingb8054bb2013-02-20 15:07:11 -0500270 !resolve_remote_symref(ref, refs)) {
271 ref->next = refs;
272 refs = ref;
273 } else {
274 free(ref);
275 }
276
277 return refs;
278}
279
Shawn O. Pearce97cc7bc2009-10-30 17:47:40 -0700280static void free_discovery(struct discovery *d)
281{
282 if (d) {
283 if (d == last_discovery)
284 last_discovery = NULL;
brian m. carlsonee3051b2017-03-26 16:01:37 +0000285 free(d->shallow.oid);
Shawn O. Pearce97cc7bc2009-10-30 17:47:40 -0700286 free(d->buf_alloc);
Jeff King2a455202013-02-20 15:07:19 -0500287 free_refs(d->refs);
Brandon Williamsf08a5d42018-03-15 10:31:36 -0700288 free(d->service);
Shawn O. Pearce97cc7bc2009-10-30 17:47:40 -0700289 free(d);
290 }
291}
292
Jeff Kingfc1b7742014-05-22 05:30:29 -0400293static int show_http_message(struct strbuf *type, struct strbuf *charset,
294 struct strbuf *msg)
Jeff King426e70d2013-04-05 18:17:23 -0400295{
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.
Jeff King426e70d2013-04-05 18:17:23 -0400301 */
Jeff Kingbf197fd2014-05-22 05:29:47 -0400302 if (strcmp(type->buf, "text/plain"))
Jeff King426e70d2013-04-05 18:17:23 -0400303 return -1;
Jeff Kingfc1b7742014-05-22 05:30:29 -0400304 if (charset->len)
305 strbuf_reencode(msg, charset->buf, get_log_output_encoding());
Jeff King426e70d2013-04-05 18:17:23 -0400306
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
Brandon Williams884e5862018-03-15 10:31:39 -0700320static 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
David Aguilar24d36f12014-08-31 13:11:31 -0700333static struct discovery *discover_refs(const char *service, int for_push)
Daniel Barkalowa2d725b2009-08-05 01:01:56 -0400334{
Shawn Pearce4656bf42013-01-31 13:02:07 -0800335 struct strbuf exp = STRBUF_INIT;
336 struct strbuf type = STRBUF_INIT;
Jeff Kingfc1b7742014-05-22 05:30:29 -0400337 struct strbuf charset = STRBUF_INIT;
Daniel Barkalowa2d725b2009-08-05 01:01:56 -0400338 struct strbuf buffer = STRBUF_INIT;
Jeff Kingc65d5692013-09-28 04:35:10 -0400339 struct strbuf refs_url = STRBUF_INIT;
Jeff King050ef362013-09-28 04:35:35 -0400340 struct strbuf effective_url = STRBUF_INIT;
Brandon Williams884e5862018-03-15 10:31:39 -0700341 struct strbuf protocol_header = STRBUF_INIT;
342 struct string_list extra_headers = STRING_LIST_INIT_DUP;
Shawn O. Pearce97cc7bc2009-10-30 17:47:40 -0700343 struct discovery *last = last_discovery;
Jeff King243c3292012-09-20 13:00:22 -0400344 int http_ret, maybe_smart = 0;
Jeff Kingfcaa6e62016-12-06 13:24:38 -0500345 struct http_get_options http_options;
Brandon Williamsa4d78ce2018-03-15 10:31:42 -0700346 enum protocol_version version = get_protocol_version_config();
Daniel Barkalowa2d725b2009-08-05 01:01:56 -0400347
Shawn O. Pearce97cc7bc2009-10-30 17:47:40 -0700348 if (last && !strcmp(service, last->service))
349 return last;
350 free_discovery(last);
Daniel Barkalowa2d725b2009-08-05 01:01:56 -0400351
Jeff Kingb227bbc2013-09-28 04:35:25 -0400352 strbuf_addf(&refs_url, "%sinfo/refs", url.buf);
Christian Couder59556542013-11-30 21:55:40 +0100353 if ((starts_with(url.buf, "http://") || starts_with(url.buf, "https://")) &&
Jeff King02572c22012-09-20 17:30:58 -0400354 git_env_bool("GIT_SMART_HTTP", 1)) {
Jeff King243c3292012-09-20 13:00:22 -0400355 maybe_smart = 1;
Jeff Kingb227bbc2013-09-28 04:35:25 -0400356 if (!strchr(url.buf, '?'))
Jeff Kingc65d5692013-09-28 04:35:10 -0400357 strbuf_addch(&refs_url, '?');
Shawn O. Pearce97cc7bc2009-10-30 17:47:40 -0700358 else
Jeff Kingc65d5692013-09-28 04:35:10 -0400359 strbuf_addch(&refs_url, '&');
360 strbuf_addf(&refs_url, "service=%s", service);
Shawn O. Pearce97cc7bc2009-10-30 17:47:40 -0700361 }
Daniel Barkalowa2d725b2009-08-05 01:01:56 -0400362
Brandon Williamsa4d78ce2018-03-15 10:31:42 -0700363 /*
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
Brandon Williams884e5862018-03-15 10:31:39 -0700371 /* Add the extra Git-Protocol header */
Brandon Williamsa4d78ce2018-03-15 10:31:42 -0700372 if (get_protocol_http_header(version, &protocol_header))
Brandon Williams884e5862018-03-15 10:31:39 -0700373 string_list_append(&extra_headers, protocol_header.buf);
374
Jeff Kingfcaa6e62016-12-06 13:24:38 -0500375 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;
Brandon Williams884e5862018-03-15 10:31:39 -0700380 http_options.extra_headers = &extra_headers;
Jeff King50d34132016-12-06 13:24:41 -0500381 http_options.initial_request = 1;
Jeff Kingfcaa6e62016-12-06 13:24:38 -0500382 http_options.no_cache = 1;
383 http_options.keep_error = 1;
Jeff King1bbcc222013-09-28 04:31:23 -0400384
Jeff Kingfcaa6e62016-12-06 13:24:38 -0500385 http_ret = http_get_strbuf(refs_url.buf, &buffer, &http_options);
Daniel Barkalowa2d725b2009-08-05 01:01:56 -0400386 switch (http_ret) {
387 case HTTP_OK:
388 break;
389 case HTTP_MISSING_TARGET:
Jeff Kingfc1b7742014-05-22 05:30:29 -0400390 show_http_message(&type, &charset, &buffer);
Jeff Kingb227bbc2013-09-28 04:35:25 -0400391 die("repository '%s' not found", url.buf);
Scott Chacon42653c02010-04-01 15:14:35 -0700392 case HTTP_NOAUTH:
Jeff Kingfc1b7742014-05-22 05:30:29 -0400393 show_http_message(&type, &charset, &buffer);
Jeff Kingb227bbc2013-09-28 04:35:25 -0400394 die("Authentication failed for '%s'", url.buf);
Daniel Barkalowa2d725b2009-08-05 01:01:56 -0400395 default:
Jeff Kingfc1b7742014-05-22 05:30:29 -0400396 show_http_message(&type, &charset, &buffer);
Jeff Kingb227bbc2013-09-28 04:35:25 -0400397 die("unable to access '%s': %s", url.buf, curl_errorstr);
Daniel Barkalowa2d725b2009-08-05 01:01:56 -0400398 }
399
Jeff King50d34132016-12-06 13:24:41 -0500400 if (options.verbosity && !starts_with(refs_url.buf, url.buf))
401 warning(_("redirecting to %s"), url.buf);
402
Shawn O. Pearce97cc7bc2009-10-30 17:47:40 -0700403 last= xcalloc(1, sizeof(*last_discovery));
Brandon Williamsf08a5d42018-03-15 10:31:36 -0700404 last->service = xstrdup(service);
Shawn O. Pearce97cc7bc2009-10-30 17:47:40 -0700405 last->buf_alloc = strbuf_detach(&buffer, &last->len);
406 last->buf = last->buf_alloc;
407
Shawn Pearce4656bf42013-01-31 13:02:07 -0800408 strbuf_addf(&exp, "application/x-%s-advertisement", service);
409 if (maybe_smart &&
410 (5 <= last->len && last->buf[4] == '#') &&
411 !strbuf_cmp(&exp, &type)) {
Jeff King4981fe72013-02-23 17:31:34 -0500412 char *line;
413
Shawn Pearce4656bf42013-01-31 13:02:07 -0800414 /*
415 * smart HTTP response; validate that the service
Shawn O. Pearce97cc7bc2009-10-30 17:47:40 -0700416 * pkt-line matches our request.
417 */
Jeff King4981fe72013-02-23 17:31:34 -0500418 line = packet_read_line_buf(&last->buf, &last->len, NULL);
Jon Simonsbb1356d2018-02-08 13:47:50 -0500419 if (!line)
420 die("invalid server response; expected service, got flush packet");
Shawn O. Pearce97cc7bc2009-10-30 17:47:40 -0700421
Shawn Pearce4656bf42013-01-31 13:02:07 -0800422 strbuf_reset(&exp);
Shawn O. Pearce97cc7bc2009-10-30 17:47:40 -0700423 strbuf_addf(&exp, "# service=%s", service);
Jeff King4981fe72013-02-23 17:31:34 -0500424 if (strcmp(line, exp.buf))
425 die("invalid server response; got '%s'", line);
Shawn O. Pearce97cc7bc2009-10-30 17:47:40 -0700426 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 */
Jeff King4981fe72013-02-23 17:31:34 -0500432 while (packet_read_line_buf(&last->buf, &last->len, NULL))
433 ;
Shawn O. Pearce97cc7bc2009-10-30 17:47:40 -0700434
435 last->proto_git = 1;
Brandon Williams237ffed2018-03-15 10:31:40 -0700436 } else if (maybe_smart &&
437 last->len > 5 && starts_with(last->buf + 4, "version 2")) {
438 last->proto_git = 1;
Shawn O. Pearce97cc7bc2009-10-30 17:47:40 -0700439 }
440
Jeff King2a455202013-02-20 15:07:19 -0500441 if (last->proto_git)
442 last->refs = parse_git_refs(last, for_push);
443 else
444 last->refs = parse_info_refs(last);
445
Jeff Kingc65d5692013-09-28 04:35:10 -0400446 strbuf_release(&refs_url);
Shawn Pearce4656bf42013-01-31 13:02:07 -0800447 strbuf_release(&exp);
448 strbuf_release(&type);
Jeff Kingfc1b7742014-05-22 05:30:29 -0400449 strbuf_release(&charset);
Jeff King050ef362013-09-28 04:35:35 -0400450 strbuf_release(&effective_url);
Shawn O. Pearce97cc7bc2009-10-30 17:47:40 -0700451 strbuf_release(&buffer);
Brandon Williams884e5862018-03-15 10:31:39 -0700452 strbuf_release(&protocol_header);
453 string_list_clear(&extra_headers, 0);
Shawn O. Pearce97cc7bc2009-10-30 17:47:40 -0700454 last_discovery = last;
455 return last;
456}
457
Shawn O. Pearce97cc7bc2009-10-30 17:47:40 -0700458static struct ref *get_refs(int for_push)
459{
460 struct discovery *heads;
461
462 if (for_push)
Jeff King2a455202013-02-20 15:07:19 -0500463 heads = discover_refs("git-receive-pack", for_push);
Shawn O. Pearce97cc7bc2009-10-30 17:47:40 -0700464 else
Jeff King2a455202013-02-20 15:07:19 -0500465 heads = discover_refs("git-upload-pack", for_push);
Shawn O. Pearce97cc7bc2009-10-30 17:47:40 -0700466
Jeff King2a455202013-02-20 15:07:19 -0500467 return heads->refs;
Shawn O. Pearce97cc7bc2009-10-30 17:47:40 -0700468}
469
Shawn O. Pearceae4efe12009-10-30 17:47:30 -0700470static 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
brian m. carlsonf4e54d02015-11-10 02:22:20 +0000477 printf("%s %s\n", oid_to_hex(&posn->old_oid), posn->name);
Shawn O. Pearceae4efe12009-10-30 17:47:30 -0700478 }
479 printf("\n");
480 fflush(stdout);
Shawn O. Pearceae4efe12009-10-30 17:47:30 -0700481}
482
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700483struct rpc_state {
484 const char *service_name;
485 const char **argv;
Ivan Todoroski81507492012-04-02 17:14:44 +0200486 struct strbuf *stdin_preamble;
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700487 char *service_url;
488 char *hdr_content_type;
489 char *hdr_accept;
Brandon Williams884e5862018-03-15 10:31:39 -0700490 char *protocol_header;
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700491 char *buf;
492 size_t alloc;
493 size_t len;
494 size_t pos;
495 int in;
496 int out;
David Turner296b8472016-11-18 15:30:49 -0500497 int any_written;
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700498 struct strbuf result;
Shawn O. Pearceb8538602009-10-30 17:47:43 -0700499 unsigned gzip_request : 1;
Martin Storsjö6c81a992009-12-01 12:33:39 +0200500 unsigned initial_buffer : 1;
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700501};
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) {
Martin Storsjö6c81a992009-12-01 12:33:39 +0200511 rpc->initial_buffer = 0;
Jeff King4981fe72013-02-23 17:31:34 -0500512 avail = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700513 if (!avail)
514 return 0;
515 rpc->pos = 0;
516 rpc->len = avail;
517 }
518
Tay Ray Chuan48310602009-11-24 10:31:30 +0800519 if (max < avail)
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700520 avail = max;
521 memcpy(ptr, rpc->buf + rpc->pos, avail);
522 rpc->pos += avail;
523 return avail;
524}
525
Martin Storsjö6c81a992009-12-01 12:33:39 +0200526#ifndef NO_CURL_IOCTL
Junio C Hamano5092d3e2010-01-11 22:30:36 -0800527static curlioerr rpc_ioctl(CURL *handle, int cmd, void *clientp)
Martin Storsjö6c81a992009-12-01 12:33:39 +0200528{
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 }
Jeff Kingb725b272014-07-09 17:47:05 -0400540 error("unable to rewind rpc post data - try increasing http.postBuffer");
Martin Storsjö6c81a992009-12-01 12:33:39 +0200541 return CURLIOE_FAILRESTART;
542
543 default:
544 return CURLIOE_UNKNOWNCMD;
545 }
546}
547#endif
548
Dan McGeea04ff3e2011-05-03 23:47:27 +0800549static size_t rpc_in(char *ptr, size_t eltsize,
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700550 size_t nmemb, void *buffer_)
551{
552 size_t size = eltsize * nmemb;
553 struct rpc_state *rpc = buffer_;
David Turner296b8472016-11-18 15:30:49 -0500554 if (size)
555 rpc->any_written = 1;
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700556 write_or_die(rpc->in, ptr, size);
557 return size;
558}
559
Jeff King3a347ed2013-10-31 02:36:26 -0400560static int run_slot(struct active_request_slot *slot,
561 struct slot_results *results)
Shawn O. Pearce206b0992011-02-15 08:57:24 -0800562{
Jeff Kingb81401c2012-08-27 09:27:15 -0400563 int err;
Jeff King3a347ed2013-10-31 02:36:26 -0400564 struct slot_results results_buf;
Shawn O. Pearce206b0992011-02-15 08:57:24 -0800565
Jeff King3a347ed2013-10-31 02:36:26 -0400566 if (!results)
567 results = &results_buf;
568
Jeff Kingbeed3362014-02-18 05:34:20 -0500569 err = run_one_slot(slot, results);
Shawn O. Pearce206b0992011-02-15 08:57:24 -0800570
Jeff Kingb81401c2012-08-27 09:27:15 -0400571 if (err != HTTP_OK && err != HTTP_REAUTH) {
Shawn Pearce00540452016-02-13 17:39:34 -0800572 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);
Shawn O. Pearce206b0992011-02-15 08:57:24 -0800586 }
587
588 return err;
589}
590
Jeff King3a347ed2013-10-31 02:36:26 -0400591static int probe_rpc(struct rpc_state *rpc, struct slot_results *results)
Shawn O. Pearce206b0992011-02-15 08:57:24 -0800592{
593 struct active_request_slot *slot;
Johannes Schindelin8cb01e22016-04-27 14:20:37 +0200594 struct curl_slist *headers = http_copy_default_headers();
Shawn O. Pearce206b0992011-02-15 08:57:24 -0800595 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);
Shawn O. Pearceaa90b962012-09-19 16:12:02 -0700606 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, NULL);
Shawn O. Pearce206b0992011-02-15 08:57:24 -0800607 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
Jeff King3a347ed2013-10-31 02:36:26 -0400613 err = run_slot(slot, results);
Shawn O. Pearce206b0992011-02-15 08:57:24 -0800614
615 curl_slist_free_all(headers);
616 strbuf_release(&buf);
617 return err;
618}
619
Nguyễn Thái Ngọc Duy3b335762018-12-09 11:25:21 +0100620static curl_off_t xcurl_off_t(size_t len)
621{
Torsten Bögershausencb8010b2018-11-09 18:41:10 +0100622 uintmax_t size = len;
623 if (size > maximum_signed_value_of_type(curl_off_t))
David Turner37ee6802017-04-11 14:13:57 -0400624 die("cannot handle pushes this big");
Torsten Bögershausencb8010b2018-11-09 18:41:10 +0100625 return (curl_off_t)size;
David Turner37ee6802017-04-11 14:13:57 -0400626}
627
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700628static int post_rpc(struct rpc_state *rpc)
629{
630 struct active_request_slot *slot;
Johannes Schindelin8cb01e22016-04-27 14:20:37 +0200631 struct curl_slist *headers = http_copy_default_headers();
Shawn O. Pearceb8538602009-10-30 17:47:43 -0700632 int use_gzip = rpc->gzip_request;
633 char *gzip_body = NULL;
Ramsay Jones37711542012-11-21 19:08:51 +0000634 size_t gzip_size = 0;
Shawn O. Pearce206b0992011-02-15 08:57:24 -0800635 int err, large_request = 0;
Brian M. Carlsonc80d96c2013-10-31 02:36:51 -0400636 int needs_100_continue = 0;
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700637
638 /* Try to load the entire request, if we can fit it into the
639 * allocated buffer space we can use HTTP/1.0 and avoid the
640 * chunked encoding mess.
641 */
642 while (1) {
643 size_t left = rpc->alloc - rpc->len;
644 char *buf = rpc->buf + rpc->len;
645 int n;
646
647 if (left < LARGE_PACKET_MAX) {
648 large_request = 1;
Shawn O. Pearceb8538602009-10-30 17:47:43 -0700649 use_gzip = 0;
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700650 break;
651 }
652
Jeff King4981fe72013-02-23 17:31:34 -0500653 n = packet_read(rpc->out, NULL, NULL, buf, left, 0);
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700654 if (!n)
655 break;
656 rpc->len += n;
657 }
658
Shawn O. Pearce206b0992011-02-15 08:57:24 -0800659 if (large_request) {
Brian M. Carlsonc80d96c2013-10-31 02:36:51 -0400660 struct slot_results results;
661
Jeff Kingb81401c2012-08-27 09:27:15 -0400662 do {
Brian M. Carlsonc80d96c2013-10-31 02:36:51 -0400663 err = probe_rpc(rpc, &results);
Jeff King2501aff2013-09-28 04:31:45 -0400664 if (err == HTTP_REAUTH)
665 credential_fill(&http_auth);
Jeff Kingb81401c2012-08-27 09:27:15 -0400666 } while (err == HTTP_REAUTH);
667 if (err != HTTP_OK)
668 return -1;
Brian M. Carlsonc80d96c2013-10-31 02:36:51 -0400669
670 if (results.auth_avail & CURLAUTH_GSSNEGOTIATE)
671 needs_100_continue = 1;
Shawn O. Pearce206b0992011-02-15 08:57:24 -0800672 }
673
Jeff Kingabf8df82012-10-12 03:35:33 -0400674 headers = curl_slist_append(headers, rpc->hdr_content_type);
675 headers = curl_slist_append(headers, rpc->hdr_accept);
Brian M. Carlsonc80d96c2013-10-31 02:36:51 -0400676 headers = curl_slist_append(headers, needs_100_continue ?
677 "Expect: 100-continue" : "Expect:");
Jeff Kingabf8df82012-10-12 03:35:33 -0400678
Brandon Williams884e5862018-03-15 10:31:39 -0700679 /* Add the extra Git-Protocol header */
680 if (rpc->protocol_header)
681 headers = curl_slist_append(headers, rpc->protocol_header);
682
Jeff Kingabf8df82012-10-12 03:35:33 -0400683retry:
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700684 slot = get_active_slot();
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700685
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700686 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
Martin Storsjöd21f9792009-11-23 11:03:28 +0800687 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700688 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
Brandon Williams1a53e692018-05-22 11:42:03 -0700689 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "");
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700690
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700691 if (large_request) {
692 /* The request body is large and the size cannot be predicted.
693 * We must use chunked encoding to send it.
694 */
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700695 headers = curl_slist_append(headers, "Transfer-Encoding: chunked");
Martin Storsjö6c81a992009-12-01 12:33:39 +0200696 rpc->initial_buffer = 1;
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700697 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, rpc_out);
698 curl_easy_setopt(slot->curl, CURLOPT_INFILE, rpc);
Martin Storsjö6c81a992009-12-01 12:33:39 +0200699#ifndef NO_CURL_IOCTL
700 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, rpc_ioctl);
701 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, rpc);
702#endif
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700703 if (options.verbosity > 1) {
704 fprintf(stderr, "POST %s (chunked)\n", rpc->service_name);
705 fflush(stderr);
706 }
707
Jeff King2e736fd2012-10-31 07:29:16 -0400708 } else if (gzip_body) {
709 /*
710 * If we are looping to retry authentication, then the previous
711 * run will have set up the headers and gzip buffer already,
712 * and we just need to send it.
713 */
714 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
David Turner37ee6802017-04-11 14:13:57 -0400715 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(gzip_size));
Jeff King2e736fd2012-10-31 07:29:16 -0400716
Shawn O. Pearceb8538602009-10-30 17:47:43 -0700717 } else if (use_gzip && 1024 < rpc->len) {
718 /* The client backend isn't giving us compressed data so
Johannes Schindelina8132412018-08-08 04:50:00 -0700719 * we can try to deflate it ourselves, this may save on
Shawn O. Pearceb8538602009-10-30 17:47:43 -0700720 * the transfer time.
721 */
Junio C Hamanoef49a7a2011-06-10 11:52:15 -0700722 git_zstream stream;
Shawn O. Pearceb8538602009-10-30 17:47:43 -0700723 int ret;
724
Junio C Hamano55bb5c92011-06-10 10:55:10 -0700725 git_deflate_init_gzip(&stream, Z_BEST_COMPRESSION);
Jeff Kingdf126e12012-10-31 07:20:15 -0400726 gzip_size = git_deflate_bound(&stream, rpc->len);
727 gzip_body = xmalloc(gzip_size);
Shawn O. Pearceb8538602009-10-30 17:47:43 -0700728
729 stream.next_in = (unsigned char *)rpc->buf;
730 stream.avail_in = rpc->len;
731 stream.next_out = (unsigned char *)gzip_body;
Jeff Kingdf126e12012-10-31 07:20:15 -0400732 stream.avail_out = gzip_size;
Shawn O. Pearceb8538602009-10-30 17:47:43 -0700733
Junio C Hamano55bb5c92011-06-10 10:55:10 -0700734 ret = git_deflate(&stream, Z_FINISH);
Shawn O. Pearceb8538602009-10-30 17:47:43 -0700735 if (ret != Z_STREAM_END)
736 die("cannot deflate request; zlib deflate error %d", ret);
737
Junio C Hamano55bb5c92011-06-10 10:55:10 -0700738 ret = git_deflate_end_gently(&stream);
Shawn O. Pearceb8538602009-10-30 17:47:43 -0700739 if (ret != Z_OK)
740 die("cannot deflate request; zlib end error %d", ret);
741
Jeff Kingdf126e12012-10-31 07:20:15 -0400742 gzip_size = stream.total_out;
Shawn O. Pearceb8538602009-10-30 17:47:43 -0700743
744 headers = curl_slist_append(headers, "Content-Encoding: gzip");
745 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
David Turner37ee6802017-04-11 14:13:57 -0400746 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(gzip_size));
Shawn O. Pearceb8538602009-10-30 17:47:43 -0700747
748 if (options.verbosity > 1) {
749 fprintf(stderr, "POST %s (gzip %lu to %lu bytes)\n",
750 rpc->service_name,
Jeff Kingdf126e12012-10-31 07:20:15 -0400751 (unsigned long)rpc->len, (unsigned long)gzip_size);
Shawn O. Pearceb8538602009-10-30 17:47:43 -0700752 fflush(stderr);
753 }
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700754 } else {
755 /* We know the complete request size in advance, use the
756 * more normal Content-Length approach.
757 */
758 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, rpc->buf);
David Turner37ee6802017-04-11 14:13:57 -0400759 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(rpc->len));
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700760 if (options.verbosity > 1) {
761 fprintf(stderr, "POST %s (%lu bytes)\n",
762 rpc->service_name, (unsigned long)rpc->len);
763 fflush(stderr);
764 }
765 }
766
767 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
768 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, rpc_in);
769 curl_easy_setopt(slot->curl, CURLOPT_FILE, rpc);
770
David Turner296b8472016-11-18 15:30:49 -0500771
772 rpc->any_written = 0;
Jeff King3a347ed2013-10-31 02:36:26 -0400773 err = run_slot(slot, NULL);
Jeff King2501aff2013-09-28 04:31:45 -0400774 if (err == HTTP_REAUTH && !large_request) {
775 credential_fill(&http_auth);
Jeff Kingabf8df82012-10-12 03:35:33 -0400776 goto retry;
Jeff King2501aff2013-09-28 04:31:45 -0400777 }
Jeff Kingb81401c2012-08-27 09:27:15 -0400778 if (err != HTTP_OK)
779 err = -1;
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700780
David Turner296b8472016-11-18 15:30:49 -0500781 if (!rpc->any_written)
782 err = -1;
783
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700784 curl_slist_free_all(headers);
Shawn O. Pearceb8538602009-10-30 17:47:43 -0700785 free(gzip_body);
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700786 return err;
787}
788
789static int rpc_service(struct rpc_state *rpc, struct discovery *heads)
790{
791 const char *svc = rpc->service_name;
792 struct strbuf buf = STRBUF_INIT;
Ivan Todoroski81507492012-04-02 17:14:44 +0200793 struct strbuf *preamble = rpc->stdin_preamble;
René Scharfed3180272014-08-19 21:09:35 +0200794 struct child_process client = CHILD_PROCESS_INIT;
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700795 int err = 0;
796
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700797 client.in = -1;
798 client.out = -1;
799 client.git_cmd = 1;
800 client.argv = rpc->argv;
801 if (start_command(&client))
802 exit(1);
Ivan Todoroski81507492012-04-02 17:14:44 +0200803 if (preamble)
804 write_or_die(client.in, preamble->buf, preamble->len);
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700805 if (heads)
806 write_or_die(client.in, heads->buf, heads->len);
807
808 rpc->alloc = http_post_buffer;
809 rpc->buf = xmalloc(rpc->alloc);
810 rpc->in = client.in;
811 rpc->out = client.out;
812 strbuf_init(&rpc->result, 0);
813
Jeff Kingb227bbc2013-09-28 04:35:25 -0400814 strbuf_addf(&buf, "%s%s", url.buf, svc);
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700815 rpc->service_url = strbuf_detach(&buf, NULL);
816
817 strbuf_addf(&buf, "Content-Type: application/x-%s-request", svc);
818 rpc->hdr_content_type = strbuf_detach(&buf, NULL);
819
Shawn O. Pearce8efa5f62010-01-12 09:54:04 -0800820 strbuf_addf(&buf, "Accept: application/x-%s-result", svc);
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700821 rpc->hdr_accept = strbuf_detach(&buf, NULL);
822
Brandon Williams884e5862018-03-15 10:31:39 -0700823 if (get_protocol_http_header(heads->version, &buf))
824 rpc->protocol_header = strbuf_detach(&buf, NULL);
825 else
826 rpc->protocol_header = NULL;
827
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700828 while (!err) {
Jeff King4981fe72013-02-23 17:31:34 -0500829 int n = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700830 if (!n)
831 break;
832 rpc->pos = 0;
833 rpc->len = n;
834 err |= post_rpc(rpc);
835 }
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700836
837 close(client.in);
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700838 client.in = -1;
Shawn O. Pearce6cdf0222011-10-04 16:20:19 -0700839 if (!err) {
840 strbuf_read(&rpc->result, client.out, 0);
841 } else {
842 char buf[4096];
843 for (;;)
844 if (xread(client.out, buf, sizeof(buf)) <= 0)
845 break;
846 }
Shawn O. Pearceb4ee10f2010-08-06 14:19:44 -0700847
848 close(client.out);
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700849 client.out = -1;
850
851 err |= finish_command(&client);
852 free(rpc->service_url);
853 free(rpc->hdr_content_type);
854 free(rpc->hdr_accept);
Brandon Williams884e5862018-03-15 10:31:39 -0700855 free(rpc->protocol_header);
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700856 free(rpc->buf);
857 strbuf_release(&buf);
858 return err;
859}
860
Shawn O. Pearce292ce462009-10-30 17:47:28 -0700861static int fetch_dumb(int nr_heads, struct ref **to_fetch)
862{
Tay Ray Chuan26e1e0b2010-03-02 18:49:31 +0800863 struct walker *walker;
Jeff Kingb32fa952016-02-22 17:44:25 -0500864 char **targets;
Shawn O. Pearce292ce462009-10-30 17:47:28 -0700865 int ret, i;
866
Jeff Kingb32fa952016-02-22 17:44:25 -0500867 ALLOC_ARRAY(targets, nr_heads);
Nguyễn Thái Ngọc Duy508ea882016-06-12 17:53:59 +0700868 if (options.depth || options.deepen_since)
869 die("dumb http transport does not support shallow capabilities");
Shawn O. Pearce292ce462009-10-30 17:47:28 -0700870 for (i = 0; i < nr_heads; i++)
brian m. carlsonf4e54d02015-11-10 02:22:20 +0000871 targets[i] = xstrdup(oid_to_hex(&to_fetch[i]->old_oid));
Shawn O. Pearce292ce462009-10-30 17:47:28 -0700872
Jeff Kingb227bbc2013-09-28 04:35:25 -0400873 walker = get_http_walker(url.buf);
Shawn O. Pearceef08ef92009-10-30 17:47:29 -0700874 walker->get_verbosely = options.verbosity >= 3;
Shawn O. Pearce292ce462009-10-30 17:47:28 -0700875 walker->get_recover = 0;
876 ret = walker_fetch(walker, nr_heads, targets, NULL, NULL);
Tay Ray Chuan26e1e0b2010-03-02 18:49:31 +0800877 walker_free(walker);
Shawn O. Pearce292ce462009-10-30 17:47:28 -0700878
879 for (i = 0; i < nr_heads; i++)
880 free(targets[i]);
881 free(targets);
882
Jeff Kingb725b272014-07-09 17:47:05 -0400883 return ret ? error("fetch failed.") : 0;
Shawn O. Pearce292ce462009-10-30 17:47:28 -0700884}
885
Shawn O. Pearce249b2002009-10-30 17:47:42 -0700886static int fetch_git(struct discovery *heads,
887 int nr_heads, struct ref **to_fetch)
888{
889 struct rpc_state rpc;
Ivan Todoroski81507492012-04-02 17:14:44 +0200890 struct strbuf preamble = STRBUF_INIT;
Nguyễn Thái Ngọc Duyb5f62eb2016-06-12 17:53:43 +0700891 int i, err;
892 struct argv_array args = ARGV_ARRAY_INIT;
Shawn O. Pearce249b2002009-10-30 17:47:42 -0700893
Nguyễn Thái Ngọc Duyb5f62eb2016-06-12 17:53:43 +0700894 argv_array_pushl(&args, "fetch-pack", "--stateless-rpc",
895 "--stdin", "--lock-pack", NULL);
Shawn O. Pearce249b2002009-10-30 17:47:42 -0700896 if (options.followtags)
Nguyễn Thái Ngọc Duyb5f62eb2016-06-12 17:53:43 +0700897 argv_array_push(&args, "--include-tag");
Shawn O. Pearce249b2002009-10-30 17:47:42 -0700898 if (options.thin)
Nguyễn Thái Ngọc Duyb5f62eb2016-06-12 17:53:43 +0700899 argv_array_push(&args, "--thin");
900 if (options.verbosity >= 3)
901 argv_array_pushl(&args, "-v", "-v", NULL);
Nguyễn Thái Ngọc Duy9ba38042013-07-21 15:18:05 +0700902 if (options.check_self_contained_and_connected)
Nguyễn Thái Ngọc Duyb5f62eb2016-06-12 17:53:43 +0700903 argv_array_push(&args, "--check-self-contained-and-connected");
Nguyễn Thái Ngọc Duy16094882013-12-05 20:02:50 +0700904 if (options.cloning)
Nguyễn Thái Ngọc Duyb5f62eb2016-06-12 17:53:43 +0700905 argv_array_push(&args, "--cloning");
Nguyễn Thái Ngọc Duy16094882013-12-05 20:02:50 +0700906 if (options.update_shallow)
Nguyễn Thái Ngọc Duyb5f62eb2016-06-12 17:53:43 +0700907 argv_array_push(&args, "--update-shallow");
Shawn O. Pearce249b2002009-10-30 17:47:42 -0700908 if (!options.progress)
Nguyễn Thái Ngọc Duyb5f62eb2016-06-12 17:53:43 +0700909 argv_array_push(&args, "--no-progress");
910 if (options.depth)
911 argv_array_pushf(&args, "--depth=%lu", options.depth);
Nguyễn Thái Ngọc Duy508ea882016-06-12 17:53:59 +0700912 if (options.deepen_since)
913 argv_array_pushf(&args, "--shallow-since=%s", options.deepen_since);
Nguyễn Thái Ngọc Duya45a2602016-06-12 17:54:04 +0700914 for (i = 0; i < options.deepen_not.nr; i++)
915 argv_array_pushf(&args, "--shallow-exclude=%s",
916 options.deepen_not.items[i].string);
Nguyễn Thái Ngọc Duycccf74e2016-06-12 17:54:09 +0700917 if (options.deepen_relative && options.depth)
918 argv_array_push(&args, "--deepen-relative");
Jonathan Tan88e2f9e2017-12-05 16:58:49 +0000919 if (options.from_promisor)
920 argv_array_push(&args, "--from-promisor");
921 if (options.no_dependents)
922 argv_array_push(&args, "--no-dependents");
Jeff Hostetleracb0c572017-12-08 15:58:44 +0000923 if (options.filter)
924 argv_array_pushf(&args, "--filter=%s", options.filter);
Nguyễn Thái Ngọc Duyb5f62eb2016-06-12 17:53:43 +0700925 argv_array_push(&args, url.buf);
Ivan Todoroski81507492012-04-02 17:14:44 +0200926
Shawn O. Pearce249b2002009-10-30 17:47:42 -0700927 for (i = 0; i < nr_heads; i++) {
928 struct ref *ref = to_fetch[i];
Jeff King94ee8e22015-01-28 12:58:50 -0500929 if (!*ref->name)
Shawn O. Pearce249b2002009-10-30 17:47:42 -0700930 die("cannot fetch by sha1 over smart http");
Nguyễn Thái Ngọc Duy58f2ed02013-12-05 20:02:49 +0700931 packet_buf_write(&preamble, "%s %s\n",
brian m. carlsonf4e54d02015-11-10 02:22:20 +0000932 oid_to_hex(&ref->old_oid), ref->name);
Shawn O. Pearce249b2002009-10-30 17:47:42 -0700933 }
Ivan Todoroski81507492012-04-02 17:14:44 +0200934 packet_buf_flush(&preamble);
Shawn O. Pearce249b2002009-10-30 17:47:42 -0700935
936 memset(&rpc, 0, sizeof(rpc));
937 rpc.service_name = "git-upload-pack",
Nguyễn Thái Ngọc Duyb5f62eb2016-06-12 17:53:43 +0700938 rpc.argv = args.argv;
Ivan Todoroski81507492012-04-02 17:14:44 +0200939 rpc.stdin_preamble = &preamble;
Shawn O. Pearceb8538602009-10-30 17:47:43 -0700940 rpc.gzip_request = 1;
Shawn O. Pearce249b2002009-10-30 17:47:42 -0700941
942 err = rpc_service(&rpc, heads);
943 if (rpc.result.len)
Jeff Kingcdf4fb82013-02-20 15:01:56 -0500944 write_or_die(1, rpc.result.buf, rpc.result.len);
Shawn O. Pearce249b2002009-10-30 17:47:42 -0700945 strbuf_release(&rpc.result);
Ivan Todoroski81507492012-04-02 17:14:44 +0200946 strbuf_release(&preamble);
Nguyễn Thái Ngọc Duyb5f62eb2016-06-12 17:53:43 +0700947 argv_array_clear(&args);
Shawn O. Pearce249b2002009-10-30 17:47:42 -0700948 return err;
949}
950
951static int fetch(int nr_heads, struct ref **to_fetch)
952{
Jeff King2a455202013-02-20 15:07:19 -0500953 struct discovery *d = discover_refs("git-upload-pack", 0);
Shawn O. Pearce249b2002009-10-30 17:47:42 -0700954 if (d->proto_git)
955 return fetch_git(d, nr_heads, to_fetch);
956 else
957 return fetch_dumb(nr_heads, to_fetch);
958}
959
Shawn O. Pearce292ce462009-10-30 17:47:28 -0700960static void parse_fetch(struct strbuf *buf)
961{
962 struct ref **to_fetch = NULL;
963 struct ref *list_head = NULL;
964 struct ref **list = &list_head;
965 int alloc_heads = 0, nr_heads = 0;
966
967 do {
Jeff King95b567c2014-06-18 15:48:29 -0400968 const char *p;
969 if (skip_prefix(buf->buf, "fetch ", &p)) {
970 const char *name;
Shawn O. Pearce292ce462009-10-30 17:47:28 -0700971 struct ref *ref;
brian m. carlson8338c912015-11-10 02:22:22 +0000972 struct object_id old_oid;
Shawn O. Pearce292ce462009-10-30 17:47:28 -0700973
brian m. carlson8338c912015-11-10 02:22:22 +0000974 if (get_oid_hex(p, &old_oid))
Shawn O. Pearce292ce462009-10-30 17:47:28 -0700975 die("protocol error: expected sha/ref, got %s'", p);
brian m. carlson8338c912015-11-10 02:22:22 +0000976 if (p[GIT_SHA1_HEXSZ] == ' ')
977 name = p + GIT_SHA1_HEXSZ + 1;
978 else if (!p[GIT_SHA1_HEXSZ])
Shawn O. Pearce292ce462009-10-30 17:47:28 -0700979 name = "";
980 else
981 die("protocol error: expected sha/ref, got %s'", p);
982
983 ref = alloc_ref(name);
brian m. carlson8338c912015-11-10 02:22:22 +0000984 oidcpy(&ref->old_oid, &old_oid);
Shawn O. Pearce292ce462009-10-30 17:47:28 -0700985
986 *list = ref;
987 list = &ref->next;
988
989 ALLOC_GROW(to_fetch, nr_heads + 1, alloc_heads);
990 to_fetch[nr_heads++] = ref;
991 }
992 else
993 die("http transport does not support %s", buf->buf);
994
995 strbuf_reset(buf);
Junio C Hamano8f309ae2016-01-13 15:31:17 -0800996 if (strbuf_getline_lf(buf, stdin) == EOF)
Shawn O. Pearce292ce462009-10-30 17:47:28 -0700997 return;
998 if (!*buf->buf)
999 break;
1000 } while (1);
1001
Shawn O. Pearce249b2002009-10-30 17:47:42 -07001002 if (fetch(nr_heads, to_fetch))
Shawn O. Pearce292ce462009-10-30 17:47:28 -07001003 exit(128); /* error already reported */
1004 free_refs(list_head);
1005 free(to_fetch);
1006
1007 printf("\n");
1008 fflush(stdout);
1009 strbuf_reset(buf);
1010}
1011
Shawn O. Pearceae4efe12009-10-30 17:47:30 -07001012static int push_dav(int nr_spec, char **specs)
1013{
Jeff King850d2fe2016-02-22 17:44:21 -05001014 struct child_process child = CHILD_PROCESS_INIT;
1015 size_t i;
Shawn O. Pearceae4efe12009-10-30 17:47:30 -07001016
Jeff King850d2fe2016-02-22 17:44:21 -05001017 child.git_cmd = 1;
1018 argv_array_push(&child.args, "http-push");
1019 argv_array_push(&child.args, "--helper-status");
Shawn O. Pearceae4efe12009-10-30 17:47:30 -07001020 if (options.dry_run)
Jeff King850d2fe2016-02-22 17:44:21 -05001021 argv_array_push(&child.args, "--dry-run");
Shawn O. Pearceae4efe12009-10-30 17:47:30 -07001022 if (options.verbosity > 1)
Jeff King850d2fe2016-02-22 17:44:21 -05001023 argv_array_push(&child.args, "--verbose");
1024 argv_array_push(&child.args, url.buf);
Shawn O. Pearceae4efe12009-10-30 17:47:30 -07001025 for (i = 0; i < nr_spec; i++)
Jeff King850d2fe2016-02-22 17:44:21 -05001026 argv_array_push(&child.args, specs[i]);
Shawn O. Pearceae4efe12009-10-30 17:47:30 -07001027
Jeff King850d2fe2016-02-22 17:44:21 -05001028 if (run_command(&child))
1029 die("git-http-push failed");
Shawn O. Pearceae4efe12009-10-30 17:47:30 -07001030 return 0;
1031}
1032
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -07001033static int push_git(struct discovery *heads, int nr_spec, char **specs)
1034{
1035 struct rpc_state rpc;
Junio C Hamano222b1212013-07-08 22:16:31 -07001036 int i, err;
1037 struct argv_array args;
Junio C Hamano05c1eb12013-08-02 15:14:50 -07001038 struct string_list_item *cas_option;
Jeff King26be19b2014-08-21 08:21:20 -04001039 struct strbuf preamble = STRBUF_INIT;
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -07001040
Junio C Hamano222b1212013-07-08 22:16:31 -07001041 argv_array_init(&args);
1042 argv_array_pushl(&args, "send-pack", "--stateless-rpc", "--helper-status",
1043 NULL);
1044
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -07001045 if (options.thin)
Junio C Hamano222b1212013-07-08 22:16:31 -07001046 argv_array_push(&args, "--thin");
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -07001047 if (options.dry_run)
Junio C Hamano222b1212013-07-08 22:16:31 -07001048 argv_array_push(&args, "--dry-run");
Dave Borowitz30261092015-08-19 11:26:46 -04001049 if (options.push_cert == SEND_PACK_PUSH_CERT_ALWAYS)
1050 argv_array_push(&args, "--signed=yes");
1051 else if (options.push_cert == SEND_PACK_PUSH_CERT_IF_ASKED)
1052 argv_array_push(&args, "--signed=if-asked");
Clemens Buchacherc207e342012-01-08 22:06:20 +01001053 if (options.verbosity == 0)
Junio C Hamano222b1212013-07-08 22:16:31 -07001054 argv_array_push(&args, "--quiet");
Clemens Buchacherc207e342012-01-08 22:06:20 +01001055 else if (options.verbosity > 1)
Junio C Hamano222b1212013-07-08 22:16:31 -07001056 argv_array_push(&args, "--verbose");
Brandon Williams511155d2017-03-22 15:22:00 -07001057 for (i = 0; i < options.push_options.nr; i++)
1058 argv_array_pushf(&args, "--push-option=%s",
1059 options.push_options.items[i].string);
Junio C Hamano222b1212013-07-08 22:16:31 -07001060 argv_array_push(&args, options.progress ? "--progress" : "--no-progress");
Junio C Hamano05c1eb12013-08-02 15:14:50 -07001061 for_each_string_list_item(cas_option, &cas_options)
Junio C Hamano2233ad42013-09-09 14:30:29 -07001062 argv_array_push(&args, cas_option->string);
Jeff Kingb227bbc2013-09-28 04:35:25 -04001063 argv_array_push(&args, url.buf);
Jeff King26be19b2014-08-21 08:21:20 -04001064
1065 argv_array_push(&args, "--stdin");
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -07001066 for (i = 0; i < nr_spec; i++)
Jeff King26be19b2014-08-21 08:21:20 -04001067 packet_buf_write(&preamble, "%s\n", specs[i]);
1068 packet_buf_flush(&preamble);
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -07001069
1070 memset(&rpc, 0, sizeof(rpc));
1071 rpc.service_name = "git-receive-pack",
Junio C Hamano222b1212013-07-08 22:16:31 -07001072 rpc.argv = args.argv;
Jeff King26be19b2014-08-21 08:21:20 -04001073 rpc.stdin_preamble = &preamble;
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -07001074
1075 err = rpc_service(&rpc, heads);
1076 if (rpc.result.len)
Jeff Kingcdf4fb82013-02-20 15:01:56 -05001077 write_or_die(1, rpc.result.buf, rpc.result.len);
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -07001078 strbuf_release(&rpc.result);
Jeff King26be19b2014-08-21 08:21:20 -04001079 strbuf_release(&preamble);
Junio C Hamano222b1212013-07-08 22:16:31 -07001080 argv_array_clear(&args);
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -07001081 return err;
1082}
1083
1084static int push(int nr_spec, char **specs)
1085{
Jeff King2a455202013-02-20 15:07:19 -05001086 struct discovery *heads = discover_refs("git-receive-pack", 1);
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -07001087 int ret;
1088
1089 if (heads->proto_git)
1090 ret = push_git(heads, nr_spec, specs);
1091 else
1092 ret = push_dav(nr_spec, specs);
1093 free_discovery(heads);
1094 return ret;
1095}
1096
Shawn O. Pearceae4efe12009-10-30 17:47:30 -07001097static void parse_push(struct strbuf *buf)
1098{
1099 char **specs = NULL;
Shawn O. Pearce5238cbf2012-01-19 19:12:09 -08001100 int alloc_spec = 0, nr_spec = 0, i, ret;
Shawn O. Pearceae4efe12009-10-30 17:47:30 -07001101
1102 do {
Christian Couder59556542013-11-30 21:55:40 +01001103 if (starts_with(buf->buf, "push ")) {
Shawn O. Pearceae4efe12009-10-30 17:47:30 -07001104 ALLOC_GROW(specs, nr_spec + 1, alloc_spec);
1105 specs[nr_spec++] = xstrdup(buf->buf + 5);
1106 }
1107 else
1108 die("http transport does not support %s", buf->buf);
1109
1110 strbuf_reset(buf);
Junio C Hamano8f309ae2016-01-13 15:31:17 -08001111 if (strbuf_getline_lf(buf, stdin) == EOF)
Jim Meyeringdc4cd762011-06-20 09:40:06 +02001112 goto free_specs;
Shawn O. Pearceae4efe12009-10-30 17:47:30 -07001113 if (!*buf->buf)
1114 break;
1115 } while (1);
1116
Shawn O. Pearce5238cbf2012-01-19 19:12:09 -08001117 ret = push(nr_spec, specs);
Shawn O. Pearceae4efe12009-10-30 17:47:30 -07001118 printf("\n");
1119 fflush(stdout);
Jim Meyeringdc4cd762011-06-20 09:40:06 +02001120
Shawn O. Pearce5238cbf2012-01-19 19:12:09 -08001121 if (ret)
1122 exit(128); /* error already reported */
1123
Jim Meyeringdc4cd762011-06-20 09:40:06 +02001124 free_specs:
1125 for (i = 0; i < nr_spec; i++)
1126 free(specs[i]);
1127 free(specs);
Shawn O. Pearceae4efe12009-10-30 17:47:30 -07001128}
1129
Brandon Williams0f1dc532018-03-15 10:31:41 -07001130/*
1131 * Used to represent the state of a connection to an HTTP server when
1132 * communicating using git's wire-protocol version 2.
1133 */
1134struct proxy_state {
1135 char *service_name;
1136 char *service_url;
1137 struct curl_slist *headers;
1138 struct strbuf request_buffer;
1139 int in;
1140 int out;
1141 struct packet_reader reader;
1142 size_t pos;
1143 int seen_flush;
1144};
1145
1146static void proxy_state_init(struct proxy_state *p, const char *service_name,
1147 enum protocol_version version)
1148{
1149 struct strbuf buf = STRBUF_INIT;
1150
1151 memset(p, 0, sizeof(*p));
1152 p->service_name = xstrdup(service_name);
1153
1154 p->in = 0;
1155 p->out = 1;
1156 strbuf_init(&p->request_buffer, 0);
1157
1158 strbuf_addf(&buf, "%s%s", url.buf, p->service_name);
1159 p->service_url = strbuf_detach(&buf, NULL);
1160
1161 p->headers = http_copy_default_headers();
1162
1163 strbuf_addf(&buf, "Content-Type: application/x-%s-request", p->service_name);
1164 p->headers = curl_slist_append(p->headers, buf.buf);
1165 strbuf_reset(&buf);
1166
1167 strbuf_addf(&buf, "Accept: application/x-%s-result", p->service_name);
1168 p->headers = curl_slist_append(p->headers, buf.buf);
1169 strbuf_reset(&buf);
1170
1171 p->headers = curl_slist_append(p->headers, "Transfer-Encoding: chunked");
1172
1173 /* Add the Git-Protocol header */
1174 if (get_protocol_http_header(version, &buf))
1175 p->headers = curl_slist_append(p->headers, buf.buf);
1176
1177 packet_reader_init(&p->reader, p->in, NULL, 0,
1178 PACKET_READ_GENTLE_ON_EOF);
1179
1180 strbuf_release(&buf);
1181}
1182
1183static void proxy_state_clear(struct proxy_state *p)
1184{
1185 free(p->service_name);
1186 free(p->service_url);
1187 curl_slist_free_all(p->headers);
1188 strbuf_release(&p->request_buffer);
1189}
1190
1191/*
1192 * CURLOPT_READFUNCTION callback function.
1193 * Attempts to copy over a single packet-line at a time into the
1194 * curl provided buffer.
1195 */
1196static size_t proxy_in(char *buffer, size_t eltsize,
1197 size_t nmemb, void *userdata)
1198{
1199 size_t max;
1200 struct proxy_state *p = userdata;
1201 size_t avail = p->request_buffer.len - p->pos;
1202
1203
1204 if (eltsize != 1)
1205 BUG("curl read callback called with size = %"PRIuMAX" != 1",
1206 (uintmax_t)eltsize);
1207 max = nmemb;
1208
1209 if (!avail) {
1210 if (p->seen_flush) {
1211 p->seen_flush = 0;
1212 return 0;
1213 }
1214
1215 strbuf_reset(&p->request_buffer);
1216 switch (packet_reader_read(&p->reader)) {
1217 case PACKET_READ_EOF:
1218 die("unexpected EOF when reading from parent process");
1219 case PACKET_READ_NORMAL:
1220 packet_buf_write_len(&p->request_buffer, p->reader.line,
1221 p->reader.pktlen);
1222 break;
1223 case PACKET_READ_DELIM:
1224 packet_buf_delim(&p->request_buffer);
1225 break;
1226 case PACKET_READ_FLUSH:
1227 packet_buf_flush(&p->request_buffer);
1228 p->seen_flush = 1;
1229 break;
1230 }
1231 p->pos = 0;
1232 avail = p->request_buffer.len;
1233 }
1234
1235 if (max < avail)
1236 avail = max;
1237 memcpy(buffer, p->request_buffer.buf + p->pos, avail);
1238 p->pos += avail;
1239 return avail;
1240}
1241
1242static size_t proxy_out(char *buffer, size_t eltsize,
1243 size_t nmemb, void *userdata)
1244{
1245 size_t size;
1246 struct proxy_state *p = userdata;
1247
1248 if (eltsize != 1)
1249 BUG("curl read callback called with size = %"PRIuMAX" != 1",
1250 (uintmax_t)eltsize);
1251 size = nmemb;
1252
1253 write_or_die(p->out, buffer, size);
1254 return size;
1255}
1256
1257/* Issues a request to the HTTP server configured in `p` */
1258static int proxy_request(struct proxy_state *p)
1259{
1260 struct active_request_slot *slot;
1261
1262 slot = get_active_slot();
1263
Brandon Williamseaf6a1b2018-05-22 11:42:04 -07001264 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "");
Brandon Williams0f1dc532018-03-15 10:31:41 -07001265 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
1266 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
1267 curl_easy_setopt(slot->curl, CURLOPT_URL, p->service_url);
1268 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, p->headers);
1269
1270 /* Setup function to read request from client */
1271 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, proxy_in);
1272 curl_easy_setopt(slot->curl, CURLOPT_READDATA, p);
1273
1274 /* Setup function to write server response to client */
1275 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, proxy_out);
1276 curl_easy_setopt(slot->curl, CURLOPT_WRITEDATA, p);
1277
1278 if (run_slot(slot, NULL) != HTTP_OK)
1279 return -1;
1280
1281 return 0;
1282}
1283
1284static int stateless_connect(const char *service_name)
1285{
1286 struct discovery *discover;
1287 struct proxy_state p;
1288
1289 /*
1290 * Run the info/refs request and see if the server supports protocol
1291 * v2. If and only if the server supports v2 can we successfully
1292 * establish a stateless connection, otherwise we need to tell the
1293 * client to fallback to using other transport helper functions to
1294 * complete their request.
1295 */
1296 discover = discover_refs(service_name, 0);
1297 if (discover->version != protocol_v2) {
1298 printf("fallback\n");
1299 fflush(stdout);
1300 return -1;
1301 } else {
1302 /* Stateless Connection established */
1303 printf("\n");
1304 fflush(stdout);
1305 }
1306
1307 proxy_state_init(&p, service_name, discover->version);
1308
1309 /*
1310 * Dump the capability listing that we got from the server earlier
1311 * during the info/refs request.
1312 */
1313 write_or_die(p.out, discover->buf, discover->len);
1314
1315 /* Peek the next packet line. Until we see EOF keep sending POSTs */
1316 while (packet_reader_peek(&p.reader) != PACKET_READ_EOF) {
1317 if (proxy_request(&p)) {
1318 /* We would have an err here */
1319 break;
1320 }
1321 }
1322
1323 proxy_state_clear(&p);
1324 return 0;
1325}
1326
Jeff King3f2e2292016-07-01 01:58:58 -04001327int cmd_main(int argc, const char **argv)
Daniel Barkalowa2d725b2009-08-05 01:01:56 -04001328{
Daniel Barkalowa2d725b2009-08-05 01:01:56 -04001329 struct strbuf buf = STRBUF_INIT;
Daniel Barkalowa45d3d72009-11-03 21:52:35 -05001330 int nongit;
Daniel Barkalowa2d725b2009-08-05 01:01:56 -04001331
Daniel Barkalowa45d3d72009-11-03 21:52:35 -05001332 setup_git_directory_gently(&nongit);
Daniel Barkalowa2d725b2009-08-05 01:01:56 -04001333 if (argc < 2) {
Jeff Kingcdaa4e92014-07-09 17:47:20 -04001334 error("remote-curl: usage: git remote-curl <remote> [<url>]");
Daniel Barkalowa2d725b2009-08-05 01:01:56 -04001335 return 1;
1336 }
1337
Shawn O. Pearceef08ef92009-10-30 17:47:29 -07001338 options.verbosity = 1;
1339 options.progress = !!isatty(2);
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -07001340 options.thin = 1;
Nguyễn Thái Ngọc Duya45a2602016-06-12 17:54:04 +07001341 string_list_init(&options.deepen_not, 1);
Brandon Williams511155d2017-03-22 15:22:00 -07001342 string_list_init(&options.push_options, 1);
Shawn O. Pearceef08ef92009-10-30 17:47:29 -07001343
Daniel Barkalowa2d725b2009-08-05 01:01:56 -04001344 remote = remote_get(argv[1]);
1345
1346 if (argc > 2) {
Jeff Kingb227bbc2013-09-28 04:35:25 -04001347 end_url_with_slash(&url, argv[2]);
Daniel Barkalowa2d725b2009-08-05 01:01:56 -04001348 } else {
Jeff Kingb227bbc2013-09-28 04:35:25 -04001349 end_url_with_slash(&url, remote->url[0]);
Daniel Barkalowa2d725b2009-08-05 01:01:56 -04001350 }
1351
Jeff Kingb227bbc2013-09-28 04:35:25 -04001352 http_init(remote, url.buf, 0);
Tay Ray Chuan888692b2010-03-02 18:49:29 +08001353
Daniel Barkalowa2d725b2009-08-05 01:01:56 -04001354 do {
Jeff King95b567c2014-06-18 15:48:29 -04001355 const char *arg;
1356
Junio C Hamano8f309ae2016-01-13 15:31:17 -08001357 if (strbuf_getline_lf(&buf, stdin) == EOF) {
Sverre Rabbelier1843f0c2011-07-16 15:03:29 +02001358 if (ferror(stdin))
Jeff Kingcdaa4e92014-07-09 17:47:20 -04001359 error("remote-curl: error reading command stream from git");
Sverre Rabbelier1843f0c2011-07-16 15:03:29 +02001360 return 1;
1361 }
1362 if (buf.len == 0)
Daniel Barkalowa2d725b2009-08-05 01:01:56 -04001363 break;
Christian Couder59556542013-11-30 21:55:40 +01001364 if (starts_with(buf.buf, "fetch ")) {
Daniel Barkalowa45d3d72009-11-03 21:52:35 -05001365 if (nongit)
Jeff Kingcdaa4e92014-07-09 17:47:20 -04001366 die("remote-curl: fetch attempted without a local repo");
Shawn O. Pearce292ce462009-10-30 17:47:28 -07001367 parse_fetch(&buf);
1368
Christian Couder59556542013-11-30 21:55:40 +01001369 } else if (!strcmp(buf.buf, "list") || starts_with(buf.buf, "list ")) {
Shawn O. Pearce97cc7bc2009-10-30 17:47:40 -07001370 int for_push = !!strstr(buf.buf + 4, "for-push");
1371 output_refs(get_refs(for_push));
Shawn O. Pearceae4efe12009-10-30 17:47:30 -07001372
Christian Couder59556542013-11-30 21:55:40 +01001373 } else if (starts_with(buf.buf, "push ")) {
Shawn O. Pearceae4efe12009-10-30 17:47:30 -07001374 parse_push(&buf);
1375
Jeff King95b567c2014-06-18 15:48:29 -04001376 } else if (skip_prefix(buf.buf, "option ", &arg)) {
1377 char *value = strchr(arg, ' ');
Shawn O. Pearceef08ef92009-10-30 17:47:29 -07001378 int result;
1379
1380 if (value)
1381 *value++ = '\0';
1382 else
1383 value = "true";
1384
Jeff King95b567c2014-06-18 15:48:29 -04001385 result = set_option(arg, value);
Shawn O. Pearceef08ef92009-10-30 17:47:29 -07001386 if (!result)
1387 printf("ok\n");
1388 else if (result < 0)
1389 printf("error invalid value\n");
1390 else
1391 printf("unsupported\n");
Daniel Barkalowa2d725b2009-08-05 01:01:56 -04001392 fflush(stdout);
Shawn O. Pearceef08ef92009-10-30 17:47:29 -07001393
Daniel Barkalowa2d725b2009-08-05 01:01:56 -04001394 } else if (!strcmp(buf.buf, "capabilities")) {
Brandon Williams0f1dc532018-03-15 10:31:41 -07001395 printf("stateless-connect\n");
Daniel Barkalowa2d725b2009-08-05 01:01:56 -04001396 printf("fetch\n");
Shawn O. Pearceef08ef92009-10-30 17:47:29 -07001397 printf("option\n");
Shawn O. Pearceae4efe12009-10-30 17:47:30 -07001398 printf("push\n");
Nguyễn Thái Ngọc Duy9ba38042013-07-21 15:18:05 +07001399 printf("check-connectivity\n");
Daniel Barkalowa2d725b2009-08-05 01:01:56 -04001400 printf("\n");
1401 fflush(stdout);
Brandon Williams0f1dc532018-03-15 10:31:41 -07001402 } else if (skip_prefix(buf.buf, "stateless-connect ", &arg)) {
1403 if (!stateless_connect(arg))
1404 break;
Daniel Barkalowa2d725b2009-08-05 01:01:56 -04001405 } else {
Jeff Kingcdaa4e92014-07-09 17:47:20 -04001406 error("remote-curl: unknown command '%s' from git", buf.buf);
Daniel Barkalowa2d725b2009-08-05 01:01:56 -04001407 return 1;
1408 }
1409 strbuf_reset(&buf);
1410 } while (1);
Tay Ray Chuan888692b2010-03-02 18:49:29 +08001411
1412 http_cleanup();
1413
Daniel Barkalowa2d725b2009-08-05 01:01:56 -04001414 return 0;
1415}