]> git.proxmox.com Git - libgit2.git/blob - src/transport.c
transport: prevent the transport determination mechanism from segfaulting when being...
[libgit2.git] / src / transport.c
1 /*
2 * Copyright (C) 2009-2011 the libgit2 contributors
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 #include "common.h"
8 #include "git2/types.h"
9 #include "git2/remote.h"
10 #include "git2/net.h"
11 #include "transport.h"
12
13 static struct {
14 char *prefix;
15 git_transport_cb fn;
16 } transports[] = {
17 {"git://", git_transport_git},
18 {"http://", git_transport_http},
19 {"https://", git_transport_dummy},
20 {"file://", git_transport_local},
21 {"git+ssh://", git_transport_dummy},
22 {"ssh+git://", git_transport_dummy},
23 {NULL, 0}
24 };
25
26 #define GIT_TRANSPORT_COUNT (sizeof(transports)/sizeof(transports[0])) - 1
27
28 static git_transport_cb transport_find_fn(const char *url)
29 {
30 size_t i = 0;
31
32 /* TODO: Parse "example.com:project.git" as an SSH URL */
33
34 for (i = 0; i < GIT_TRANSPORT_COUNT; ++i) {
35 if (!strncasecmp(url, transports[i].prefix, strlen(transports[i].prefix)))
36 return transports[i].fn;
37 }
38
39 return NULL;
40 }
41
42 /**************
43 * Public API *
44 **************/
45
46 int git_transport_dummy(git_transport **GIT_UNUSED(transport))
47 {
48 GIT_UNUSED_ARG(transport);
49 return git__throw(GIT_ENOTIMPLEMENTED, "This protocol isn't implemented. Sorry");
50 }
51
52 int git_transport_new(git_transport **out, const char *url)
53 {
54 git_transport_cb fn;
55 git_transport *transport;
56 int error;
57
58 fn = transport_find_fn(url);
59
60 /*
61 * If we haven't found the transport, we assume we mean a
62 * local file.
63 */
64 if (fn == NULL)
65 fn = &git_transport_local;
66
67 error = fn(&transport);
68 if (error < GIT_SUCCESS)
69 return git__rethrow(error, "Failed to create new transport");
70
71 transport->url = git__strdup(url);
72 if (transport->url == NULL)
73 return GIT_ENOMEM;
74
75 *out = transport;
76
77 return GIT_SUCCESS;
78 }
79
80 /* from remote.h */
81 int git_remote_valid_url(const char *url)
82 {
83 return transport_find_fn(url) != NULL;
84 }
85