serve: advertise session ID in v2 capabilities
[git] / serve.c
1 #include "cache.h"
2 #include "repository.h"
3 #include "config.h"
4 #include "pkt-line.h"
5 #include "version.h"
6 #include "strvec.h"
7 #include "ls-refs.h"
8 #include "serve.h"
9 #include "upload-pack.h"
10
11 static int advertise_sid;
12
13 static int always_advertise(struct repository *r,
14                             struct strbuf *value)
15 {
16         return 1;
17 }
18
19 static 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
27 static 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
35 static 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
44 struct 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,
70                        struct strvec *keys,
71                        struct packet_reader *request);
72 };
73
74 static struct protocol_capability capabilities[] = {
75         { "agent", agent_advertise, NULL },
76         { "ls-refs", always_advertise, ls_refs },
77         { "fetch", upload_pack_advertise, upload_pack_v2 },
78         { "server-option", always_advertise, NULL },
79         { "object-format", object_format_advertise, NULL },
80         { "session-id", session_id_advertise, NULL },
81 };
82
83 static 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
113 static 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
130 static 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
137 static 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
157 int has_capability(const struct strvec *keys, const char *capability,
158                    const char **value)
159 {
160         int i;
161         for (i = 0; i < keys->nr; i++) {
162                 const char *out;
163                 if (skip_prefix(keys->v[i], capability, &out) &&
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
177 static void check_algorithm(struct repository *r, struct strvec *keys)
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
193 enum request_state {
194         PROCESS_REQUEST_KEYS,
195         PROCESS_REQUEST_DONE,
196 };
197
198 static int process_request(void)
199 {
200         enum request_state state = PROCESS_REQUEST_KEYS;
201         struct packet_reader reader;
202         struct strvec keys = STRVEC_INIT;
203         struct protocol_capability *command = NULL;
204
205         packet_reader_init(&reader, 0, NULL, 0,
206                            PACKET_READ_CHOMP_NEWLINE |
207                            PACKET_READ_GENTLE_ON_EOF |
208                            PACKET_READ_DIE_ON_ERR_PACKET);
209
210         /*
211          * Check to see if the client closed their end before sending another
212          * request.  If so we can terminate the connection.
213          */
214         if (packet_reader_peek(&reader) == PACKET_READ_EOF)
215                 return 1;
216         reader.options &= ~PACKET_READ_GENTLE_ON_EOF;
217
218         while (state != PROCESS_REQUEST_DONE) {
219                 switch (packet_reader_peek(&reader)) {
220                 case PACKET_READ_EOF:
221                         BUG("Should have already died when seeing EOF");
222                 case PACKET_READ_NORMAL:
223                         /* collect request; a sequence of keys and values */
224                         if (is_command(reader.line, &command) ||
225                             is_valid_capability(reader.line))
226                                 strvec_push(&keys, reader.line);
227                         else
228                                 die("unknown capability '%s'", reader.line);
229
230                         /* Consume the peeked line */
231                         packet_reader_read(&reader);
232                         break;
233                 case PACKET_READ_FLUSH:
234                         /*
235                          * If no command and no keys were given then the client
236                          * wanted to terminate the connection.
237                          */
238                         if (!keys.nr)
239                                 return 1;
240
241                         /*
242                          * The flush packet isn't consume here like it is in
243                          * the other parts of this switch statement.  This is
244                          * so that the command can read the flush packet and
245                          * see the end of the request in the same way it would
246                          * if command specific arguments were provided after a
247                          * delim packet.
248                          */
249                         state = PROCESS_REQUEST_DONE;
250                         break;
251                 case PACKET_READ_DELIM:
252                         /* Consume the peeked line */
253                         packet_reader_read(&reader);
254
255                         state = PROCESS_REQUEST_DONE;
256                         break;
257                 case PACKET_READ_RESPONSE_END:
258                         BUG("unexpected stateless separator packet");
259                 }
260         }
261
262         if (!command)
263                 die("no command requested");
264
265         check_algorithm(the_repository, &keys);
266
267         command->command(the_repository, &keys, &reader);
268
269         strvec_clear(&keys);
270         return 0;
271 }
272
273 /* Main serve loop for protocol version 2 */
274 void serve(struct serve_options *options)
275 {
276         git_config_get_bool("transfer.advertisesid", &advertise_sid);
277
278         if (options->advertise_capabilities || !options->stateless_rpc) {
279                 /* serve by default supports v2 */
280                 packet_write_fmt(1, "version 2\n");
281
282                 advertise_capabilities();
283                 /*
284                  * If only the list of capabilities was requested exit
285                  * immediately after advertising capabilities
286                  */
287                 if (options->advertise_capabilities)
288                         return;
289         }
290
291         /*
292          * If stateless-rpc was requested then exit after
293          * a single request/response exchange
294          */
295         if (options->stateless_rpc) {
296                 process_request();
297         } else {
298                 for (;;)
299                         if (process_request())
300                                 break;
301         }
302 }