The sixteenth batch
[git/gitster.git] / serve.c
blob53ecab3b42b44fd6aa96eea1e1fdfeba9336e644
1 #include "git-compat-util.h"
2 #include "repository.h"
3 #include "config.h"
4 #include "hash.h"
5 #include "pkt-line.h"
6 #include "version.h"
7 #include "ls-refs.h"
8 #include "protocol-caps.h"
9 #include "serve.h"
10 #include "upload-pack.h"
11 #include "bundle-uri.h"
12 #include "trace2.h"
13 #include "promisor-remote.h"
15 static int advertise_sid = -1;
16 static int advertise_object_info = -1;
17 static int client_hash_algo = GIT_HASH_SHA1_LEGACY;
19 static int always_advertise(struct repository *r UNUSED,
20 struct strbuf *value UNUSED)
22 return 1;
25 static int agent_advertise(struct repository *r UNUSED,
26 struct strbuf *value)
28 if (value)
29 strbuf_addstr(value, git_user_agent_sanitized());
30 return 1;
33 static int promisor_remote_advertise(struct repository *r,
34 struct strbuf *value)
36 if (value) {
37 char *info = promisor_remote_info(r);
38 if (!info)
39 return 0;
40 strbuf_addstr(value, info);
41 free(info);
43 return 1;
46 static void promisor_remote_receive(struct repository *r,
47 const char *remotes)
49 mark_promisor_remotes_as_accepted(r, remotes);
53 static int object_format_advertise(struct repository *r,
54 struct strbuf *value)
56 if (value)
57 strbuf_addstr(value, r->hash_algo->name);
58 return 1;
61 static void object_format_receive(struct repository *r UNUSED,
62 const char *algo_name)
64 if (!algo_name)
65 die("object-format capability requires an argument");
67 client_hash_algo = hash_algo_by_name(algo_name);
68 if (client_hash_algo == GIT_HASH_UNKNOWN)
69 die("unknown object format '%s'", algo_name);
72 static int session_id_advertise(struct repository *r, struct strbuf *value)
74 if (advertise_sid == -1 &&
75 repo_config_get_bool(r, "transfer.advertisesid", &advertise_sid))
76 advertise_sid = 0;
77 if (!advertise_sid)
78 return 0;
79 if (value)
80 strbuf_addstr(value, trace2_session_id());
81 return 1;
84 static void session_id_receive(struct repository *r UNUSED,
85 const char *client_sid)
87 if (!client_sid)
88 client_sid = "";
89 trace2_data_string("transfer", NULL, "client-sid", client_sid);
92 static int object_info_advertise(struct repository *r, struct strbuf *value UNUSED)
94 if (advertise_object_info == -1 &&
95 repo_config_get_bool(r, "transfer.advertiseobjectinfo",
96 &advertise_object_info)) {
97 /* disabled by default */
98 advertise_object_info = 0;
100 return advertise_object_info;
103 struct protocol_capability {
105 * The name of the capability. The server uses this name when
106 * advertising this capability, and the client uses this name to
107 * specify this capability.
109 const char *name;
112 * Function queried to see if a capability should be advertised.
113 * Optionally a value can be specified by adding it to 'value'.
114 * If a value is added to 'value', the server will advertise this
115 * capability as "<name>=<value>" instead of "<name>".
117 int (*advertise)(struct repository *r, struct strbuf *value);
120 * Function called when a client requests the capability as a command.
121 * Will be provided a struct packet_reader 'request' which it should
122 * use to read the command specific part of the request. Every command
123 * MUST read until a flush packet is seen before sending a response.
125 * This field should be NULL for capabilities which are not commands.
127 int (*command)(struct repository *r, struct packet_reader *request);
130 * Function called when a client requests the capability as a
131 * non-command. This may be NULL if the capability does nothing.
133 * For a capability of the form "foo=bar", the value string points to
134 * the content after the "=" (i.e., "bar"). For simple capabilities
135 * (just "foo"), it is NULL.
137 void (*receive)(struct repository *r, const char *value);
140 static struct protocol_capability capabilities[] = {
142 .name = "agent",
143 .advertise = agent_advertise,
146 .name = "ls-refs",
147 .advertise = ls_refs_advertise,
148 .command = ls_refs,
151 .name = "fetch",
152 .advertise = upload_pack_advertise,
153 .command = upload_pack_v2,
156 .name = "server-option",
157 .advertise = always_advertise,
160 .name = "object-format",
161 .advertise = object_format_advertise,
162 .receive = object_format_receive,
165 .name = "session-id",
166 .advertise = session_id_advertise,
167 .receive = session_id_receive,
170 .name = "object-info",
171 .advertise = object_info_advertise,
172 .command = cap_object_info,
175 .name = "bundle-uri",
176 .advertise = bundle_uri_advertise,
177 .command = bundle_uri_command,
180 .name = "promisor-remote",
181 .advertise = promisor_remote_advertise,
182 .receive = promisor_remote_receive,
186 void protocol_v2_advertise_capabilities(struct repository *r)
188 struct strbuf capability = STRBUF_INIT;
189 struct strbuf value = STRBUF_INIT;
191 /* serve by default supports v2 */
192 packet_write_fmt(1, "version 2\n");
194 for (size_t i = 0; i < ARRAY_SIZE(capabilities); i++) {
195 struct protocol_capability *c = &capabilities[i];
197 if (c->advertise(r, &value)) {
198 strbuf_addstr(&capability, c->name);
200 if (value.len) {
201 strbuf_addch(&capability, '=');
202 strbuf_addbuf(&capability, &value);
205 strbuf_addch(&capability, '\n');
206 packet_write(1, capability.buf, capability.len);
209 strbuf_reset(&capability);
210 strbuf_reset(&value);
213 packet_flush(1);
214 strbuf_release(&capability);
215 strbuf_release(&value);
218 static struct protocol_capability *get_capability(const char *key, const char **value)
220 if (!key)
221 return NULL;
223 for (size_t i = 0; i < ARRAY_SIZE(capabilities); i++) {
224 struct protocol_capability *c = &capabilities[i];
225 const char *out;
226 if (!skip_prefix(key, c->name, &out))
227 continue;
228 if (!*out) {
229 *value = NULL;
230 return c;
232 if (*out++ == '=') {
233 *value = out;
234 return c;
238 return NULL;
241 static int receive_client_capability(struct repository *r, const char *key)
243 const char *value;
244 const struct protocol_capability *c = get_capability(key, &value);
246 if (!c || c->command || !c->advertise(r, NULL))
247 return 0;
249 if (c->receive)
250 c->receive(r, value);
251 return 1;
254 static int parse_command(struct repository *r, const char *key, struct protocol_capability **command)
256 const char *out;
258 if (skip_prefix(key, "command=", &out)) {
259 const char *value;
260 struct protocol_capability *cmd = get_capability(out, &value);
262 if (*command)
263 die("command '%s' requested after already requesting command '%s'",
264 out, (*command)->name);
265 if (!cmd || !cmd->advertise(r, NULL) || !cmd->command || value)
266 die("invalid command '%s'", out);
268 *command = cmd;
269 return 1;
272 return 0;
275 enum request_state {
276 PROCESS_REQUEST_KEYS,
277 PROCESS_REQUEST_DONE,
280 static int process_request(struct repository *r)
282 enum request_state state = PROCESS_REQUEST_KEYS;
283 struct packet_reader reader;
284 int seen_capability_or_command = 0;
285 struct protocol_capability *command = NULL;
287 packet_reader_init(&reader, 0, NULL, 0,
288 PACKET_READ_CHOMP_NEWLINE |
289 PACKET_READ_GENTLE_ON_EOF |
290 PACKET_READ_DIE_ON_ERR_PACKET);
293 * Check to see if the client closed their end before sending another
294 * request. If so we can terminate the connection.
296 if (packet_reader_peek(&reader) == PACKET_READ_EOF)
297 return 1;
298 reader.options &= ~PACKET_READ_GENTLE_ON_EOF;
300 while (state != PROCESS_REQUEST_DONE) {
301 switch (packet_reader_peek(&reader)) {
302 case PACKET_READ_EOF:
303 BUG("Should have already died when seeing EOF");
304 case PACKET_READ_NORMAL:
305 if (parse_command(r, reader.line, &command) ||
306 receive_client_capability(r, reader.line))
307 seen_capability_or_command = 1;
308 else
309 die("unknown capability '%s'", reader.line);
311 /* Consume the peeked line */
312 packet_reader_read(&reader);
313 break;
314 case PACKET_READ_FLUSH:
316 * If no command and no keys were given then the client
317 * wanted to terminate the connection.
319 if (!seen_capability_or_command)
320 return 1;
323 * The flush packet isn't consume here like it is in
324 * the other parts of this switch statement. This is
325 * so that the command can read the flush packet and
326 * see the end of the request in the same way it would
327 * if command specific arguments were provided after a
328 * delim packet.
330 state = PROCESS_REQUEST_DONE;
331 break;
332 case PACKET_READ_DELIM:
333 /* Consume the peeked line */
334 packet_reader_read(&reader);
336 state = PROCESS_REQUEST_DONE;
337 break;
338 case PACKET_READ_RESPONSE_END:
339 BUG("unexpected response end packet");
343 if (!command)
344 die("no command requested");
346 if (client_hash_algo != hash_algo_by_ptr(r->hash_algo))
347 die("mismatched object format: server %s; client %s",
348 r->hash_algo->name,
349 hash_algos[client_hash_algo].name);
351 command->command(r, &reader);
353 return 0;
356 void protocol_v2_serve_loop(struct repository *r, int stateless_rpc)
358 if (!stateless_rpc)
359 protocol_v2_advertise_capabilities(r);
362 * If stateless-rpc was requested then exit after
363 * a single request/response exchange
365 if (stateless_rpc) {
366 process_request(r);
367 } else {
368 for (;;)
369 if (process_request(r))
370 break;