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 struct protocol_capability {
27 * The name of the capability. The server uses this name when
28 * advertising this capability, and the client uses this name to
29 * specify this capability.
34 * Function queried to see if a capability should be advertised.
35 * Optionally a value can be specified by adding it to 'value'.
36 * If a value is added to 'value', the server will advertise this
37 * capability as "<name>=<value>" instead of "<name>".
39 int (*advertise)(struct repository *r, struct strbuf *value);
42 * Function called when a client requests the capability as a command.
43 * The command request will be provided to the function via 'keys', the
44 * capabilities requested, and 'args', the command specific parameters.
46 * This field should be NULL for capabilities which are not commands.
48 int (*command)(struct repository *r,
49 struct argv_array *keys,
50 struct argv_array *args);
53 static struct protocol_capability capabilities[] = {
54 { "agent", agent_advertise, NULL },
55 { "ls-refs", always_advertise, ls_refs },
56 { "fetch", upload_pack_advertise, upload_pack_v2 },
59 static void advertise_capabilities(void)
61 struct strbuf capability = STRBUF_INIT;
62 struct strbuf value = STRBUF_INIT;
65 for (i = 0; i < ARRAY_SIZE(capabilities); i++) {
66 struct protocol_capability *c = &capabilities[i];
68 if (c->advertise(the_repository, &value)) {
69 strbuf_addstr(&capability, c->name);
72 strbuf_addch(&capability, '=');
73 strbuf_addbuf(&capability, &value);
76 strbuf_addch(&capability, '\n');
77 packet_write(1, capability.buf, capability.len);
80 strbuf_reset(&capability);
85 strbuf_release(&capability);
86 strbuf_release(&value);
89 static struct protocol_capability *get_capability(const char *key)
96 for (i = 0; i < ARRAY_SIZE(capabilities); i++) {
97 struct protocol_capability *c = &capabilities[i];
99 if (skip_prefix(key, c->name, &out) && (!*out || *out == '='))
106 static int is_valid_capability(const char *key)
108 const struct protocol_capability *c = get_capability(key);
110 return c && c->advertise(the_repository, NULL);
113 static int is_command(const char *key, struct protocol_capability **command)
117 if (skip_prefix(key, "command=", &out)) {
118 struct protocol_capability *cmd = get_capability(out);
120 if (!cmd || !cmd->advertise(the_repository, NULL) || !cmd->command)
121 die("invalid command '%s'", out);
123 die("command already requested");
132 int has_capability(const struct argv_array *keys, const char *capability,
136 for (i = 0; i < keys->argc; i++) {
138 if (skip_prefix(keys->argv[i], capability, &out) &&
139 (!*out || *out == '=')) {
153 PROCESS_REQUEST_KEYS = 0,
154 PROCESS_REQUEST_ARGS,
155 PROCESS_REQUEST_DONE,
158 static int process_request(void)
160 enum request_state state = PROCESS_REQUEST_KEYS;
161 struct packet_reader reader;
162 struct argv_array keys = ARGV_ARRAY_INIT;
163 struct argv_array args = ARGV_ARRAY_INIT;
164 struct protocol_capability *command = NULL;
166 packet_reader_init(&reader, 0, NULL, 0,
167 PACKET_READ_CHOMP_NEWLINE |
168 PACKET_READ_GENTLE_ON_EOF);
171 * Check to see if the client closed their end before sending another
172 * request. If so we can terminate the connection.
174 if (packet_reader_peek(&reader) == PACKET_READ_EOF)
176 reader.options = PACKET_READ_CHOMP_NEWLINE;
178 while (state != PROCESS_REQUEST_DONE) {
179 switch (packet_reader_read(&reader)) {
180 case PACKET_READ_EOF:
181 BUG("Should have already died when seeing EOF");
182 case PACKET_READ_NORMAL:
184 case PACKET_READ_FLUSH:
185 state = PROCESS_REQUEST_DONE;
187 case PACKET_READ_DELIM:
188 if (state != PROCESS_REQUEST_KEYS)
189 die("protocol error");
190 state = PROCESS_REQUEST_ARGS;
192 * maybe include a check to make sure that a
193 * command/capabilities were given.
199 case PROCESS_REQUEST_KEYS:
200 /* collect request; a sequence of keys and values */
201 if (is_command(reader.line, &command) ||
202 is_valid_capability(reader.line))
203 argv_array_push(&keys, reader.line);
205 die("unknown capability '%s'", reader.line);
207 case PROCESS_REQUEST_ARGS:
208 /* collect arguments for the requested command */
209 argv_array_push(&args, reader.line);
211 case PROCESS_REQUEST_DONE:
217 * If no command and no keys were given then the client wanted to
218 * terminate the connection.
220 if (!keys.argc && !args.argc)
224 die("no command requested");
226 command->command(the_repository, &keys, &args);
228 argv_array_clear(&keys);
229 argv_array_clear(&args);
233 /* Main serve loop for protocol version 2 */
234 void serve(struct serve_options *options)
236 if (options->advertise_capabilities || !options->stateless_rpc) {
237 /* serve by default supports v2 */
238 packet_write_fmt(1, "version 2\n");
240 advertise_capabilities();
242 * If only the list of capabilities was requested exit
243 * immediately after advertising capabilities
245 if (options->advertise_capabilities)
250 * If stateless-rpc was requested then exit after
251 * a single request/response exchange
253 if (options->stateless_rpc) {
257 if (process_request())