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