static int rpc_service(struct rpc_state *rpc, struct discovery *heads) { const char *svc = rpc->service_name; struct strbuf buf = STRBUF_INIT; struct child_process client; int err = 0; memset(&client, 0, sizeof(client)); client.in = -1; client.out = -1; client.git_cmd = 1; client.argv = rpc->argv; if (start_command(&client)) exit(1); if (heads) write_or_die(client.in, heads->buf, heads->len); rpc->alloc = http_post_buffer; rpc->buf = xmalloc(rpc->alloc); rpc->in = client.in; rpc->out = client.out; strbuf_init(&rpc->result, 0); strbuf_addf(&buf, "%s%s", url, svc); rpc->service_url = strbuf_detach(&buf, NULL); strbuf_addf(&buf, "Content-Type: application/x-%s-request", svc); rpc->hdr_content_type = strbuf_detach(&buf, NULL); strbuf_addf(&buf, "Accept: application/x-%s-result", svc); rpc->hdr_accept = strbuf_detach(&buf, NULL); while (!err) { int n = packet_read_line(rpc->out, rpc->buf, rpc->alloc); if (!n) break; rpc->pos = 0; rpc->len = n; err |= post_rpc(rpc); } strbuf_read(&rpc->result, client.out, 0); close(client.in); close(client.out); client.in = -1; client.out = -1; err |= finish_command(&client); free(rpc->service_url); free(rpc->hdr_content_type); free(rpc->hdr_accept); free(rpc->buf); strbuf_release(&buf); return err; }
static int rpc_service(struct rpc_state *rpc, struct discovery *heads) { const char *svc = rpc->service_name; struct strbuf buf = STRBUF_INIT; struct strbuf *preamble = rpc->stdin_preamble; struct child_process client = CHILD_PROCESS_INIT; int err = 0; client.in = -1; client.out = -1; client.git_cmd = 1; client.argv = rpc->argv; if (start_command(&client)) exit(1); if (preamble) write_or_die(client.in, preamble->buf, preamble->len); if (heads) write_or_die(client.in, heads->buf, heads->len); rpc->alloc = http_post_buffer; rpc->buf = xmalloc(rpc->alloc); rpc->in = client.in; rpc->out = client.out; strbuf_init(&rpc->result, 0); strbuf_addf(&buf, "%s%s", url.buf, svc); rpc->service_url = strbuf_detach(&buf, NULL); strbuf_addf(&buf, "Content-Type: application/x-%s-request", svc); rpc->hdr_content_type = strbuf_detach(&buf, NULL); strbuf_addf(&buf, "Accept: application/x-%s-result", svc); rpc->hdr_accept = strbuf_detach(&buf, NULL); while (!err) { int n = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0); if (!n) break; rpc->pos = 0; rpc->len = n; err |= post_rpc(rpc); } close(client.in); client.in = -1; if (!err) { strbuf_read(&rpc->result, client.out, 0); } else { char buf[4096]; for (;;) if (xread(client.out, buf, sizeof(buf)) <= 0) break; } close(client.out); client.out = -1; err |= finish_command(&client); free(rpc->service_url); free(rpc->hdr_content_type); free(rpc->hdr_accept); free(rpc->buf); strbuf_release(&buf); return err; }