2 #include "repository.h"
9 #include "upload-pack.h"
11 static int advertise_sid;
13 static int always_advertise(struct repository *r,
19 static int agent_advertise(struct repository *r,
23 strbuf_addstr(value, git_user_agent_sanitized());
27 static int object_format_advertise(struct repository *r,
31 strbuf_addstr(value, r->hash_algo->name);
35 static int session_id_advertise(struct repository *r, struct strbuf *value)
40 strbuf_addstr(value, trace2_session_id());
44 struct protocol_capability {
46 * The name of the capability. The server uses this name when
47 * advertising this capability, and the client uses this name to
48 * specify this capability.
53 * Function queried to see if a capability should be advertised.
54 * Optionally a value can be specified by adding it to 'value'.
55 * If a value is added to 'value', the server will advertise this
56 * capability as "<name>=<value>" instead of "<name>".
58 int (*advertise)(struct repository *r, struct strbuf *value);
61 * Function called when a client requests the capability as a command.
62 * The function will be provided the capabilities requested via 'keys'
63 * as well as a struct packet_reader 'request' which the command should
64 * use to read the command specific part of the request. Every command
65 * MUST read until a flush packet is seen before sending a response.
67 * This field should be NULL for capabilities which are not commands.
69 int (*command)(struct repository *r,
71 struct packet_reader *request);
74 static struct protocol_capability capabilities[] = {
75 { "agent", agent_advertise, NULL },
76 { "ls-refs", ls_refs_advertise, ls_refs },
77 { "fetch", upload_pack_advertise, upload_pack_v2 },
78 { "server-option", always_advertise, NULL },
79 { "object-format", object_format_advertise, NULL },
80 { "session-id", session_id_advertise, NULL },
83 static void advertise_capabilities(void)
85 struct strbuf capability = STRBUF_INIT;
86 struct strbuf value = STRBUF_INIT;
89 for (i = 0; i < ARRAY_SIZE(capabilities); i++) {
90 struct protocol_capability *c = &capabilities[i];
92 if (c->advertise(the_repository, &value)) {
93 strbuf_addstr(&capability, c->name);
96 strbuf_addch(&capability, '=');
97 strbuf_addbuf(&capability, &value);
100 strbuf_addch(&capability, '\n');
101 packet_write(1, capability.buf, capability.len);
104 strbuf_reset(&capability);
105 strbuf_reset(&value);
109 strbuf_release(&capability);
110 strbuf_release(&value);
113 static struct protocol_capability *get_capability(const char *key)
120 for (i = 0; i < ARRAY_SIZE(capabilities); i++) {
121 struct protocol_capability *c = &capabilities[i];
123 if (skip_prefix(key, c->name, &out) && (!*out || *out == '='))
130 static int is_valid_capability(const char *key)
132 const struct protocol_capability *c = get_capability(key);
134 return c && c->advertise(the_repository, NULL);
137 static int is_command(const char *key, struct protocol_capability **command)
141 if (skip_prefix(key, "command=", &out)) {
142 struct protocol_capability *cmd = get_capability(out);
145 die("command '%s' requested after already requesting command '%s'",
146 out, (*command)->name);
147 if (!cmd || !cmd->advertise(the_repository, NULL) || !cmd->command)
148 die("invalid command '%s'", out);
157 int has_capability(const struct strvec *keys, const char *capability,
161 for (i = 0; i < keys->nr; i++) {
163 if (skip_prefix(keys->v[i], capability, &out) &&
164 (!*out || *out == '=')) {
177 static void check_algorithm(struct repository *r, struct strvec *keys)
179 int client = GIT_HASH_SHA1, server = hash_algo_by_ptr(r->hash_algo);
180 const char *algo_name;
182 if (has_capability(keys, "object-format", &algo_name)) {
183 client = hash_algo_by_name(algo_name);
184 if (client == GIT_HASH_UNKNOWN)
185 die("unknown object format '%s'", algo_name);
188 if (client != server)
189 die("mismatched object format: server %s; client %s\n",
190 r->hash_algo->name, hash_algos[client].name);
194 PROCESS_REQUEST_KEYS,
195 PROCESS_REQUEST_DONE,
198 static int process_request(void)
200 enum request_state state = PROCESS_REQUEST_KEYS;
201 struct packet_reader reader;
202 struct strvec keys = STRVEC_INIT;
203 struct protocol_capability *command = NULL;
204 const char *client_sid;
206 packet_reader_init(&reader, 0, NULL, 0,
207 PACKET_READ_CHOMP_NEWLINE |
208 PACKET_READ_GENTLE_ON_EOF |
209 PACKET_READ_DIE_ON_ERR_PACKET);
212 * Check to see if the client closed their end before sending another
213 * request. If so we can terminate the connection.
215 if (packet_reader_peek(&reader) == PACKET_READ_EOF)
217 reader.options &= ~PACKET_READ_GENTLE_ON_EOF;
219 while (state != PROCESS_REQUEST_DONE) {
220 switch (packet_reader_peek(&reader)) {
221 case PACKET_READ_EOF:
222 BUG("Should have already died when seeing EOF");
223 case PACKET_READ_NORMAL:
224 /* collect request; a sequence of keys and values */
225 if (is_command(reader.line, &command) ||
226 is_valid_capability(reader.line))
227 strvec_push(&keys, reader.line);
229 die("unknown capability '%s'", reader.line);
231 /* Consume the peeked line */
232 packet_reader_read(&reader);
234 case PACKET_READ_FLUSH:
236 * If no command and no keys were given then the client
237 * wanted to terminate the connection.
243 * The flush packet isn't consume here like it is in
244 * the other parts of this switch statement. This is
245 * so that the command can read the flush packet and
246 * see the end of the request in the same way it would
247 * if command specific arguments were provided after a
250 state = PROCESS_REQUEST_DONE;
252 case PACKET_READ_DELIM:
253 /* Consume the peeked line */
254 packet_reader_read(&reader);
256 state = PROCESS_REQUEST_DONE;
258 case PACKET_READ_RESPONSE_END:
259 BUG("unexpected stateless separator packet");
264 die("no command requested");
266 check_algorithm(the_repository, &keys);
268 if (has_capability(&keys, "session-id", &client_sid))
269 trace2_data_string("transfer", NULL, "client-sid", client_sid);
271 command->command(the_repository, &keys, &reader);
277 /* Main serve loop for protocol version 2 */
278 void serve(struct serve_options *options)
280 git_config_get_bool("transfer.advertisesid", &advertise_sid);
282 if (options->advertise_capabilities || !options->stateless_rpc) {
283 /* serve by default supports v2 */
284 packet_write_fmt(1, "version 2\n");
286 advertise_capabilities();
288 * If only the list of capabilities was requested exit
289 * immediately after advertising capabilities
291 if (options->advertise_capabilities)
296 * If stateless-rpc was requested then exit after
297 * a single request/response exchange
299 if (options->stateless_rpc) {
303 if (process_request())