git

Форк
0
/
serve.c 
351 строка · 8.4 Кб
1
#define USE_THE_REPOSITORY_VARIABLE
2

3
#include "git-compat-util.h"
4
#include "repository.h"
5
#include "config.h"
6
#include "hash.h"
7
#include "pkt-line.h"
8
#include "version.h"
9
#include "ls-refs.h"
10
#include "protocol-caps.h"
11
#include "serve.h"
12
#include "upload-pack.h"
13
#include "bundle-uri.h"
14
#include "trace2.h"
15

16
static int advertise_sid = -1;
17
static int advertise_object_info = -1;
18
static int client_hash_algo = GIT_HASH_SHA1;
19

20
static int always_advertise(struct repository *r UNUSED,
21
			    struct strbuf *value UNUSED)
22
{
23
	return 1;
24
}
25

26
static int agent_advertise(struct repository *r UNUSED,
27
			   struct strbuf *value)
28
{
29
	if (value)
30
		strbuf_addstr(value, git_user_agent_sanitized());
31
	return 1;
32
}
33

34
static int object_format_advertise(struct repository *r,
35
				   struct strbuf *value)
36
{
37
	if (value)
38
		strbuf_addstr(value, r->hash_algo->name);
39
	return 1;
40
}
41

42
static void object_format_receive(struct repository *r UNUSED,
43
				  const char *algo_name)
44
{
45
	if (!algo_name)
46
		die("object-format capability requires an argument");
47

48
	client_hash_algo = hash_algo_by_name(algo_name);
49
	if (client_hash_algo == GIT_HASH_UNKNOWN)
50
		die("unknown object format '%s'", algo_name);
51
}
52

53
static int session_id_advertise(struct repository *r, struct strbuf *value)
54
{
55
	if (advertise_sid == -1 &&
56
	    repo_config_get_bool(r, "transfer.advertisesid", &advertise_sid))
57
		advertise_sid = 0;
58
	if (!advertise_sid)
59
		return 0;
60
	if (value)
61
		strbuf_addstr(value, trace2_session_id());
62
	return 1;
63
}
64

65
static void session_id_receive(struct repository *r UNUSED,
66
			       const char *client_sid)
67
{
68
	if (!client_sid)
69
		client_sid = "";
70
	trace2_data_string("transfer", NULL, "client-sid", client_sid);
71
}
72

73
static int object_info_advertise(struct repository *r, struct strbuf *value UNUSED)
74
{
75
	if (advertise_object_info == -1 &&
76
	    repo_config_get_bool(r, "transfer.advertiseobjectinfo",
77
				 &advertise_object_info)) {
78
		/* disabled by default */
79
		advertise_object_info = 0;
80
	}
81
	return advertise_object_info;
82
}
83

84
struct protocol_capability {
85
	/*
86
	 * The name of the capability.  The server uses this name when
87
	 * advertising this capability, and the client uses this name to
88
	 * specify this capability.
89
	 */
90
	const char *name;
91

92
	/*
93
	 * Function queried to see if a capability should be advertised.
94
	 * Optionally a value can be specified by adding it to 'value'.
95
	 * If a value is added to 'value', the server will advertise this
96
	 * capability as "<name>=<value>" instead of "<name>".
97
	 */
98
	int (*advertise)(struct repository *r, struct strbuf *value);
99

100
	/*
101
	 * Function called when a client requests the capability as a command.
102
	 * Will be provided a struct packet_reader 'request' which it should
103
	 * use to read the command specific part of the request.  Every command
104
	 * MUST read until a flush packet is seen before sending a response.
105
	 *
106
	 * This field should be NULL for capabilities which are not commands.
107
	 */
108
	int (*command)(struct repository *r, struct packet_reader *request);
109

110
	/*
111
	 * Function called when a client requests the capability as a
112
	 * non-command. This may be NULL if the capability does nothing.
113
	 *
114
	 * For a capability of the form "foo=bar", the value string points to
115
	 * the content after the "=" (i.e., "bar"). For simple capabilities
116
	 * (just "foo"), it is NULL.
117
	 */
118
	void (*receive)(struct repository *r, const char *value);
119
};
120

121
static struct protocol_capability capabilities[] = {
122
	{
123
		.name = "agent",
124
		.advertise = agent_advertise,
125
	},
126
	{
127
		.name = "ls-refs",
128
		.advertise = ls_refs_advertise,
129
		.command = ls_refs,
130
	},
131
	{
132
		.name = "fetch",
133
		.advertise = upload_pack_advertise,
134
		.command = upload_pack_v2,
135
	},
136
	{
137
		.name = "server-option",
138
		.advertise = always_advertise,
139
	},
140
	{
141
		.name = "object-format",
142
		.advertise = object_format_advertise,
143
		.receive = object_format_receive,
144
	},
145
	{
146
		.name = "session-id",
147
		.advertise = session_id_advertise,
148
		.receive = session_id_receive,
149
	},
150
	{
151
		.name = "object-info",
152
		.advertise = object_info_advertise,
153
		.command = cap_object_info,
154
	},
155
	{
156
		.name = "bundle-uri",
157
		.advertise = bundle_uri_advertise,
158
		.command = bundle_uri_command,
159
	},
160
};
161

