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