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