162
void protocol_v2_advertise_capabilities(void)
163
{
164
	struct strbuf capability = STRBUF_INIT;
165
	struct strbuf value = STRBUF_INIT;
166
	int i;
167

168
	/* serve by default supports v2 */
169
	packet_write_fmt(1, "version 2\n");
170

171
	for (i = 0; i < ARRAY_SIZE(capabilities); i++) {
172
		struct protocol_capability *c = &capabilities[i];
173

174
		if (c->advertise(the_repository, &value)) {
175
			strbuf_addstr(&capability, c->name);
176

177
			if (value.len) {
178
				strbuf_addch(&capability, '=');
179
				strbuf_addbuf(&capability, &value);
180
			}
181

182
			strbuf_addch(&capability, '\n');
183
			packet_write(1, capability.buf, capability.len);
184
		}
185

186
		strbuf_reset(&capability);
187
		strbuf_reset(&value);
188
	}
189

190
	packet_flush(1);
191
	strbuf_release(&capability);
192
	strbuf_release(&value);
193
}
194

195
static struct protocol_capability *get_capability(const char *key, const char **value)
196
{
197
	int i;
198

199
	if (!key)
200
		return NULL;
201

202
	for (i = 0; i < ARRAY_SIZE(capabilities); i++) {
203
		struct protocol_capability *c = &capabilities[i];
204
		const char *out;
205
		if (!skip_prefix(key, c->name, &out))
206
			continue;
207
		if (!*out) {
208
			*value = NULL;
209
			return c;
210
		}
211
		if (*out++ == '=') {
212
			*value = out;
213
			return c;
214
		}
215
	}
216

217
	return NULL;
218
}
219

220
static int receive_client_capability(const char *key)
221
{
222
	const char *value;
223
	const struct protocol_capability *c = get_capability(key, &value);
224

225
	if (!c || c->command || !c->advertise(the_repository, NULL))
226
		return 0;
227

228
	if (c->receive)
229
		c->receive(the_repository, value);
230
	return 1;
231
}
232

233
static int parse_command(const char *key, struct protocol_capability **command)
234
{
235
	const char *out;
236

237
	if (skip_prefix(key, "command=", &out)) {
238
		const char *value;
239
		struct protocol_capability *cmd = get_capability(out, &value);
240

241
		if (*command)
242
			die("command '%s' requested after already requesting command '%s'",
243
			    out, (*command)->name);
244
		if (!cmd || !cmd->advertise(the_repository, NULL) || !cmd->command || value)
245
			die("invalid command '%s'", out);
246

247
		*command = cmd;
248
		return 1;
249
	}
250

251
	return 0;
252
}
253

254
enum request_state {
255
	PROCESS_REQUEST_KEYS,
256
	PROCESS_REQUEST_DONE,
257
};
258

259
static int process_request(void)
260
{
261
	enum request_state state = PROCESS_REQUEST_KEYS;
262
	struct packet_reader reader;
263
	int seen_capability_or_command = 0;
264
	struct protocol_capability *command = NULL;
265

266
	packet_reader_init(&reader, 0, NULL, 0,
267
			   PACKET_READ_CHOMP_NEWLINE |
268
			   PACKET_READ_GENTLE_ON_EOF |
269
			   PACKET_READ_DIE_ON_ERR_PACKET);
270

271
	/*
272
	 * Check to see if the client closed their end before sending another
273
	 * request.  If so we can terminate the connection.
274
	 */
275
	if (packet_reader_peek(&reader) == PACKET_READ_EOF)
276
		return 1;
277
	reader.options &= ~PACKET_READ_GENTLE_ON_EOF;
278

279
	while (state != PROCESS_REQUEST_DONE) {
280
		switch (packet_reader_peek(&reader)) {
281
		case PACKET_READ_EOF:
282
			BUG("Should have already died when seeing EOF");
283
		case PACKET_READ_NORMAL:
284
			if (parse_command(reader.line, &command) ||
285
			    receive_client_capability(reader.line))
286
				seen_capability_or_command = 1;
287
			else
288
				die("unknown capability '%s'", reader.line);
289

290
			/* Consume the peeked line */
291
			packet_reader_read(&reader);
292
			break;
293
		case PACKET_READ_FLUSH:
294
			/*
295
			 * If no command and no keys were given then the client
296
			 * wanted to terminate the connection.
297
			 */
298
			if (!seen_capability_or_command)
299
				return 1;
300

301
			/*
302
			 * The flush packet isn't consume here like it is in
303
			 * the other parts of this switch statement.  This is
304
			 * so that the command can read the flush packet and
305
			 * see the end of the request in the same way it would
306
			 * if command specific arguments were provided after a
307
			 * delim packet.
308
			 */
309
			state = PROCESS_REQUEST_DONE;
310
			break;
311
		case PACKET_READ_DELIM:
312
			/* Consume the peeked line */
313
			packet_reader_read(&reader);
314

315
			state = PROCESS_REQUEST_DONE;
316
			break;
317
		case PACKET_READ_RESPONSE_END:
318
			BUG("unexpected response end packet");
319
		}
320
	}
321

322
	if (!command)
323
		die("no command requested");
324

325
	if (client_hash_algo != hash_algo_by_ptr(the_repository->hash_algo))
326
		die("mismatched object format: server %s; client %s\n",
327
		    the_repository->hash_algo->name,
328
		    hash_algos[client_hash_algo].name);
329

330
	command->command(the_repository, &reader);
331

332
	return 0;
333
}
334

335
void protocol_v2_serve_loop(int stateless_rpc)
336
{
337
	if (!stateless_rpc)
338
		protocol_v2_advertise_capabilities();
339

340
	/*
341
	 * If stateless-rpc was requested then exit after
342
	 * a single request/response exchange
343
	 */
344
	if (stateless_rpc) {
345
		process_request();
346
	} else {
347
		for (;;)
348
			if (process_request())
349
				break;
350
	}
351
}
352

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.