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