1 #include "git-compat-util.h"
2 #include "repository.h"
8 #include "protocol-caps.h"
10 #include "upload-pack.h"
11 #include "bundle-uri.h"
13 #include "promisor-remote.h"
15 static int advertise_sid
= -1;
16 static int advertise_object_info
= -1;
17 static int client_hash_algo
= GIT_HASH_SHA1_LEGACY
;
19 static int always_advertise(struct repository
*r UNUSED
,
20 struct strbuf
*value UNUSED
)
25 static int agent_advertise(struct repository
*r UNUSED
,
29 strbuf_addstr(value
, git_user_agent_sanitized());
33 static int promisor_remote_advertise(struct repository
*r
,
37 char *info
= promisor_remote_info(r
);
40 strbuf_addstr(value
, info
);
46 static void promisor_remote_receive(struct repository
*r
,
49 mark_promisor_remotes_as_accepted(r
, remotes
);
53 static int object_format_advertise(struct repository
*r
,
57 strbuf_addstr(value
, r
->hash_algo
->name
);
61 static void object_format_receive(struct repository
*r UNUSED
,
62 const char *algo_name
)
65 die("object-format capability requires an argument");
67 client_hash_algo
= hash_algo_by_name(algo_name
);
68 if (client_hash_algo
== GIT_HASH_UNKNOWN
)
69 die("unknown object format '%s'", algo_name
);
72 static int session_id_advertise(struct repository
*r
, struct strbuf
*value
)
74 if (advertise_sid
== -1 &&
75 repo_config_get_bool(r
, "transfer.advertisesid", &advertise_sid
))
80 strbuf_addstr(value
, trace2_session_id());
84 static void session_id_receive(struct repository
*r UNUSED
,
85 const char *client_sid
)
89 trace2_data_string("transfer", NULL
, "client-sid", client_sid
);
92 static int object_info_advertise(struct repository
*r
, struct strbuf
*value UNUSED
)
94 if (advertise_object_info
== -1 &&
95 repo_config_get_bool(r
, "transfer.advertiseobjectinfo",
96 &advertise_object_info
)) {
97 /* disabled by default */
98 advertise_object_info
= 0;
100 return advertise_object_info
;
103 struct protocol_capability
{
105 * The name of the capability. The server uses this name when
106 * advertising this capability, and the client uses this name to
107 * specify this capability.
112 * Function queried to see if a capability should be advertised.
113 * Optionally a value can be specified by adding it to 'value'.
114 * If a value is added to 'value', the server will advertise this
115 * capability as "<name>=<value>" instead of "<name>".
117 int (*advertise
)(struct repository
*r
, struct strbuf
*value
);
120 * Function called when a client requests the capability as a command.
121 * Will be provided a struct packet_reader 'request' which it should
122 * use to read the command specific part of the request. Every command
123 * MUST read until a flush packet is seen before sending a response.
125 * This field should be NULL for capabilities which are not commands.
127 int (*command
)(struct repository
*r
, struct packet_reader
*request
);
130 * Function called when a client requests the capability as a
131 * non-command. This may be NULL if the capability does nothing.
133 * For a capability of the form "foo=bar", the value string points to
134 * the content after the "=" (i.e., "bar"). For simple capabilities
135 * (just "foo"), it is NULL.
137 void (*receive
)(struct repository
*r
, const char *value
);
140 static struct protocol_capability capabilities
[] = {
143 .advertise
= agent_advertise
,
147 .advertise
= ls_refs_advertise
,
152 .advertise
= upload_pack_advertise
,
153 .command
= upload_pack_v2
,
156 .name
= "server-option",
157 .advertise
= always_advertise
,
160 .name
= "object-format",
161 .advertise
= object_format_advertise
,
162 .receive
= object_format_receive
,
165 .name
= "session-id",
166 .advertise
= session_id_advertise
,
167 .receive
= session_id_receive
,
170 .name
= "object-info",
171 .advertise
= object_info_advertise
,
172 .command
= cap_object_info
,
175 .name
= "bundle-uri",
176 .advertise
= bundle_uri_advertise
,
177 .command
= bundle_uri_command
,
180 .name
= "promisor-remote",
181 .advertise
= promisor_remote_advertise
,
182 .receive
= promisor_remote_receive
,
186 void protocol_v2_advertise_capabilities(struct repository
*r
)
188 struct strbuf capability
= STRBUF_INIT
;
189 struct strbuf value
= STRBUF_INIT
;
191 /* serve by default supports v2 */
192 packet_write_fmt(1, "version 2\n");
194 for (size_t i
= 0; i
< ARRAY_SIZE(capabilities
); i
++) {
195 struct protocol_capability
*c
= &capabilities
[i
];
197 if (c
->advertise(r
, &value
)) {
198 strbuf_addstr(&capability
, c
->name
);
201 strbuf_addch(&capability
, '=');
202 strbuf_addbuf(&capability
, &value
);
205 strbuf_addch(&capability
, '\n');
206 packet_write(1, capability
.buf
, capability
.len
);
209 strbuf_reset(&capability
);
210 strbuf_reset(&value
);
214 strbuf_release(&capability
);
215 strbuf_release(&value
);
218 static struct protocol_capability
*get_capability(const char *key
, const char **value
)
223 for (size_t i
= 0; i
< ARRAY_SIZE(capabilities
); i
++) {
224 struct protocol_capability
*c
= &capabilities
[i
];
226 if (!skip_prefix(key
, c
->name
, &out
))
241 static int receive_client_capability(struct repository
*r
, const char *key
)
244 const struct protocol_capability
*c
= get_capability(key
, &value
);
246 if (!c
|| c
->command
|| !c
->advertise(r
, NULL
))
250 c
->receive(r
, value
);
254 static int parse_command(struct repository
*r
, const char *key
, struct protocol_capability
**command
)
258 if (skip_prefix(key
, "command=", &out
)) {
260 struct protocol_capability
*cmd
= get_capability(out
, &value
);
263 die("command '%s' requested after already requesting command '%s'",
264 out
, (*command
)->name
);
265 if (!cmd
|| !cmd
->advertise(r
, NULL
) || !cmd
->command
|| value
)
266 die("invalid command '%s'", out
);
276 PROCESS_REQUEST_KEYS
,
277 PROCESS_REQUEST_DONE
,
280 static int process_request(struct repository
*r
)
282 enum request_state state
= PROCESS_REQUEST_KEYS
;
283 struct packet_reader reader
;
284 int seen_capability_or_command
= 0;
285 struct protocol_capability
*command
= NULL
;
287 packet_reader_init(&reader
, 0, NULL
, 0,
288 PACKET_READ_CHOMP_NEWLINE
|
289 PACKET_READ_GENTLE_ON_EOF
|
290 PACKET_READ_DIE_ON_ERR_PACKET
);
293 * Check to see if the client closed their end before sending another
294 * request. If so we can terminate the connection.
296 if (packet_reader_peek(&reader
) == PACKET_READ_EOF
)
298 reader
.options
&= ~PACKET_READ_GENTLE_ON_EOF
;
300 while (state
!= PROCESS_REQUEST_DONE
) {
301 switch (packet_reader_peek(&reader
)) {
302 case PACKET_READ_EOF
:
303 BUG("Should have already died when seeing EOF");
304 case PACKET_READ_NORMAL
:
305 if (parse_command(r
, reader
.line
, &command
) ||
306 receive_client_capability(r
, reader
.line
))
307 seen_capability_or_command
= 1;
309 die("unknown capability '%s'", reader
.line
);
311 /* Consume the peeked line */
312 packet_reader_read(&reader
);
314 case PACKET_READ_FLUSH
:
316 * If no command and no keys were given then the client
317 * wanted to terminate the connection.
319 if (!seen_capability_or_command
)
323 * The flush packet isn't consume here like it is in
324 * the other parts of this switch statement. This is
325 * so that the command can read the flush packet and
326 * see the end of the request in the same way it would
327 * if command specific arguments were provided after a
330 state
= PROCESS_REQUEST_DONE
;
332 case PACKET_READ_DELIM
:
333 /* Consume the peeked line */
334 packet_reader_read(&reader
);
336 state
= PROCESS_REQUEST_DONE
;
338 case PACKET_READ_RESPONSE_END
:
339 BUG("unexpected response end packet");
344 die("no command requested");
346 if (client_hash_algo
!= hash_algo_by_ptr(r
->hash_algo
))
347 die("mismatched object format: server %s; client %s",
349 hash_algos
[client_hash_algo
].name
);
351 command
->command(r
, &reader
);
356 void protocol_v2_serve_loop(struct repository
*r
, int stateless_rpc
)
359 protocol_v2_advertise_capabilities(r
);
362 * If stateless-rpc was requested then exit after
363 * a single request/response exchange
369 if (process_request(r
))