2 #include "repository.h"
6 #include "argv-array.h"
9 #include "upload-pack.h"
11 static int always_advertise(struct repository *r,
17 static int agent_advertise(struct repository *r,
21 strbuf_addstr(value, git_user_agent_sanitized());
25 static int object_format_advertise(struct repository *r,
29 strbuf_addstr(value, r->hash_algo->name);
33 struct protocol_capability {
35 * The name of the capability. The server uses this name when
36 * advertising this capability, and the client uses this name to
37 * specify this capability.
42 * Function queried to see if a capability should be advertised.
43 * Optionally a value can be specified by adding it to 'value'.
44 * If a value is added to 'value', the server will advertise this
45 * capability as "<name>=<value>" instead of "<name>".
47 int (*advertise)(struct repository *r, struct strbuf *value);
50 * Function called when a client requests the capability as a command.
51 * The function will be provided the capabilities requested via 'keys'
52 * as well as a struct packet_reader 'request' which the command should
53 * use to read the command specific part of the request. Every command
54 * MUST read until a flush packet is seen before sending a response.
56 * This field should be NULL for capabilities which are not commands.
58 int (*command)(struct repository *r,
59 struct argv_array *keys,
60 struct packet_reader *request);
63 static struct protocol_capability capabilities[] = {
64 { "agent", agent_advertise, NULL },
65 { "ls-refs", always_advertise, ls_refs },
66 { "fetch", upload_pack_advertise, upload_pack_v2 },
67 { "server-option", always_advertise, NULL },
68 { "object-format", object_format_advertise, NULL },
71 static void advertise_capabilities(void)
73 struct strbuf capability = STRBUF_INIT;
74 struct strbuf value = STRBUF_INIT;
77 for (i = 0; i < ARRAY_SIZE(capabilities); i++) {
78 struct protocol_capability *c = &capabilities[i];
80 if (c->advertise(the_repository, &value)) {
81 strbuf_addstr(&capability, c->name);
84 strbuf_addch(&capability, '=');
85 strbuf_addbuf(&capability, &value);
88 strbuf_addch(&capability, '\n');
89 packet_write(1, capability.buf, capability.len);
92 strbuf_reset(&capability);
97 strbuf_release(&capability);
98 strbuf_release(&value);
101 static struct protocol_capability *get_capability(const char *key)
108 for (i = 0; i < ARRAY_SIZE(capabilities); i++) {
109 struct protocol_capability *c = &capabilities[i];
111 if (skip_prefix(key, c->name, &out) && (!*out || *out == '='))
118 static int is_valid_capability(const char *key)
120 const struct protocol_capability *c = get_capability(key);
122 return c && c->advertise(the_repository, NULL);
125 static int is_command(const char *key, struct protocol_capability **command)
129 if (skip_prefix(key, "command=", &out)) {
130 struct protocol_capability *cmd = get_capability(out);
133 die("command '%s' requested after already requesting command '%s'",
134 out, (*command)->name);
135 if (!cmd || !cmd->advertise(the_repository, NULL) || !cmd->command)
136 die("invalid command '%s'", out);
145 int has_capability(const struct argv_array *keys, const char *capability,
149 for (i = 0; i < keys->argc; i++) {
151 if (skip_prefix(keys->argv[i], capability, &out) &&
152 (!*out || *out == '=')) {
165 static void check_algorithm(struct repository *r, struct argv_array *keys)
167 int client = GIT_HASH_SHA1, server = hash_algo_by_ptr(r->hash_algo);
168 const char *algo_name;
170 if (has_capability(keys, "object-format", &algo_name)) {
171 client = hash_algo_by_name(algo_name);
172 if (client == GIT_HASH_UNKNOWN)
173 die("unknown object format '%s'", algo_name);
176 if (client != server)
177 die("mismatched object format: server %s; client %s\n",
178 r->hash_algo->name, hash_algos[client].name);
182 PROCESS_REQUEST_KEYS,
183 PROCESS_REQUEST_DONE,
186 static int process_request(void)
188 enum request_state state = PROCESS_REQUEST_KEYS;
189 struct packet_reader reader;
190 struct argv_array keys = ARGV_ARRAY_INIT;
191 struct protocol_capability *command = NULL;
193 packet_reader_init(&reader, 0, NULL, 0,
194 PACKET_READ_CHOMP_NEWLINE |
195 PACKET_READ_GENTLE_ON_EOF |
196 PACKET_READ_DIE_ON_ERR_PACKET);
199 * Check to see if the client closed their end before sending another
200 * request. If so we can terminate the connection.
202 if (packet_reader_peek(&reader) == PACKET_READ_EOF)
204 reader.options &= ~PACKET_READ_GENTLE_ON_EOF;
206 while (state != PROCESS_REQUEST_DONE) {
207 switch (packet_reader_peek(&reader)) {
208 case PACKET_READ_EOF:
209 BUG("Should have already died when seeing EOF");
210 case PACKET_READ_NORMAL:
211 /* collect request; a sequence of keys and values */
212 if (is_command(reader.line, &command) ||
213 is_valid_capability(reader.line))
214 argv_array_push(&keys, reader.line);
216 die("unknown capability '%s'", reader.line);
218 /* Consume the peeked line */
219 packet_reader_read(&reader);
221 case PACKET_READ_FLUSH:
223 * If no command and no keys were given then the client
224 * wanted to terminate the connection.
230 * The flush packet isn't consume here like it is in
231 * the other parts of this switch statement. This is
232 * so that the command can read the flush packet and
233 * see the end of the request in the same way it would
234 * if command specific arguments were provided after a
237 state = PROCESS_REQUEST_DONE;
239 case PACKET_READ_DELIM:
240 /* Consume the peeked line */
241 packet_reader_read(&reader);
243 state = PROCESS_REQUEST_DONE;
245 case PACKET_READ_RESPONSE_END:
246 BUG("unexpected stateless separator packet");
251 die("no command requested");
253 check_algorithm(the_repository, &keys);
255 command->command(the_repository, &keys, &reader);
257 argv_array_clear(&keys);
261 /* Main serve loop for protocol version 2 */
262 void serve(struct serve_options *options)
264 if (options->advertise_capabilities || !options->stateless_rpc) {
265 /* serve by default supports v2 */
266 packet_write_fmt(1, "version 2\n");
268 advertise_capabilities();
270 * If only the list of capabilities was requested exit
271 * immediately after advertising capabilities
273 if (options->advertise_capabilities)
278 * If stateless-rpc was requested then exit after
279 * a single request/response exchange
281 if (options->stateless_rpc) {
285 if (process_request())