Commit 1d7d6ff9 by Edward Thomson

net: introduce http-biased url parsing

Introduce a url parser that defaults to treating poorly specified URLs
as http URLs. For example: `localhost:8080` is treated as
`http://localhost:8080/` by the http-biased url parsing, instead of a
URL with a scheme `localhost` and a path of `8080`..
parent 95d4cb6e
...@@ -565,6 +565,49 @@ done: ...@@ -565,6 +565,49 @@ done:
return error; return error;
} }
int git_net_url_parse_http(
git_net_url *url,
const char *given)
{
git_net_url_parser parser = GIT_NET_URL_PARSER_INIT;
const char *c, *authority, *path = NULL;
size_t authority_len = 0, path_len = 0;
int error;
/* Hopefully this is a proper URL with a scheme. */
if (git_net_str_is_url(given))
return git_net_url_parse(url, given);
memset(url, 0, sizeof(git_net_url));
/* Without a scheme, we are in the host (authority) section. */
for (c = authority = given; *c; c++) {
if (!path && *c == '/') {
authority_len = (c - authority);
path = c;
}
}
if (path)
path_len = (c - path);
else
authority_len = (c - authority);
parser.scheme = "http";
parser.scheme_len = 4;
parser.hierarchical = 1;
if (authority_len &&
(error = url_parse_authority(&parser, authority, authority_len)) < 0)
return error;
if (path_len &&
(error = url_parse_path(&parser, path, path_len)) < 0)
return error;
return url_parse_finalize(url, &parser);
}
static int scp_invalid(const char *message) static int scp_invalid(const char *message)
{ {
git_error_set(GIT_ERROR_NET, "invalid scp-style path: %s", message); git_error_set(GIT_ERROR_NET, "invalid scp-style path: %s", message);
......
...@@ -57,6 +57,14 @@ extern int git_net_url_parse_scp(git_net_url *url, const char *str); ...@@ -57,6 +57,14 @@ extern int git_net_url_parse_scp(git_net_url *url, const char *str);
*/ */
extern int git_net_url_parse_standard_or_scp(git_net_url *url, const char *str); extern int git_net_url_parse_standard_or_scp(git_net_url *url, const char *str);
/**
* Parses a string containing an HTTP endpoint that may not be a
* well-formed URL. For example, "localhost" or "localhost:port".
*/
extern int git_net_url_parse_http(
git_net_url *url,
const char *str);
/** Appends a path and/or query string to the given URL */ /** Appends a path and/or query string to the given URL */
extern int git_net_url_joinpath( extern int git_net_url_joinpath(
git_net_url *out, git_net_url *out,
......
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