2 #include "repository.h"
8 #include "protocol-caps.h"
10 #include "upload-pack.h"
12 static int advertise_sid
= -1;
13 static int client_hash_algo
= GIT_HASH_SHA1
;
15 static int always_advertise(struct repository
*r
,
21 static int agent_advertise(struct repository
*r
,
25 strbuf_addstr(value
, git_user_agent_sanitized());
29 static int object_format_advertise(struct repository
*r
,
33 strbuf_addstr(value
, r
->hash_algo
->name
);
37 static void object_format_receive(struct repository
*r
,
38 const char *algo_name
)
41 die("object-format capability requires an argument");
43 client_hash_algo
= hash_algo_by_name(algo_name
);
44 if (client_hash_algo
== GIT_HASH_UNKNOWN
)
45 die("unknown object format '%s'", algo_name
);
48 static int session_id_advertise(struct repository
*r
, struct strbuf
*value
)
50 if (advertise_sid
== -1 &&
51 git_config_get_bool("transfer.advertisesid", &advertise_sid
))
56 strbuf_addstr(value
, trace2_session_id());
60 static void session_id_receive(struct repository
*r
,
61 const char *client_sid
)
65 trace2_data_string("transfer", NULL
, "client-sid", client_sid
);
68 struct protocol_capability
{
70 * The name of the capability. The server uses this name when
71 * advertising this capability, and the client uses this name to
72 * specify this capability.
77 * Function queried to see if a capability should be advertised.
78 * Optionally a value can be specified by adding it to 'value'.
79 * If a value is added to 'value', the server will advertise this
80 * capability as "<name>=<value>" instead of "<name>".
82 int (*advertise
)(struct repository
*r
, struct strbuf
*value
);
85 * Function called when a client requests the capability as a command.
86 * Will be provided a struct packet_reader 'request' which it should
87 * use to read the command specific part of the request. Every command
88 * MUST read until a flush packet is seen before sending a response.
90 * This field should be NULL for capabilities which are not commands.
92 int (*command
)(struct repository
*r
, struct packet_reader
*request
);
95 * Function called when a client requests the capability as a
96 * non-command. This may be NULL if the capability does nothing.
98 * For a capability of the form "foo=bar", the value string points to
99 * the content after the "=" (i.e., "bar"). For simple capabilities
100 * (just "foo"), it is NULL.
102 void (*receive
)(struct repository
*r
, const char *value
);
105 static struct protocol_capability capabilities
[] = {
108 .advertise
= agent_advertise
,
112 .advertise
= ls_refs_advertise
,
117 .advertise
= upload_pack_advertise
,
118 .command
= upload_pack_v2
,
121 .name
= "server-option",
122 .advertise
= always_advertise
,
125 .name
= "object-format",
126 .advertise
= object_format_advertise
,
127 .receive
= object_format_receive
,
130 .name
= "session-id",
131 .advertise
= session_id_advertise
,
132 .receive
= session_id_receive
,
135 .name
= "object-info",
136 .advertise
= always_advertise
,
137 .command
= cap_object_info
,
141 void protocol_v2_advertise_capabilities(void)
143 struct strbuf capability
= STRBUF_INIT
;
144 struct strbuf value
= STRBUF_INIT
;
147 /* serve by default supports v2 */
148 packet_write_fmt(1, "version 2\n");
150 for (i
= 0; i
< ARRAY_SIZE(capabilities
); i
++) {
151 struct protocol_capability
*c
= &capabilities
[i
];
153 if (c
->advertise(the_repository
, &value
)) {
154 strbuf_addstr(&capability
, c
->name
);
157 strbuf_addch(&capability
, '=');
158 strbuf_addbuf(&capability
, &value
);
161 strbuf_addch(&capability
, '\n');
162 packet_write(1, capability
.buf
, capability
.len
);
165 strbuf_reset(&capability
);
166 strbuf_reset(&value
);
170 strbuf_release(&capability
);
171 strbuf_release(&value
);
174 static struct protocol_capability
*get_capability(const char *key
, const char **value
)
181 for (i
= 0; i
< ARRAY_SIZE(capabilities
); i
++) {
182 struct protocol_capability
*c
= &capabilities
[i
];
184 if (!skip_prefix(key
, c
->name
, &out
))
199 static int receive_client_capability(const char *key
)
202 const struct protocol_capability
*c
= get_capability(key
, &value
);
204 if (!c
|| c
->command
|| !c
->advertise(the_repository
, NULL
))
208 c
->receive(the_repository
, value
);
212 static int parse_command(const char *key
, struct protocol_capability
**command
)
216 if (skip_prefix(key
, "command=", &out
)) {
218 struct protocol_capability
*cmd
= get_capability(out
, &value
);
221 die("command '%s' requested after already requesting command '%s'",
222 out
, (*command
)->name
);
223 if (!cmd
|| !cmd
->advertise(the_repository
, NULL
) || !cmd
->command
|| value
)
224 die("invalid command '%s'", out
);
234 PROCESS_REQUEST_KEYS
,
235 PROCESS_REQUEST_DONE
,
238 static int process_request(void)
240 enum request_state state
= PROCESS_REQUEST_KEYS
;
241 struct packet_reader reader
;
242 int seen_capability_or_command
= 0;
243 struct protocol_capability
*command
= NULL
;
245 packet_reader_init(&reader
, 0, NULL
, 0,
246 PACKET_READ_CHOMP_NEWLINE
|
247 PACKET_READ_GENTLE_ON_EOF
|
248 PACKET_READ_DIE_ON_ERR_PACKET
);
251 * Check to see if the client closed their end before sending another
252 * request. If so we can terminate the connection.
254 if (packet_reader_peek(&reader
) == PACKET_READ_EOF
)
256 reader
.options
&= ~PACKET_READ_GENTLE_ON_EOF
;
258 while (state
!= PROCESS_REQUEST_DONE
) {
259 switch (packet_reader_peek(&reader
)) {
260 case PACKET_READ_EOF
:
261 BUG("Should have already died when seeing EOF");
262 case PACKET_READ_NORMAL
:
263 if (parse_command(reader
.line
, &command
) ||
264 receive_client_capability(reader
.line
))
265 seen_capability_or_command
= 1;
267 die("unknown capability '%s'", reader
.line
);
269 /* Consume the peeked line */
270 packet_reader_read(&reader
);
272 case PACKET_READ_FLUSH
:
274 * If no command and no keys were given then the client
275 * wanted to terminate the connection.
277 if (!seen_capability_or_command
)
281 * The flush packet isn't consume here like it is in
282 * the other parts of this switch statement. This is
283 * so that the command can read the flush packet and
284 * see the end of the request in the same way it would
285 * if command specific arguments were provided after a
288 state
= PROCESS_REQUEST_DONE
;
290 case PACKET_READ_DELIM
:
291 /* Consume the peeked line */
292 packet_reader_read(&reader
);
294 state
= PROCESS_REQUEST_DONE
;
296 case PACKET_READ_RESPONSE_END
:
297 BUG("unexpected response end packet");
302 die("no command requested");
304 if (client_hash_algo
!= hash_algo_by_ptr(the_repository
->hash_algo
))
305 die("mismatched object format: server %s; client %s\n",
306 the_repository
->hash_algo
->name
,
307 hash_algos
[client_hash_algo
].name
);
309 command
->command(the_repository
, &reader
);
314 void protocol_v2_serve_loop(int stateless_rpc
)
317 protocol_v2_advertise_capabilities();
320 * If stateless-rpc was requested then exit after
321 * a single request/response exchange
327 if (process_request())