]> git.ipfire.org Git - thirdparty/git.git/blame - serve.c
Merge branch 'tz/send-email-helpfix'
[thirdparty/git.git] / serve.c
CommitLineData
15db4e7f 1#include "git-compat-util.h"
ed10cb95
BW
2#include "repository.h"
3#include "config.h"
b9a7ac2c 4#include "hash-ll.h"
ed10cb95
BW
5#include "pkt-line.h"
6#include "version.h"
72d0ea00 7#include "ls-refs.h"
a2ba162c 8#include "protocol-caps.h"
ed10cb95 9#include "serve.h"
3145ea95 10#include "upload-pack.h"
8b8d9a22 11#include "bundle-uri.h"
15db4e7f 12#include "trace2.h"
ed10cb95 13
eea7f7a9 14static int advertise_sid = -1;
c7d3aabd 15static int client_hash_algo = GIT_HASH_SHA1;
6b5b6e42 16
74595cca
JK
17static int always_advertise(struct repository *r UNUSED,
18 struct strbuf *value UNUSED)
72d0ea00
BW
19{
20 return 1;
21}
22
74595cca 23static int agent_advertise(struct repository *r UNUSED,
ed10cb95
BW
24 struct strbuf *value)
25{
26 if (value)
27 strbuf_addstr(value, git_user_agent_sanitized());
28 return 1;
29}
30
9de0dd36 31static int object_format_advertise(struct repository *r,
32 struct strbuf *value)
33{
34 if (value)
35 strbuf_addstr(value, r->hash_algo->name);
36 return 1;
37}
38
74595cca 39static void object_format_receive(struct repository *r UNUSED,
c7d3aabd
JK
40 const char *algo_name)
41{
42 if (!algo_name)
43 die("object-format capability requires an argument");
44
45 client_hash_algo = hash_algo_by_name(algo_name);
46 if (client_hash_algo == GIT_HASH_UNKNOWN)
47 die("unknown object format '%s'", algo_name);
48}
49
6b5b6e42
JS
50static int session_id_advertise(struct repository *r, struct strbuf *value)
51{
eea7f7a9 52 if (advertise_sid == -1 &&
4b4e75dd 53 repo_config_get_bool(r, "transfer.advertisesid", &advertise_sid))
eea7f7a9 54 advertise_sid = 0;
6b5b6e42
JS
55 if (!advertise_sid)
56 return 0;
57 if (value)
58 strbuf_addstr(value, trace2_session_id());
59 return 1;
60}
61
74595cca 62static void session_id_receive(struct repository *r UNUSED,
ab539c90
JK
63 const char *client_sid)
64{
65 if (!client_sid)
66 client_sid = "";
67 trace2_data_string("transfer", NULL, "client-sid", client_sid);
68}
69
ed10cb95
BW
70struct protocol_capability {
71 /*
72 * The name of the capability. The server uses this name when
73 * advertising this capability, and the client uses this name to
74 * specify this capability.
75 */
76 const char *name;
77
78 /*
79 * Function queried to see if a capability should be advertised.
80 * Optionally a value can be specified by adding it to 'value'.
81 * If a value is added to 'value', the server will advertise this
82 * capability as "<name>=<value>" instead of "<name>".
83 */
84 int (*advertise)(struct repository *r, struct strbuf *value);
85
86 /*
87 * Function called when a client requests the capability as a command.
28a592e4 88 * Will be provided a struct packet_reader 'request' which it should
ed10cb95
BW
89 * use to read the command specific part of the request. Every command
90 * MUST read until a flush packet is seen before sending a response.
91 *
92 * This field should be NULL for capabilities which are not commands.
93 */
28a592e4 94 int (*command)(struct repository *r, struct packet_reader *request);
e56e5306
JK
95
96 /*
97 * Function called when a client requests the capability as a
98 * non-command. This may be NULL if the capability does nothing.
99 *
100 * For a capability of the form "foo=bar", the value string points to
101 * the content after the "=" (i.e., "bar"). For simple capabilities
102 * (just "foo"), it is NULL.
103 */
104 void (*receive)(struct repository *r, const char *value);
ed10cb95
BW
105};
106
107static struct protocol_capability capabilities[] = {
85baaed4
ÆAB
108 {
109 .name = "agent",
110 .advertise = agent_advertise,
111 },
112 {
113 .name = "ls-refs",
114 .advertise = ls_refs_advertise,
115 .command = ls_refs,
116 },
117 {
118 .name = "fetch",
119 .advertise = upload_pack_advertise,
120 .command = upload_pack_v2,
121 },
122 {
123 .name = "server-option",
124 .advertise = always_advertise,
125 },
126 {
127 .name = "object-format",
128 .advertise = object_format_advertise,
c7d3aabd 129 .receive = object_format_receive,
85baaed4
ÆAB
130 },
131 {
132 .name = "session-id",
133 .advertise = session_id_advertise,
ab539c90 134 .receive = session_id_receive,
85baaed4
ÆAB
135 },
136 {
137 .name = "object-info",
138 .advertise = always_advertise,
139 .command = cap_object_info,
140 },
8b8d9a22
ÆAB
141 {
142 .name = "bundle-uri",
143 .advertise = bundle_uri_advertise,
144 .command = bundle_uri_command,
145 },
ed10cb95
BW
146};
147
f234da80 148void protocol_v2_advertise_capabilities(void)
ed10cb95
BW
149{
150 struct strbuf capability = STRBUF_INIT;
151 struct strbuf value = STRBUF_INIT;
152 int i;
153
5befe8a1
ÆAB
154 /* serve by default supports v2 */
155 packet_write_fmt(1, "version 2\n");
156
ed10cb95
BW
157 for (i = 0; i < ARRAY_SIZE(capabilities); i++) {
158 struct protocol_capability *c = &capabilities[i];
159
160 if (c->advertise(the_repository, &value)) {
161 strbuf_addstr(&capability, c->name);
162
163 if (value.len) {
164 strbuf_addch(&capability, '=');
165 strbuf_addbuf(&capability, &value);
166 }
167
168 strbuf_addch(&capability, '\n');
169 packet_write(1, capability.buf, capability.len);
170 }
171
172 strbuf_reset(&capability);
173 strbuf_reset(&value);
174 }
175
176 packet_flush(1);
177 strbuf_release(&capability);
178 strbuf_release(&value);
179}
180
5ef260d2 181static struct protocol_capability *get_capability(const char *key, const char **value)
ed10cb95
BW
182{
183 int i;
184
185 if (!key)
186 return NULL;
187
188 for (i = 0; i < ARRAY_SIZE(capabilities); i++) {
189 struct protocol_capability *c = &capabilities[i];
190 const char *out;
5ef260d2
JK
191 if (!skip_prefix(key, c->name, &out))
192 continue;
193 if (!*out) {
194 *value = NULL;
ed10cb95 195 return c;
5ef260d2
JK
196 }
197 if (*out++ == '=') {
198 *value = out;
199 return c;
200 }
ed10cb95
BW
201 }
202
203 return NULL;
204}
205
e56e5306 206static int receive_client_capability(const char *key)
ed10cb95 207{
5ef260d2
JK
208 const char *value;
209 const struct protocol_capability *c = get_capability(key, &value);
ed10cb95 210
0ab7eecc 211 if (!c || c->command || !c->advertise(the_repository, NULL))
e56e5306
JK
212 return 0;
213
214 if (c->receive)
215 c->receive(the_repository, value);
216 return 1;
ed10cb95
BW
217}
218
76804526 219static int parse_command(const char *key, struct protocol_capability **command)
ed10cb95
BW
220{
221 const char *out;
222
223 if (skip_prefix(key, "command=", &out)) {
5ef260d2
JK
224 const char *value;
225 struct protocol_capability *cmd = get_capability(out, &value);
ed10cb95
BW
226
227 if (*command)
228 die("command '%s' requested after already requesting command '%s'",
229 out, (*command)->name);
108c265f 230 if (!cmd || !cmd->advertise(the_repository, NULL) || !cmd->command || value)
ed10cb95
BW
231 die("invalid command '%s'", out);
232
233 *command = cmd;
234 return 1;
235 }
236
237 return 0;
238}
239
ed10cb95
BW
240enum request_state {
241 PROCESS_REQUEST_KEYS,
242 PROCESS_REQUEST_DONE,
243};
244
245static int process_request(void)
246{
247 enum request_state state = PROCESS_REQUEST_KEYS;
248 struct packet_reader reader;
f0a35c9c 249 int seen_capability_or_command = 0;
ed10cb95
BW
250 struct protocol_capability *command = NULL;
251
252 packet_reader_init(&reader, 0, NULL, 0,
253 PACKET_READ_CHOMP_NEWLINE |
2d103c31
MS
254 PACKET_READ_GENTLE_ON_EOF |
255 PACKET_READ_DIE_ON_ERR_PACKET);
ed10cb95
BW
256
257 /*
258 * Check to see if the client closed their end before sending another
259 * request. If so we can terminate the connection.
260 */
261 if (packet_reader_peek(&reader) == PACKET_READ_EOF)
262 return 1;
2d103c31 263 reader.options &= ~PACKET_READ_GENTLE_ON_EOF;
ed10cb95
BW
264
265 while (state != PROCESS_REQUEST_DONE) {
266 switch (packet_reader_peek(&reader)) {
267 case PACKET_READ_EOF:
268 BUG("Should have already died when seeing EOF");
269 case PACKET_READ_NORMAL:
76804526 270 if (parse_command(reader.line, &command) ||
e56e5306 271 receive_client_capability(reader.line))
f0a35c9c 272 seen_capability_or_command = 1;
ed10cb95
BW
273 else
274 die("unknown capability '%s'", reader.line);
275
276 /* Consume the peeked line */
277 packet_reader_read(&reader);
278 break;
279 case PACKET_READ_FLUSH:
280 /*
281 * If no command and no keys were given then the client
282 * wanted to terminate the connection.
283 */
f0a35c9c 284 if (!seen_capability_or_command)
ed10cb95
BW
285 return 1;
286
287 /*
288 * The flush packet isn't consume here like it is in
289 * the other parts of this switch statement. This is
290 * so that the command can read the flush packet and
291 * see the end of the request in the same way it would
292 * if command specific arguments were provided after a
293 * delim packet.
294 */
295 state = PROCESS_REQUEST_DONE;
296 break;
297 case PACKET_READ_DELIM:
298 /* Consume the peeked line */
299 packet_reader_read(&reader);
300
301 state = PROCESS_REQUEST_DONE;
302 break;
0181b600 303 case PACKET_READ_RESPONSE_END:
8232a0ff 304 BUG("unexpected response end packet");
ed10cb95
BW
305 }
306 }
307
308 if (!command)
309 die("no command requested");
310
c7d3aabd
JK
311 if (client_hash_algo != hash_algo_by_ptr(the_repository->hash_algo))
312 die("mismatched object format: server %s; client %s\n",
313 the_repository->hash_algo->name,
314 hash_algos[client_hash_algo].name);
9de0dd36 315
28a592e4 316 command->command(the_repository, &reader);
ed10cb95 317
ed10cb95
BW
318 return 0;
319}
320
f234da80 321void protocol_v2_serve_loop(int stateless_rpc)
ed10cb95 322{
f234da80
ÆAB
323 if (!stateless_rpc)
324 protocol_v2_advertise_capabilities();
ed10cb95
BW
325
326 /*
327 * If stateless-rpc was requested then exit after
328 * a single request/response exchange
329 */
f234da80 330 if (stateless_rpc) {
ed10cb95
BW
331 process_request();
332 } else {
333 for (;;)
334 if (process_request())
335 break;
336 }
337}