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