repository-version.txt: partialClone casing change
[git/debian.git] / serve.c
blob733347f602aa1ede3e45fee94050163790d65bf9
1 #include "cache.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"
11 static int advertise_sid = -1;
12 static int client_hash_algo = GIT_HASH_SHA1;
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 void object_format_receive(struct repository *r,
37 const char *algo_name)
39 if (!algo_name)
40 die("object-format capability requires an argument");
42 client_hash_algo = hash_algo_by_name(algo_name);
43 if (client_hash_algo == GIT_HASH_UNKNOWN)
44 die("unknown object format '%s'", algo_name);
47 static int session_id_advertise(struct repository *r, struct strbuf *value)
49 if (advertise_sid == -1 &&
50 git_config_get_bool("transfer.advertisesid", &advertise_sid))
51 advertise_sid = 0;
52 if (!advertise_sid)
53 return 0;
54 if (value)
55 strbuf_addstr(value, trace2_session_id());
56 return 1;
59 static void session_id_receive(struct repository *r,
60 const char *client_sid)
62 if (!client_sid)
63 client_sid = "";
64 trace2_data_string("transfer", NULL, "client-sid", client_sid);
67 struct protocol_capability {
69 * The name of the capability. The server uses this name when
70 * advertising this capability, and the client uses this name to
71 * specify this capability.
73 const char *name;
76 * Function queried to see if a capability should be advertised.
77 * Optionally a value can be specified by adding it to 'value'.
78 * If a value is added to 'value', the server will advertise this
79 * capability as "<name>=<value>" instead of "<name>".
81 int (*advertise)(struct repository *r, struct strbuf *value);
84 * Function called when a client requests the capability as a command.
85 * Will be provided a struct packet_reader 'request' which it should
86 * use to read the command specific part of the request. Every command
87 * MUST read until a flush packet is seen before sending a response.
89 * This field should be NULL for capabilities which are not commands.
91 int (*command)(struct repository *r, struct packet_reader *request);
94 * Function called when a client requests the capability as a
95 * non-command. This may be NULL if the capability does nothing.
97 * For a capability of the form "foo=bar", the value string points to
98 * the content after the "=" (i.e., "bar"). For simple capabilities
99 * (just "foo"), it is NULL.
101 void (*receive)(struct repository *r, const char *value);
104 static struct protocol_capability capabilities[] = {
106 .name = "agent",
107 .advertise = agent_advertise,
110 .name = "ls-refs",
111 .advertise = ls_refs_advertise,
112 .command = ls_refs,
115 .name = "fetch",
116 .advertise = upload_pack_advertise,
117 .command = upload_pack_v2,
120 .name = "server-option",
121 .advertise = always_advertise,
124 .name = "object-format",
125 .advertise = object_format_advertise,
126 .receive = object_format_receive,
129 .name = "session-id",
130 .advertise = session_id_advertise,
131 .receive = session_id_receive,
134 .name = "object-info",
135 .advertise = always_advertise,
136 .command = cap_object_info,
140 void protocol_v2_advertise_capabilities(void)
142 struct strbuf capability = STRBUF_INIT;
143 struct strbuf value = STRBUF_INIT;
144 int i;
146 /* serve by default supports v2 */
147 packet_write_fmt(1, "version 2\n");
149 for (i = 0; i < ARRAY_SIZE(capabilities); i++) {
150 struct protocol_capability *c = &capabilities[i];
152 if (c->advertise(the_repository, &value)) {
153 strbuf_addstr(&capability, c->name);
155 if (value.len) {
156 strbuf_addch(&capability, '=');
157 strbuf_addbuf(&capability, &value);
160 strbuf_addch(&capability, '\n');
161 packet_write(1, capability.buf, capability.len);
164 strbuf_reset(&capability);
165 strbuf_reset(&value);
168 packet_flush(1);
169 strbuf_release(&capability);
170 strbuf_release(&value);
173 static struct protocol_capability *get_capability(const char *key, const char **value)
175 int i;
177 if (!key)
178 return NULL;
180 for (i = 0; i < ARRAY_SIZE(capabilities); i++) {
181 struct protocol_capability *c = &capabilities[i];
182 const char *out;
183 if (!skip_prefix(key, c->name, &out))
184 continue;
185 if (!*out) {
186 *value = NULL;
187 return c;
189 if (*out++ == '=') {
190 *value = out;
191 return c;
195 return NULL;
198 static int receive_client_capability(const char *key)
200 const char *value;
201 const struct protocol_capability *c = get_capability(key, &value);
203 if (!c || c->command || !c->advertise(the_repository, NULL))
204 return 0;
206 if (c->receive)
207 c->receive(the_repository, value);
208 return 1;
211 static int parse_command(const char *key, struct protocol_capability **command)
213 const char *out;
215 if (skip_prefix(key, "command=", &out)) {
216 const char *value;
217 struct protocol_capability *cmd = get_capability(out, &value);
219 if (*command)
220 die("command '%s' requested after already requesting command '%s'",
221 out, (*command)->name);
222 if (!cmd || !cmd->advertise(the_repository, NULL) || !cmd->command || value)
223 die("invalid command '%s'", out);
225 *command = cmd;
226 return 1;
229 return 0;
232 enum request_state {
233 PROCESS_REQUEST_KEYS,
234 PROCESS_REQUEST_DONE,
237 static int process_request(void)
239 enum request_state state = PROCESS_REQUEST_KEYS;
240 struct packet_reader reader;
241 int seen_capability_or_command = 0;
242 struct protocol_capability *command = NULL;
244 packet_reader_init(&reader, 0, NULL, 0,
245 PACKET_READ_CHOMP_NEWLINE |
246 PACKET_READ_GENTLE_ON_EOF |
247 PACKET_READ_DIE_ON_ERR_PACKET);
250 * Check to see if the client closed their end before sending another
251 * request. If so we can terminate the connection.
253 if (packet_reader_peek(&reader) == PACKET_READ_EOF)
254 return 1;
255 reader.options &= ~PACKET_READ_GENTLE_ON_EOF;
257 while (state != PROCESS_REQUEST_DONE) {
258 switch (packet_reader_peek(&reader)) {
259 case PACKET_READ_EOF:
260 BUG("Should have already died when seeing EOF");
261 case PACKET_READ_NORMAL:
262 if (parse_command(reader.line, &command) ||
263 receive_client_capability(reader.line))
264 seen_capability_or_command = 1;
265 else
266 die("unknown capability '%s'", reader.line);
268 /* Consume the peeked line */
269 packet_reader_read(&reader);
270 break;
271 case PACKET_READ_FLUSH:
273 * If no command and no keys were given then the client
274 * wanted to terminate the connection.
276 if (!seen_capability_or_command)
277 return 1;
280 * The flush packet isn't consume here like it is in
281 * the other parts of this switch statement. This is
282 * so that the command can read the flush packet and
283 * see the end of the request in the same way it would
284 * if command specific arguments were provided after a
285 * delim packet.
287 state = PROCESS_REQUEST_DONE;
288 break;
289 case PACKET_READ_DELIM:
290 /* Consume the peeked line */
291 packet_reader_read(&reader);
293 state = PROCESS_REQUEST_DONE;
294 break;
295 case PACKET_READ_RESPONSE_END:
296 BUG("unexpected response end packet");
300 if (!command)
301 die("no command requested");
303 if (client_hash_algo != hash_algo_by_ptr(the_repository->hash_algo))
304 die("mismatched object format: server %s; client %s\n",
305 the_repository->hash_algo->name,
306 hash_algos[client_hash_algo].name);
308 command->command(the_repository, &reader);
310 return 0;
313 void protocol_v2_serve_loop(int stateless_rpc)
315 if (!stateless_rpc)
316 protocol_v2_advertise_capabilities();
319 * If stateless-rpc was requested then exit after
320 * a single request/response exchange
322 if (stateless_rpc) {
323 process_request();
324 } else {
325 for (;;)
326 if (process_request())
327 break;