Git 2.30.5
[git/debian.git] / serve.c
blobeec2fe6f294cd1c0c5e8a76b66b09d78dd4e22da
1 #include "cache.h"
2 #include "repository.h"
3 #include "config.h"
4 #include "pkt-line.h"
5 #include "version.h"
6 #include "strvec.h"
7 #include "ls-refs.h"
8 #include "serve.h"
9 #include "upload-pack.h"
11 static int advertise_sid;
13 static int always_advertise(struct repository *r,
14 struct strbuf *value)
16 return 1;
19 static int agent_advertise(struct repository *r,
20 struct strbuf *value)
22 if (value)
23 strbuf_addstr(value, git_user_agent_sanitized());
24 return 1;
27 static int object_format_advertise(struct repository *r,
28 struct strbuf *value)
30 if (value)
31 strbuf_addstr(value, r->hash_algo->name);
32 return 1;
35 static int session_id_advertise(struct repository *r, struct strbuf *value)
37 if (!advertise_sid)
38 return 0;
39 if (value)
40 strbuf_addstr(value, trace2_session_id());
41 return 1;
44 struct protocol_capability {
46 * The name of the capability. The server uses this name when
47 * advertising this capability, and the client uses this name to
48 * specify this capability.
50 const char *name;
53 * Function queried to see if a capability should be advertised.
54 * Optionally a value can be specified by adding it to 'value'.
55 * If a value is added to 'value', the server will advertise this
56 * capability as "<name>=<value>" instead of "<name>".
58 int (*advertise)(struct repository *r, struct strbuf *value);
61 * Function called when a client requests the capability as a command.
62 * The function will be provided the capabilities requested via 'keys'
63 * as well as a struct packet_reader 'request' which the command should
64 * use to read the command specific part of the request. Every command
65 * MUST read until a flush packet is seen before sending a response.
67 * This field should be NULL for capabilities which are not commands.
69 int (*command)(struct repository *r,
70 struct strvec *keys,
71 struct packet_reader *request);
74 static struct protocol_capability capabilities[] = {
75 { "agent", agent_advertise, NULL },
76 { "ls-refs", always_advertise, ls_refs },
77 { "fetch", upload_pack_advertise, upload_pack_v2 },
78 { "server-option", always_advertise, NULL },
79 { "object-format", object_format_advertise, NULL },
80 { "session-id", session_id_advertise, NULL },
83 static void advertise_capabilities(void)
85 struct strbuf capability = STRBUF_INIT;
86 struct strbuf value = STRBUF_INIT;
87 int i;
89 for (i = 0; i < ARRAY_SIZE(capabilities); i++) {
90 struct protocol_capability *c = &capabilities[i];
92 if (c->advertise(the_repository, &value)) {
93 strbuf_addstr(&capability, c->name);
95 if (value.len) {
96 strbuf_addch(&capability, '=');
97 strbuf_addbuf(&capability, &value);
100 strbuf_addch(&capability, '\n');
101 packet_write(1, capability.buf, capability.len);
104 strbuf_reset(&capability);
105 strbuf_reset(&value);
108 packet_flush(1);
109 strbuf_release(&capability);
110 strbuf_release(&value);
113 static struct protocol_capability *get_capability(const char *key)
115 int i;
117 if (!key)
118 return NULL;
120 for (i = 0; i < ARRAY_SIZE(capabilities); i++) {
121 struct protocol_capability *c = &capabilities[i];
122 const char *out;
123 if (skip_prefix(key, c->name, &out) && (!*out || *out == '='))
124 return c;
127 return NULL;
130 static int is_valid_capability(const char *key)
132 const struct protocol_capability *c = get_capability(key);
134 return c && c->advertise(the_repository, NULL);
137 static int is_command(const char *key, struct protocol_capability **command)
139 const char *out;
141 if (skip_prefix(key, "command=", &out)) {
142 struct protocol_capability *cmd = get_capability(out);
144 if (*command)
145 die("command '%s' requested after already requesting command '%s'",
146 out, (*command)->name);
147 if (!cmd || !cmd->advertise(the_repository, NULL) || !cmd->command)
148 die("invalid command '%s'", out);
150 *command = cmd;
151 return 1;
154 return 0;
157 int has_capability(const struct strvec *keys, const char *capability,
158 const char **value)
160 int i;
161 for (i = 0; i < keys->nr; i++) {
162 const char *out;
163 if (skip_prefix(keys->v[i], capability, &out) &&
164 (!*out || *out == '=')) {
165 if (value) {
166 if (*out == '=')
167 out++;
168 *value = out;
170 return 1;
174 return 0;
177 static void check_algorithm(struct repository *r, struct strvec *keys)
179 int client = GIT_HASH_SHA1, server = hash_algo_by_ptr(r->hash_algo);
180 const char *algo_name;
182 if (has_capability(keys, "object-format", &algo_name)) {
183 client = hash_algo_by_name(algo_name);
184 if (client == GIT_HASH_UNKNOWN)
185 die("unknown object format '%s'", algo_name);
188 if (client != server)
189 die("mismatched object format: server %s; client %s\n",
190 r->hash_algo->name, hash_algos[client].name);
193 enum request_state {
194 PROCESS_REQUEST_KEYS,
195 PROCESS_REQUEST_DONE,
198 static int process_request(void)
200 enum request_state state = PROCESS_REQUEST_KEYS;
201 struct packet_reader reader;
202 struct strvec keys = STRVEC_INIT;
203 struct protocol_capability *command = NULL;
204 const char *client_sid;
206 packet_reader_init(&reader, 0, NULL, 0,
207 PACKET_READ_CHOMP_NEWLINE |
208 PACKET_READ_GENTLE_ON_EOF |
209 PACKET_READ_DIE_ON_ERR_PACKET);
212 * Check to see if the client closed their end before sending another
213 * request. If so we can terminate the connection.
215 if (packet_reader_peek(&reader) == PACKET_READ_EOF)
216 return 1;
217 reader.options &= ~PACKET_READ_GENTLE_ON_EOF;
219 while (state != PROCESS_REQUEST_DONE) {
220 switch (packet_reader_peek(&reader)) {
221 case PACKET_READ_EOF:
222 BUG("Should have already died when seeing EOF");
223 case PACKET_READ_NORMAL:
224 /* collect request; a sequence of keys and values */
225 if (is_command(reader.line, &command) ||
226 is_valid_capability(reader.line))
227 strvec_push(&keys, reader.line);
228 else
229 die("unknown capability '%s'", reader.line);
231 /* Consume the peeked line */
232 packet_reader_read(&reader);
233 break;
234 case PACKET_READ_FLUSH:
236 * If no command and no keys were given then the client
237 * wanted to terminate the connection.
239 if (!keys.nr)
240 return 1;
243 * The flush packet isn't consume here like it is in
244 * the other parts of this switch statement. This is
245 * so that the command can read the flush packet and
246 * see the end of the request in the same way it would
247 * if command specific arguments were provided after a
248 * delim packet.
250 state = PROCESS_REQUEST_DONE;
251 break;
252 case PACKET_READ_DELIM:
253 /* Consume the peeked line */
254 packet_reader_read(&reader);
256 state = PROCESS_REQUEST_DONE;
257 break;
258 case PACKET_READ_RESPONSE_END:
259 BUG("unexpected stateless separator packet");
263 if (!command)
264 die("no command requested");
266 check_algorithm(the_repository, &keys);
268 if (has_capability(&keys, "session-id", &client_sid))
269 trace2_data_string("transfer", NULL, "client-sid", client_sid);
271 command->command(the_repository, &keys, &reader);
273 strvec_clear(&keys);
274 return 0;
277 /* Main serve loop for protocol version 2 */
278 void serve(struct serve_options *options)
280 git_config_get_bool("transfer.advertisesid", &advertise_sid);
282 if (options->advertise_capabilities || !options->stateless_rpc) {
283 /* serve by default supports v2 */
284 packet_write_fmt(1, "version 2\n");
286 advertise_capabilities();
288 * If only the list of capabilities was requested exit
289 * immediately after advertising capabilities
291 if (options->advertise_capabilities)
292 return;
296 * If stateless-rpc was requested then exit after
297 * a single request/response exchange
299 if (options->stateless_rpc) {
300 process_request();
301 } else {
302 for (;;)
303 if (process_request())
304 break;