serve: add "receive" method for v2 capabilities table

We have a capabilities table that tells us what we should tell the client we are capable of, and what to do when a client gives us a particular command (e.g., "command=ls-refs"). But it doesn't tell us what to do when the client sends us back a capability (e.g., "object-format=sha256"). We just collect them all in a strvec and hope somebody can use them later. Instead, let's provide a function pointer in the table to act on these. This will eventually help us avoid collecting the strings, which will be more efficient and less prone to mischief. Using the new method is optional, which helps in two ways: - we can move existing capabilities over to this new system gradually in individual commits - some capabilities we don't actually do anything with anyway. For example, the client is free to say "agent=git/1.2.3" to us, but we do not act on the information in any way. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Sep 14, 2021 at 11:31 UTC e56e53067fc3d31899e837293a5bf72e2d086b4a
1 file changed +18 -3
serve.c
+18 -3
@@ -70,6 +70,16 @@ struct protocol_capability {
70 * This field should be NULL for capabilities which are not commands.
71 */
72 int (*command)(struct repository *r, struct packet_reader *request);
73 +
74 + /*
75 + * Function called when a client requests the capability as a
76 + * non-command. This may be NULL if the capability does nothing.
77 + *
78 + * For a capability of the form "foo=bar", the value string points to
79 + * the content after the "=" (i.e., "bar"). For simple capabilities
80 + * (just "foo"), it is NULL.
81 + */
82 + void (*receive)(struct repository *r, const char *value);
83 };
84
85 static struct protocol_capability capabilities[] = {
@@ -164,12 +174,17 @@ static struct protocol_capability *get_capability(const char *key, const char **
174 return NULL;
175 }
176
167 -static int is_valid_capability(const char *key)
177 +static int receive_client_capability(const char *key)
178 {
179 const char *value;
180 const struct protocol_capability *c = get_capability(key, &value);
181
172 - return c && c->advertise(the_repository, NULL);
182 + if (!c || !c->advertise(the_repository, NULL))
183 + return 0;
184 +
185 + if (c->receive)
186 + c->receive(the_repository, value);
187 + return 1;
188 }
189
190 static int parse_command(const char *key, struct protocol_capability **command)
@@ -262,7 +277,7 @@ static int process_request(void)
277 case PACKET_READ_NORMAL:
278 /* collect request; a sequence of keys and values */
279 if (parse_command(reader.line, &command) ||
265 - is_valid_capability(reader.line))
280 + receive_client_capability(reader.line))
281 strvec_push(&keys, reader.line);
282 else
283 die("unknown capability '%s'", reader.line);