git.c 7.22 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 11
#include "git2.h"
#include "buffer.h"
12
#include "netops.h"
13
#include "git2/sys/transport.h"
14
#include "stream.h"
15
#include "streams/socket.h"
16 17 18 19 20

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

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

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

typedef struct {
	git_smart_subtransport parent;
	git_transport *owner;
34
	git_proto_stream *current_stream;
35
} git_subtransport;
36

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

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

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

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

62
	len = 4 + strlen(cmd) + 1 + strlen(repo) + 1 + strlen(host) + (delim - url) + 1;
63

64
	git_buf_grow(request, len);
65 66
	git_buf_printf(request, "%04x%s %s%c%s",
		(unsigned int)(len & 0x0FFFF), cmd, repo, 0, host);
67 68
	git_buf_put(request, url, delim - url);
	git_buf_putc(request, '\0');
69

70
	if (git_buf_oom(request))
71
		return -1;
72 73

	return 0;
74 75
}

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

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

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

87
	s->sent_command = 1;
88 89

cleanup:
90
	git_buf_dispose(&request);
91 92
	return error;
}
93

94
static int git_proto_stream_read(
95 96 97 98
	git_smart_subtransport_stream *stream,
	char *buffer,
	size_t buf_size,
	size_t *bytes_read)
99
{
100
	int error;
101
	git_proto_stream *s = (git_proto_stream *)stream;
102
	gitno_buffer buf;
103

104
	*bytes_read = 0;
105

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

109
	gitno_buffer_setup_fromstream(s->io, &buf, buffer, buf_size);
110

111 112
	if ((error = gitno_recv(&buf)) < 0)
		return error;
113

114
	*bytes_read = buf.offset;
115

116
	return 0;
117 118
}

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

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

130
	return git_stream__write_full(s->io, buffer, len, 0);
131 132
}

133
static void git_proto_stream_free(git_smart_subtransport_stream *stream)
134
{
135 136
	git_proto_stream *s;
	git_subtransport *t;
137

138 139
	if (!stream)
		return;
140

141 142 143
	s = (git_proto_stream *)stream;
	t = OWNING_SUBTRANSPORT(s);

144 145
	t->current_stream = NULL;

146
	git_stream_close(s->io);
147
	git_stream_free(s->io);
148
	git__free(s->url);
149
	git__free(s);
150
}
151

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

	if (!stream)
163
		return -1;
164

165
	s = git__calloc(1, sizeof(git_proto_stream));
166
	GIT_ERROR_CHECK_ALLOC(s);
167

168
	s->parent.subtransport = &t->parent;
169 170 171
	s->parent.read = git_proto_stream_read;
	s->parent.write = git_proto_stream_write;
	s->parent.free = git_proto_stream_free;
172

173 174 175 176 177
	s->cmd = cmd;
	s->url = git__strdup(url);

	if (!s->url) {
		git__free(s);
178
		return -1;
179
	}
180

181 182 183
	if ((git_socket_stream_new(&s->io, host, port)) < 0)
		return -1;

184
	GIT_ERROR_CHECK_VERSION(s->io, GIT_STREAM_VERSION, "git_stream");
185

186
	*stream = &s->parent;
187 188 189
	return 0;
}

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

201
	*stream = NULL;
202

203
	if (!git__prefixcmp(url, prefix_git))
204
		stream_url += strlen(prefix_git);
205

206
	if ((error = git_net_url_parse(&urldata, url)) < 0)
207
		return error;
208

209 210
	host = urldata.host;
	port = urldata.port ? urldata.port : GIT_DEFAULT_PORT;
211

212
	error = git_proto_stream_alloc(t, stream_url, cmd_uploadpack, host, port, stream);
213

214
	git_net_url_dispose(&urldata);
215

216 217 218 219
	if (error < 0) {
		git_proto_stream_free(*stream);
		return error;
	}
220

221 222 223 224 225 226 227 228 229
	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;
230 231
}

232
static int _git_uploadpack(
233 234 235
	git_subtransport *t,
	const char *url,
	git_smart_subtransport_stream **stream)
236
{
237
	GIT_UNUSED(url);
238

239 240 241
	if (t->current_stream) {
		*stream = &t->current_stream->parent;
		return 0;
242 243
	}

244
	git_error_set(GIT_ERROR_NET, "must call UPLOADPACK_LS before UPLOADPACK");
245 246 247
	return -1;
}

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

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

262
	if ((error = git_net_url_parse(&urldata, url)) < 0)
263 264
		return error;

265
	error = git_proto_stream_alloc(t, stream_url, cmd_receivepack, urldata.host, urldata.port, stream);
266

267
	git_net_url_dispose(&urldata);
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
}

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

296
	git_error_set(GIT_ERROR_NET, "must call RECEIVEPACK_LS before RECEIVEPACK");
297 298 299
	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
static int _git_close(git_smart_subtransport *subtransport)
{
	git_subtransport *t = (git_subtransport *) subtransport;

330
	GIT_ASSERT(!t->current_stream);
331 332 333 334 335 336 337

	GIT_UNUSED(t);

	return 0;
}

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

341 342
	git__free(t);
}
343

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

348 349
	GIT_UNUSED(param);

350 351
	if (!out)
		return -1;
352

353
	t = git__calloc(1, sizeof(git_subtransport));
354
	GIT_ERROR_CHECK_ALLOC(t);
355

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

361
	*out = (git_smart_subtransport *) t;
362
	return 0;
363
}