git.c 7.31 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 "git2.h"
#include "buffer.h"
10
#include "netops.h"
11
#include "git2/sys/transport.h"
12 13
#include "stream.h"
#include "socket_stream.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_buf *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 48 49 50
	if (delim == NULL) {
		giterr_set(GITERR_NET, "Malformed URL");
		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
	git_buf_grow(request, len);
63 64
	git_buf_printf(request, "%04x%s %s%c%s",
		(unsigned int)(len & 0x0FFFF), cmd, repo, 0, host);
65 66
	git_buf_put(request, url, delim - url);
	git_buf_putc(request, '\0');
67

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

	return 0;
72 73
}

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

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

83
	error = git_stream_write(s->io, request.ptr, request.size, 0);
84 85
	if (error >= 0)
		s->sent_command = 1;
86 87

cleanup:
88
	git_buf_free(&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
	int error;
99
	git_proto_stream *s = (git_proto_stream *)stream;
100
	gitno_buffer buf;
101

102
	*bytes_read = 0;
103

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

107
	gitno_buffer_setup_fromstream(s->io, &buf, buffer, buf_size);
108

109 110
	if ((error = gitno_recv(&buf)) < 0)
		return error;
111

112
	*bytes_read = buf.offset;
113

114
	return 0;
115 116
}

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

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

128
	return git_stream_write(s->io, buffer, len, 0);
129 130
}

131
static void git_proto_stream_free(git_smart_subtransport_stream *stream)
132
{
133
	git_proto_stream *s = (git_proto_stream *)stream;
134 135 136 137 138 139 140
	git_subtransport *t = OWNING_SUBTRANSPORT(s);
	int ret;

	GIT_UNUSED(ret);

	t->current_stream = NULL;

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

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

	if (!stream)
158
		return -1;
159

160
	s = git__calloc(1, sizeof(git_proto_stream));
161
	GITERR_CHECK_ALLOC(s);
162

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

168 169 170 171 172
	s->cmd = cmd;
	s->url = git__strdup(url);

	if (!s->url) {
		git__free(s);
173
		return -1;
174
	}
175

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

	GITERR_CHECK_VERSION(s->io, GIT_STREAM_VERSION, "git_stream");

181
	*stream = &s->parent;
182 183 184
	return 0;
}

185
static int _git_uploadpack_ls(
186 187 188
	git_subtransport *t,
	const char *url,
	git_smart_subtransport_stream **stream)
189
{
190 191
	char *host=NULL, *port=NULL, *path=NULL, *user=NULL, *pass=NULL;
	const char *stream_url = url;
192
	git_proto_stream *s;
193
	int error;
Carlos Martín Nieto committed
194

195
	*stream = NULL;
196

197
	if (!git__prefixcmp(url, prefix_git))
198
		stream_url += strlen(prefix_git);
199

200
	if ((error = gitno_extract_url_parts(&host, &port, &path, &user, &pass, url, GIT_DEFAULT_PORT)) < 0)
201
		return error;
202

203
	error = git_proto_stream_alloc(t, stream_url, cmd_uploadpack, host, port, stream);
204

205 206 207 208 209
	git__free(host);
	git__free(port);
	git__free(path);
	git__free(user);
	git__free(pass);
210

211

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

217 218 219 220 221 222 223 224 225
	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;
226 227
}

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

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

240 241 242 243
	giterr_set(GITERR_NET, "Must call UPLOADPACK_LS before UPLOADPACK");
	return -1;
}

244 245 246 247 248
static int _git_receivepack_ls(
	git_subtransport *t,
	const char *url,
	git_smart_subtransport_stream **stream)
{
249 250
	char *host=NULL, *port=NULL, *path=NULL, *user=NULL, *pass=NULL;
	const char *stream_url = url;
251
	git_proto_stream *s;
252
	int error;
253 254 255

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

258 259 260 261 262 263 264 265 266 267
	if ((error = gitno_extract_url_parts(&host, &port, &path, &user, &pass, url, GIT_DEFAULT_PORT)) < 0)
		return error;

	error = git_proto_stream_alloc(t, stream_url, cmd_receivepack, host, port, stream);

	git__free(host);
	git__free(port);
	git__free(path);
	git__free(user);
	git__free(pass);
268

269 270 271 272 273 274
	if (error < 0) {
		git_proto_stream_free(*stream);
		return error;
	}

	s = (git_proto_stream *) *stream;
275

276 277
	if ((error = git_stream_connect(s->io)) < 0)
		return error;
278

279
	t->current_stream = s;
280

281
	return 0;
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
}

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;
	}

	giterr_set(GITERR_NET, "Must call RECEIVEPACK_LS before RECEIVEPACK");
	return -1;
}

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

	switch (action) {
		case GIT_SERVICE_UPLOADPACK_LS:
310
			return _git_uploadpack_ls(t, url, stream);
311 312

		case GIT_SERVICE_UPLOADPACK:
313 314 315 316 317 318 319
			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);
320 321
	}

322 323
	*stream = NULL;
	return -1;
324 325
}

326 327 328 329 330 331 332 333 334 335 336 337
static int _git_close(git_smart_subtransport *subtransport)
{
	git_subtransport *t = (git_subtransport *) subtransport;

	assert(!t->current_stream);

	GIT_UNUSED(t);

	return 0;
}

static void _git_free(git_smart_subtransport *subtransport)
338
{
339
	git_subtransport *t = (git_subtransport *) subtransport;
340

341
	assert(!t->current_stream);
342

343 344
	git__free(t);
}
345

346
int git_smart_subtransport_git(git_smart_subtransport **out, git_transport *owner, void *param)
347 348
{
	git_subtransport *t;
349

350 351
	GIT_UNUSED(param);

352 353
	if (!out)
		return -1;
354

355
	t = git__calloc(1, sizeof(git_subtransport));
356
	GITERR_CHECK_ALLOC(t);
357

358 359
	t->owner = owner;
	t->parent.action = _git_action;
360
	t->parent.close = _git_close;
361
	t->parent.free = _git_free;
362

363
	*out = (git_smart_subtransport *) t;
364
	return 0;
365
}