]>
Commit | Line | Data |
---|---|---|
ed10cb95 BW |
1 | #include "cache.h" |
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" |
ed10cb95 | 11 | |
eea7f7a9 | 12 | static int advertise_sid = -1; |
c7d3aabd | 13 | static int client_hash_algo = GIT_HASH_SHA1; |
6b5b6e42 | 14 | |
72d0ea00 BW |
15 | static int always_advertise(struct repository *r, |
16 | struct strbuf *value) | |
17 | { | |
18 | return 1; | |
19 | } | |
20 | ||
ed10cb95 BW |
21 | static 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 | ||
9de0dd36 | 29 | static 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 | ||
c7d3aabd JK |
37 | static 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 | ||
6b5b6e42 JS |
48 | static int session_id_advertise(struct repository *r, struct strbuf *value) |
49 | { | |
eea7f7a9 ÆAB |
50 | if (advertise_sid == -1 && |
51 | git_config_get_bool("transfer.advertisesid", &advertise_sid)) | |
52 | advertise_sid = 0; | |
6b5b6e42 JS |
53 | if (!advertise_sid) |
54 | return 0; | |
55 | if (value) | |
56 | strbuf_addstr(value, trace2_session_id()); | |
57 | return 1; | |
58 | } | |
59 | ||
ab539c90 JK |
60 | static 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 | ||
ed10cb95 BW |
68 | struct 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. | |
28a592e4 | 86 | * Will be provided a struct packet_reader 'request' which it should |
ed10cb95 BW |
87 | * 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 | */ | |
28a592e4 | 92 | int (*command)(struct repository *r, struct packet_reader *request); |
e56e5306 JK |
93 | |
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); | |
ed10cb95 BW |
103 | }; |
104 | ||
105 | static struct protocol_capability capabilities[] = { | |
85baaed4 ÆAB |
106 | { |
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, | |
c7d3aabd | 127 | .receive = object_format_receive, |
85baaed4 ÆAB |
128 | }, |
129 | { | |
130 | .name = "session-id", | |
131 | .advertise = session_id_advertise, | |
ab539c90 | 132 | .receive = session_id_receive, |
85baaed4 ÆAB |
133 | }, |
134 | { | |
135 | .name = "object-info", | |
136 | .advertise = always_advertise, | |
137 | .command = cap_object_info, | |
138 | }, | |
8b8d9a22 ÆAB |
139 | { |
140 | .name = "bundle-uri", | |
141 | .advertise = bundle_uri_advertise, | |
142 | .command = bundle_uri_command, | |
143 | }, | |
ed10cb95 BW |
144 | }; |
145 | ||
f234da80 | 146 | void protocol_v2_advertise_capabilities(void) |
ed10cb95 BW |
147 | { |
148 | struct strbuf capability = STRBUF_INIT; | |
149 | struct strbuf value = STRBUF_INIT; | |
150 | int i; | |
151 | ||
5befe8a1 ÆAB |
152 | /* serve by default supports v2 */ |
153 | packet_write_fmt(1, "version 2\n"); | |
154 | ||
ed10cb95 BW |
155 | for (i = 0; i < ARRAY_SIZE(capabilities); i++) { |
156 | struct protocol_capability *c = &capabilities[i]; | |
157 | ||
158 | if (c->advertise(the_repository, &value)) { | |
159 | strbuf_addstr(&capability, c->name); | |
160 | ||
161 | if (value.len) { | |
162 | strbuf_addch(&capability, '='); | |
163 | strbuf_addbuf(&capability, &value); | |
164 | } | |
165 | ||
166 | strbuf_addch(&capability, '\n'); | |
167 | packet_write(1, capability.buf, capability.len); | |
168 | } | |
169 | ||
170 | strbuf_reset(&capability); | |
171 | strbuf_reset(&value); | |
172 | } | |
173 | ||
174 | packet_flush(1); | |
175 | strbuf_release(&capability); | |
176 | strbuf_release(&value); | |
177 | } | |
178 | ||
5ef260d2 | 179 | static struct protocol_capability *get_capability(const char *key, const char **value) |
ed10cb95 BW |
180 | { |
181 | int i; | |
182 | ||
183 | if (!key) | |
184 | return NULL; | |
185 | ||
186 | for (i = 0; i < ARRAY_SIZE(capabilities); i++) { | |
187 | struct protocol_capability *c = &capabilities[i]; | |
188 | const char *out; | |
5ef260d2 JK |
189 | if (!skip_prefix(key, c->name, &out)) |
190 | continue; | |
191 | if (!*out) { | |
192 | *value = NULL; | |
ed10cb95 | 193 | return c; |
5ef260d2 JK |
194 | } |
195 | if (*out++ == '=') { | |
196 | *value = out; | |
197 | return c; | |
198 | } | |
ed10cb95 BW |
199 | } |
200 | ||
201 | return NULL; | |
202 | } | |
203 | ||
e56e5306 | 204 | static int receive_client_capability(const char *key) |
ed10cb95 | 205 | { |
5ef260d2 JK |
206 | const char *value; |
207 | const struct protocol_capability *c = get_capability(key, &value); | |
ed10cb95 | 208 | |
0ab7eecc | 209 | if (!c || c->command || !c->advertise(the_repository, NULL)) |
e56e5306 JK |
210 | return 0; |
211 | ||
212 | if (c->receive) | |
213 | c->receive(the_repository, value); | |
214 | return 1; | |
ed10cb95 BW |
215 | } |
216 | ||
76804526 | 217 | static int parse_command(const char *key, struct protocol_capability **command) |
ed10cb95 BW |
218 | { |
219 | const char *out; | |
220 | ||
221 | if (skip_prefix(key, "command=", &out)) { | |
5ef260d2 JK |
222 | const char *value; |
223 | struct protocol_capability *cmd = get_capability(out, &value); | |
ed10cb95 BW |
224 | |
225 | if (*command) | |
226 | die("command '%s' requested after already requesting command '%s'", | |
227 | out, (*command)->name); | |
108c265f | 228 | if (!cmd || !cmd->advertise(the_repository, NULL) || !cmd->command || value) |
ed10cb95 BW |
229 | die("invalid command '%s'", out); |
230 | ||
231 | *command = cmd; | |
232 | return 1; | |
233 | } | |
234 | ||
235 | return 0; | |
236 | } | |
237 | ||
ed10cb95 BW |
238 | enum request_state { |
239 | PROCESS_REQUEST_KEYS, | |
240 | PROCESS_REQUEST_DONE, | |
241 | }; | |
242 | ||
243 | static int process_request(void) | |
244 | { | |
245 | enum request_state state = PROCESS_REQUEST_KEYS; | |
246 | struct packet_reader reader; | |
f0a35c9c | 247 | int seen_capability_or_command = 0; |
ed10cb95 BW |
248 | struct protocol_capability *command = NULL; |
249 | ||
250 | packet_reader_init(&reader, 0, NULL, 0, | |
251 | PACKET_READ_CHOMP_NEWLINE | | |
2d103c31 MS |
252 | PACKET_READ_GENTLE_ON_EOF | |
253 | PACKET_READ_DIE_ON_ERR_PACKET); | |
ed10cb95 BW |
254 | |
255 | /* | |
256 | * Check to see if the client closed their end before sending another | |
257 | * request. If so we can terminate the connection. | |
258 | */ | |
259 | if (packet_reader_peek(&reader) == PACKET_READ_EOF) | |
260 | return 1; | |
2d103c31 | 261 | reader.options &= ~PACKET_READ_GENTLE_ON_EOF; |
ed10cb95 BW |
262 | |
263 | while (state != PROCESS_REQUEST_DONE) { | |
264 | switch (packet_reader_peek(&reader)) { | |
265 | case PACKET_READ_EOF: | |
266 | BUG("Should have already died when seeing EOF"); | |
267 | case PACKET_READ_NORMAL: | |
76804526 | 268 | if (parse_command(reader.line, &command) || |
e56e5306 | 269 | receive_client_capability(reader.line)) |
f0a35c9c | 270 | seen_capability_or_command = 1; |
ed10cb95 BW |
271 | else |
272 | die("unknown capability '%s'", reader.line); | |
273 | ||
274 | /* Consume the peeked line */ | |
275 | packet_reader_read(&reader); | |
276 | break; | |
277 | case PACKET_READ_FLUSH: | |
278 | /* | |
279 | * If no command and no keys were given then the client | |
280 | * wanted to terminate the connection. | |
281 | */ | |
f0a35c9c | 282 | if (!seen_capability_or_command) |
ed10cb95 BW |
283 | return 1; |
284 | ||
285 | /* | |
286 | * The flush packet isn't consume here like it is in | |
287 | * the other parts of this switch statement. This is | |
288 | * so that the command can read the flush packet and | |
289 | * see the end of the request in the same way it would | |
290 | * if command specific arguments were provided after a | |
291 | * delim packet. | |
292 | */ | |
293 | state = PROCESS_REQUEST_DONE; | |
294 | break; | |
295 | case PACKET_READ_DELIM: | |
296 | /* Consume the peeked line */ | |
297 | packet_reader_read(&reader); | |
298 | ||
299 | state = PROCESS_REQUEST_DONE; | |
300 | break; | |
0181b600 | 301 | case PACKET_READ_RESPONSE_END: |
8232a0ff | 302 | BUG("unexpected response end packet"); |
ed10cb95 BW |
303 | } |
304 | } | |
305 | ||
306 | if (!command) | |
307 | die("no command requested"); | |
308 | ||
c7d3aabd JK |
309 | if (client_hash_algo != hash_algo_by_ptr(the_repository->hash_algo)) |
310 | die("mismatched object format: server %s; client %s\n", | |
311 | the_repository->hash_algo->name, | |
312 | hash_algos[client_hash_algo].name); | |
9de0dd36 | 313 | |
28a592e4 | 314 | command->command(the_repository, &reader); |
ed10cb95 | 315 | |
ed10cb95 BW |
316 | return 0; |
317 | } | |
318 | ||
f234da80 | 319 | void protocol_v2_serve_loop(int stateless_rpc) |
ed10cb95 | 320 | { |
f234da80 ÆAB |
321 | if (!stateless_rpc) |
322 | protocol_v2_advertise_capabilities(); | |
ed10cb95 BW |
323 | |
324 | /* | |
325 | * If stateless-rpc was requested then exit after | |
326 | * a single request/response exchange | |
327 | */ | |
f234da80 | 328 | if (stateless_rpc) { |
ed10cb95 BW |
329 | process_request(); |
330 | } else { | |
331 | for (;;) | |
332 | if (process_request()) | |
333 | break; | |
334 | } | |
335 | } |