git.c 7.15 KB
Newer Older
1
/*
Edward Thomson committed
2
 * Copyright (C) the libgit2 contributors. All rights reserved.
3
 *
Vicent Marti committed
4 5
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * a Linking Exception. For full terms see the included COPYING file.
6 7
 */

8 9
#include "common.h"

10
#include "net.h"
11
#include "stream.h"
12
#include "streams/socket.h"
13
#include "git2/sys/transport.h"
14 15 16 17 18

#define OWNING_SUBTRANSPORT(s) ((git_subtransport *)(s)->parent.subtransport)

static const char prefix_git[] = "git://";
static const char cmd_uploadpack[] = "git-upload-pack";
19
static const char cmd_receivepack[] = "git-receive-pack";
20 21

typedef struct {
22
	git_smart_subtransport_stream parent;
23
	git_stream *io;
24 25 26
	const char *cmd;
	char *url;
	unsigned sent_command : 1;
27
} git_proto_stream;
28 29 30 31

typedef struct {
	git_smart_subtransport parent;
	git_transport *owner;
32
	git_proto_stream *current_stream;
33
} git_subtransport;
34

35
/*
36
 * Create a git protocol request.
37 38 39
 *
 * For example: 0035git-upload-pack /libgit2/libgit2\0host=github.com\0
 */
40
static int gen_proto(git_str *request, const char *cmd, const char *url)
41
{
42
	char *delim, *repo;
43
	char host[] = "host=";
44
	size_t len;
45 46

	delim = strchr(url, '/');
47
	if (delim == NULL) {
48
		git_error_set(GIT_ERROR_NET, "malformed URL");
49 50
		return -1;
	}
51 52

	repo = delim;
53 54
	if (repo[1] == '~')
		++repo;
55 56 57 58 59

	delim = strchr(url, ':');
	if (delim == NULL)
		delim = strchr(url, '/');

60
	len = 4 + strlen(cmd) + 1 + strlen(repo) + 1 + strlen(host) + (delim - url) + 1;
61

62 63
	git_str_grow(request, len);
	git_str_printf(request, "%04x%s %s%c%s",
64
		(unsigned int)(len & 0x0FFFF), cmd, repo, 0, host);
65 66
	git_str_put(request, url, delim - url);
	git_str_putc(request, '\0');
67

68
	if (git_str_oom(request))
69
		return -1;
70 71

	return 0;
72 73
}

74
static int send_command(git_proto_stream *s)
75
{
76
	git_str request = GIT_STR_INIT;
77
	int error;
78

79
	if ((error = gen_proto(&request, s->cmd, s->url)) < 0)
80 81
		goto cleanup;

82 83
	if ((error = git_stream__write_full(s->io, request.ptr, request.size, 0)) < 0)
		goto cleanup;
84

85
	s->sent_command = 1;
86 87

cleanup:
88
	git_str_dispose(&request);
89 90
	return error;
}
91

92
static int git_proto_stream_read(
93 94 95 96
	git_smart_subtransport_stream *stream,
	char *buffer,
	size_t buf_size,
	size_t *bytes_read)
97
{
98
	git_proto_stream *s = (git_proto_stream *)stream;
99 100
	ssize_t ret;
	int error;
101

102
	*bytes_read = 0;
103

104 105
	if (!s->sent_command && (error = send_command(s)) < 0)
		return error;
106

107
	ret = git_stream_read(s->io, buffer, min(buf_size, INT_MAX));
108

109 110
	if (ret < 0)
		return -1;
111

112
	*bytes_read = (size_t)ret;
113
	return 0;
114 115
}

116
static int git_proto_stream_write(
117 118
	git_smart_subtransport_stream *stream,
	const char *buffer,
119
	size_t len)
120
{
121
	git_proto_stream *s = (git_proto_stream *)stream;
122
	int error;
123

124 125
	if (!s->sent_command && (error = send_command(s)) < 0)
		return error;
126

127
	return git_stream__write_full(s->io, buffer, len, 0);
128 129
}

130
static void git_proto_stream_free(git_smart_subtransport_stream *stream)
131
{
132 133
	git_proto_stream *s;
	git_subtransport *t;
134

135 136
	if (!stream)
		return;
137

138 139 140
	s = (git_proto_stream *)stream;
	t = OWNING_SUBTRANSPORT(s);

141 142
	t->current_stream = NULL;

143
	git_stream_close(s->io);
144
	git_stream_free(s->io);
145
	git__free(s->url);
146
	git__free(s);
147
}
148

149
static int git_proto_stream_alloc(
150 151 152
	git_subtransport *t,
	const char *url,
	const char *cmd,
153 154
	const char *host,
	const char *port,
155 156
	git_smart_subtransport_stream **stream)
{
157
	git_proto_stream *s;
158 159

	if (!stream)
160
		return -1;
161

162
	s = git__calloc(1, sizeof(git_proto_stream));
163
	GIT_ERROR_CHECK_ALLOC(s);
164

165
	s->parent.subtransport = &t->parent;
166 167 168
	s->parent.read = git_proto_stream_read;
	s->parent.write = git_proto_stream_write;
	s->parent.free = git_proto_stream_free;
169

170 171 172 173 174
	s->cmd = cmd;
	s->url = git__strdup(url);

	if (!s->url) {
		git__free(s);
175
		return -1;
176
	}
177

178 179 180
	if ((git_socket_stream_new(&s->io, host, port)) < 0)
		return -1;

181
	GIT_ERROR_CHECK_VERSION(s->io, GIT_STREAM_VERSION, "git_stream");
182

183
	*stream = &s->parent;
184 185 186
	return 0;
}

