netops: make the path optional in URLs When we're dealing with proxy addresses, we only want a hostname and port, and the user would not provide a path, so make it optional so we can use this same function to parse git as well as proxy URLs.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
diff --git a/src/netops.c b/src/netops.c
index c424198..90326ea 100644
--- a/src/netops.c
+++ b/src/netops.c
@@ -257,16 +257,18 @@ int gitno_extract_url_parts(
*port = git__strdup(default_port);
GITERR_CHECK_ALLOC(*port);
- if (u.field_set & (1 << UF_PATH)) {
- *path = git__substrdup(_path, u.field_data[UF_PATH].len);
- GITERR_CHECK_ALLOC(*path);
- } else {
- git__free(*port);
- *port = NULL;
- git__free(*host);
- *host = NULL;
- giterr_set(GITERR_NET, "invalid url, missing path");
- return GIT_EINVALIDSPEC;
+ if (path) {
+ if (u.field_set & (1 << UF_PATH)) {
+ *path = git__substrdup(_path, u.field_data[UF_PATH].len);
+ GITERR_CHECK_ALLOC(*path);
+ } else {
+ git__free(*port);
+ *port = NULL;
+ git__free(*host);
+ *host = NULL;
+ giterr_set(GITERR_NET, "invalid url, missing path");
+ return GIT_EINVALIDSPEC;
+ }
}
if (u.field_set & (1 << UF_USERINFO)) {
diff --git a/tests/network/urlparse.c b/tests/network/urlparse.c
index b3ac8ae..4a3096b 100644
--- a/tests/network/urlparse.c
+++ b/tests/network/urlparse.c
@@ -121,6 +121,15 @@ void test_network_urlparse__user_pass_port(void)
cl_assert_equal_s(pass, "pass");
}
+void test_network_urlparse__optional_path(void)
+{
+ cl_git_fail(gitno_extract_url_parts(&host, &port, &path, &user, &pass,
+ "https://user:pass@example.com:9191", "8080"));
+
+ cl_git_pass(gitno_extract_url_parts(&host, &port, NULL, &user, &pass,
+ "https://user:pass@example.com:9191", "8080"));
+}
+
void test_network_urlparse__connection_data_http(void)
{
cl_git_pass(gitno_connection_data_from_url(&conndata,