8000 net: introduce http-biased url parsing · libgit2/libgit2@1d7d6ff · GitHub
[go: up one dir, main page]

Skip to content

Commit 1d7d6ff

Browse files
committed
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`..
1 parent 95d4cb6 commit 1d7d6ff

File tree

3 files changed

+803
-0
lines changed

3 files changed

+803
-0
lines changed

src/util/net.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,49 @@ int git_net_url_parse(git_net_url *url, const char *given)
565565
return error;
566566
}
567567

568+
int git_net_url_parse_http(
569+
git_net_url *url,
570+
const char *given)
571+
{
572+
git_net_url_parser parser = GIT_NET_URL_PARSER_INIT;
573+
const char *c, *authority, *path = NULL;
574+
size_t authority_len = 0, path_len = 0;
575+
int error;
576+
577+
/* Hopefully this is a proper URL with a scheme. */
578+
if (git_net_str_is_url(given))
579+
return git_net_url_parse(url, given);
580+
581+
memset(url, 0, sizeof(git_net_url));
582+
583+
/* Without a scheme, we are in the host (authority) section. */
584+
for (c = authority = given; *c; c++) {
585+
if (!path && *c == '/') {
586+
authority_len = (c - authority);
587+
path = c;
588+
}
589+
}
590+
591+
if (path)
592+
path_len = (c - path);
593+
else
594+
authority_len = (c - authority);
595+
596+
parser.scheme = "http";
597+
parser.scheme_len = 4;
598+
parser.hierarchical = 1;
599+
600+
if (authority_len &&
601+
(error = url_parse_authority(&parser, authority, authority_len)) < 0)
602+
return error;
603+
604+
if (path_len &&
605+
(error = url_parse_path(&parser, path, path_len)) < 0)
606+
return error;
607+
608+
return url_parse_finalize(url, &parser);
609+
}
610+
568611
static int scp_invalid(const char *message)
569612
{
570613
git_error_set(GIT_ERROR_NET, "invalid scp-style path: %s", message);

src/util/net.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@ extern int git_net_url_parse_scp(git_net_url *url, const char *str);
5757
*/
5858
extern int git_net_url_parse_standard_or_scp(git_net_url *url, const char *str);
5959

60+
/**
61+
* Parses a string containing an HTTP endpoint that may not be a
62+
* well-formed URL. For example, "localhost" or "localhost:port".
63+
*/
64+
extern int git_net_url_parse_http(
65+
git_net_url *url,
66+
const char *str);
67+
6068
/** Appends a path and/or query string to the given URL */
6169
extern int git_net_url_joinpath(
6270
git_net_url *out,

0 commit comments

Comments
 (0)
0