transport.c 4.08 KB
Newer Older
Vicent Marti committed
1
/*
Edward Thomson committed
2
 * Copyright (C) the libgit2 contributors. All rights reserved.
Vicent Marti committed
3 4 5 6
 *
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * a Linking Exception. For full terms see the included COPYING file.
 */
7 8
#include "common.h"
#include "git2/types.h"
9
#include "git2/remote.h"
10
#include "git2/net.h"
11
#include "git2/transport.h"
12
#include "path.h"
13

14
typedef struct transport_definition {
15
	char *prefix;
16
	unsigned priority;
17
	git_transport_cb fn;
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
	void *param;
} transport_definition;

static transport_definition local_transport_definition = { "file://", 1, git_transport_local, NULL };
static transport_definition dummy_transport_definition = { NULL, 1, git_transport_dummy, NULL };

static git_smart_subtransport_definition http_subtransport_definition = { git_smart_subtransport_http, 1 };
static git_smart_subtransport_definition git_subtransport_definition = { git_smart_subtransport_git, 0 };

static transport_definition transports[] = {
	{"git://", 1, git_transport_smart, &git_subtransport_definition},
	{"http://", 1, git_transport_smart, &http_subtransport_definition},
	{"https://", 1, git_transport_smart, &http_subtransport_definition},
	{"file://", 1, git_transport_local, NULL},
	{"git+ssh://", 1, git_transport_dummy, NULL},
	{"ssh+git://", 1, git_transport_dummy, NULL},
	{NULL, 0, 0}
35 36
};

37
#define GIT_TRANSPORT_COUNT (sizeof(transports)/sizeof(transports[0])) - 1
38

39
static int transport_find_fn(const char *url, git_transport_cb *callback, void **param)
40
{
41
	size_t i = 0;
42 43
	unsigned priority = 0;
	transport_definition *definition = NULL, *definition_iter;
44

45
	// First, check to see if it's an obvious URL, which a URL scheme
46
	for (i = 0; i < GIT_TRANSPORT_COUNT; ++i) {
47 48 49 50 51 52 53
		definition_iter = &transports[i];

		if (strncasecmp(url, definition_iter->prefix, strlen(definition_iter->prefix)))
			continue;

		if (definition_iter->priority > priority)
			definition = definition_iter;
54 55
	}

56 57 58 59 60 61 62 63 64
#ifdef GIT_WIN32
	/* On Windows, it might not be possible to discern between absolute local
	 * and ssh paths - first check if this is a valid local path that points
	 * to a directory and if so assume local path, else assume SSH */

	/* Check to see if the path points to a file on the local file system */
	if (!definition && git_path_exists(url) && git_path_isdir(url))
		definition = &local_transport_definition;

65 66
	/* It could be a SSH remote path. Check to see if there's a :
	 * SSH is an unsupported transport mechanism in this version of libgit2 */
67
	if (!definition && strrchr(url, ':'))
68
		definition = &dummy_transport_definition; 
69 70 71 72
#else
	/* For other systems, perform the SSH check first, to avoid going to the
	 * filesystem if it is not necessary */

73 74
	/* It could be a SSH remote path. Check to see if there's a :
	 * SSH is an unsupported transport mechanism in this version of libgit2 */
75
	if (!definition && strrchr(url, ':'))
76
		definition = &dummy_transport_definition;
77 78 79 80 81

	/* Check to see if the path points to a file on the local file system */
	if (!definition && git_path_exists(url) && git_path_isdir(url))
		definition = &local_transport_definition;
#endif
82

83 84
	if (!definition)
		return -1;
85

86 87 88 89
	*callback = definition->fn;
	*param = definition->param;
	
	return 0;
90 91 92 93 94 95
}

/**************
 * Public API *
 **************/

96
int git_transport_dummy(git_transport **transport, git_remote *owner, void *param)
97
{
98
	GIT_UNUSED(transport);
99
	GIT_UNUSED(owner);
100
	GIT_UNUSED(param);
101 102
	giterr_set(GITERR_NET, "This transport isn't implemented. Sorry");
	return -1;
103 104
}

105
int git_transport_new(git_transport **out, git_remote *owner, const char *url)
106 107 108
{
	git_transport_cb fn;
	git_transport *transport;
109
	void *param;
110 111
	int error;

112
	if (transport_find_fn(url, &fn, &param) < 0) {
113 114 115
		giterr_set(GITERR_NET, "Unsupported URL protocol");
		return -1;
	}
116

117
	error = fn(&transport, owner, param);
118
	if (error < 0)
119
		return error;
120

121 122
	*out = transport;

123
	return 0;
124
}
125 126 127 128

/* from remote.h */
int git_remote_valid_url(const char *url)
{
129 130 131 132
	git_transport_cb fn;
	void *param;

	return !transport_find_fn(url, &fn, &param);
133 134
}

135 136
int git_remote_supported_url(const char* url)
{
137 138 139 140 141
	git_transport_cb fn;
	void *param;

	if (transport_find_fn(url, &fn, &param) < 0)
		return 0;
142

143
	return fn != &git_transport_dummy;
144
}