blob: b3fe9b5126a3347784d501e296102ab87cc4c5ab [file] [log] [blame]
Brandon Williamsed10cb92018-03-15 10:31:19 -07001#include "cache.h"
2#include "repository.h"
3#include "config.h"
4#include "pkt-line.h"
5#include "version.h"
Jeff Kingdbbcd442020-07-28 16:23:39 -04006#include "strvec.h"
Brandon Williams72d0ea02018-03-15 10:31:20 -07007#include "ls-refs.h"
Bruno Albuquerquea2ba1622021-04-20 16:38:31 -07008#include "protocol-caps.h"
Brandon Williamsed10cb92018-03-15 10:31:19 -07009#include "serve.h"
Brandon Williams3145ea92018-03-15 10:31:27 -070010#include "upload-pack.h"
Brandon Williamsed10cb92018-03-15 10:31:19 -070011
Ævar Arnfjörð Bjarmasoneea7f7a2021-08-05 03:25:39 +020012static int advertise_sid = -1;
Jeff Kingc7d3aab2021-09-14 19:51:27 -040013static int client_hash_algo = GIT_HASH_SHA1;
Josh Steadmon6b5b6e42020-11-11 15:29:29 -080014
Brandon Williams72d0ea02018-03-15 10:31:20 -070015static int always_advertise(struct repository *r,
16 struct strbuf *value)
17{
18 return 1;
19}
20
Brandon Williamsed10cb92018-03-15 10:31:19 -070021static int agent_advertise(struct repository *r,
22 struct strbuf *value)
23{
24 if (value)
25 strbuf_addstr(value, git_user_agent_sanitized());
26 return 1;
27}
28
brian m. carlson9de0dd32020-05-25 19:59:17 +000029static int object_format_advertise(struct repository *r,
30 struct strbuf *value)
31{
32 if (value)
33 strbuf_addstr(value, r->hash_algo->name);
34 return 1;
35}
36
Jeff Kingc7d3aab2021-09-14 19:51:27 -040037static void object_format_receive(struct repository *r,
38 const char *algo_name)
39{
40 if (!algo_name)
41 die("object-format capability requires an argument");
42
43 client_hash_algo = hash_algo_by_name(algo_name);
44 if (client_hash_algo == GIT_HASH_UNKNOWN)
45 die("unknown object format '%s'", algo_name);
46}
47
Josh Steadmon6b5b6e42020-11-11 15:29:29 -080048static int session_id_advertise(struct repository *r, struct strbuf *value)
49{
Ævar Arnfjörð Bjarmasoneea7f7a2021-08-05 03:25:39 +020050 if (advertise_sid == -1 &&
51 git_config_get_bool("transfer.advertisesid", &advertise_sid))
52 advertise_sid = 0;
Josh Steadmon6b5b6e42020-11-11 15:29:29 -080053 if (!advertise_sid)
54 return 0;
55 if (value)
56 strbuf_addstr(value, trace2_session_id());
57 return 1;
58}
59
Jeff Kingab539c92021-09-14 19:51:30 -040060static void session_id_receive(struct repository *r,
61 const char *client_sid)
62{
63 if (!client_sid)
64 client_sid = "";
65 trace2_data_string("transfer", NULL, "client-sid", client_sid);
66}
67
Brandon Williamsed10cb92018-03-15 10:31:19 -070068struct protocol_capability {
69 /*
70 * The name of the capability. The server uses this name when
71 * advertising this capability, and the client uses this name to
72 * specify this capability.
73 */
74 const char *name;
75
76 /*
77 * Function queried to see if a capability should be advertised.
78 * Optionally a value can be specified by adding it to 'value'.
79 * If a value is added to 'value', the server will advertise this
80 * capability as "<name>=<value>" instead of "<name>".
81 */
82 int (*advertise)(struct repository *r, struct strbuf *value);
83
84 /*
85 * Function called when a client requests the capability as a command.
Ævar Arnfjörð Bjarmason28a592e2021-08-05 03:25:38 +020086 * Will be provided a struct packet_reader 'request' which it should
Brandon Williamsed10cb92018-03-15 10:31:19 -070087 * use to read the command specific part of the request. Every command
88 * MUST read until a flush packet is seen before sending a response.
89 *
90 * This field should be NULL for capabilities which are not commands.
91 */
Ævar Arnfjörð Bjarmason28a592e2021-08-05 03:25:38 +020092 int (*command)(struct repository *r, struct packet_reader *request);
Jeff Kinge56e5302021-09-14 11:31:07 -040093
94 /*
95 * Function called when a client requests the capability as a
96 * non-command. This may be NULL if the capability does nothing.
97 *
98 * For a capability of the form "foo=bar", the value string points to
99 * the content after the "=" (i.e., "bar"). For simple capabilities
100 * (just "foo"), it is NULL.
101 */
102 void (*receive)(struct repository *r, const char *value);
Brandon Williamsed10cb92018-03-15 10:31:19 -0700103};
104
105static struct protocol_capability capabilities[] = {
Ævar Arnfjörð Bjarmason85baaed2021-08-05 03:25:37 +0200106 {
107 .name = "agent",
108 .advertise = agent_advertise,
109 },
110 {
111 .name = "ls-refs",
112 .advertise = ls_refs_advertise,
113 .command = ls_refs,
114 },
115 {
116 .name = "fetch",
117 .advertise = upload_pack_advertise,
118 .command = upload_pack_v2,
119 },
120 {
121 .name = "server-option",
122 .advertise = always_advertise,
123 },
124 {
125 .name = "object-format",
126 .advertise = object_format_advertise,
Jeff Kingc7d3aab2021-09-14 19:51:27 -0400127 .receive = object_format_receive,
Ævar Arnfjörð Bjarmason85baaed2021-08-05 03:25:37 +0200128 },
129 {
130 .name = "session-id",
131 .advertise = session_id_advertise,
Jeff Kingab539c92021-09-14 19:51:30 -0400132 .receive = session_id_receive,
Ævar Arnfjörð Bjarmason85baaed2021-08-05 03:25:37 +0200133 },
134 {
135 .name = "object-info",
136 .advertise = always_advertise,
137 .command = cap_object_info,
138 },
Brandon Williamsed10cb92018-03-15 10:31:19 -0700139};
140
Ævar Arnfjörð Bjarmasonf234da82021-08-05 03:25:42 +0200141void protocol_v2_advertise_capabilities(void)
Brandon Williamsed10cb92018-03-15 10:31:19 -0700142{
143 struct strbuf capability = STRBUF_INIT;
144 struct strbuf value = STRBUF_INIT;
145 int i;
146
Ævar Arnfjörð Bjarmason5befe8a2021-08-05 03:25:40 +0200147 /* serve by default supports v2 */
148 packet_write_fmt(1, "version 2\n");
149
Brandon Williamsed10cb92018-03-15 10:31:19 -0700150 for (i = 0; i < ARRAY_SIZE(capabilities); i++) {
151 struct protocol_capability *c = &capabilities[i];
152
153 if (c->advertise(the_repository, &value)) {
154 strbuf_addstr(&capability, c->name);
155
156 if (value.len) {
157 strbuf_addch(&capability, '=');
158 strbuf_addbuf(&capability, &value);
159 }
160
161 strbuf_addch(&capability, '\n');
162 packet_write(1, capability.buf, capability.len);
163 }
164
165 strbuf_reset(&capability);
166 strbuf_reset(&value);
167 }
168
169 packet_flush(1);
170 strbuf_release(&capability);
171 strbuf_release(&value);
172}
173
Jeff King5ef260d2021-09-14 11:30:50 -0400174static struct protocol_capability *get_capability(const char *key, const char **value)
Brandon Williamsed10cb92018-03-15 10:31:19 -0700175{
176 int i;
177
178 if (!key)
179 return NULL;
180
181 for (i = 0; i < ARRAY_SIZE(capabilities); i++) {
182 struct protocol_capability *c = &capabilities[i];
183 const char *out;
Jeff King5ef260d2021-09-14 11:30:50 -0400184 if (!skip_prefix(key, c->name, &out))
185 continue;
186 if (!*out) {
187 *value = NULL;
Brandon Williamsed10cb92018-03-15 10:31:19 -0700188 return c;
Jeff King5ef260d2021-09-14 11:30:50 -0400189 }
190 if (*out++ == '=') {
191 *value = out;
192 return c;
193 }
Brandon Williamsed10cb92018-03-15 10:31:19 -0700194 }
195
196 return NULL;
197}
198
Jeff Kinge56e5302021-09-14 11:31:07 -0400199static int receive_client_capability(const char *key)
Brandon Williamsed10cb92018-03-15 10:31:19 -0700200{
Jeff King5ef260d2021-09-14 11:30:50 -0400201 const char *value;
202 const struct protocol_capability *c = get_capability(key, &value);
Brandon Williamsed10cb92018-03-15 10:31:19 -0700203
Jeff King0ab7eec2021-09-15 14:36:36 -0400204 if (!c || c->command || !c->advertise(the_repository, NULL))
Jeff Kinge56e5302021-09-14 11:31:07 -0400205 return 0;
206
207 if (c->receive)
208 c->receive(the_repository, value);
209 return 1;
Brandon Williamsed10cb92018-03-15 10:31:19 -0700210}
211
Jeff King76804522021-09-14 11:30:20 -0400212static int parse_command(const char *key, struct protocol_capability **command)
Brandon Williamsed10cb92018-03-15 10:31:19 -0700213{
214 const char *out;
215
216 if (skip_prefix(key, "command=", &out)) {
Jeff King5ef260d2021-09-14 11:30:50 -0400217 const char *value;
218 struct protocol_capability *cmd = get_capability(out, &value);
Brandon Williamsed10cb92018-03-15 10:31:19 -0700219
220 if (*command)
221 die("command '%s' requested after already requesting command '%s'",
222 out, (*command)->name);
Jeff King108c2652021-09-15 14:36:33 -0400223 if (!cmd || !cmd->advertise(the_repository, NULL) || !cmd->command || value)
Brandon Williamsed10cb92018-03-15 10:31:19 -0700224 die("invalid command '%s'", out);
225
226 *command = cmd;
227 return 1;
228 }
229
230 return 0;
231}
232
Brandon Williamsed10cb92018-03-15 10:31:19 -0700233enum request_state {
234 PROCESS_REQUEST_KEYS,
235 PROCESS_REQUEST_DONE,
236};
237
238static int process_request(void)
239{
240 enum request_state state = PROCESS_REQUEST_KEYS;
241 struct packet_reader reader;
Jeff Kingf0a35c92021-09-15 14:35:29 -0400242 int seen_capability_or_command = 0;
Brandon Williamsed10cb92018-03-15 10:31:19 -0700243 struct protocol_capability *command = NULL;
244
245 packet_reader_init(&reader, 0, NULL, 0,
246 PACKET_READ_CHOMP_NEWLINE |
Masaya Suzuki2d103c32018-12-29 13:19:15 -0800247 PACKET_READ_GENTLE_ON_EOF |
248 PACKET_READ_DIE_ON_ERR_PACKET);
Brandon Williamsed10cb92018-03-15 10:31:19 -0700249
250 /*
251 * Check to see if the client closed their end before sending another
252 * request. If so we can terminate the connection.
253 */
254 if (packet_reader_peek(&reader) == PACKET_READ_EOF)
255 return 1;
Masaya Suzuki2d103c32018-12-29 13:19:15 -0800256 reader.options &= ~PACKET_READ_GENTLE_ON_EOF;
Brandon Williamsed10cb92018-03-15 10:31:19 -0700257
258 while (state != PROCESS_REQUEST_DONE) {
259 switch (packet_reader_peek(&reader)) {
260 case PACKET_READ_EOF:
261 BUG("Should have already died when seeing EOF");
262 case PACKET_READ_NORMAL:
Jeff King76804522021-09-14 11:30:20 -0400263 if (parse_command(reader.line, &command) ||
Jeff Kinge56e5302021-09-14 11:31:07 -0400264 receive_client_capability(reader.line))
Jeff Kingf0a35c92021-09-15 14:35:29 -0400265 seen_capability_or_command = 1;
Brandon Williamsed10cb92018-03-15 10:31:19 -0700266 else
267 die("unknown capability '%s'", reader.line);
268
269 /* Consume the peeked line */
270 packet_reader_read(&reader);
271 break;
272 case PACKET_READ_FLUSH:
273 /*
274 * If no command and no keys were given then the client
275 * wanted to terminate the connection.
276 */
Jeff Kingf0a35c92021-09-15 14:35:29 -0400277 if (!seen_capability_or_command)
Brandon Williamsed10cb92018-03-15 10:31:19 -0700278 return 1;
279
280 /*
281 * The flush packet isn't consume here like it is in
282 * the other parts of this switch statement. This is
283 * so that the command can read the flush packet and
284 * see the end of the request in the same way it would
285 * if command specific arguments were provided after a
286 * delim packet.
287 */
288 state = PROCESS_REQUEST_DONE;
289 break;
290 case PACKET_READ_DELIM:
291 /* Consume the peeked line */
292 packet_reader_read(&reader);
293
294 state = PROCESS_REQUEST_DONE;
295 break;
Denton Liu0181b602020-05-19 06:53:59 -0400296 case PACKET_READ_RESPONSE_END:
Denton Liu8232a0f2021-07-08 19:27:22 -0700297 BUG("unexpected response end packet");
Brandon Williamsed10cb92018-03-15 10:31:19 -0700298 }
299 }
300
301 if (!command)
302 die("no command requested");
303
Jeff Kingc7d3aab2021-09-14 19:51:27 -0400304 if (client_hash_algo != hash_algo_by_ptr(the_repository->hash_algo))
305 die("mismatched object format: server %s; client %s\n",
306 the_repository->hash_algo->name,
307 hash_algos[client_hash_algo].name);
brian m. carlson9de0dd32020-05-25 19:59:17 +0000308
Ævar Arnfjörð Bjarmason28a592e2021-08-05 03:25:38 +0200309 command->command(the_repository, &reader);
Brandon Williamsed10cb92018-03-15 10:31:19 -0700310
Brandon Williamsed10cb92018-03-15 10:31:19 -0700311 return 0;
312}
313
Ævar Arnfjörð Bjarmasonf234da82021-08-05 03:25:42 +0200314void protocol_v2_serve_loop(int stateless_rpc)
Brandon Williamsed10cb92018-03-15 10:31:19 -0700315{
Ævar Arnfjörð Bjarmasonf234da82021-08-05 03:25:42 +0200316 if (!stateless_rpc)
317 protocol_v2_advertise_capabilities();
Brandon Williamsed10cb92018-03-15 10:31:19 -0700318
319 /*
320 * If stateless-rpc was requested then exit after
321 * a single request/response exchange
322 */
Ævar Arnfjörð Bjarmasonf234da82021-08-05 03:25:42 +0200323 if (stateless_rpc) {
Brandon Williamsed10cb92018-03-15 10:31:19 -0700324 process_request();
325 } else {
326 for (;;)
327 if (process_request())
328 break;
329 }
330}