2 #include "repository.h"
8 #include "protocol-caps.h"
10 #include "upload-pack.h"
12 static int advertise_sid;
14 static int always_advertise(struct repository *r,
20 static int agent_advertise(struct repository *r,
24 strbuf_addstr(value, git_user_agent_sanitized());
28 static int object_format_advertise(struct repository *r,
32 strbuf_addstr(value, r->hash_algo->name);
36 static int session_id_advertise(struct repository *r, struct strbuf *value)
41 strbuf_addstr(value, trace2_session_id());
45 struct protocol_capability {
47 * The name of the capability. The server uses this name when
48 * advertising this capability, and the client uses this name to
49 * specify this capability.
54 * Function queried to see if a capability should be advertised.
55 * Optionally a value can be specified by adding it to 'value'.
56 * If a value is added to 'value', the server will advertise this
57 * capability as "<name>=<value>" instead of "<name>".
59 int (*advertise)(struct repository *r, struct strbuf *value);
62 * Function called when a client requests the capability as a command.
63 * The function will be provided the capabilities requested via 'keys'
64 * as well as a struct packet_reader 'request' which the command should
65 * use to read the command specific part of the request. Every command
66 * MUST read until a flush packet is seen before sending a response.
68 * This field should be NULL for capabilities which are not commands.
70 int (*command)(struct repository *r,
72 struct packet_reader *request);
75 static struct protocol_capability capabilities[] = {
78 .advertise = agent_advertise,
82 .advertise = ls_refs_advertise,
87 .advertise = upload_pack_advertise,
88 .command = upload_pack_v2,
91 .name = "server-option",
92 .advertise = always_advertise,
95 .name = "object-format",
96 .advertise = object_format_advertise,
100 .advertise = session_id_advertise,
103 .name = "object-info",
104 .advertise = always_advertise,
105 .command = cap_object_info,
109 static void advertise_capabilities(void)
111 struct strbuf capability = STRBUF_INIT;
112 struct strbuf value = STRBUF_INIT;
115 for (i = 0; i < ARRAY_SIZE(capabilities); i++) {
116 struct protocol_capability *c = &capabilities[i];
118 if (c->advertise(the_repository, &value)) {
119 strbuf_addstr(&capability, c->name);
122 strbuf_addch(&capability, '=');
123 strbuf_addbuf(&capability, &value);
126 strbuf_addch(&capability, '\n');
127 packet_write(1, capability.buf, capability.len);
130 strbuf_reset(&capability);
131 strbuf_reset(&value);
135 strbuf_release(&capability);
136 strbuf_release(&value);
139 static struct protocol_capability *get_capability(const char *key)
146 for (i = 0; i < ARRAY_SIZE(capabilities); i++) {
147 struct protocol_capability *c = &capabilities[i];
149 if (skip_prefix(key, c->name, &out) && (!*out || *out == '='))
156 static int is_valid_capability(const char *key)
158 const struct protocol_capability *c = get_capability(key);
160 return c && c->advertise(the_repository, NULL);
163 static int is_command(const char *key, struct protocol_capability **command)
167 if (skip_prefix(key, "command=", &out)) {
168 struct protocol_capability *cmd = get_capability(out);
171 die("command '%s' requested after already requesting command '%s'",
172 out, (*command)->name);
173 if (!cmd || !cmd->advertise(the_repository, NULL) || !cmd->command)
174 die("invalid command '%s'", out);
183 static int has_capability(const struct strvec *keys, const char *capability,
187 for (i = 0; i < keys->nr; i++) {
189 if (skip_prefix(keys->v[i], capability, &out) &&
190 (!*out || *out == '=')) {
203 static void check_algorithm(struct repository *r, struct strvec *keys)
205 int client = GIT_HASH_SHA1, server = hash_algo_by_ptr(r->hash_algo);
206 const char *algo_name;
208 if (has_capability(keys, "object-format", &algo_name)) {
209 client = hash_algo_by_name(algo_name);
210 if (client == GIT_HASH_UNKNOWN)
211 die("unknown object format '%s'", algo_name);
214 if (client != server)
215 die("mismatched object format: server %s; client %s\n",
216 r->hash_algo->name, hash_algos[client].name);
220 PROCESS_REQUEST_KEYS,
221 PROCESS_REQUEST_DONE,
224 static int process_request(void)
226 enum request_state state = PROCESS_REQUEST_KEYS;
227 struct packet_reader reader;
228 struct strvec keys = STRVEC_INIT;
229 struct protocol_capability *command = NULL;
230 const char *client_sid;
232 packet_reader_init(&reader, 0, NULL, 0,
233 PACKET_READ_CHOMP_NEWLINE |
234 PACKET_READ_GENTLE_ON_EOF |
235 PACKET_READ_DIE_ON_ERR_PACKET);
238 * Check to see if the client closed their end before sending another
239 * request. If so we can terminate the connection.
241 if (packet_reader_peek(&reader) == PACKET_READ_EOF)
243 reader.options &= ~PACKET_READ_GENTLE_ON_EOF;
245 while (state != PROCESS_REQUEST_DONE) {
246 switch (packet_reader_peek(&reader)) {
247 case PACKET_READ_EOF:
248 BUG("Should have already died when seeing EOF");
249 case PACKET_READ_NORMAL:
250 /* collect request; a sequence of keys and values */
251 if (is_command(reader.line, &command) ||
252 is_valid_capability(reader.line))
253 strvec_push(&keys, reader.line);
255 die("unknown capability '%s'", reader.line);
257 /* Consume the peeked line */
258 packet_reader_read(&reader);
260 case PACKET_READ_FLUSH:
262 * If no command and no keys were given then the client
263 * wanted to terminate the connection.
269 * The flush packet isn't consume here like it is in
270 * the other parts of this switch statement. This is
271 * so that the command can read the flush packet and
272 * see the end of the request in the same way it would
273 * if command specific arguments were provided after a
276 state = PROCESS_REQUEST_DONE;
278 case PACKET_READ_DELIM:
279 /* Consume the peeked line */
280 packet_reader_read(&reader);
282 state = PROCESS_REQUEST_DONE;
284 case PACKET_READ_RESPONSE_END:
285 BUG("unexpected stateless separator packet");
290 die("no command requested");
292 check_algorithm(the_repository, &keys);
294 if (has_capability(&keys, "session-id", &client_sid))
295 trace2_data_string("transfer", NULL, "client-sid", client_sid);
297 command->command(the_repository, &keys, &reader);
303 /* Main serve loop for protocol version 2 */
304 void serve(struct serve_options *options)
306 git_config_get_bool("transfer.advertisesid", &advertise_sid);
308 if (options->advertise_capabilities || !options->stateless_rpc) {
309 /* serve by default supports v2 */
310 packet_write_fmt(1, "version 2\n");
312 advertise_capabilities();
314 * If only the list of capabilities was requested exit
315 * immediately after advertising capabilities
317 if (options->advertise_capabilities)
322 * If stateless-rpc was requested then exit after
323 * a single request/response exchange
325 if (options->stateless_rpc) {
329 if (process_request())