summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEdward Thomson <ethomson@edwardthomson.com>2020-03-31 22:38:34 +0100
committerEdward Thomson <ethomson@edwardthomson.com>2020-06-05 07:14:25 +0100
commit7999c662e8ed67250e03a3d47c821b13a64d1177 (patch)
tree4362891d4bf827fb72688285126889795ea61938
parent0a6ea41ccd68fab6f3d5bc2d45987d8eece7634f (diff)
downloadlibgit2-7999c662e8ed67250e03a3d47c821b13a64d1177.tar.gz
net: provide function to parse ssh paths
SSH paths come in a variety of formats, either URLs (ssh://user@host/path) or SCP style (user@host:path). Provide a mechanism to parse them.
-rw-r--r--src/util/net.c34
-rw-r--r--src/util/net.h6
2 files changed, 40 insertions, 0 deletions
diff --git a/src/util/net.c b/src/util/net.c
index 2b85ea2d5..230903b7b 100644
--- a/src/util/net.c
+++ b/src/util/net.c
@@ -408,3 +408,37 @@ void git_net_url_dispose(git_net_url *url)
git__free(url->username); url->username = NULL;
git__free(url->password); url->password = NULL;
}
+
+int git_net_url_parse_ssh(git_net_url *url, const char *given)
+{
+ const char *at, *colon;
+ size_t i;
+
+ static const char *ssh_prefixes[] = {
+ "ssh://", "ssh+git://", "git+ssh://"
+ };
+
+ for (i = 0; i < ARRAY_SIZE(ssh_prefixes); i++) {
+ if (!git__prefixcmp(given, ssh_prefixes[i]))
+ return git_net_url_parse(url, given);
+ }
+
+ if ((at = strchr(given, '@')) != NULL) {
+ url->username = git__substrdup(given, at - given);
+ GIT_ERROR_CHECK_ALLOC(url->username);
+
+ given = at + 1;
+ }
+
+ if ((colon = strchr(given, ':')) != NULL) {
+ url->host = git__substrdup(given, colon - given);
+ GIT_ERROR_CHECK_ALLOC(url->host);
+
+ given = colon + 1;
+ }
+
+ url->path = git__strdup(given);
+ GIT_ERROR_CHECK_ALLOC(url->path);
+
+ return 0;
+}
diff --git a/src/util/net.h b/src/util/net.h
index 5422a0a44..23ebb1e79 100644
--- a/src/util/net.h
+++ b/src/util/net.h
@@ -54,4 +54,10 @@ extern int git_net_url_fmt_path(git_buf *buf, git_net_url *url);
/** Disposes the contents of the structure. */
extern void git_net_url_dispose(git_net_url *url);
+/**
+ * Parses an SSH path string in user@host:path or ssh://host/path
+ * format and returns the results as a URL structure.
+ */
+extern int git_net_url_parse_ssh(git_net_url *url, const char *str);
+
#endif