]> git.ipfire.org Git - thirdparty/git.git/blame - serve.c
config.mak.uname: remove unused the NO_R_TO_GCC_LINKER flag
[thirdparty/git.git] / serve.c
CommitLineData
ed10cb95
BW
1#include "cache.h"
2#include "repository.h"
3#include "config.h"
4#include "pkt-line.h"
5#include "version.h"
dbbcd44f 6#include "strvec.h"
72d0ea00 7#include "ls-refs.h"
ed10cb95 8#include "serve.h"
3145ea95 9#include "upload-pack.h"
ed10cb95 10
72d0ea00
BW
11static int always_advertise(struct repository *r,
12 struct strbuf *value)
13{
14 return 1;
15}
16
ed10cb95
BW
17static int agent_advertise(struct repository *r,
18 struct strbuf *value)
19{
20 if (value)
21 strbuf_addstr(value, git_user_agent_sanitized());
22 return 1;
23}
24
9de0dd36 25static int object_format_advertise(struct repository *r,
26 struct strbuf *value)
27{
28 if (value)
29 strbuf_addstr(value, r->hash_algo->name);
30 return 1;
31}
32
ed10cb95
BW
33struct protocol_capability {
34 /*
35 * The name of the capability. The server uses this name when
36 * advertising this capability, and the client uses this name to
37 * specify this capability.
38 */
39 const char *name;
40
41 /*
42 * Function queried to see if a capability should be advertised.
43 * Optionally a value can be specified by adding it to 'value'.
44 * If a value is added to 'value', the server will advertise this
45 * capability as "<name>=<value>" instead of "<name>".
46 */
47 int (*advertise)(struct repository *r, struct strbuf *value);
48
49 /*
50 * Function called when a client requests the capability as a command.
51 * The function will be provided the capabilities requested via 'keys'
52 * as well as a struct packet_reader 'request' which the command should
53 * use to read the command specific part of the request. Every command
54 * MUST read until a flush packet is seen before sending a response.
55 *
56 * This field should be NULL for capabilities which are not commands.
57 */
58 int (*command)(struct repository *r,
c972bf4c 59 struct strvec *keys,
ed10cb95
BW
60 struct packet_reader *request);
61};
62
63static struct protocol_capability capabilities[] = {
64 { "agent", agent_advertise, NULL },
72d0ea00 65 { "ls-refs", always_advertise, ls_refs },
685fbd32 66 { "fetch", upload_pack_advertise, upload_pack_v2 },
ecc3e534 67 { "server-option", always_advertise, NULL },
9de0dd36 68 { "object-format", object_format_advertise, NULL },
ed10cb95
BW
69};
70
71static void advertise_capabilities(void)
72{
73 struct strbuf capability = STRBUF_INIT;
74 struct strbuf value = STRBUF_INIT;
75 int i;
76
77 for (i = 0; i < ARRAY_SIZE(capabilities); i++) {
78 struct protocol_capability *c = &capabilities[i];
79
80 if (c->advertise(the_repository, &value)) {
81 strbuf_addstr(&capability, c->name);
82
83 if (value.len) {
84 strbuf_addch(&capability, '=');
85 strbuf_addbuf(&capability, &value);
86 }
87
88 strbuf_addch(&capability, '\n');
89 packet_write(1, capability.buf, capability.len);
90 }
91
92 strbuf_reset(&capability);
93 strbuf_reset(&value);
94 }
95
96 packet_flush(1);
97 strbuf_release(&capability);
98 strbuf_release(&value);
99}
100
101static struct protocol_capability *get_capability(const char *key)
102{
103 int i;
104
105 if (!key)
106 return NULL;
107
108 for (i = 0; i < ARRAY_SIZE(capabilities); i++) {
109 struct protocol_capability *c = &capabilities[i];
110 const char *out;
111 if (skip_prefix(key, c->name, &out) && (!*out || *out == '='))
112 return c;
113 }
114
115 return NULL;
116}
117
118static int is_valid_capability(const char *key)
119{
120 const struct protocol_capability *c = get_capability(key);
121
122 return c && c->advertise(the_repository, NULL);
123}
124
125static int is_command(const char *key, struct protocol_capability **command)
126{
127 const char *out;
128
129 if (skip_prefix(key, "command=", &out)) {
130 struct protocol_capability *cmd = get_capability(out);
131
132 if (*command)
133 die("command '%s' requested after already requesting command '%s'",
134 out, (*command)->name);
135 if (!cmd || !cmd->advertise(the_repository, NULL) || !cmd->command)
136 die("invalid command '%s'", out);
137
138 *command = cmd;
139 return 1;
140 }
141
142 return 0;
143}
144
c972bf4c 145int has_capability(const struct strvec *keys, const char *capability,
ed10cb95
BW
146 const char **value)
147{
148 int i;
d70a9eb6 149 for (i = 0; i < keys->nr; i++) {
ed10cb95 150 const char *out;
d70a9eb6 151 if (skip_prefix(keys->v[i], capability, &out) &&
ed10cb95
BW
152 (!*out || *out == '=')) {
153 if (value) {
154 if (*out == '=')
155 out++;
156 *value = out;
157 }
158 return 1;
159 }
160 }
161
162 return 0;
163}
164
c972bf4c 165static void check_algorithm(struct repository *r, struct strvec *keys)
9de0dd36 166{
167 int client = GIT_HASH_SHA1, server = hash_algo_by_ptr(r->hash_algo);
168 const char *algo_name;
169
170 if (has_capability(keys, "object-format", &algo_name)) {
171 client = hash_algo_by_name(algo_name);
172 if (client == GIT_HASH_UNKNOWN)
173 die("unknown object format '%s'", algo_name);
174 }
175
176 if (client != server)
177 die("mismatched object format: server %s; client %s\n",
178 r->hash_algo->name, hash_algos[client].name);
179}
180
ed10cb95
BW
181enum request_state {
182 PROCESS_REQUEST_KEYS,
183 PROCESS_REQUEST_DONE,
184};
185
186static int process_request(void)
187{
188 enum request_state state = PROCESS_REQUEST_KEYS;
189 struct packet_reader reader;
c972bf4c 190 struct strvec keys = STRVEC_INIT;
ed10cb95
BW
191 struct protocol_capability *command = NULL;
192
193 packet_reader_init(&reader, 0, NULL, 0,
194 PACKET_READ_CHOMP_NEWLINE |
2d103c31
MS
195 PACKET_READ_GENTLE_ON_EOF |
196 PACKET_READ_DIE_ON_ERR_PACKET);
ed10cb95
BW
197
198 /*
199 * Check to see if the client closed their end before sending another
200 * request. If so we can terminate the connection.
201 */
202 if (packet_reader_peek(&reader) == PACKET_READ_EOF)
203 return 1;
2d103c31 204 reader.options &= ~PACKET_READ_GENTLE_ON_EOF;
ed10cb95
BW
205
206 while (state != PROCESS_REQUEST_DONE) {
207 switch (packet_reader_peek(&reader)) {
208 case PACKET_READ_EOF:
209 BUG("Should have already died when seeing EOF");
210 case PACKET_READ_NORMAL:
211 /* collect request; a sequence of keys and values */
212 if (is_command(reader.line, &command) ||
213 is_valid_capability(reader.line))
c972bf4c 214 strvec_push(&keys, reader.line);
ed10cb95
BW
215 else
216 die("unknown capability '%s'", reader.line);
217
218 /* Consume the peeked line */
219 packet_reader_read(&reader);
220 break;
221 case PACKET_READ_FLUSH:
222 /*
223 * If no command and no keys were given then the client
224 * wanted to terminate the connection.
225 */
d70a9eb6 226 if (!keys.nr)
ed10cb95
BW
227 return 1;
228
229 /*
230 * The flush packet isn't consume here like it is in
231 * the other parts of this switch statement. This is
232 * so that the command can read the flush packet and
233 * see the end of the request in the same way it would
234 * if command specific arguments were provided after a
235 * delim packet.
236 */
237 state = PROCESS_REQUEST_DONE;
238 break;
239 case PACKET_READ_DELIM:
240 /* Consume the peeked line */
241 packet_reader_read(&reader);
242
243 state = PROCESS_REQUEST_DONE;
244 break;
0181b600
DL
245 case PACKET_READ_RESPONSE_END:
246 BUG("unexpected stateless separator packet");
ed10cb95
BW
247 }
248 }
249
250 if (!command)
251 die("no command requested");
252
9de0dd36 253 check_algorithm(the_repository, &keys);
254
ed10cb95
BW
255 command->command(the_repository, &keys, &reader);
256
c972bf4c 257 strvec_clear(&keys);
ed10cb95
BW
258 return 0;
259}
260
261/* Main serve loop for protocol version 2 */
262void serve(struct serve_options *options)
263{
264 if (options->advertise_capabilities || !options->stateless_rpc) {
265 /* serve by default supports v2 */
266 packet_write_fmt(1, "version 2\n");
267
268 advertise_capabilities();
269 /*
270 * If only the list of capabilities was requested exit
271 * immediately after advertising capabilities
272 */
273 if (options->advertise_capabilities)
274 return;
275 }
276
277 /*
278 * If stateless-rpc was requested then exit after
279 * a single request/response exchange
280 */
281 if (options->stateless_rpc) {
282 process_request();
283 } else {
284 for (;;)
285 if (process_request())
286 break;
287 }
288}