Merge https://github.com/j6t/git-gui
[git.git] / serve.c
blob884cd84ca8a0ea146ec1f82bc3584a7a297e7275
1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
4 #include "repository.h"
5 #include "config.h"
6 #include "hash.h"
7 #include "pkt-line.h"
8 #include "version.h"
9 #include "ls-refs.h"
10 #include "protocol-caps.h"
11 #include "serve.h"
12 #include "upload-pack.h"
13 #include "bundle-uri.h"
14 #include "trace2.h"
16 static int advertise_sid = -1;
17 static int advertise_object_info = -1;
18 static int client_hash_algo = GIT_HASH_SHA1;
20 static int always_advertise(struct repository *r UNUSED,
21 struct strbuf *value UNUSED)
23 return 1;
26 static int agent_advertise(struct repository *r UNUSED,
27 struct strbuf *value)
29 if (value)
30 strbuf_addstr(value, git_user_agent_sanitized());
31 return 1;
34 static int object_format_advertise(struct repository *r,
35 struct strbuf *value)
37 if (value)
38 strbuf_addstr(value, r->hash_algo->name);
39 return 1;
42 static void object_format_receive(struct repository *r UNUSED,
43 const char *algo_name)
45 if (!algo_name)
46 die("object-format capability requires an argument");
48 client_hash_algo = hash_algo_by_name(algo_name);
49 if (client_hash_algo == GIT_HASH_UNKNOWN)
50 die("unknown object format '%s'", algo_name);
53 static int session_id_advertise(struct repository *r, struct strbuf *value)
55 if (advertise_sid == -1 &&
56 repo_config_get_bool(r, "transfer.advertisesid", &advertise_sid))
57 advertise_sid = 0;
58 if (!advertise_sid)
59 return 0;
60 if (value)
61 strbuf_addstr(value, trace2_session_id());
62 return 1;
65 static void session_id_receive(struct repository *r UNUSED,
66 const char *client_sid)
68 if (!client_sid)
69 client_sid = "";
70 trace2_data_string("transfer", NULL, "client-sid", client_sid);
73 static int object_info_advertise(struct repository *r, struct strbuf *value UNUSED)
75 if (advertise_object_info == -1 &&
76 repo_config_get_bool(r, "transfer.advertiseobjectinfo",
77 &advertise_object_info)) {
78 /* disabled by default */
79 advertise_object_info = 0;
81 return advertise_object_info;
84 struct protocol_capability {
86 * The name of the capability. The server uses this name when
87 * advertising this capability, and the client uses this name to
88 * specify this capability.
90 const char *name;
93 * Function queried to see if a capability should be advertised.
94 * Optionally a value can be specified by adding it to 'value'.
95 * If a value is added to 'value', the server will advertise this
96 * capability as "<name>=<value>" instead of "<name>".
98 int (*advertise)(struct repository *r, struct strbuf *value);
101 * Function called when a client requests the capability as a command.
102 * Will be provided a struct packet_reader 'request' which it should
103 * use to read the command specific part of the request. Every command
104 * MUST read until a flush packet is seen before sending a response.
106 * This field should be NULL for capabilities which are not commands.
108 int (*command)(struct repository *r, struct packet_reader *request);
111 * Function called when a client requests the capability as a
112 * non-command. This may be NULL if the capability does nothing.
114 * For a capability of the form "foo=bar", the value string points to
115 * the content after the "=" (i.e., "bar"). For simple capabilities
116 * (just "foo"), it is NULL.
118 void (*receive)(struct repository *r, const char *value);
121 static struct protocol_capability capabilities[] = {
123 .name = "agent",
124 .advertise = agent_advertise,
127 .name = "ls-refs",
128 .advertise = ls_refs_advertise,
129 .command = ls_refs,
132 .name = "fetch",
133 .advertise = upload_pack_advertise,
134 .command = upload_pack_v2,
137 .name = "server-option",
138 .advertise = always_advertise,
141 .name = "object-format",
142 .advertise = object_format_advertise,
143 .receive = object_format_receive,
146 .name = "session-id",
147 .advertise = session_id_advertise,
148 .receive = session_id_receive,
151 .name = "object-info",
152 .advertise = object_info_advertise,
153 .command = cap_object_info,
156 .name = "bundle-uri",
157 .advertise = bundle_uri_advertise,
158 .command = bundle_uri_command,
162 void protocol_v2_advertise_capabilities(void)
164 struct strbuf capability = STRBUF_INIT;
165 struct strbuf value = STRBUF_INIT;
166 int i;
168 /* serve by default supports v2 */
169 packet_write_fmt(1, "version 2\n");
171 for (i = 0; i < ARRAY_SIZE(capabilities); i++) {
172 struct protocol_capability *c = &capabilities[i];
174 if (c->advertise(the_repository, &value)) {
175 strbuf_addstr(&capability, c->name);
177 if (value.len) {
178 strbuf_addch(&capability, '=');
179 strbuf_addbuf(&capability, &value);
182 strbuf_addch(&capability, '\n');
183 packet_write(1, capability.buf, capability.len);
186 strbuf_reset(&capability);
187 strbuf_reset(&value);
190 packet_flush(1);
191 strbuf_release(&capability);
192 strbuf_release(&value);
195 static struct protocol_capability *get_capability(const char *key, const char **value)
197 int i;
199 if (!key)
200 return NULL;
202 for (i = 0; i < ARRAY_SIZE(capabilities); i++) {
203 struct protocol_capability *c = &capabilities[i];
204 const char *out;
205 if (!skip_prefix(key, c->name, &out))
206 continue;
207 if (!*out) {
208 *value = NULL;
209 return c;
211 if (*out++ == '=') {
212 *value = out;
213 return c;
217 return NULL;
220 static int receive_client_capability(const char *key)
222 const char *value;
223 const struct protocol_capability *c = get_capability(key, &value);
225 if (!c || c->command || !c->advertise(the_repository, NULL))
226 return 0;
228 if (c->receive)
229 c->receive(the_repository, value);
230 return 1;
233 static int parse_command(const char *key, struct protocol_capability **command)
235 const char *out;
237 if (skip_prefix(key, "command=", &out)) {
238 const char *value;
239 struct protocol_capability *cmd = get_capability(out, &value);
241 if (*command)
242 die("command '%s' requested after already requesting command '%s'",
243 out, (*command)->name);
244 if (!cmd || !cmd->advertise(the_repository, NULL) || !cmd->command || value)
245 die("invalid command '%s'", out);
247 *command = cmd;
248 return 1;
251 return 0;
254 enum request_state {
255 PROCESS_REQUEST_KEYS,
256 PROCESS_REQUEST_DONE,
259 static int process_request(void)
261 enum request_state state = PROCESS_REQUEST_KEYS;
262 struct packet_reader reader;
263 int seen_capability_or_command = 0;
264 struct protocol_capability *command = NULL;
266 packet_reader_init(&reader, 0, NULL, 0,
267 PACKET_READ_CHOMP_NEWLINE |
268 PACKET_READ_GENTLE_ON_EOF |
269 PACKET_READ_DIE_ON_ERR_PACKET);
272 * Check to see if the client closed their end before sending another
273 * request. If so we can terminate the connection.
275 if (packet_reader_peek(&reader) == PACKET_READ_EOF)
276 return 1;
277 reader.options &= ~PACKET_READ_GENTLE_ON_EOF;
279 while (state != PROCESS_REQUEST_DONE) {
280 switch (packet_reader_peek(&reader)) {
281 case PACKET_READ_EOF:
282 BUG("Should have already died when seeing EOF");
283 case PACKET_READ_NORMAL:
284 if (parse_command(reader.line, &command) ||
285 receive_client_capability(reader.line))
286 seen_capability_or_command = 1;
287 else
288 die("unknown capability '%s'", reader.line);
290 /* Consume the peeked line */
291 packet_reader_read(&reader);
292 break;
293 case PACKET_READ_FLUSH:
295 * If no command and no keys were given then the client
296 * wanted to terminate the connection.
298 if (!seen_capability_or_command)
299 return 1;
302 * The flush packet isn't consume here like it is in
303 * the other parts of this switch statement. This is
304 * so that the command can read the flush packet and
305 * see the end of the request in the same way it would
306 * if command specific arguments were provided after a
307 * delim packet.
309 state = PROCESS_REQUEST_DONE;
310 break;
311 case PACKET_READ_DELIM:
312 /* Consume the peeked line */
313 packet_reader_read(&reader);
315 state = PROCESS_REQUEST_DONE;
316 break;
317 case PACKET_READ_RESPONSE_END:
318 BUG("unexpected response end packet");
322 if (!command)
323 die("no command requested");
325 if (client_hash_algo != hash_algo_by_ptr(the_repository->hash_algo))
326 die("mismatched object format: server %s; client %s\n",
327 the_repository->hash_algo->name,
328 hash_algos[client_hash_algo].name);
330 command->command(the_repository, &reader);
332 return 0;
335 void protocol_v2_serve_loop(int stateless_rpc)
337 if (!stateless_rpc)
338 protocol_v2_advertise_capabilities();
341 * If stateless-rpc was requested then exit after
342 * a single request/response exchange
344 if (stateless_rpc) {
345 process_request();
346 } else {
347 for (;;)
348 if (process_request())
349 break;