git
/
serve.c
351 строка · 8.4 Кб
1#define USE_THE_REPOSITORY_VARIABLE2
3#include "git-compat-util.h"4#include "repository.h"5#include "config.h"6#include "hash.h"7#include "pkt-line.h"8#include "version.h"9#include "ls-refs.h"10#include "protocol-caps.h"11#include "serve.h"12#include "upload-pack.h"13#include "bundle-uri.h"14#include "trace2.h"15
16static int advertise_sid = -1;17static int advertise_object_info = -1;18static int client_hash_algo = GIT_HASH_SHA1;19
20static int always_advertise(struct repository *r UNUSED,21struct strbuf *value UNUSED)22{
23return 1;24}
25
26static int agent_advertise(struct repository *r UNUSED,27struct strbuf *value)28{
29if (value)30strbuf_addstr(value, git_user_agent_sanitized());31return 1;32}
33
34static int object_format_advertise(struct repository *r,35struct strbuf *value)36{
37if (value)38strbuf_addstr(value, r->hash_algo->name);39return 1;40}
41
42static void object_format_receive(struct repository *r UNUSED,43const char *algo_name)44{
45if (!algo_name)46die("object-format capability requires an argument");47
48client_hash_algo = hash_algo_by_name(algo_name);49if (client_hash_algo == GIT_HASH_UNKNOWN)50die("unknown object format '%s'", algo_name);51}
52
53static int session_id_advertise(struct repository *r, struct strbuf *value)54{
55if (advertise_sid == -1 &&56repo_config_get_bool(r, "transfer.advertisesid", &advertise_sid))57advertise_sid = 0;58if (!advertise_sid)59return 0;60if (value)61strbuf_addstr(value, trace2_session_id());62return 1;63}
64
65static void session_id_receive(struct repository *r UNUSED,66const char *client_sid)67{
68if (!client_sid)69client_sid = "";70trace2_data_string("transfer", NULL, "client-sid", client_sid);71}
72
73static int object_info_advertise(struct repository *r, struct strbuf *value UNUSED)74{
75if (advertise_object_info == -1 &&76repo_config_get_bool(r, "transfer.advertiseobjectinfo",77&advertise_object_info)) {78/* disabled by default */79advertise_object_info = 0;80}81return advertise_object_info;82}
83
84struct protocol_capability {85/*86* The name of the capability. The server uses this name when
87* advertising this capability, and the client uses this name to
88* specify this capability.
89*/
90const char *name;91
92/*93* Function queried to see if a capability should be advertised.
94* Optionally a value can be specified by adding it to 'value'.
95* If a value is added to 'value', the server will advertise this
96* capability as "<name>=<value>" instead of "<name>".
97*/
98int (*advertise)(struct repository *r, struct strbuf *value);99
100/*101* Function called when a client requests the capability as a command.
102* Will be provided a struct packet_reader 'request' which it should
103* use to read the command specific part of the request. Every command
104* MUST read until a flush packet is seen before sending a response.
105*
106* This field should be NULL for capabilities which are not commands.
107*/
108int (*command)(struct repository *r, struct packet_reader *request);109
110/*111* Function called when a client requests the capability as a
112* non-command. This may be NULL if the capability does nothing.
113*
114* For a capability of the form "foo=bar", the value string points to
115* the content after the "=" (i.e., "bar"). For simple capabilities
116* (just "foo"), it is NULL.
117*/
118void (*receive)(struct repository *r, const char *value);119};120
121static struct protocol_capability capabilities[] = {122{123.name = "agent",124.advertise = agent_advertise,125},126{127.name = "ls-refs",128.advertise = ls_refs_advertise,129.command = ls_refs,130},131{132.name = "fetch",133.advertise = upload_pack_advertise,134.command = upload_pack_v2,135},136{137.name = "server-option",138.advertise = always_advertise,139},140{141.name = "object-format",142.advertise = object_format_advertise,143.receive = object_format_receive,144},145{146.name = "session-id",147.advertise = session_id_advertise,148.receive = session_id_receive,149},150{151.name = "object-info",152.advertise = object_info_advertise,153.command = cap_object_info,154},155{156.name = "bundle-uri",157.advertise = bundle_uri_advertise,158.command = bundle_uri_command,159},160};161
162void protocol_v2_advertise_capabilities(void)163{
164struct strbuf capability = STRBUF_INIT;165struct strbuf value = STRBUF_INIT;166int i;167
168/* serve by default supports v2 */169packet_write_fmt(1, "version 2\n");170
171for (i = 0; i < ARRAY_SIZE(capabilities); i++) {172struct protocol_capability *c = &capabilities[i];173
174if (c->advertise(the_repository, &value)) {175strbuf_addstr(&capability, c->name);176
177if (value.len) {178strbuf_addch(&capability, '=');179strbuf_addbuf(&capability, &value);180}181
182strbuf_addch(&capability, '\n');183packet_write(1, capability.buf, capability.len);184}185
186strbuf_reset(&capability);187strbuf_reset(&value);188}189
190packet_flush(1);191strbuf_release(&capability);192strbuf_release(&value);193}
194
195static struct protocol_capability *get_capability(const char *key, const char **value)196{
197int i;198
199if (!key)200return NULL;201
202for (i = 0; i < ARRAY_SIZE(capabilities); i++) {203struct protocol_capability *c = &capabilities[i];204const char *out;205if (!skip_prefix(key, c->name, &out))206continue;207if (!*out) {208*value = NULL;209return c;210}211if (*out++ == '=') {212*value = out;213return c;214}215}216
217return NULL;218}
219
220static int receive_client_capability(const char *key)221{
222const char *value;223const struct protocol_capability *c = get_capability(key, &value);224
225if (!c || c->command || !c->advertise(the_repository, NULL))226return 0;227
228if (c->receive)229c->receive(the_repository, value);230return 1;231}
232
233static int parse_command(const char *key, struct protocol_capability **command)234{
235const char *out;236
237if (skip_prefix(key, "command=", &out)) {238const char *value;239struct protocol_capability *cmd = get_capability(out, &value);240
241if (*command)242die("command '%s' requested after already requesting command '%s'",243out, (*command)->name);244if (!cmd || !cmd->advertise(the_repository, NULL) || !cmd->command || value)245die("invalid command '%s'", out);246
247*command = cmd;248return 1;249}250
251return 0;252}
253
254enum request_state {255PROCESS_REQUEST_KEYS,256PROCESS_REQUEST_DONE,257};258
259static int process_request(void)260{
261enum request_state state = PROCESS_REQUEST_KEYS;262struct packet_reader reader;263int seen_capability_or_command = 0;264struct protocol_capability *command = NULL;265
266packet_reader_init(&reader, 0, NULL, 0,267PACKET_READ_CHOMP_NEWLINE |268PACKET_READ_GENTLE_ON_EOF |269PACKET_READ_DIE_ON_ERR_PACKET);270
271/*272* Check to see if the client closed their end before sending another
273* request. If so we can terminate the connection.
274*/
275if (packet_reader_peek(&reader) == PACKET_READ_EOF)276return 1;277reader.options &= ~PACKET_READ_GENTLE_ON_EOF;278
279while (state != PROCESS_REQUEST_DONE) {280switch (packet_reader_peek(&reader)) {281case PACKET_READ_EOF:282BUG("Should have already died when seeing EOF");283case PACKET_READ_NORMAL:284if (parse_command(reader.line, &command) ||285receive_client_capability(reader.line))286seen_capability_or_command = 1;287else288die("unknown capability '%s'", reader.line);289
290/* Consume the peeked line */291packet_reader_read(&reader);292break;293case PACKET_READ_FLUSH:294/*295* If no command and no keys were given then the client
296* wanted to terminate the connection.
297*/
298if (!seen_capability_or_command)299return 1;300
301/*302* The flush packet isn't consume here like it is in
303* the other parts of this switch statement. This is
304* so that the command can read the flush packet and
305* see the end of the request in the same way it would
306* if command specific arguments were provided after a
307* delim packet.
308*/
309state = PROCESS_REQUEST_DONE;310break;311case PACKET_READ_DELIM:312/* Consume the peeked line */313packet_reader_read(&reader);314
315state = PROCESS_REQUEST_DONE;316break;317case PACKET_READ_RESPONSE_END:318BUG("unexpected response end packet");319}320}321
322if (!command)323die("no command requested");324
325if (client_hash_algo != hash_algo_by_ptr(the_repository->hash_algo))326die("mismatched object format: server %s; client %s\n",327the_repository->hash_algo->name,328hash_algos[client_hash_algo].name);329
330command->command(the_repository, &reader);331
332return 0;333}
334
335void protocol_v2_serve_loop(int stateless_rpc)336{
337if (!stateless_rpc)338protocol_v2_advertise_capabilities();339
340/*341* If stateless-rpc was requested then exit after
342* a single request/response exchange
343*/
344if (stateless_rpc) {345process_request();346} else {347for (;;)348if (process_request())349break;350}351}
352