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