]> git.ipfire.org Git - thirdparty/git.git/blob - serve.c
init-db: document existing bug with core.bare in template config
[thirdparty/git.git] / serve.c
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"
12
13 static int advertise_sid = -1;
14 static int client_hash_algo = GIT_HASH_SHA1;
15
16 static int always_advertise(struct repository *r UNUSED,
17 struct strbuf *value UNUSED)
18 {
19 return 1;
20 }
21
22 static int agent_advertise(struct repository *r UNUSED,
23 struct strbuf *value)
24 {
25 if (value)
26 strbuf_addstr(value, git_user_agent_sanitized());
27 return 1;
28 }
29
30 static int object_format_advertise(struct repository *r,
31 struct strbuf *value)
32 {
33 if (value)
34 strbuf_addstr(value, r->hash_algo->name);
35 return 1;
36 }
37
38 static void object_format_receive(struct repository *r UNUSED,
39 const char *algo_name)
40 {
41 if (!algo_name)
42 die("object-format capability requires an argument");
43
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);
47 }
48
49 static int session_id_advertise(struct repository *r, struct strbuf *value)
50 {
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;
59 }
60
61 static void session_id_receive(struct repository *r UNUSED,
62 const char *client_sid)
63 {
64 if (!client_sid)
65 client_sid = "";
66 trace2_data_string("transfer", NULL, "client-sid", client_sid);
67 }
68
69 struct protocol_capability {
70 /*
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.
74 */
75 const char *name;
76
77 /*
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>".
82 */
83 int (*advertise)(struct repository *r, struct strbuf *value);
84
85 /*
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.
90 *
91 * This field should be NULL for capabilities which are not commands.
92 */
93 int (*command)(struct repository *r, struct packet_reader *request);
94
95 /*
96 * Function called when a client requests the capability as a
97 * non-command. This may be NULL if the capability does nothing.
98 *
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.
102 */
103 void (*receive)(struct repository *r, const char *value);
104 };
105
106 static struct protocol_capability capabilities[] = {
107 {
108 .name = "agent",
109 .advertise = agent_advertise,
110 },
111 {
112 .name = "ls-refs",
113 .advertise = ls_refs_advertise,
114 .command = ls_refs,
115 },
116 {
117 .name = "fetch",
118 .advertise = upload_pack_advertise,
119 .command = upload_pack_v2,
120 },
121 {
122 .name = "server-option",
123 .advertise = always_advertise,
124 },
125 {
126 .name = "object-format",
127 .advertise = object_format_advertise,
128 .receive = object_format_receive,
129 },
130 {
131 .name = "session-id",
132 .advertise = session_id_advertise,
133 .receive = session_id_receive,
134 },
135 {
136 .name = "object-info",
137 .advertise = always_advertise,
138 .command = cap_object_info,
139 },
140 {
141 .name = "bundle-uri",
142 .advertise = bundle_uri_advertise,
143 .command = bundle_uri_command,
144 },
145 };
146
147 void protocol_v2_advertise_capabilities(void)
148 {
149 struct strbuf capability = STRBUF_INIT;
150 struct strbuf value = STRBUF_INIT;
151 int i;
152
153 /* serve by default supports v2 */
154 packet_write_fmt(1, "version 2\n");
155
156 for (i = 0; i < ARRAY_SIZE(capabilities); i++) {
157 struct protocol_capability *c = &capabilities[i];
158
159 if (c->advertise(the_repository, &value)) {
160 strbuf_addstr(&capability, c->name);
161
162 if (value.len) {
163 strbuf_addch(&capability, '=');
164 strbuf_addbuf(&capability, &value);
165 }
166
167 strbuf_addch(&capability, '\n');
168 packet_write(1, capability.buf, capability.len);
169 }
170
171 strbuf_reset(&capability);
172 strbuf_reset(&value);
173 }
174
175 packet_flush(1);
176 strbuf_release(&capability);
177 strbuf_release(&value);
178 }
179
180 static struct protocol_capability *get_capability(const char *key, const char **value)
181 {
182 int i;
183
184 if (!key)
185 return NULL;
186
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;
195 }
196 if (*out++ == '=') {
197 *value = out;
198 return c;
199 }
200 }
201
202 return NULL;
203 }
204
205 static int receive_client_capability(const char *key)
206 {
207 const char *value;
208 const struct protocol_capability *c = get_capability(key, &value);
209
210 if (!c || c->command || !c->advertise(the_repository, NULL))
211 return 0;
212
213 if (c->receive)
214 c->receive(the_repository, value);
215 return 1;
216 }
217
218 static int parse_command(const char *key, struct protocol_capability **command)
219 {
220 const char *out;
221
222 if (skip_prefix(key, "command=", &out)) {
223 const char *value;
224 struct protocol_capability *cmd = get_capability(out, &value);
225
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);
231
232 *command = cmd;
233 return 1;
234 }
235
236 return 0;
237 }
238
239 enum request_state {
240 PROCESS_REQUEST_KEYS,
241 PROCESS_REQUEST_DONE,
242 };
243
244 static int process_request(void)
245 {
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;
250
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);
255
256 /*
257 * Check to see if the client closed their end before sending another
258 * request. If so we can terminate the connection.
259 */
260 if (packet_reader_peek(&reader) == PACKET_READ_EOF)
261 return 1;
262 reader.options &= ~PACKET_READ_GENTLE_ON_EOF;
263
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);
274
275 /* Consume the peeked line */
276 packet_reader_read(&reader);
277 break;
278 case PACKET_READ_FLUSH:
279 /*
280 * If no command and no keys were given then the client
281 * wanted to terminate the connection.
282 */
283 if (!seen_capability_or_command)
284 return 1;
285
286 /*
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.
293 */
294 state = PROCESS_REQUEST_DONE;
295 break;
296 case PACKET_READ_DELIM:
297 /* Consume the peeked line */
298 packet_reader_read(&reader);
299
300 state = PROCESS_REQUEST_DONE;
301 break;
302 case PACKET_READ_RESPONSE_END:
303 BUG("unexpected response end packet");
304 }
305 }
306
307 if (!command)
308 die("no command requested");
309
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);
314
315 command->command(the_repository, &reader);
316
317 return 0;
318 }
319
320 void protocol_v2_serve_loop(int stateless_rpc)
321 {
322 if (!stateless_rpc)
323 protocol_v2_advertise_capabilities();
324
325 /*
326 * If stateless-rpc was requested then exit after
327 * a single request/response exchange
328 */
329 if (stateless_rpc) {
330 process_request();
331 } else {
332 for (;;)
333 if (process_request())
334 break;
335 }
336 }