2 #include "repository.h"
8 #include "protocol-caps.h"
10 #include "upload-pack.h"
12 static int advertise_sid
= -1;
14 static int always_advertise(struct repository
*r
,
20 static int agent_advertise(struct repository
*r
,
24 strbuf_addstr(value
, git_user_agent_sanitized());
28 static int object_format_advertise(struct repository
*r
,
32 strbuf_addstr(value
, r
->hash_algo
->name
);
36 static int session_id_advertise(struct repository
*r
, struct strbuf
*value
)
38 if (advertise_sid
== -1 &&
39 git_config_get_bool("transfer.advertisesid", &advertise_sid
))
44 strbuf_addstr(value
, trace2_session_id());
48 struct protocol_capability
{
50 * The name of the capability. The server uses this name when
51 * advertising this capability, and the client uses this name to
52 * specify this capability.
57 * Function queried to see if a capability should be advertised.
58 * Optionally a value can be specified by adding it to 'value'.
59 * If a value is added to 'value', the server will advertise this
60 * capability as "<name>=<value>" instead of "<name>".
62 int (*advertise
)(struct repository
*r
, struct strbuf
*value
);
65 * Function called when a client requests the capability as a command.
66 * Will be provided a struct packet_reader 'request' which it should
67 * use to read the command specific part of the request. Every command
68 * MUST read until a flush packet is seen before sending a response.
70 * This field should be NULL for capabilities which are not commands.
72 int (*command
)(struct repository
*r
, struct packet_reader
*request
);
75 static struct protocol_capability capabilities
[] = {
78 .advertise
= agent_advertise
,
82 .advertise
= ls_refs_advertise
,
87 .advertise
= upload_pack_advertise
,
88 .command
= upload_pack_v2
,
91 .name
= "server-option",
92 .advertise
= always_advertise
,
95 .name
= "object-format",
96 .advertise
= object_format_advertise
,
100 .advertise
= session_id_advertise
,
103 .name
= "object-info",
104 .advertise
= always_advertise
,
105 .command
= cap_object_info
,
109 void protocol_v2_advertise_capabilities(void)
111 struct strbuf capability
= STRBUF_INIT
;
112 struct strbuf value
= STRBUF_INIT
;
115 /* serve by default supports v2 */
116 packet_write_fmt(1, "version 2\n");
118 for (i
= 0; i
< ARRAY_SIZE(capabilities
); i
++) {
119 struct protocol_capability
*c
= &capabilities
[i
];
121 if (c
->advertise(the_repository
, &value
)) {
122 strbuf_addstr(&capability
, c
->name
);
125 strbuf_addch(&capability
, '=');
126 strbuf_addbuf(&capability
, &value
);
129 strbuf_addch(&capability
, '\n');
130 packet_write(1, capability
.buf
, capability
.len
);
133 strbuf_reset(&capability
);
134 strbuf_reset(&value
);
138 strbuf_release(&capability
);
139 strbuf_release(&value
);
142 static struct protocol_capability
*get_capability(const char *key
)
149 for (i
= 0; i
< ARRAY_SIZE(capabilities
); i
++) {
150 struct protocol_capability
*c
= &capabilities
[i
];
152 if (skip_prefix(key
, c
->name
, &out
) && (!*out
|| *out
== '='))
159 static int is_valid_capability(const char *key
)
161 const struct protocol_capability
*c
= get_capability(key
);
163 return c
&& c
->advertise(the_repository
, NULL
);
166 static int is_command(const char *key
, struct protocol_capability
**command
)
170 if (skip_prefix(key
, "command=", &out
)) {
171 struct protocol_capability
*cmd
= get_capability(out
);
174 die("command '%s' requested after already requesting command '%s'",
175 out
, (*command
)->name
);
176 if (!cmd
|| !cmd
->advertise(the_repository
, NULL
) || !cmd
->command
)
177 die("invalid command '%s'", out
);
186 static int has_capability(const struct strvec
*keys
, const char *capability
,
190 for (i
= 0; i
< keys
->nr
; i
++) {
192 if (skip_prefix(keys
->v
[i
], capability
, &out
) &&
193 (!*out
|| *out
== '=')) {
206 static void check_algorithm(struct repository
*r
, struct strvec
*keys
)
208 int client
= GIT_HASH_SHA1
, server
= hash_algo_by_ptr(r
->hash_algo
);
209 const char *algo_name
;
211 if (has_capability(keys
, "object-format", &algo_name
)) {
212 client
= hash_algo_by_name(algo_name
);
213 if (client
== GIT_HASH_UNKNOWN
)
214 die("unknown object format '%s'", algo_name
);
217 if (client
!= server
)
218 die("mismatched object format: server %s; client %s\n",
219 r
->hash_algo
->name
, hash_algos
[client
].name
);
223 PROCESS_REQUEST_KEYS
,
224 PROCESS_REQUEST_DONE
,
227 static int process_request(void)
229 enum request_state state
= PROCESS_REQUEST_KEYS
;
230 struct packet_reader reader
;
231 struct strvec keys
= STRVEC_INIT
;
232 struct protocol_capability
*command
= NULL
;
233 const char *client_sid
;
235 packet_reader_init(&reader
, 0, NULL
, 0,
236 PACKET_READ_CHOMP_NEWLINE
|
237 PACKET_READ_GENTLE_ON_EOF
|
238 PACKET_READ_DIE_ON_ERR_PACKET
);
241 * Check to see if the client closed their end before sending another
242 * request. If so we can terminate the connection.
244 if (packet_reader_peek(&reader
) == PACKET_READ_EOF
)
246 reader
.options
&= ~PACKET_READ_GENTLE_ON_EOF
;
248 while (state
!= PROCESS_REQUEST_DONE
) {
249 switch (packet_reader_peek(&reader
)) {
250 case PACKET_READ_EOF
:
251 BUG("Should have already died when seeing EOF");
252 case PACKET_READ_NORMAL
:
253 /* collect request; a sequence of keys and values */
254 if (is_command(reader
.line
, &command
) ||
255 is_valid_capability(reader
.line
))
256 strvec_push(&keys
, reader
.line
);
258 die("unknown capability '%s'", reader
.line
);
260 /* Consume the peeked line */
261 packet_reader_read(&reader
);
263 case PACKET_READ_FLUSH
:
265 * If no command and no keys were given then the client
266 * wanted to terminate the connection.
272 * The flush packet isn't consume here like it is in
273 * the other parts of this switch statement. This is
274 * so that the command can read the flush packet and
275 * see the end of the request in the same way it would
276 * if command specific arguments were provided after a
279 state
= PROCESS_REQUEST_DONE
;
281 case PACKET_READ_DELIM
:
282 /* Consume the peeked line */
283 packet_reader_read(&reader
);
285 state
= PROCESS_REQUEST_DONE
;
287 case PACKET_READ_RESPONSE_END
:
288 BUG("unexpected response end packet");
293 die("no command requested");
295 check_algorithm(the_repository
, &keys
);
297 if (has_capability(&keys
, "session-id", &client_sid
))
298 trace2_data_string("transfer", NULL
, "client-sid", client_sid
);
300 command
->command(the_repository
, &reader
);
306 void protocol_v2_serve_loop(int stateless_rpc
)
309 protocol_v2_advertise_capabilities();
312 * If stateless-rpc was requested then exit after
313 * a single request/response exchange
319 if (process_request())