interplane.Get Omega
Read in your language

git

[Ported from github.com/git/git] Sync with Git 2.56.1

Brought by drakestapleton

branch master Branches Tags History

git / serve.c

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