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