]> git.ipfire.org Git - thirdparty/git.git/blob - serve.c
Merge branch 'jx/t5411-unique-filenames'
[thirdparty/git.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", ls_refs_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 const char *client_sid;
205
206 packet_reader_init(&reader, 0, NULL, 0,
207 PACKET_READ_CHOMP_NEWLINE |
208 PACKET_READ_GENTLE_ON_EOF |
209 PACKET_READ_DIE_ON_ERR_PACKET);
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;
217 reader.options &= ~PACKET_READ_GENTLE_ON_EOF;
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))
227 strvec_push(&keys, reader.line);
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 */
239 if (!keys.nr)
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;
258 case PACKET_READ_RESPONSE_END:
259 BUG("unexpected stateless separator packet");
260 }
261 }
262
263 if (!command)
264 die("no command requested");
265
266 check_algorithm(the_repository, &keys);
267
268 if (has_capability(&keys, "session-id", &client_sid))
269 trace2_data_string("transfer", NULL, "client-sid", client_sid);
270
271 command->command(the_repository, &keys, &reader);
272
273 strvec_clear(&keys);
274 return 0;
275 }
276
277 /* Main serve loop for protocol version 2 */
278 void serve(struct serve_options *options)
279 {
280 git_config_get_bool("transfer.advertisesid", &advertise_sid);
281
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 }