Commit 373a3c9a by Edward Thomson

url_parse: introduce our own url parsing

Provide our own url parser, so that we can handle Google Code's "fun"
URLs that have a userinfo with an `@` in it. 😢
parent ffef8e01
...@@ -93,121 +93,360 @@ int git_net_url_dup(git_net_url *out, git_net_url *in) ...@@ -93,121 +93,360 @@ int git_net_url_dup(git_net_url *out, git_net_url *in)
return 0; return 0;
} }
int git_net_url_parse(git_net_url *url, const char *given) static int url_invalid(const char *message)
{ {
struct http_parser_url u = {0}; git_error_set(GIT_ERROR_NET, "invalid url: %s", message);
bool has_scheme, has_host, has_port, has_path, has_query, has_userinfo; return GIT_EINVALIDSPEC;
git_str scheme = GIT_STR_INIT, }
host = GIT_STR_INIT,
port = GIT_STR_INIT,
path = GIT_STR_INIT,
username = GIT_STR_INIT,
password = GIT_STR_INIT,
query = GIT_STR_INIT;
int error = GIT_EINVALIDSPEC;
if (http_parser_parse_url(given, strlen(given), false, &u)) {
git_error_set(GIT_ERROR_NET, "malformed URL '%s'", given);
goto done;
}
has_scheme = !!(u.field_set & (1 << UF_SCHEMA)); static int url_parse_authority(
has_host = !!(u.field_set & (1 << UF_HOST)); const char **user_start, size_t *user_len,
has_port = !!(u.field_set & (1 << UF_PORT)); const char **password_start, size_t *password_len,
has_path = !!(u.field_set & (1 << UF_PATH)); const char **host_start, size_t *host_len,
has_query = !!(u.field_set & (1 << UF_QUERY)); const char **port_start, size_t *port_len,
has_userinfo = !!(u.field_set & (1 << UF_USERINFO)); const char *authority_start, size_t len)
{
const char *c, *hostport_end, *host_end = NULL,
*userpass_end, *user_end = NULL;
if (has_scheme) { enum {
const char *url_scheme = given + u.field_data[UF_SCHEMA].off; HOSTPORT, HOST, IPV6, HOST_END, USERPASS, USER
size_t url_scheme_len = u.field_data[UF_SCHEMA].len; } state = HOSTPORT;
git_str_put(&scheme, url_scheme, url_scheme_len);
git__strntolower(scheme.ptr, scheme.size);
} else {
git_error_set(GIT_ERROR_NET, "malformed URL '%s'", given);
goto done;
}
if (has_host) { if (len == 0)
const char *url_host = given + u.field_data[UF_HOST].off; return 0;
size_t url_host_len = u.field_data[UF_HOST].len;
git_str_decode_percent(&host, url_host, url_host_len);
}
if (has_port) { /*
const char *url_port = given + u.field_data[UF_PORT].off; * walk the authority backwards so that we can parse google code's
size_t url_port_len = u.field_data[UF_PORT].len; * ssh urls that are not rfc compliant and allow @ in the username
git_str_put(&port, url_port, url_port_len); */
} else { for (hostport_end = authority_start + len, c = hostport_end - 1;
const char *default_port = default_port_for_scheme(scheme.ptr); c >= authority_start && !user_end;
c--) {
switch (state) {
case HOSTPORT:
if (*c == ':') {
*port_start = c + 1;
*port_len = hostport_end - *port_start;
host_end = c;
state = HOST;
break;
}
if (default_port == NULL) { /*
git_error_set(GIT_ERROR_NET, "unknown scheme for URL '%s'", given); * if we've only seen digits then we don't know
goto done; * if we're parsing just a host or a host and port.
} * if we see a non-digit, then we're in a host,
* otherwise, fall through to possibly match the
* "@" (user/host separator).
*/
if (*c < '0' || *c > '9') {
host_end = hostport_end;
state = HOST;
}
git_str_puts(&port, default_port); /* fall through */
}
if (has_path) { case HOST:
const char *url_path = given + u.field_data[UF_PATH].off; if (*c == ']' && host_end == c + 1) {
size_t url_path_len = u.field_data[UF_PATH].len; host_end = c;
git_str_put(&path, url_path, url_path_len); state = IPV6;
} else { }
git_str_puts(&path, "/");
else if (*c == '@') {
*host_start = c + 1;
*host_len = host_end ? host_end - *host_start :
hostport_end - *host_start;
userpass_end = c;
state = USERPASS;
}
else if (*c == '[' || *c == ']' || *c == ':') {
return url_invalid("malformed hostname");
}
break;
case IPV6:
if (*c == '[') {
*host_start = c + 1;
*host_len = host_end - *host_start;
state = HOST_END;
}
else if ((*c < '0' || *c > '9') &&
(*c < 'a' || *c > 'f') &&
(*c < 'A' || *c > 'F') &&
(*c != ':')) {
return url_invalid("malformed hostname");
}
break;
case HOST_END:
if (*c == '@') {
userpass_end = c;
state = USERPASS;
break;
}
return url_invalid("malformed hostname");
case USERPASS:
if (*c == ':') {
*password_start = c + 1;
*password_len = userpass_end - *password_start;
user_end = c;
state = USER;
break;
}
break;
default:
GIT_ASSERT(!"unhandled state");
}
} }
if (has_query) { switch (state) {
const char *url_query = given + u.field_data[UF_QUERY].off; case HOSTPORT:
size_t url_query_len = u.field_data[UF_QUERY].len; *host_start = authority_start;
git_str_decode_percent(&query, url_query, url_query_len); *host_len = (hostport_end - *host_start);
break;
case HOST:
*host_start = authority_start;
*host_len = (host_end - *host_start);
break;
case IPV6:
return url_invalid("malformed hostname");
case HOST_END:
break;
case USERPASS:
*user_start = authority_start;
*user_len = (userpass_end - *user_start);
break;
case USER:
*user_start = authority_start;
*user_len = (user_end - *user_start);
break;
default:
GIT_ASSERT(!"unhandled state");
} }
if (has_userinfo) { return 0;
const char *url_userinfo = given + u.field_data[UF_USERINFO].off; }
size_t url_userinfo_len = u.field_data[UF_USERINFO].len;
const char *colon = memchr(url_userinfo, ':', url_userinfo_len); int git_net_url_parse(git_net_url *url, const char *given)
{
const char *c, *scheme_start, *authority_start, *user_start,
*password_start, *host_start, *port_start, *path_start,
*query_start, *fragment_start, *default_port;
git_str scheme = GIT_STR_INIT, user = GIT_STR_INIT,
password = GIT_STR_INIT, host = GIT_STR_INIT,
port = GIT_STR_INIT, path = GIT_STR_INIT,
query = GIT_STR_INIT, fragment = GIT_STR_INIT;
size_t scheme_len = 0, user_len = 0, password_len = 0, host_len = 0,
port_len = 0, path_len = 0, query_len = 0, fragment_len = 0;
bool hierarchical = false;
int error = 0;
enum {
SCHEME,
AUTHORITY_START, AUTHORITY,
PATH_START, PATH,
QUERY,
FRAGMENT
} state = SCHEME;
memset(url, 0, sizeof(git_net_url));
for (c = scheme_start = given; *c; c++) {
switch (state) {
case SCHEME:
if (*c == ':') {
scheme_len = (c - scheme_start);
if (*(c+1) == '/' && *(c+2) == '/') {
c += 2;
hierarchical = true;
state = AUTHORITY_START;
} else {
state = PATH_START;
}
} else if ((*c < 'A' || *c > 'Z') &&
(*c < 'a' || *c > 'z') &&
(*c < '0' || *c > '9') &&
(*c != '+' && *c != '-' && *c != '.')) {
/*
* an illegal scheme character means that we
* were just given a relative path
*/
path_start = given;
state = PATH;
break;
}
break;
case AUTHORITY_START:
authority_start = c;
state = AUTHORITY;
/* fall through */
case AUTHORITY:
if (*c != '/')
break;
/*
* authority is sufficiently complex that we parse
* it separately
*/
if ((error = url_parse_authority(
&user_start, &user_len,
&password_start,&password_len,
&host_start, &host_len,
&port_start, &port_len,
authority_start, (c - authority_start))) < 0)
goto done;
/* fall through */
case PATH_START:
path_start = c;
state = PATH;
/* fall through */
case PATH:
switch (*c) {
case '?':
path_len = (c - path_start);
query_start = c + 1;
state = QUERY;
break;
case '#':
path_len = (c - path_start);
fragment_start = c + 1;
state = FRAGMENT;
break;
}
break;
case QUERY:
if (*c == '#') {
query_len = (c - query_start);
fragment_start = c + 1;
state = FRAGMENT;
}
break;
if (colon) { case FRAGMENT:
const char *url_username = url_userinfo; break;
size_t url_username_len = colon - url_userinfo;
const char *url_password = colon + 1;
size_t url_password_len = url_userinfo_len - (url_username_len + 1);
git_str_decode_percent(&username, url_username, url_username_len); default:
git_str_decode_percent(&password, url_password, url_password_len); GIT_ASSERT(!"unhandled state");
} else {
git_str_decode_percent(&username, url_userinfo, url_userinfo_len);
} }
} }
if (git_str_oom(&scheme) || switch (state) {
git_str_oom(&host) || case SCHEME:
git_str_oom(&port) || /*
git_str_oom(&path) || * if we never saw a ':' then we were given a relative
git_str_oom(&query) || * path, not a bare scheme
git_str_oom(&username) || */
git_str_oom(&password)) path_start = given;
return -1; path_len = (c - scheme_start);
break;
case AUTHORITY_START:
break;
case AUTHORITY:
if ((error = url_parse_authority(
&user_start, &user_len,
&password_start,&password_len,
&host_start, &host_len,
&port_start, &port_len,
authority_start, (c - authority_start))) < 0)
goto done;
break;
case PATH_START:
break;
case PATH:
path_len = (c - path_start);
break;
case QUERY:
query_len = (c - query_start);
break;
case FRAGMENT:
fragment_len = (c - fragment_start);
break;
default:
GIT_ASSERT(!"unhandled state");
}
if (scheme_len) {
if ((error = git_str_put(&scheme, scheme_start, scheme_len)) < 0)
goto done;
git__strntolower(scheme.ptr, scheme.size);
}
if (user_len &&
(error = git_str_decode_percent(&user, user_start, user_len)) < 0)
goto done;
if (password_len &&
(error = git_str_decode_percent(&password, password_start, password_len)) < 0)
goto done;
if (host_len &&
(error = git_str_decode_percent(&host, host_start, host_len)) < 0)
goto done;
if (port_len)
error = git_str_put(&port, port_start, port_len);
else if (scheme_len && (default_port = default_port_for_scheme(scheme.ptr)) != NULL)
error = git_str_puts(&port, default_port);
if (error < 0)
goto done;
if (path_len)
error = git_str_put(&path, path_start, path_len);
else if (hierarchical)
error = git_str_puts(&path, "/");
if (error < 0)
goto done;
if (query_len &&
(error = git_str_decode_percent(&query, query_start, query_len)) < 0)
goto done;
if (fragment_len &&
(error = git_str_decode_percent(&fragment, fragment_start, fragment_len)) < 0)
goto done;
url->scheme = git_str_detach(&scheme); url->scheme = git_str_detach(&scheme);
url->host = git_str_detach(&host); url->host = git_str_detach(&host);
url->port = git_str_detach(&port); url->port = git_str_detach(&port);
url->path = git_str_detach(&path); url->path = git_str_detach(&path);
url->query = git_str_detach(&query); url->query = git_str_detach(&query);
url->username = git_str_detach(&username); url->fragment = git_str_detach(&fragment);
url->username = git_str_detach(&user);
url->password = git_str_detach(&password); url->password = git_str_detach(&password);
error = 0; error = 0;
done: done:
git_str_dispose(&scheme); git_str_dispose(&scheme);
git_str_dispose(&user);
git_str_dispose(&password);
git_str_dispose(&host); git_str_dispose(&host);
git_str_dispose(&port); git_str_dispose(&port);
git_str_dispose(&path); git_str_dispose(&path);
git_str_dispose(&query); git_str_dispose(&query);
git_str_dispose(&username); git_str_dispose(&fragment);
git_str_dispose(&password);
return error; return error;
} }
...@@ -374,7 +613,7 @@ int git_net_url_parse_scp(git_net_url *url, const char *given) ...@@ -374,7 +613,7 @@ int git_net_url_parse_scp(git_net_url *url, const char *given)
break; break;
default: default:
GIT_ASSERT("unhandled state"); GIT_ASSERT(!"unhandled state");
} }
} }
...@@ -588,7 +827,7 @@ bool git_net_url_is_default_port(git_net_url *url) ...@@ -588,7 +827,7 @@ bool git_net_url_is_default_port(git_net_url *url)
{ {
const char *default_port; const char *default_port;
if ((default_port = default_port_for_scheme(url->scheme)) != NULL) if (url->scheme && (default_port = default_port_for_scheme(url->scheme)) != NULL)
return (strcmp(url->port, default_port) == 0); return (strcmp(url->port, default_port) == 0);
else else
return false; return false;
...@@ -744,6 +983,7 @@ void git_net_url_dispose(git_net_url *url) ...@@ -744,6 +983,7 @@ void git_net_url_dispose(git_net_url *url)
git__free(url->port); url->port = NULL; git__free(url->port); url->port = NULL;
git__free(url->path); url->path = NULL; git__free(url->path); url->path = NULL;
git__free(url->query); url->query = NULL; git__free(url->query); url->query = NULL;
git__free(url->fragment); url->fragment = NULL;
git__free(url->username); url->username = NULL; git__free(url->username); url->username = NULL;
git__free(url->password); url->password = NULL; git__free(url->password); url->password = NULL;
} }
...@@ -15,6 +15,7 @@ typedef struct git_net_url { ...@@ -15,6 +15,7 @@ typedef struct git_net_url {
char *port; char *port;
char *path; char *path;
char *query; char *query;
char *fragment;
char *username; char *username;
char *password; char *password;
} git_net_url; } git_net_url;
......
...@@ -24,6 +24,8 @@ void test_url_parse__hostname_trivial(void) ...@@ -24,6 +24,8 @@ void test_url_parse__hostname_trivial(void)
cl_assert_equal_s(conndata.path, "/resource"); cl_assert_equal_s(conndata.path, "/resource");
cl_assert_equal_p(conndata.username, NULL); cl_assert_equal_p(conndata.username, NULL);
cl_assert_equal_p(conndata.password, NULL); cl_assert_equal_p(conndata.password, NULL);
cl_assert_equal_p(conndata.query, NULL);
cl_assert_equal_p(conndata.fragment, NULL);
cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1);
} }
...@@ -36,6 +38,8 @@ void test_url_parse__hostname_root(void) ...@@ -36,6 +38,8 @@ void test_url_parse__hostname_root(void)
cl_assert_equal_s(conndata.path, "/"); cl_assert_equal_s(conndata.path, "/");
cl_assert_equal_p(conndata.username, NULL); cl_assert_equal_p(conndata.username, NULL);
cl_assert_equal_p(conndata.password, NULL); cl_assert_equal_p(conndata.password, NULL);
cl_assert_equal_p(conndata.query, NULL);
cl_assert_equal_p(conndata.fragment, NULL);
cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1);
} }
...@@ -48,6 +52,22 @@ void test_url_parse__hostname_implied_root(void) ...@@ -48,6 +52,22 @@ void test_url_parse__hostname_implied_root(void)
cl_assert_equal_s(conndata.path, "/"); cl_assert_equal_s(conndata.path, "/");
cl_assert_equal_p(conndata.username, NULL); cl_assert_equal_p(conndata.username, NULL);
cl_assert_equal_p(conndata.password, NULL); cl_assert_equal_p(conndata.password, NULL);
cl_assert_equal_p(conndata.query, NULL);
cl_assert_equal_p(conndata.fragment, NULL);
cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1);
}
void test_url_parse__hostname_numeric(void)
{
cl_git_pass(git_net_url_parse(&conndata, "http://8888888/"));
cl_assert_equal_s(conndata.scheme, "http");
cl_assert_equal_s(conndata.host, "8888888");
cl_assert_equal_s(conndata.port, "80");
cl_assert_equal_s(conndata.path, "/");
cl_assert_equal_p(conndata.username, NULL);
cl_assert_equal_p(conndata.password, NULL);
cl_assert_equal_p(conndata.query, NULL);
cl_assert_equal_p(conndata.fragment, NULL);
cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1);
} }
...@@ -60,6 +80,8 @@ void test_url_parse__hostname_implied_root_custom_port(void) ...@@ -60,6 +80,8 @@ void test_url_parse__hostname_implied_root_custom_port(void)
cl_assert_equal_s(conndata.path, "/"); cl_assert_equal_s(conndata.path, "/");
cl_assert_equal_p(conndata.username, NULL); cl_assert_equal_p(conndata.username, NULL);
cl_assert_equal_p(conndata.password, NULL); cl_assert_equal_p(conndata.password, NULL);
cl_assert_equal_p(conndata.query, NULL);
cl_assert_equal_p(conndata.fragment, NULL);
cl_assert_equal_i(git_net_url_is_default_port(&conndata), 0); cl_assert_equal_i(git_net_url_is_default_port(&conndata), 0);
} }
...@@ -72,6 +94,8 @@ void test_url_parse__hostname_implied_root_empty_port(void) ...@@ -72,6 +94,8 @@ void test_url_parse__hostname_implied_root_empty_port(void)
cl_assert_equal_s(conndata.path, "/"); cl_assert_equal_s(conndata.path, "/");
cl_assert_equal_p(conndata.username, NULL); cl_assert_equal_p(conndata.username, NULL);
cl_assert_equal_p(conndata.password, NULL); cl_assert_equal_p(conndata.password, NULL);
cl_assert_equal_p(conndata.query, NULL);
cl_assert_equal_p(conndata.fragment, NULL);
cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1);
} }
...@@ -85,6 +109,8 @@ void test_url_parse__hostname_encoded_password(void) ...@@ -85,6 +109,8 @@ void test_url_parse__hostname_encoded_password(void)
cl_assert_equal_s(conndata.path, "/"); cl_assert_equal_s(conndata.path, "/");
cl_assert_equal_s(conndata.username, "user"); cl_assert_equal_s(conndata.username, "user");
cl_assert_equal_s(conndata.password, "pass/is@bad"); cl_assert_equal_s(conndata.password, "pass/is@bad");
cl_assert_equal_p(conndata.query, NULL);
cl_assert_equal_p(conndata.fragment, NULL);
cl_assert_equal_i(git_net_url_is_default_port(&conndata), 0); cl_assert_equal_i(git_net_url_is_default_port(&conndata), 0);
} }
...@@ -98,6 +124,8 @@ void test_url_parse__hostname_user(void) ...@@ -98,6 +124,8 @@ void test_url_parse__hostname_user(void)
cl_assert_equal_s(conndata.path, "/resource"); cl_assert_equal_s(conndata.path, "/resource");
cl_assert_equal_s(conndata.username, "user"); cl_assert_equal_s(conndata.username, "user");
cl_assert_equal_p(conndata.password, NULL); cl_assert_equal_p(conndata.password, NULL);
cl_assert_equal_p(conndata.query, NULL);
cl_assert_equal_p(conndata.fragment, NULL);
cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1);
} }
...@@ -112,6 +140,8 @@ void test_url_parse__hostname_user_pass(void) ...@@ -112,6 +140,8 @@ void test_url_parse__hostname_user_pass(void)
cl_assert_equal_s(conndata.path, "/resource"); cl_assert_equal_s(conndata.path, "/resource");
cl_assert_equal_s(conndata.username, "user"); cl_assert_equal_s(conndata.username, "user");
cl_assert_equal_s(conndata.password, "pass"); cl_assert_equal_s(conndata.password, "pass");
cl_assert_equal_p(conndata.query, NULL);
cl_assert_equal_p(conndata.fragment, NULL);
cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1);
} }
...@@ -126,6 +156,8 @@ void test_url_parse__hostname_port(void) ...@@ -126,6 +156,8 @@ void test_url_parse__hostname_port(void)
cl_assert_equal_s(conndata.path, "/resource"); cl_assert_equal_s(conndata.path, "/resource");
cl_assert_equal_p(conndata.username, NULL); cl_assert_equal_p(conndata.username, NULL);
cl_assert_equal_p(conndata.password, NULL); cl_assert_equal_p(conndata.password, NULL);
cl_assert_equal_p(conndata.query, NULL);
cl_assert_equal_p(conndata.fragment, NULL);
cl_assert_equal_i(git_net_url_is_default_port(&conndata), 0); cl_assert_equal_i(git_net_url_is_default_port(&conndata), 0);
} }
...@@ -138,6 +170,8 @@ void test_url_parse__hostname_empty_port(void) ...@@ -138,6 +170,8 @@ void test_url_parse__hostname_empty_port(void)
cl_assert_equal_s(conndata.path, "/resource"); cl_assert_equal_s(conndata.path, "/resource");
cl_assert_equal_p(conndata.username, NULL); cl_assert_equal_p(conndata.username, NULL);
cl_assert_equal_p(conndata.password, NULL); cl_assert_equal_p(conndata.password, NULL);
cl_assert_equal_p(conndata.query, NULL);
cl_assert_equal_p(conndata.fragment, NULL);
cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1);
} }
...@@ -152,6 +186,8 @@ void test_url_parse__hostname_user_port(void) ...@@ -152,6 +186,8 @@ void test_url_parse__hostname_user_port(void)
cl_assert_equal_s(conndata.path, "/resource"); cl_assert_equal_s(conndata.path, "/resource");
cl_assert_equal_s(conndata.username, "user"); cl_assert_equal_s(conndata.username, "user");
cl_assert_equal_p(conndata.password, NULL); cl_assert_equal_p(conndata.password, NULL);
cl_assert_equal_p(conndata.query, NULL);
cl_assert_equal_p(conndata.fragment, NULL);
cl_assert_equal_i(git_net_url_is_default_port(&conndata), 0); cl_assert_equal_i(git_net_url_is_default_port(&conndata), 0);
} }
...@@ -166,6 +202,72 @@ void test_url_parse__hostname_user_pass_port(void) ...@@ -166,6 +202,72 @@ void test_url_parse__hostname_user_pass_port(void)
cl_assert_equal_s(conndata.path, "/resource"); cl_assert_equal_s(conndata.path, "/resource");
cl_assert_equal_s(conndata.username, "user"); cl_assert_equal_s(conndata.username, "user");
cl_assert_equal_s(conndata.password, "pass"); cl_assert_equal_s(conndata.password, "pass");
cl_assert_equal_p(conndata.query, NULL);
cl_assert_equal_p(conndata.fragment, NULL);
cl_assert_equal_i(git_net_url_is_default_port(&conndata), 0);
}
void test_url_parse__hostname_user_pass_port_query(void)
{
/* user:pass@hostname.tld:port/resource */
cl_git_pass(git_net_url_parse(&conndata,
"https://user:pass@example.com:9191/resource?query=q&foo=bar&z=asdf"));
cl_assert_equal_s(conndata.scheme, "https");
cl_assert_equal_s(conndata.host, "example.com");
cl_assert_equal_s(conndata.port, "9191");
cl_assert_equal_s(conndata.path, "/resource");
cl_assert_equal_s(conndata.username, "user");
cl_assert_equal_s(conndata.password, "pass");
cl_assert_equal_s(conndata.query, "query=q&foo=bar&z=asdf");
cl_assert_equal_p(conndata.fragment, NULL);
cl_assert_equal_i(git_net_url_is_default_port(&conndata), 0);
}
void test_url_parse__hostname_user_pass_port_fragment(void)
{
/* user:pass@hostname.tld:port/resource */
cl_git_pass(git_net_url_parse(&conndata,
"https://user:pass@example.com:9191/resource#fragment"));
cl_assert_equal_s(conndata.scheme, "https");
cl_assert_equal_s(conndata.host, "example.com");
cl_assert_equal_s(conndata.port, "9191");
cl_assert_equal_s(conndata.path, "/resource");
cl_assert_equal_s(conndata.username, "user");
cl_assert_equal_s(conndata.password, "pass");
cl_assert_equal_p(conndata.query, NULL);
cl_assert_equal_s(conndata.fragment, "fragment");
cl_assert_equal_i(git_net_url_is_default_port(&conndata), 0);
}
void test_url_parse__hostname_user_pass_port_query_fragment(void)
{
/* user:pass@hostname.tld:port/resource */
cl_git_pass(git_net_url_parse(&conndata,
"https://user:pass@example.com:9191/resource?query=q&foo=bar&z=asdf#fragment"));
cl_assert_equal_s(conndata.scheme, "https");
cl_assert_equal_s(conndata.host, "example.com");
cl_assert_equal_s(conndata.port, "9191");
cl_assert_equal_s(conndata.path, "/resource");
cl_assert_equal_s(conndata.username, "user");
cl_assert_equal_s(conndata.password, "pass");
cl_assert_equal_s(conndata.query, "query=q&foo=bar&z=asdf");
cl_assert_equal_s(conndata.fragment, "fragment");
cl_assert_equal_i(git_net_url_is_default_port(&conndata), 0);
}
void test_url_parse__fragment_with_question_mark(void)
{
/* user:pass@hostname.tld:port/resource */
cl_git_pass(git_net_url_parse(&conndata,
"https://user:pass@example.com:9191/resource#fragment_with?question_mark"));
cl_assert_equal_s(conndata.scheme, "https");
cl_assert_equal_s(conndata.host, "example.com");
cl_assert_equal_s(conndata.port, "9191");
cl_assert_equal_s(conndata.path, "/resource");
cl_assert_equal_s(conndata.username, "user");
cl_assert_equal_s(conndata.password, "pass");
cl_assert_equal_p(conndata.query, NULL);
cl_assert_equal_s(conndata.fragment, "fragment_with?question_mark");
cl_assert_equal_i(git_net_url_is_default_port(&conndata), 0); cl_assert_equal_i(git_net_url_is_default_port(&conndata), 0);
} }
...@@ -526,6 +628,7 @@ void test_url_parse__ipv6_invalid_addresses(void) ...@@ -526,6 +628,7 @@ void test_url_parse__ipv6_invalid_addresses(void)
"https://user@[fe80::dcad:beff:fe00:0001:9191/resource")); "https://user@[fe80::dcad:beff:fe00:0001:9191/resource"));
cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata,
"https://user:pass@[fe80::dcad:beff:fe00:0001:9191/resource")); "https://user:pass@[fe80::dcad:beff:fe00:0001:9191/resource"));
/* Both brackets missing */ /* Both brackets missing */
cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata,
"http://fe80::dcad:beff:fe00:0001/resource")); "http://fe80::dcad:beff:fe00:0001/resource"));
...@@ -554,6 +657,120 @@ void test_url_parse__ipv6_invalid_addresses(void) ...@@ -554,6 +657,120 @@ void test_url_parse__ipv6_invalid_addresses(void)
/* Invalid character inside address */ /* Invalid character inside address */
cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, "http://[fe8o::dcad:beff:fe00:0001]/resource")); cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, "http://[fe8o::dcad:beff:fe00:0001]/resource"));
/* Characters before/after braces */
cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata,
"http://fe80::[dcad:beff:fe00:0001]/resource"));
cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata,
"http://cafe[fe80::dcad:beff:fe00:0001]/resource"));
cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata,
"http://[fe80::dcad:beff:fe00:0001]cafe/resource"));
}
/* Oddities */
void test_url_parse__invalid_scheme_is_relative(void)
{
cl_git_pass(git_net_url_parse(&conndata, "foo!bar://host:42/path/to/project?query_string=yes"));
cl_assert_equal_p(conndata.scheme, NULL);
cl_assert_equal_p(conndata.host, NULL);
cl_assert_equal_p(conndata.port, NULL);
cl_assert_equal_s(conndata.path, "foo!bar://host:42/path/to/project");
cl_assert_equal_p(conndata.username, NULL);
cl_assert_equal_p(conndata.password, NULL);
cl_assert_equal_s(conndata.query, "query_string=yes");
cl_assert_equal_p(conndata.fragment, NULL);
cl_assert_equal_i(git_net_url_is_default_port(&conndata), 0);
}
void test_url_parse__scheme_case_is_normalized(void)
{
cl_git_pass(git_net_url_parse(&conndata, "GIT+SSH://host:42/path/to/project"));
cl_assert_equal_s(conndata.scheme, "git+ssh");
}
void test_url_parse__nonhierarchical_scheme(void)
{
cl_git_pass(git_net_url_parse(&conndata, "mailto:foobar@example.com"));
cl_assert_equal_s(conndata.scheme, "mailto");
cl_assert_equal_p(conndata.host, NULL);
cl_assert_equal_p(conndata.port, NULL);
cl_assert_equal_s(conndata.path, "foobar@example.com");
cl_assert_equal_p(conndata.username, NULL);
cl_assert_equal_p(conndata.password, NULL);
cl_assert_equal_i(git_net_url_is_default_port(&conndata), 0);
}
void test_url_parse__no_scheme_relative_path(void)
{
cl_git_pass(git_net_url_parse(&conndata, "path"));
cl_assert_equal_p(conndata.scheme, NULL);
cl_assert_equal_p(conndata.host, NULL);
cl_assert_equal_p(conndata.port, NULL);
cl_assert_equal_s(conndata.path, "path");
cl_assert_equal_p(conndata.username, NULL);
cl_assert_equal_p(conndata.password, NULL);
cl_assert_equal_i(git_net_url_is_default_port(&conndata), 0);
}
void test_url_parse__no_scheme_absolute_path(void)
{
cl_git_pass(git_net_url_parse(&conndata, "/path"));
cl_assert_equal_p(conndata.scheme, NULL);
cl_assert_equal_p(conndata.host, NULL);
cl_assert_equal_p(conndata.port, NULL);
cl_assert_equal_s(conndata.path, "/path");
cl_assert_equal_p(conndata.username, NULL);
cl_assert_equal_p(conndata.password, NULL);
cl_assert_equal_i(git_net_url_is_default_port(&conndata), 0);
}
void test_url_parse__empty_path(void)
{
cl_git_pass(git_net_url_parse(&conndata, "mailto:"));
cl_assert_equal_s(conndata.scheme, "mailto");
cl_assert_equal_p(conndata.host, NULL);
cl_assert_equal_p(conndata.port, NULL);
cl_assert_equal_s(conndata.path, NULL);
cl_assert_equal_p(conndata.username, NULL);
cl_assert_equal_p(conndata.password, NULL);
cl_assert_equal_i(git_net_url_is_default_port(&conndata), 0);
}
void test_url_parse__empty_path_with_empty_authority(void)
{
cl_git_pass(git_net_url_parse(&conndata, "http://"));
cl_assert_equal_s(conndata.scheme, "http");
cl_assert_equal_p(conndata.host, NULL);
cl_assert_equal_s(conndata.port, "80");
cl_assert_equal_s(conndata.path, "/");
cl_assert_equal_p(conndata.username, NULL);
cl_assert_equal_p(conndata.password, NULL);
cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1);
}
void test_url_parse__ssh_from_terrible_google_rfc_violating_products(void)
{
cl_git_pass(git_net_url_parse(&conndata, "ssh://my.email.address@gmail.com@source.developers.google.com:2022/p/my-project/r/my-repository"));
cl_assert_equal_s(conndata.scheme, "ssh");
cl_assert_equal_s(conndata.host, "source.developers.google.com");
cl_assert_equal_s(conndata.port, "2022");
cl_assert_equal_s(conndata.path, "/p/my-project/r/my-repository");
cl_assert_equal_s(conndata.username, "my.email.address@gmail.com");
cl_assert_equal_p(conndata.password, NULL);
cl_assert_equal_i(git_net_url_is_default_port(&conndata), 0);
}
void test_url_parse__ssh_with_password_from_terrible_google_rfc_violating_products(void)
{
cl_git_pass(git_net_url_parse(&conndata, "ssh://my.email.address@gmail.com:seekret@source.developers.google.com:2022/p/my-project/r/my-repository"));
cl_assert_equal_s(conndata.scheme, "ssh");
cl_assert_equal_s(conndata.host, "source.developers.google.com");
cl_assert_equal_s(conndata.port, "2022");
cl_assert_equal_s(conndata.path, "/p/my-project/r/my-repository");
cl_assert_equal_s(conndata.username, "my.email.address@gmail.com");
cl_assert_equal_s(conndata.password, "seekret");
cl_assert_equal_i(git_net_url_is_default_port(&conndata), 0);
} }
void test_url_parse__spaces_in_the_name(void) void test_url_parse__spaces_in_the_name(void)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment