1 #include "git-compat-util.h"
2 #include "repository.h"
7 #include "protocol-caps.h"
9 #include "upload-pack.h"
10 #include "bundle-uri.h"
13 static int advertise_sid
= -1;
14 static int client_hash_algo
= GIT_HASH_SHA1
;
16 static int always_advertise(struct repository
*r
,
22 static int agent_advertise(struct repository
*r
,
26 strbuf_addstr(value
, git_user_agent_sanitized());
30 static int object_format_advertise(struct repository
*r
,
34 strbuf_addstr(value
, r
->hash_algo
->name
);
38 static void object_format_receive(struct repository
*r
,
39 const char *algo_name
)
42 die("object-format capability requires an argument");
44 client_hash_algo
= hash_algo_by_name(algo_name
);
45 if (client_hash_algo
== GIT_HASH_UNKNOWN
)
46 die("unknown object format '%s'", algo_name
);
49 static int session_id_advertise(struct repository
*r
, struct strbuf
*value
)
51 if (advertise_sid
== -1 &&
52 git_config_get_bool("transfer.advertisesid", &advertise_sid
))
57 strbuf_addstr(value
, trace2_session_id());
61 static void session_id_receive(struct repository
*r
,
62 const char *client_sid
)
66 trace2_data_string("transfer", NULL
, "client-sid", client_sid
);
69 struct protocol_capability
{
71 * The name of the capability. The server uses this name when
72 * advertising this capability, and the client uses this name to
73 * specify this capability.
78 * Function queried to see if a capability should be advertised.
79 * Optionally a value can be specified by adding it to 'value'.
80 * If a value is added to 'value', the server will advertise this
81 * capability as "<name>=<value>" instead of "<name>".
83 int (*advertise
)(struct repository
*r
, struct strbuf
*value
);
86 * Function called when a client requests the capability as a command.
87 * Will be provided a struct packet_reader 'request' which it should
88 * use to read the command specific part of the request. Every command
89 * MUST read until a flush packet is seen before sending a response.
91 * This field should be NULL for capabilities which are not commands.
93 int (*command
)(struct repository
*r
, struct packet_reader
*request
);
96 * Function called when a client requests the capability as a
97 * non-command. This may be NULL if the capability does nothing.
99 * For a capability of the form "foo=bar", the value string points to
100 * the content after the "=" (i.e., "bar"). For simple capabilities
101 * (just "foo"), it is NULL.
103 void (*receive
)(struct repository
*r
, const char *value
);
106 static struct protocol_capability capabilities
[] = {
109 .advertise
= agent_advertise
,
113 .advertise
= ls_refs_advertise
,
118 .advertise
= upload_pack_advertise
,
119 .command
= upload_pack_v2
,
122 .name
= "server-option",
123 .advertise
= always_advertise
,
126 .name
= "object-format",
127 .advertise
= object_format_advertise
,
128 .receive
= object_format_receive
,
131 .name
= "session-id",
132 .advertise
= session_id_advertise
,
133 .receive
= session_id_receive
,
136 .name
= "object-info",
137 .advertise
= always_advertise
,
138 .command
= cap_object_info
,
141 .name
= "bundle-uri",
142 .advertise
= bundle_uri_advertise
,
143 .command
= bundle_uri_command
,
147 void protocol_v2_advertise_capabilities(void)
149 struct strbuf capability
= STRBUF_INIT
;
150 struct strbuf value
= STRBUF_INIT
;
153 /* serve by default supports v2 */
154 packet_write_fmt(1, "version 2\n");
156 for (i
= 0; i
< ARRAY_SIZE(capabilities
); i
++) {
157 struct protocol_capability
*c
= &capabilities
[i
];
159 if (c
->advertise(the_repository
, &value
)) {
160 strbuf_addstr(&capability
, c
->name
);
163 strbuf_addch(&capability
, '=');
164 strbuf_addbuf(&capability
, &value
);
167 strbuf_addch(&capability
, '\n');
168 packet_write(1, capability
.buf
, capability
.len
);
171 strbuf_reset(&capability
);
172 strbuf_reset(&value
);
176 strbuf_release(&capability
);
177 strbuf_release(&value
);
180 static struct protocol_capability
*get_capability(const char *key
, const char **value
)
187 for (i
= 0; i
< ARRAY_SIZE(capabilities
); i
++) {
188 struct protocol_capability
*c
= &capabilities
[i
];
190 if (!skip_prefix(key
, c
->name
, &out
))
205 static int receive_client_capability(const char *key
)
208 const struct protocol_capability
*c
= get_capability(key
, &value
);
210 if (!c
|| c
->command
|| !c
->advertise(the_repository
, NULL
))
214 c
->receive(the_repository
, value
);
218 static int parse_command(const char *key
, struct protocol_capability
**command
)
222 if (skip_prefix(key
, "command=", &out
)) {
224 struct protocol_capability
*cmd
= get_capability(out
, &value
);
227 die("command '%s' requested after already requesting command '%s'",
228 out
, (*command
)->name
);
229 if (!cmd
|| !cmd
->advertise(the_repository
, NULL
) || !cmd
->command
|| value
)
230 die("invalid command '%s'", out
);
240 PROCESS_REQUEST_KEYS
,
241 PROCESS_REQUEST_DONE
,
244 static int process_request(void)
246 enum request_state state
= PROCESS_REQUEST_KEYS
;
247 struct packet_reader reader
;
248 int seen_capability_or_command
= 0;
249 struct protocol_capability
*command
= NULL
;
251 packet_reader_init(&reader
, 0, NULL
, 0,
252 PACKET_READ_CHOMP_NEWLINE
|
253 PACKET_READ_GENTLE_ON_EOF
|
254 PACKET_READ_DIE_ON_ERR_PACKET
);
257 * Check to see if the client closed their end before sending another
258 * request. If so we can terminate the connection.
260 if (packet_reader_peek(&reader
) == PACKET_READ_EOF
)
262 reader
.options
&= ~PACKET_READ_GENTLE_ON_EOF
;
264 while (state
!= PROCESS_REQUEST_DONE
) {
265 switch (packet_reader_peek(&reader
)) {
266 case PACKET_READ_EOF
:
267 BUG("Should have already died when seeing EOF");
268 case PACKET_READ_NORMAL
:
269 if (parse_command(reader
.line
, &command
) ||
270 receive_client_capability(reader
.line
))
271 seen_capability_or_command
= 1;
273 die("unknown capability '%s'", reader
.line
);
275 /* Consume the peeked line */
276 packet_reader_read(&reader
);
278 case PACKET_READ_FLUSH
:
280 * If no command and no keys were given then the client
281 * wanted to terminate the connection.
283 if (!seen_capability_or_command
)
287 * The flush packet isn't consume here like it is in
288 * the other parts of this switch statement. This is
289 * so that the command can read the flush packet and
290 * see the end of the request in the same way it would
291 * if command specific arguments were provided after a
294 state
= PROCESS_REQUEST_DONE
;
296 case PACKET_READ_DELIM
:
297 /* Consume the peeked line */
298 packet_reader_read(&reader
);
300 state
= PROCESS_REQUEST_DONE
;
302 case PACKET_READ_RESPONSE_END
:
303 BUG("unexpected response end packet");
308 die("no command requested");
310 if (client_hash_algo
!= hash_algo_by_ptr(the_repository
->hash_algo
))
311 die("mismatched object format: server %s; client %s\n",
312 the_repository
->hash_algo
->name
,
313 hash_algos
[client_hash_algo
].name
);
315 command
->command(the_repository
, &reader
);
320 void protocol_v2_serve_loop(int stateless_rpc
)
323 protocol_v2_advertise_capabilities();
326 * If stateless-rpc was requested then exit after
327 * a single request/response exchange
333 if (process_request())