remote-curl.c 40 KB

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