cocci: remove 'unused.cocci'
[git.git] / serve.c
blob5329c91011f2919fc24bcc3039c264a0c9e2c877
1 #include "git-compat-util.h"
2 #include "repository.h"
3 #include "config.h"
4 #include "pkt-line.h"
5 #include "version.h"
6 #include "ls-refs.h"
7 #include "protocol-caps.h"
8 #include "serve.h"
9 #include "upload-pack.h"
10 #include "bundle-uri.h"
11 #include "trace2.h"
13 static int advertise_sid = -1;
14 static int client_hash_algo = GIT_HASH_SHA1;
16 static int always_advertise(struct repository *r UNUSED,
17 struct strbuf *value UNUSED)
19 return 1;
22 static int agent_advertise(struct repository *r UNUSED,
23 struct strbuf *value)
25 if (value)
26 strbuf_addstr(value, git_user_agent_sanitized());
27 return 1;
30 static int object_format_advertise(struct repository *r,
31 struct strbuf *value)
33 if (value)
34 strbuf_addstr(value, r->hash_algo->name);
35 return 1;
38 static void object_format_receive(struct repository *r UNUSED,
39 const char *algo_name)
41 if (!algo_name)
42 die("object-format capability requires an argument");
44 client_hash_algo = hash_algo_by_name(algo_name);
45 if (client_hash_algo == GIT_HASH_UNKNOWN)
46 die("unknown object format '%s'", algo_name);
49 static int session_id_advertise(struct repository *r, struct strbuf *value)
51 if (advertise_sid == -1 &&
52 repo_config_get_bool(r, "transfer.advertisesid", &advertise_sid))
53 advertise_sid = 0;
54 if (!advertise_sid)
55 return 0;
56 if (value)
57 strbuf_addstr(value, trace2_session_id());
58 return 1;
61 static void session_id_receive(struct repository *r UNUSED,
62 const char *client_sid)
64 if (!client_sid)
65 client_sid = "";
66 trace2_data_string("transfer", NULL, "client-sid", client_sid);
69 struct protocol_capability {
71 * The name of the capability. The server uses this name when
72 * advertising this capability, and the client uses this name to
73 * specify this capability.
75 const char *name;
78 * Function queried to see if a capability should be advertised.
79 * Optionally a value can be specified by adding it to 'value'.
80 * If a value is added to 'value', the server will advertise this
81 * capability as "<name>=<value>" instead of "<name>".
83 int (*advertise)(struct repository *r, struct strbuf *value);
86 * Function called when a client requests the capability as a command.
87 * Will be provided a struct packet_reader 'request' which it should
88 * use to read the command specific part of the request. Every command
89 * MUST read until a flush packet is seen before sending a response.
91 * This field should be NULL for capabilities which are not commands.
93 int (*command)(struct repository *r, struct packet_reader *request);
96 * Function called when a client requests the capability as a
97 * non-command. This may be NULL if the capability does nothing.
99 * For a capability of the form "foo=bar", the value string points to
100 * the content after the "=" (i.e., "bar"). For simple capabilities
101 * (just "foo"), it is NULL.
103 void (*receive)(struct repository *r, const char *value);
106 static struct protocol_capability capabilities[] = {
108 .name = "agent",
109 .advertise = agent_advertise,
112 .name = "ls-refs",
113 .advertise = ls_refs_advertise,
114 .command = ls_refs,
117 .name = "fetch",
118 .advertise = upload_pack_advertise,
119 .command = upload_pack_v2,
122 .name = "server-option",
123 .advertise = always_advertise,
126 .name = "object-format",
127 .advertise = object_format_advertise,
128 .receive = object_format_receive,
131 .name = "session-id",
132 .advertise = session_id_advertise,
133 .receive = session_id_receive,
136 .name = "object-info",
137 .advertise = always_advertise,
138 .command = cap_object_info,
141 .name = "bundle-uri",
142 .advertise = bundle_uri_advertise,
143 .command = bundle_uri_command,
147 void protocol_v2_advertise_capabilities(void)
149 struct strbuf capability = STRBUF_INIT;
150 struct strbuf value = STRBUF_INIT;
151 int i;
153 /* serve by default supports v2 */
154 packet_write_fmt(1, "version 2\n");
156 for (i = 0; i < ARRAY_SIZE(capabilities); i++) {
157 struct protocol_capability *c = &capabilities[i];
159 if (c->advertise(the_repository, &value)) {
160 strbuf_addstr(&capability, c->name);
162 if (value.len) {
163 strbuf_addch(&capability, '=');
164 strbuf_addbuf(&capability, &value);
167 strbuf_addch(&capability, '\n');
168 packet_write(1, capability.buf, capability.len);
171 strbuf_reset(&capability);
172 strbuf_reset(&value);
175 packet_flush(1);
176 strbuf_release(&capability);
177 strbuf_release(&value);
180 static struct protocol_capability *get_capability(const char *key, const char **value)
182 int i;
184 if (!key)
185 return NULL;
187 for (i = 0; i < ARRAY_SIZE(capabilities); i++) {
188 struct protocol_capability *c = &capabilities[i];
189 const char *out;
190 if (!skip_prefix(key, c->name, &out))
191 continue;
192 if (!*out) {
193 *value = NULL;
194 return c;
196 if (*out++ == '=') {
197 *value = out;
198 return c;
202 return NULL;
205 static int receive_client_capability(const char *key)
207 const char *value;
208 const struct protocol_capability *c = get_capability(key, &value);
210 if (!c || c->command || !c->advertise(the_repository, NULL))
211 return 0;
213 if (c->receive)
214 c->receive(the_repository, value);
215 return 1;
218 static int parse_command(const char *key, struct protocol_capability **command)
220 const char *out;
222 if (skip_prefix(key, "command=", &out)) {
223 const char *value;
224 struct protocol_capability *cmd = get_capability(out, &value);
226 if (*command)
227 die("command '%s' requested after already requesting command '%s'",
228 out, (*command)->name);
229 if (!cmd || !cmd->advertise(the_repository, NULL) || !cmd->command || value)
230 die("invalid command '%s'", out);
232 *command = cmd;
233 return 1;
236 return 0;
239 enum request_state {
240 PROCESS_REQUEST_KEYS,
241 PROCESS_REQUEST_DONE,
244 static int process_request(void)
246 enum request_state state = PROCESS_REQUEST_KEYS;
247 struct packet_reader reader;
248 int seen_capability_or_command = 0;
249 struct protocol_capability *command = NULL;
251 packet_reader_init(&reader, 0, NULL, 0,
252 PACKET_READ_CHOMP_NEWLINE |
253 PACKET_READ_GENTLE_ON_EOF |
254 PACKET_READ_DIE_ON_ERR_PACKET);
257 * Check to see if the client closed their end before sending another
258 * request. If so we can terminate the connection.
260 if (packet_reader_peek(&reader) == PACKET_READ_EOF)
261 return 1;
262 reader.options &= ~PACKET_READ_GENTLE_ON_EOF;
264 while (state != PROCESS_REQUEST_DONE) {
265 switch (packet_reader_peek(&reader)) {
266 case PACKET_READ_EOF:
267 BUG("Should have already died when seeing EOF");
268 case PACKET_READ_NORMAL:
269 if (parse_command(reader.line, &command) ||
270 receive_client_capability(reader.line))
271 seen_capability_or_command = 1;
272 else
273 die("unknown capability '%s'", reader.line);
275 /* Consume the peeked line */
276 packet_reader_read(&reader);
277 break;
278 case PACKET_READ_FLUSH:
280 * If no command and no keys were given then the client
281 * wanted to terminate the connection.
283 if (!seen_capability_or_command)
284 return 1;
287 * The flush packet isn't consume here like it is in
288 * the other parts of this switch statement. This is
289 * so that the command can read the flush packet and
290 * see the end of the request in the same way it would
291 * if command specific arguments were provided after a
292 * delim packet.
294 state = PROCESS_REQUEST_DONE;
295 break;
296 case PACKET_READ_DELIM:
297 /* Consume the peeked line */
298 packet_reader_read(&reader);
300 state = PROCESS_REQUEST_DONE;
301 break;
302 case PACKET_READ_RESPONSE_END:
303 BUG("unexpected response end packet");
307 if (!command)
308 die("no command requested");
310 if (client_hash_algo != hash_algo_by_ptr(the_repository->hash_algo))
311 die("mismatched object format: server %s; client %s\n",
312 the_repository->hash_algo->name,
313 hash_algos[client_hash_algo].name);
315 command->command(the_repository, &reader);
317 return 0;
320 void protocol_v2_serve_loop(int stateless_rpc)
322 if (!stateless_rpc)
323 protocol_v2_advertise_capabilities();
326 * If stateless-rpc was requested then exit after
327 * a single request/response exchange
329 if (stateless_rpc) {
330 process_request();
331 } else {
332 for (;;)
333 if (process_request())
334 break;