187
static int _git_uploadpack_ls(
188 189 190
	git_subtransport *t,
	const char *url,
	git_smart_subtransport_stream **stream)
191
{
192
	git_net_url urldata = GIT_NET_URL_INIT;
193
	const char *stream_url = url;
194
	const char *host, *port;
195
	git_proto_stream *s;
196
	int error;
Carlos Martín Nieto committed
197

198
	*stream = NULL;
199

200
	if (!git__prefixcmp(url, prefix_git))
201
		stream_url += strlen(prefix_git);
202

203
	if ((error = git_net_url_parse(&urldata, url)) < 0)
204
		return error;
205

206 207
	host = urldata.host;
	port = urldata.port ? urldata.port : GIT_DEFAULT_PORT;
208

209
	error = git_proto_stream_alloc(t, stream_url, cmd_uploadpack, host, port, stream);
210

211
	git_net_url_dispose(&urldata);
212

213 214 215 216
	if (error < 0) {
		git_proto_stream_free(*stream);
		return error;
	}
217

218 219 220 221 222 223 224 225 226
	s = (git_proto_stream *) *stream;
	if ((error = git_stream_connect(s->io)) < 0) {
		git_proto_stream_free(*stream);
		return error;
	}

	t->current_stream = s;

	return 0;
227 228
}

229
static int _git_uploadpack(
230 231 232
	git_subtransport *t,
	const char *url,
	git_smart_subtransport_stream **stream)
233
{
234
	GIT_UNUSED(url);
235

236 237 238
	if (t->current_stream) {
		*stream = &t->current_stream->parent;
		return 0;
239 240
	}

241
	git_error_set(GIT_ERROR_NET, "must call UPLOADPACK_LS before UPLOADPACK");
242 243 244
	return -1;
}

245 246 247 248 249
static int _git_receivepack_ls(
	git_subtransport *t,
	const char *url,
	git_smart_subtransport_stream **stream)
{
250
	git_net_url urldata = GIT_NET_URL_INIT;
251
	const char *stream_url = url;
252
	git_proto_stream *s;
253
	int error;
254 255 256

	*stream = NULL;
	if (!git__prefixcmp(url, prefix_git))
257
		stream_url += strlen(prefix_git);
258

259
	if ((error = git_net_url_parse(&urldata, url)) < 0)
260 261
		return error;

262
	error = git_proto_stream_alloc(t, stream_url, cmd_receivepack, urldata.host, urldata.port, stream);
263

264
	git_net_url_dispose(&urldata);
265

266 267 268 269 270 271
	if (error < 0) {
		git_proto_stream_free(*stream);
		return error;
	}

	s = (git_proto_stream *) *stream;
272

273 274
	if ((error = git_stream_connect(s->io)) < 0)
		return error;
275

276
	t->current_stream = s;
277

278
	return 0;
279 280 281 282 283 284 285 286 287 288 289 290 291 292
}

static int _git_receivepack(
	git_subtransport *t,
	const char *url,
	git_smart_subtransport_stream **stream)
{
	GIT_UNUSED(url);

	if (t->current_stream) {
		*stream = &t->current_stream->parent;
		return 0;
	}

293
	git_error_set(GIT_ERROR_NET, "must call RECEIVEPACK_LS before RECEIVEPACK");
294 295 296
	return -1;
}

297 298
static int _git_action(
	git_smart_subtransport_stream **stream,
299
	git_smart_subtransport *subtransport,
300 301 302
	const char *url,
	git_smart_service_t action)
{
303
	git_subtransport *t = (git_subtransport *) subtransport;
304 305 306

	switch (action) {
		case GIT_SERVICE_UPLOADPACK_LS:
307
			return _git_uploadpack_ls(t, url, stream);
308 309

		case GIT_SERVICE_UPLOADPACK:
310 311 312 313 314 315 316
			return _git_uploadpack(t, url, stream);

		case GIT_SERVICE_RECEIVEPACK_LS:
			return _git_receivepack_ls(t, url, stream);

		case GIT_SERVICE_RECEIVEPACK:
			return _git_receivepack(t, url, stream);
317 318
	}

319 320
	*stream = NULL;
	return -1;
321 322
}

323 324 325 326
static int _git_close(git_smart_subtransport *subtransport)
{
	git_subtransport *t = (git_subtransport *) subtransport;

327
	GIT_ASSERT(!t->current_stream);
328 329 330 331 332 333 334

	GIT_UNUSED(t);

	return 0;
}

static void _git_free(git_smart_subtransport *subtransport)
335
{
336
	git_subtransport *t = (git_subtransport *) subtransport;
337

338 339
	git__free(t);
}
340

341
int git_smart_subtransport_git(git_smart_subtransport **out, git_transport *owner, void *param)
342 343
{
	git_subtransport *t;
344

345 346
	GIT_UNUSED(param);

347 348
	if (!out)
		return -1;
349

350
	t = git__calloc(1, sizeof(git_subtransport));
351
	GIT_ERROR_CHECK_ALLOC(t);
352

353 354
	t->owner = owner;
	t->parent.action = _git_action;
355
	t->parent.close = _git_close;
356
	t->parent.free = _git_free;
357

358
	*out = (git_smart_subtransport *) t;
359
	return 0;
360
}