Merge branch 'ab/unused-script-helpers'
[git.git] / serve.c
blob1817edc7f57a30492a60a814ae5507b718ecb87f
1 #include "cache.h"
2 #include "repository.h"
3 #include "config.h"
4 #include "pkt-line.h"
5 #include "version.h"
6 #include "strvec.h"
7 #include "ls-refs.h"
8 #include "protocol-caps.h"
9 #include "serve.h"
10 #include "upload-pack.h"
12 static int advertise_sid = -1;
14 static int always_advertise(struct repository *r,
15 struct strbuf *value)
17 return 1;
20 static int agent_advertise(struct repository *r,
21 struct strbuf *value)
23 if (value)
24 strbuf_addstr(value, git_user_agent_sanitized());
25 return 1;
28 static int object_format_advertise(struct repository *r,
29 struct strbuf *value)
31 if (value)
32 strbuf_addstr(value, r->hash_algo->name);
33 return 1;
36 static int session_id_advertise(struct repository *r, struct strbuf *value)
38 if (advertise_sid == -1 &&
39 git_config_get_bool("transfer.advertisesid", &advertise_sid))
40 advertise_sid = 0;
41 if (!advertise_sid)
42 return 0;
43 if (value)
44 strbuf_addstr(value, trace2_session_id());
45 return 1;
48 struct protocol_capability {
50 * The name of the capability. The server uses this name when
51 * advertising this capability, and the client uses this name to
52 * specify this capability.
54 const char *name;
57 * Function queried to see if a capability should be advertised.
58 * Optionally a value can be specified by adding it to 'value'.
59 * If a value is added to 'value', the server will advertise this
60 * capability as "<name>=<value>" instead of "<name>".
62 int (*advertise)(struct repository *r, struct strbuf *value);
65 * Function called when a client requests the capability as a command.
66 * Will be provided a struct packet_reader 'request' which it should
67 * use to read the command specific part of the request. Every command
68 * MUST read until a flush packet is seen before sending a response.
70 * This field should be NULL for capabilities which are not commands.
72 int (*command)(struct repository *r, struct packet_reader *request);
75 static struct protocol_capability capabilities[] = {
77 .name = "agent",
78 .advertise = agent_advertise,
81 .name = "ls-refs",
82 .advertise = ls_refs_advertise,
83 .command = ls_refs,
86 .name = "fetch",
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,
99 .name = "session-id",
100 .advertise = session_id_advertise,
103 .name = "object-info",
104 .advertise = always_advertise,
105 .command = cap_object_info,
109 void protocol_v2_advertise_capabilities(void)
111 struct strbuf capability = STRBUF_INIT;
112 struct strbuf value = STRBUF_INIT;
113 int i;
115 /* serve by default supports v2 */
116 packet_write_fmt(1, "version 2\n");
118 for (i = 0; i < ARRAY_SIZE(capabilities); i++) {
119 struct protocol_capability *c = &capabilities[i];
121 if (c->advertise(the_repository, &value)) {
122 strbuf_addstr(&capability, c->name);
124 if (value.len) {
125 strbuf_addch(&capability, '=');
126 strbuf_addbuf(&capability, &value);
129 strbuf_addch(&capability, '\n');
130 packet_write(1, capability.buf, capability.len);
133 strbuf_reset(&capability);
134 strbuf_reset(&value);
137 packet_flush(1);
138 strbuf_release(&capability);
139 strbuf_release(&value);
142 static struct protocol_capability *get_capability(const char *key)
144 int i;
146 if (!key)
147 return NULL;
149 for (i = 0; i < ARRAY_SIZE(capabilities); i++) {
150 struct protocol_capability *c = &capabilities[i];
151 const char *out;
152 if (skip_prefix(key, c->name, &out) && (!*out || *out == '='))
153 return c;
156 return NULL;
159 static int is_valid_capability(const char *key)
161 const struct protocol_capability *c = get_capability(key);
163 return c && c->advertise(the_repository, NULL);
166 static int is_command(const char *key, struct protocol_capability **command)
168 const char *out;
170 if (skip_prefix(key, "command=", &out)) {
171 struct protocol_capability *cmd = get_capability(out);
173 if (*command)
174 die("command '%s' requested after already requesting command '%s'",
175 out, (*command)->name);
176 if (!cmd || !cmd->advertise(the_repository, NULL) || !cmd->command)
177 die("invalid command '%s'", out);
179 *command = cmd;
180 return 1;
183 return 0;
186 static int has_capability(const struct strvec *keys, const char *capability,
187 const char **value)
189 int i;
190 for (i = 0; i < keys->nr; i++) {
191 const char *out;
192 if (skip_prefix(keys->v[i], capability, &out) &&
193 (!*out || *out == '=')) {
194 if (value) {
195 if (*out == '=')
196 out++;
197 *value = out;
199 return 1;
203 return 0;
206 static void check_algorithm(struct repository *r, struct strvec *keys)
208 int client = GIT_HASH_SHA1, server = hash_algo_by_ptr(r->hash_algo);
209 const char *algo_name;
211 if (has_capability(keys, "object-format", &algo_name)) {
212 client = hash_algo_by_name(algo_name);
213 if (client == GIT_HASH_UNKNOWN)
214 die("unknown object format '%s'", algo_name);
217 if (client != server)
218 die("mismatched object format: server %s; client %s\n",
219 r->hash_algo->name, hash_algos[client].name);
222 enum request_state {
223 PROCESS_REQUEST_KEYS,
224 PROCESS_REQUEST_DONE,
227 static int process_request(void)
229 enum request_state state = PROCESS_REQUEST_KEYS;
230 struct packet_reader reader;
231 struct strvec keys = STRVEC_INIT;
232 struct protocol_capability *command = NULL;
233 const char *client_sid;
235 packet_reader_init(&reader, 0, NULL, 0,
236 PACKET_READ_CHOMP_NEWLINE |
237 PACKET_READ_GENTLE_ON_EOF |
238 PACKET_READ_DIE_ON_ERR_PACKET);
241 * Check to see if the client closed their end before sending another
242 * request. If so we can terminate the connection.
244 if (packet_reader_peek(&reader) == PACKET_READ_EOF)
245 return 1;
246 reader.options &= ~PACKET_READ_GENTLE_ON_EOF;
248 while (state != PROCESS_REQUEST_DONE) {
249 switch (packet_reader_peek(&reader)) {
250 case PACKET_READ_EOF:
251 BUG("Should have already died when seeing EOF");
252 case PACKET_READ_NORMAL:
253 /* collect request; a sequence of keys and values */
254 if (is_command(reader.line, &command) ||
255 is_valid_capability(reader.line))
256 strvec_push(&keys, reader.line);
257 else
258 die("unknown capability '%s'", reader.line);
260 /* Consume the peeked line */
261 packet_reader_read(&reader);
262 break;
263 case PACKET_READ_FLUSH:
265 * If no command and no keys were given then the client
266 * wanted to terminate the connection.
268 if (!keys.nr)
269 return 1;
272 * The flush packet isn't consume here like it is in
273 * the other parts of this switch statement. This is
274 * so that the command can read the flush packet and
275 * see the end of the request in the same way it would
276 * if command specific arguments were provided after a
277 * delim packet.
279 state = PROCESS_REQUEST_DONE;
280 break;
281 case PACKET_READ_DELIM:
282 /* Consume the peeked line */
283 packet_reader_read(&reader);
285 state = PROCESS_REQUEST_DONE;
286 break;
287 case PACKET_READ_RESPONSE_END:
288 BUG("unexpected response end packet");
292 if (!command)
293 die("no command requested");
295 check_algorithm(the_repository, &keys);
297 if (has_capability(&keys, "session-id", &client_sid))
298 trace2_data_string("transfer", NULL, "client-sid", client_sid);
300 command->command(the_repository, &reader);
302 strvec_clear(&keys);
303 return 0;
306 void protocol_v2_serve_loop(int stateless_rpc)
308 if (!stateless_rpc)
309 protocol_v2_advertise_capabilities();
312 * If stateless-rpc was requested then exit after
313 * a single request/response exchange
315 if (stateless_rpc) {
316 process_request();
317 } else {
318 for (;;)
319 if (process_request())
320 break;