source src/transports/git.c
Line | Flow | Count | Block(s) | Source |
---|---|---|---|---|
1 | - | /* | ||
2 | - | * Copyright (C) the libgit2 contributors. All rights reserved. | ||
3 | - | * | ||
4 | - | * This file is part of libgit2, distributed under the GNU GPL v2 with | ||
5 | - | * a Linking Exception. For full terms see the included COPYING file. | ||
6 | - | */ | ||
7 | - | |||
8 | - | #include "common.h" | ||
9 | - | |||
10 | - | #include "git2.h" | ||
11 | - | #include "buffer.h" | ||
12 | - | #include "netops.h" | ||
13 | - | #include "git2/sys/transport.h" | ||
14 | - | #include "stream.h" | ||
15 | - | #include "streams/socket.h" | ||
16 | - | |||
17 | - | #define OWNING_SUBTRANSPORT(s) ((git_subtransport *)(s)->parent.subtransport) | ||
18 | - | |||
19 | - | static const char prefix_git[] = "git://"; | ||
20 | - | 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 | - | const char *cmd; | ||
27 | - | char *url; | ||
28 | - | unsigned sent_command : 1; | ||
29 | - | } git_proto_stream; | ||
30 | - | |||
31 | - | typedef struct { | ||
32 | - | git_smart_subtransport parent; | ||
33 | - | git_transport *owner; | ||
34 | - | git_proto_stream *current_stream; | ||
35 | - | } git_subtransport; | ||
36 | - | |||
37 | - | /* | ||
38 | - | * Create a git protocol request. | ||
39 | - | * | ||
40 | - | * For example: 0035git-upload-pack /libgit2/libgit2\0host=github.com\0 | ||
41 | - | */ | ||
42 | 26 | 2 | static int gen_proto(git_buf *request, const char *cmd, const char *url) | |
43 | - | { | ||
44 | - | char *delim, *repo; | ||
45 | 26 | 2 | char host[] = "host="; | |
46 | - | size_t len; | ||
47 | - | |||
48 | 26 | 2 | delim = strchr(url, '/'); | |
49 | 26 | 2 | if (delim == NULL) { | |
50 | ##### | 3 | git_error_set(GIT_ERROR_NET, "malformed URL"); | |
51 | ##### | 4 | return -1; | |
52 | - | } | ||
53 | - | |||
54 | 26 | 5 | repo = delim; | |
55 | 26 | 5 | if (repo[1] == '~') | |
56 | ##### | 6 | ++repo; | |
57 | - | |||
58 | 26 | 7 | delim = strchr(url, ':'); | |
59 | 26 | 7 | if (delim == NULL) | |
60 | 25 | 8 | delim = strchr(url, '/'); | |
61 | - | |||
62 | 26 | 9 | len = 4 + strlen(cmd) + 1 + strlen(repo) + 1 + strlen(host) + (delim - url) + 1; | |
63 | - | |||
64 | 26 | 9 | git_buf_grow(request, len); | |
65 | 26 | 10 | git_buf_printf(request, "%04x%s %s%c%s", | |
66 | - | (unsigned int)(len & 0x0FFFF), cmd, repo, 0, host); | ||
67 | 26 | 11 | git_buf_put(request, url, delim - url); | |
68 | 26 | 12 | git_buf_putc(request, '\0'); | |
69 | - | |||
70 | 26 | 13,14 | if (git_buf_oom(request)) | |
71 | ##### | 15 | return -1; | |
72 | - | |||
73 | 26 | 16 | return 0; | |
74 | - | } | ||
75 | - | |||
76 | 26 | 2 | static int send_command(git_proto_stream *s) | |
77 | - | { | ||
78 | 26 | 2 | git_buf request = GIT_BUF_INIT; | |
79 | - | int error; | ||
80 | - | |||
81 | 26 | 2,3 | if ((error = gen_proto(&request, s->cmd, s->url)) < 0) | |
82 | ##### | 4 | goto cleanup; | |
83 | - | |||
84 | 26 | 5,6 | if ((error = git_stream__write_full(s->io, request.ptr, request.size, 0)) < 0) | |
85 | ##### | 7 | goto cleanup; | |
86 | - | |||
87 | 26 | 8 | s->sent_command = 1; | |
88 | - | |||
89 | - | cleanup: | ||
90 | 26 | 9 | git_buf_dispose(&request); | |
91 | 26 | 10 | return error; | |
92 | - | } | ||
93 | - | |||
94 | 101 | 2 | static int git_proto_stream_read( | |
95 | - | git_smart_subtransport_stream *stream, | ||
96 | - | char *buffer, | ||
97 | - | size_t buf_size, | ||
98 | - | size_t *bytes_read) | ||
99 | - | { | ||
100 | - | int error; | ||
101 | 101 | 2 | git_proto_stream *s = (git_proto_stream *)stream; | |
102 | - | gitno_buffer buf; | ||
103 | - | |||
104 | 101 | 2 | *bytes_read = 0; | |
105 | - | |||
106 | 101 | 2-4 | if (!s->sent_command && (error = send_command(s)) < 0) | |
107 | ##### | 5 | return error; | |
108 | - | |||
109 | 101 | 6 | gitno_buffer_setup_fromstream(s->io, &buf, buffer, buf_size); | |
110 | - | |||
111 | 101 | 7,8 | if ((error = gitno_recv(&buf)) < 0) | |
112 | ##### | 9 | return error; | |
113 | - | |||
114 | 101 | 10 | *bytes_read = buf.offset; | |
115 | - | |||
116 | 101 | 10 | return 0; | |
117 | - | } | ||
118 | - | |||
119 | 36 | 2 | static int git_proto_stream_write( | |
120 | - | git_smart_subtransport_stream *stream, | ||
121 | - | const char *buffer, | ||
122 | - | size_t len) | ||
123 | - | { | ||
124 | 36 | 2 | git_proto_stream *s = (git_proto_stream *)stream; | |
125 | - | int error; | ||
126 | - | |||
127 | 36 | 2-4 | if (!s->sent_command && (error = send_command(s)) < 0) | |
128 | ##### | 5 | return error; | |
129 | - | |||
130 | 36 | 6 | return git_stream__write_full(s->io, buffer, len, 0); | |
131 | - | } | ||
132 | - | |||
133 | 26 | 2 | static void git_proto_stream_free(git_smart_subtransport_stream *stream) | |
134 | - | { | ||
135 | - | git_proto_stream *s; | ||
136 | - | git_subtransport *t; | ||
137 | - | |||
138 | 26 | 2 | if (!stream) | |
139 | 26 | 3,8 | return; | |
140 | - | |||
141 | 26 | 4 | s = (git_proto_stream *)stream; | |
142 | 26 | 4 | t = OWNING_SUBTRANSPORT(s); | |
143 | - | |||
144 | 26 | 4 | t->current_stream = NULL; | |
145 | - | |||
146 | 26 | 4 | git_stream_close(s->io); | |
147 | 26 | 5 | git_stream_free(s->io); | |
148 | 26 | 6 | git__free(s->url); | |
149 | 26 | 7 | git__free(s); | |
150 | - | } | ||
151 | - | |||
152 | 26 | 2 | static int git_proto_stream_alloc( | |
153 | - | git_subtransport *t, | ||
154 | - | const char *url, | ||
155 | - | const char *cmd, | ||
156 | - | const char *host, | ||
157 | - | const char *port, | ||
158 | - | git_smart_subtransport_stream **stream) | ||
159 | - | { | ||
160 | - | git_proto_stream *s; | ||
161 | - | |||
162 | 26 | 2 | if (!stream) | |
163 | ##### | 3 | return -1; | |
164 | - | |||
165 | 26 | 4 | s = git__calloc(1, sizeof(git_proto_stream)); | |
166 | 26 | 5,6 | GIT_ERROR_CHECK_ALLOC(s); | |
167 | - | |||
168 | 26 | 7 | s->parent.subtransport = &t->parent; | |
169 | 26 | 7 | s->parent.read = git_proto_stream_read; | |
170 | 26 | 7 | s->parent.write = git_proto_stream_write; | |
171 | 26 | 7 | s->parent.free = git_proto_stream_free; | |
172 | - | |||
173 | 26 | 7 | s->cmd = cmd; | |
174 | 26 | 7 | s->url = git__strdup(url); | |
175 | - | |||
176 | 26 | 8 | if (!s->url) { | |
177 | ##### | 9 | git__free(s); | |
178 | ##### | 10 | return -1; | |
179 | - | } | ||
180 | - | |||
181 | 26 | 11,12 | if ((git_socket_stream_new(&s->io, host, port)) < 0) | |
182 | ##### | 13 | return -1; | |
183 | - | |||
184 | 26 | 14-16 | GIT_ERROR_CHECK_VERSION(s->io, GIT_STREAM_VERSION, "git_stream"); | |
185 | - | |||
186 | 26 | 17 | *stream = &s->parent; | |
187 | 26 | 17 | return 0; | |
188 | - | } | ||
189 | - | |||
190 | 27 | 2 | static int _git_uploadpack_ls( | |
191 | - | git_subtransport *t, | ||
192 | - | const char *url, | ||
193 | - | git_smart_subtransport_stream **stream) | ||
194 | - | { | ||
195 | 27 | 2 | git_net_url urldata = GIT_NET_URL_INIT; | |
196 | 27 | 2 | const char *stream_url = url; | |
197 | - | const char *host, *port; | ||
198 | - | git_proto_stream *s; | ||
199 | - | int error; | ||
200 | - | |||
201 | 27 | 2 | *stream = NULL; | |
202 | - | |||
203 | 27 | 2,3 | if (!git__prefixcmp(url, prefix_git)) | |
204 | 27 | 4 | stream_url += strlen(prefix_git); | |
205 | - | |||
206 | 27 | 5,6 | if ((error = git_net_url_parse(&urldata, url)) < 0) | |
207 | 1 | 7 | return error; | |
208 | - | |||
209 | 26 | 8 | host = urldata.host; | |
210 | 26 | 8-10 | port = urldata.port ? urldata.port : GIT_DEFAULT_PORT; | |
211 | - | |||
212 | 26 | 11 | error = git_proto_stream_alloc(t, stream_url, cmd_uploadpack, host, port, stream); | |
213 | - | |||
214 | 26 | 12 | git_net_url_dispose(&urldata); | |
215 | - | |||
216 | 26 | 13 | if (error < 0) { | |
217 | ##### | 14 | git_proto_stream_free(*stream); | |
218 | ##### | 15 | return error; | |
219 | - | } | ||
220 | - | |||
221 | 26 | 16 | s = (git_proto_stream *) *stream; | |
222 | 26 | 16,17 | if ((error = git_stream_connect(s->io)) < 0) { | |
223 | ##### | 18 | git_proto_stream_free(*stream); | |
224 | ##### | 19 | return error; | |
225 | - | } | ||
226 | - | |||
227 | 26 | 20 | t->current_stream = s; | |
228 | - | |||
229 | 26 | 20 | return 0; | |
230 | - | } | ||
231 | - | |||
232 | 36 | 2 | static int _git_uploadpack( | |
233 | - | git_subtransport *t, | ||
234 | - | const char *url, | ||
235 | - | git_smart_subtransport_stream **stream) | ||
236 | - | { | ||
237 | - | GIT_UNUSED(url); | ||
238 | - | |||
239 | 36 | 2 | if (t->current_stream) { | |
240 | 36 | 3 | *stream = &t->current_stream->parent; | |
241 | 36 | 3 | return 0; | |
242 | - | } | ||
243 | - | |||
244 | ##### | 4 | git_error_set(GIT_ERROR_NET, "must call UPLOADPACK_LS before UPLOADPACK"); | |
245 | ##### | 5 | return -1; | |
246 | - | } | ||
247 | - | |||
248 | ##### | 2 | static int _git_receivepack_ls( | |
249 | - | git_subtransport *t, | ||
250 | - | const char *url, | ||
251 | - | git_smart_subtransport_stream **stream) | ||
252 | - | { | ||
253 | ##### | 2 | git_net_url urldata = GIT_NET_URL_INIT; | |
254 | ##### | 2 | const char *stream_url = url; | |
255 | - | git_proto_stream *s; | ||
256 | - | int error; | ||
257 | - | |||
258 | ##### | 2 | *stream = NULL; | |
259 | ##### | 2,3 | if (!git__prefixcmp(url, prefix_git)) | |
260 | ##### | 4 | stream_url += strlen(prefix_git); | |
261 | - | |||
262 | ##### | 5,6 | if ((error = git_net_url_parse(&urldata, url)) < 0) | |
263 | ##### | 7 | return error; | |
264 | - | |||
265 | ##### | 8 | error = git_proto_stream_alloc(t, stream_url, cmd_receivepack, urldata.host, urldata.port, stream); | |
266 | - | |||
267 | ##### | 9 | git_net_url_dispose(&urldata); | |
268 | - | |||
269 | ##### | 10 | if (error < 0) { | |
270 | ##### | 11 | git_proto_stream_free(*stream); | |
271 | ##### | 12 | return error; | |
272 | - | } | ||
273 | - | |||
274 | ##### | 13 | s = (git_proto_stream *) *stream; | |
275 | - | |||
276 | ##### | 13,14 | if ((error = git_stream_connect(s->io)) < 0) | |
277 | ##### | 15 | return error; | |
278 | - | |||
279 | ##### | 16 | t->current_stream = s; | |
280 | - | |||
281 | ##### | 16 | return 0; | |
282 | - | } | ||
283 | - | |||
284 | ##### | 2 | static int _git_receivepack( | |
285 | - | git_subtransport *t, | ||
286 | - | const char *url, | ||
287 | - | git_smart_subtransport_stream **stream) | ||
288 | - | { | ||
289 | - | GIT_UNUSED(url); | ||
290 | - | |||
291 | ##### | 2 | if (t->current_stream) { | |
292 | ##### | 3 | *stream = &t->current_stream->parent; | |
293 | ##### | 3 | return 0; | |
294 | - | } | ||
295 | - | |||
296 | ##### | 4 | git_error_set(GIT_ERROR_NET, "must call RECEIVEPACK_LS before RECEIVEPACK"); | |
297 | ##### | 5 | return -1; | |
298 | - | } | ||
299 | - | |||
300 | 63 | 2 | static int _git_action( | |
301 | - | git_smart_subtransport_stream **stream, | ||
302 | - | git_smart_subtransport *subtransport, | ||
303 | - | const char *url, | ||
304 | - | git_smart_service_t action) | ||
305 | - | { | ||
306 | 63 | 2 | git_subtransport *t = (git_subtransport *) subtransport; | |
307 | - | |||
308 | 63 | 2 | switch (action) { | |
309 | - | case GIT_SERVICE_UPLOADPACK_LS: | ||
310 | 27 | 3 | return _git_uploadpack_ls(t, url, stream); | |
311 | - | |||
312 | - | case GIT_SERVICE_UPLOADPACK: | ||
313 | 36 | 4 | return _git_uploadpack(t, url, stream); | |
314 | - | |||
315 | - | case GIT_SERVICE_RECEIVEPACK_LS: | ||
316 | ##### | 5 | return _git_receivepack_ls(t, url, stream); | |
317 | - | |||
318 | - | case GIT_SERVICE_RECEIVEPACK: | ||
319 | ##### | 6 | return _git_receivepack(t, url, stream); | |
320 | - | } | ||
321 | - | |||
322 | ##### | 7 | *stream = NULL; | |
323 | ##### | 7 | return -1; | |
324 | - | } | ||
325 | - | |||
326 | 78 | 2 | static int _git_close(git_smart_subtransport *subtransport) | |
327 | - | { | ||
328 | 78 | 2 | git_subtransport *t = (git_subtransport *) subtransport; | |
329 | - | |||
330 | 78 | 2,3 | assert(!t->current_stream); | |
331 | - | |||
332 | - | GIT_UNUSED(t); | ||
333 | - | |||
334 | 78 | 4 | return 0; | |
335 | - | } | ||
336 | - | |||
337 | 26 | 2 | static void _git_free(git_smart_subtransport *subtransport) | |
338 | - | { | ||
339 | 26 | 2 | git_subtransport *t = (git_subtransport *) subtransport; | |
340 | - | |||
341 | 26 | 2,3 | assert(!t->current_stream); | |
342 | - | |||
343 | 26 | 4 | git__free(t); | |
344 | 26 | 5 | } | |
345 | - | |||
346 | 26 | 2 | int git_smart_subtransport_git(git_smart_subtransport **out, git_transport *owner, void *param) | |
347 | - | { | ||
348 | - | git_subtransport *t; | ||
349 | - | |||
350 | - | GIT_UNUSED(param); | ||
351 | - | |||
352 | 26 | 2 | if (!out) | |
353 | ##### | 3 | return -1; | |
354 | - | |||
355 | 26 | 4 | t = git__calloc(1, sizeof(git_subtransport)); | |
356 | 26 | 5,6 | GIT_ERROR_CHECK_ALLOC(t); | |
357 | - | |||
358 | 26 | 7 | t->owner = owner; | |
359 | 26 | 7 | t->parent.action = _git_action; | |
360 | 26 | 7 | t->parent.close = _git_close; | |
361 | 26 | 7 | t->parent.free = _git_free; | |
362 | - | |||
363 | 26 | 7 | *out = (git_smart_subtransport *) t; | |
364 | 26 | 7 | return 0; | |
365 | - | } |