2 #include "repository.h"
7 #include "protocol-caps.h"
9 #include "upload-pack.h"
11 static int advertise_sid
= -1;
12 static int client_hash_algo
= GIT_HASH_SHA1
;
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 void object_format_receive(struct repository
*r
,
37 const char *algo_name
)
40 die("object-format capability requires an argument");
42 client_hash_algo
= hash_algo_by_name(algo_name
);
43 if (client_hash_algo
== GIT_HASH_UNKNOWN
)
44 die("unknown object format '%s'", algo_name
);
47 static int session_id_advertise(struct repository
*r
, struct strbuf
*value
)
49 if (advertise_sid
== -1 &&
50 git_config_get_bool("transfer.advertisesid", &advertise_sid
))
55 strbuf_addstr(value
, trace2_session_id());
59 static void session_id_receive(struct repository
*r
,
60 const char *client_sid
)
64 trace2_data_string("transfer", NULL
, "client-sid", client_sid
);
67 struct protocol_capability
{
69 * The name of the capability. The server uses this name when
70 * advertising this capability, and the client uses this name to
71 * specify this capability.
76 * Function queried to see if a capability should be advertised.
77 * Optionally a value can be specified by adding it to 'value'.
78 * If a value is added to 'value', the server will advertise this
79 * capability as "<name>=<value>" instead of "<name>".
81 int (*advertise
)(struct repository
*r
, struct strbuf
*value
);
84 * Function called when a client requests the capability as a command.
85 * Will be provided a struct packet_reader 'request' which it should
86 * use to read the command specific part of the request. Every command
87 * MUST read until a flush packet is seen before sending a response.
89 * This field should be NULL for capabilities which are not commands.
91 int (*command
)(struct repository
*r
, struct packet_reader
*request
);
94 * Function called when a client requests the capability as a
95 * non-command. This may be NULL if the capability does nothing.
97 * For a capability of the form "foo=bar", the value string points to
98 * the content after the "=" (i.e., "bar"). For simple capabilities
99 * (just "foo"), it is NULL.
101 void (*receive
)(struct repository
*r
, const char *value
);
104 static struct protocol_capability capabilities
[] = {
107 .advertise
= agent_advertise
,
111 .advertise
= ls_refs_advertise
,
116 .advertise
= upload_pack_advertise
,
117 .command
= upload_pack_v2
,
120 .name
= "server-option",
121 .advertise
= always_advertise
,
124 .name
= "object-format",
125 .advertise
= object_format_advertise
,
126 .receive
= object_format_receive
,
129 .name
= "session-id",
130 .advertise
= session_id_advertise
,
131 .receive
= session_id_receive
,
134 .name
= "object-info",
135 .advertise
= always_advertise
,
136 .command
= cap_object_info
,
140 void protocol_v2_advertise_capabilities(void)
142 struct strbuf capability
= STRBUF_INIT
;
143 struct strbuf value
= STRBUF_INIT
;
146 /* serve by default supports v2 */
147 packet_write_fmt(1, "version 2\n");
149 for (i
= 0; i
< ARRAY_SIZE(capabilities
); i
++) {
150 struct protocol_capability
*c
= &capabilities
[i
];
152 if (c
->advertise(the_repository
, &value
)) {
153 strbuf_addstr(&capability
, c
->name
);
156 strbuf_addch(&capability
, '=');
157 strbuf_addbuf(&capability
, &value
);
160 strbuf_addch(&capability
, '\n');
161 packet_write(1, capability
.buf
, capability
.len
);
164 strbuf_reset(&capability
);
165 strbuf_reset(&value
);
169 strbuf_release(&capability
);
170 strbuf_release(&value
);
173 static struct protocol_capability
*get_capability(const char *key
, const char **value
)
180 for (i
= 0; i
< ARRAY_SIZE(capabilities
); i
++) {
181 struct protocol_capability
*c
= &capabilities
[i
];
183 if (!skip_prefix(key
, c
->name
, &out
))
198 static int receive_client_capability(const char *key
)
201 const struct protocol_capability
*c
= get_capability(key
, &value
);
203 if (!c
|| c
->command
|| !c
->advertise(the_repository
, NULL
))
207 c
->receive(the_repository
, value
);
211 static int parse_command(const char *key
, struct protocol_capability
**command
)
215 if (skip_prefix(key
, "command=", &out
)) {
217 struct protocol_capability
*cmd
= get_capability(out
, &value
);
220 die("command '%s' requested after already requesting command '%s'",
221 out
, (*command
)->name
);
222 if (!cmd
|| !cmd
->advertise(the_repository
, NULL
) || !cmd
->command
|| value
)
223 die("invalid command '%s'", out
);
233 PROCESS_REQUEST_KEYS
,
234 PROCESS_REQUEST_DONE
,
237 static int process_request(void)
239 enum request_state state
= PROCESS_REQUEST_KEYS
;
240 struct packet_reader reader
;
241 int seen_capability_or_command
= 0;
242 struct protocol_capability
*command
= NULL
;
244 packet_reader_init(&reader
, 0, NULL
, 0,
245 PACKET_READ_CHOMP_NEWLINE
|
246 PACKET_READ_GENTLE_ON_EOF
|
247 PACKET_READ_DIE_ON_ERR_PACKET
);
250 * Check to see if the client closed their end before sending another
251 * request. If so we can terminate the connection.
253 if (packet_reader_peek(&reader
) == PACKET_READ_EOF
)
255 reader
.options
&= ~PACKET_READ_GENTLE_ON_EOF
;
257 while (state
!= PROCESS_REQUEST_DONE
) {
258 switch (packet_reader_peek(&reader
)) {
259 case PACKET_READ_EOF
:
260 BUG("Should have already died when seeing EOF");
261 case PACKET_READ_NORMAL
:
262 if (parse_command(reader
.line
, &command
) ||
263 receive_client_capability(reader
.line
))
264 seen_capability_or_command
= 1;
266 die("unknown capability '%s'", reader
.line
);
268 /* Consume the peeked line */
269 packet_reader_read(&reader
);
271 case PACKET_READ_FLUSH
:
273 * If no command and no keys were given then the client
274 * wanted to terminate the connection.
276 if (!seen_capability_or_command
)
280 * The flush packet isn't consume here like it is in
281 * the other parts of this switch statement. This is
282 * so that the command can read the flush packet and
283 * see the end of the request in the same way it would
284 * if command specific arguments were provided after a
287 state
= PROCESS_REQUEST_DONE
;
289 case PACKET_READ_DELIM
:
290 /* Consume the peeked line */
291 packet_reader_read(&reader
);
293 state
= PROCESS_REQUEST_DONE
;
295 case PACKET_READ_RESPONSE_END
:
296 BUG("unexpected response end packet");
301 die("no command requested");
303 if (client_hash_algo
!= hash_algo_by_ptr(the_repository
->hash_algo
))
304 die("mismatched object format: server %s; client %s\n",
305 the_repository
->hash_algo
->name
,
306 hash_algos
[client_hash_algo
].name
);
308 command
->command(the_repository
, &reader
);
313 void protocol_v2_serve_loop(int stateless_rpc
)
316 protocol_v2_advertise_capabilities();
319 * If stateless-rpc was requested then exit after
320 * a single request/response exchange
326 if (process_request())