git.c 6.94 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 14 15 16

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

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

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

typedef struct {
	git_smart_subtransport parent;
	git_transport *owner;
	git_stream *current_stream;
} git_subtransport;
32

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

	delim = strchr(url, '/');
45 46 47 48
	if (delim == NULL) {
		giterr_set(GITERR_NET, "Malformed URL");
		return -1;
	}
49 50 51 52 53 54 55

	repo = delim;

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

56
	len = 4 + strlen(cmd) + 1 + strlen(repo) + 1 + strlen(host) + (delim - url) + 1;
57

58
	git_buf_grow(request, len);
59 60
	git_buf_printf(request, "%04x%s %s%c%s",
		(unsigned int)(len & 0x0FFFF), cmd, repo, 0, host);
61 62
	git_buf_put(request, url, delim - url);
	git_buf_putc(request, '\0');
63

64
	if (git_buf_oom(request))
65
		return -1;
66 67

	return 0;
68 69
}

70
static int send_command(git_stream *s)
71
{
72 73
	int error;
	git_buf request = GIT_BUF_INIT;
74

75
	error = gen_proto(&request, s->cmd, s->url);
76
	if (error < 0)
77 78
		goto cleanup;

79 80 81 82 83 84
	/* It looks like negative values are errors here, and positive values
	 * are the number of bytes sent. */
	error = gitno_send(&s->socket, request.ptr, request.size, 0);

	if (error >= 0)
		s->sent_command = 1;
85 86

cleanup:
87
	git_buf_free(&request);
88 89
	return error;
}
90

91 92 93 94 95
static int git_stream_read(
	git_smart_subtransport_stream *stream,
	char *buffer,
	size_t buf_size,
	size_t *bytes_read)
96
{
97
	int error;
98 99
	git_stream *s = (git_stream *)stream;
	gitno_buffer buf;
100

101
	*bytes_read = 0;
102

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

106
	gitno_buffer_setup(&s->socket, &buf, buffer, buf_size);
107

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

111
	*bytes_read = buf.offset;
112

113
	return 0;
114 115
}

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

124 125
	if (!s->sent_command && (error = send_command(s)) < 0)
		return error;
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142

	return gitno_send(&s->socket, buffer, len, 0);
}

static void git_stream_free(git_smart_subtransport_stream *stream)
{
	git_stream *s = (git_stream *)stream;
	git_subtransport *t = OWNING_SUBTRANSPORT(s);
	int ret;

	GIT_UNUSED(ret);

	t->current_stream = NULL;

	if (s->socket.socket) {
		ret = gitno_close(&s->socket);
		assert(!ret);
143
	}
144

145
	git__free(s->url);
146
	git__free(s);
147
}
148

149 150 151 152 153 154 155 156 157
static int git_stream_alloc(
	git_subtransport *t,
	const char *url,
	const char *cmd,
	git_smart_subtransport_stream **stream)
{
	git_stream *s;

	if (!stream)
158
		return -1;
159

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

163 164 165 166
	s->parent.subtransport = &t->parent;
	s->parent.read = git_stream_read;
	s->parent.write = git_stream_write;
	s->parent.free = git_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
	*stream = &s->parent;
177 178 179
	return 0;
}

180
static int _git_uploadpack_ls(
181 182 183
	git_subtransport *t,
	const char *url,
	git_smart_subtransport_stream **stream)
184
{
185 186
	char *host=NULL, *port=NULL, *path=NULL, *user=NULL, *pass=NULL;
	const char *stream_url = url;
187
	git_stream *s;
188
	int error;
Carlos Martín Nieto committed
189

190
	*stream = NULL;
191

192
	if (!git__prefixcmp(url, prefix_git))
193
		stream_url += strlen(prefix_git);
194

195 196
	if ((error = git_stream_alloc(t, stream_url, cmd_uploadpack, stream)) < 0)
		return error;
197

198 199
	s = (git_stream *)*stream;

200 201 202
	if (!(error = gitno_extract_url_parts(
			&host, &port, &path, &user, &pass, url, GIT_DEFAULT_PORT))) {

203 204
		if (!(error = gitno_connect(&s->socket, host, port, 0)))
			t->current_stream = s;
205

206 207 208 209 210 211
		git__free(host);
		git__free(port);
		git__free(path);
		git__free(user);
		git__free(pass);
	} else if (*stream)
212 213
		git_stream_free(*stream);

214
	return error;
215 216
}

217
static int _git_uploadpack(
218 219 220
	git_subtransport *t,
	const char *url,
	git_smart_subtransport_stream **stream)
221
{
222
	GIT_UNUSED(url);
223

224 225 226
	if (t->current_stream) {
		*stream = &t->current_stream->parent;
		return 0;
227 228
	}

229 230 231 232
	giterr_set(GITERR_NET, "Must call UPLOADPACK_LS before UPLOADPACK");
	return -1;
}

233 234 235 236 237
static int _git_receivepack_ls(
	git_subtransport *t,
	const char *url,
	git_smart_subtransport_stream **stream)
{
238 239
	char *host=NULL, *port=NULL, *path=NULL, *user=NULL, *pass=NULL;
	const char *stream_url = url;
240
	git_stream *s;
241
	int error;
242 243 244

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

247
	if (git_stream_alloc(t, stream_url, cmd_receivepack, stream) < 0)
248 249 250 251
		return -1;

	s = (git_stream *)*stream;

252 253 254
	if (!(error = gitno_extract_url_parts(&host, &port, &path, &user, &pass, url, GIT_DEFAULT_PORT))) {
		if (!(error = gitno_connect(&s->socket, host, port, 0)))
			t->current_stream = s;
255

256 257 258 259 260 261
		git__free(host);
		git__free(port);
		git__free(path);
		git__free(user);
		git__free(pass);
	} else if (*stream)
262 263
		git_stream_free(*stream);

264
	return error;
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
}

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

283 284
static int _git_action(
	git_smart_subtransport_stream **stream,
285
	git_smart_subtransport *subtransport,
286 287 288
	const char *url,
	git_smart_service_t action)
{
289
	git_subtransport *t = (git_subtransport *) subtransport;
290 291 292

	switch (action) {
		case GIT_SERVICE_UPLOADPACK_LS:
293
			return _git_uploadpack_ls(t, url, stream);
294 295

		case GIT_SERVICE_UPLOADPACK:
296 297 298 299 300 301 302
			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);
303 304
	}

305 306
	*stream = NULL;
	return -1;
307 308
}

309 310 311 312 313 314 315 316 317 318 319 320
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)
321
{
322
	git_subtransport *t = (git_subtransport *) subtransport;
323

324
	assert(!t->current_stream);
325

326 327
	git__free(t);
}
328

329 330 331
int git_smart_subtransport_git(git_smart_subtransport **out, git_transport *owner)
{
	git_subtransport *t;
332

333 334
	if (!out)
		return -1;
335

336
	t = git__calloc(sizeof(git_subtransport), 1);
337
	GITERR_CHECK_ALLOC(t);
338

339 340
	t->owner = owner;
	t->parent.action = _git_action;
341
	t->parent.close = _git_close;
342
	t->parent.free = _git_free;
343

344
	*out = (git_smart_subtransport *) t;
345
	return 0;
346
}