Merge branch 'ps/contains-id-error-message' into jch
[git] / serve.c
1 #include "cache.h"
2 #include "repository.h"
3 #include "config.h"
4 #include "pkt-line.h"
5 #include "version.h"
6 #include "argv-array.h"
7 #include "ls-refs.h"
8 #include "serve.h"
9 #include "upload-pack.h"
10
11 static int always_advertise(struct repository *r,
12                             struct strbuf *value)
13 {
14         return 1;
15 }
16
17 static 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
25 struct protocol_capability {
26         /*
27          * The name of the capability.  The server uses this name when
28          * advertising this capability, and the client uses this name to
29          * specify this capability.
30          */
31         const char *name;
32
33         /*
34          * Function queried to see if a capability should be advertised.
35          * Optionally a value can be specified by adding it to 'value'.
36          * If a value is added to 'value', the server will advertise this
37          * capability as "<name>=<value>" instead of "<name>".
38          */
39         int (*advertise)(struct repository *r, struct strbuf *value);
40
41         /*
42          * Function called when a client requests the capability as a command.
43          * The command request will be provided to the function via 'keys', the
44          * capabilities requested, and 'args', the command specific parameters.
45          *
46          * This field should be NULL for capabilities which are not commands.
47          */
48         int (*command)(struct repository *r,
49                        struct argv_array *keys,
50                        struct argv_array *args);
51 };
52
53 static struct protocol_capability capabilities[] = {
54         { "agent", agent_advertise, NULL },
55         { "ls-refs", always_advertise, ls_refs },
56         { "fetch", upload_pack_advertise, upload_pack_v2 },
57 };
58
59 static void advertise_capabilities(void)
60 {
61         struct strbuf capability = STRBUF_INIT;
62         struct strbuf value = STRBUF_INIT;
63         int i;
64
65         for (i = 0; i < ARRAY_SIZE(capabilities); i++) {
66                 struct protocol_capability *c = &capabilities[i];
67
68                 if (c->advertise(the_repository, &value)) {
69                         strbuf_addstr(&capability, c->name);
70
71                         if (value.len) {
72                                 strbuf_addch(&capability, '=');
73                                 strbuf_addbuf(&capability, &value);
74                         }
75
76                         strbuf_addch(&capability, '\n');
77                         packet_write(1, capability.buf, capability.len);
78                 }
79
80                 strbuf_reset(&capability);
81                 strbuf_reset(&value);
82         }
83
84         packet_flush(1);
85         strbuf_release(&capability);
86         strbuf_release(&value);
87 }
88
89 static struct protocol_capability *get_capability(const char *key)
90 {
91         int i;
92
93         if (!key)
94                 return NULL;
95
96         for (i = 0; i < ARRAY_SIZE(capabilities); i++) {
97                 struct protocol_capability *c = &capabilities[i];
98                 const char *out;
99                 if (skip_prefix(key, c->name, &out) && (!*out || *out == '='))
100                         return c;
101         }
102
103         return NULL;
104 }
105
106 static int is_valid_capability(const char *key)
107 {
108         const struct protocol_capability *c = get_capability(key);
109
110         return c && c->advertise(the_repository, NULL);
111 }
112
113 static int is_command(const char *key, struct protocol_capability **command)
114 {
115         const char *out;
116
117         if (skip_prefix(key, "command=", &out)) {
118                 struct protocol_capability *cmd = get_capability(out);
119
120                 if (!cmd || !cmd->advertise(the_repository, NULL) || !cmd->command)
121                         die("invalid command '%s'", out);
122                 if (*command)
123                         die("command already requested");
124
125                 *command = cmd;
126                 return 1;
127         }
128
129         return 0;
130 }
131
132 int has_capability(const struct argv_array *keys, const char *capability,
133                    const char **value)
134 {
135         int i;
136         for (i = 0; i < keys->argc; i++) {
137                 const char *out;
138                 if (skip_prefix(keys->argv[i], capability, &out) &&
139                     (!*out || *out == '=')) {
140                         if (value) {
141                                 if (*out == '=')
142                                         out++;
143                                 *value = out;
144                         }
145                         return 1;
146                 }
147         }
148
149         return 0;
150 }
151
152 enum request_state {
153         PROCESS_REQUEST_KEYS = 0,
154         PROCESS_REQUEST_ARGS,
155         PROCESS_REQUEST_DONE,
156 };
157
158 static int process_request(void)
159 {
160         enum request_state state = PROCESS_REQUEST_KEYS;
161         struct packet_reader reader;
162         struct argv_array keys = ARGV_ARRAY_INIT;
163         struct argv_array args = ARGV_ARRAY_INIT;
164         struct protocol_capability *command = NULL;
165
166         packet_reader_init(&reader, 0, NULL, 0,
167                            PACKET_READ_CHOMP_NEWLINE |
168                            PACKET_READ_GENTLE_ON_EOF);
169
170         /*
171          * Check to see if the client closed their end before sending another
172          * request.  If so we can terminate the connection.
173          */
174         if (packet_reader_peek(&reader) == PACKET_READ_EOF)
175                 return 1;
176         reader.options = PACKET_READ_CHOMP_NEWLINE;
177
178         while (state != PROCESS_REQUEST_DONE) {
179                 switch (packet_reader_read(&reader)) {
180                 case PACKET_READ_EOF:
181                         BUG("Should have already died when seeing EOF");
182                 case PACKET_READ_NORMAL:
183                         break;
184                 case PACKET_READ_FLUSH:
185                         state = PROCESS_REQUEST_DONE;
186                         continue;
187                 case PACKET_READ_DELIM:
188                         if (state != PROCESS_REQUEST_KEYS)
189                                 die("protocol error");
190                         state = PROCESS_REQUEST_ARGS;
191                         /*
192                          * maybe include a check to make sure that a
193                          * command/capabilities were given.
194                          */
195                         continue;
196                 }
197
198                 switch (state) {
199                 case PROCESS_REQUEST_KEYS:
200                         /* collect request; a sequence of keys and values */
201                         if (is_command(reader.line, &command) ||
202                             is_valid_capability(reader.line))
203                                 argv_array_push(&keys, reader.line);
204                         else
205                                 die("unknown capability '%s'", reader.line);
206                         break;
207                 case PROCESS_REQUEST_ARGS:
208                         /* collect arguments for the requested command */
209                         argv_array_push(&args, reader.line);
210                         break;
211                 case PROCESS_REQUEST_DONE:
212                         continue;
213                 }
214         }
215
216         /*
217          * If no command and no keys were given then the client wanted to
218          * terminate the connection.
219          */
220         if (!keys.argc && !args.argc)
221                 return 1;
222
223         if (!command)
224                 die("no command requested");
225
226         command->command(the_repository, &keys, &args);
227
228         argv_array_clear(&keys);
229         argv_array_clear(&args);
230         return 0;
231 }
232
233 /* Main serve loop for protocol version 2 */
234 void serve(struct serve_options *options)
235 {
236         if (options->advertise_capabilities || !options->stateless_rpc) {
237                 /* serve by default supports v2 */
238                 packet_write_fmt(1, "version 2\n");
239
240                 advertise_capabilities();
241                 /*
242                  * If only the list of capabilities was requested exit
243                  * immediately after advertising capabilities
244                  */
245                 if (options->advertise_capabilities)
246                         return;
247         }
248
249         /*
250          * If stateless-rpc was requested then exit after
251          * a single request/response exchange
252          */
253         if (options->stateless_rpc) {
254                 process_request();
255         } else {
256                 for (;;)
257                         if (process_request())
258                                 break;
259         }
260 }