urlmatch: split host and port fields in `struct url_info`

The `url_info` structure contains information about a normalized URL
with the URL's components being represented by different fields. The
host and port part though are to be accessed by the same `host` field,
so that getting the host and/or port separately becomes more involved
than really necessary.

To make the port more readily accessible, split up the host and port
fields. Namely, the `host_len` will not include the port length anymore
and a new `port_off` field has been added which includes the offset to
the port, if available.

The only user of these fields is `url_normalize_1`. This change makes it
easier later on to treat host and port differently when introducing
globs for domains.

Signed-off-by: Patrick Steinhardt <patrick.steinhardt@elego.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Patrick Steinhardt 2017-01-31 10:01:45 +01:00 коммит произвёл Junio C Hamano
Родитель 3e6a0e64a4
Коммит 3ec6e6e8a0
2 изменённых файлов: 17 добавлений и 8 удалений

Просмотреть файл

@ -104,7 +104,7 @@ static char *url_normalize_1(const char *url, struct url_info *out_info, char al
struct strbuf norm; struct strbuf norm;
size_t spanned; size_t spanned;
size_t scheme_len, user_off=0, user_len=0, passwd_off=0, passwd_len=0; size_t scheme_len, user_off=0, user_len=0, passwd_off=0, passwd_len=0;
size_t host_off=0, host_len=0, port_len=0, path_off, path_len, result_len; size_t host_off=0, host_len=0, port_off=0, port_len=0, path_off, path_len, result_len;
const char *slash_ptr, *at_ptr, *colon_ptr, *path_start; const char *slash_ptr, *at_ptr, *colon_ptr, *path_start;
char *result; char *result;
@ -263,6 +263,7 @@ static char *url_normalize_1(const char *url, struct url_info *out_info, char al
return NULL; return NULL;
} }
strbuf_addch(&norm, ':'); strbuf_addch(&norm, ':');
port_off = norm.len;
strbuf_add(&norm, url, slash_ptr - url); strbuf_add(&norm, url, slash_ptr - url);
port_len = slash_ptr - url; port_len = slash_ptr - url;
} }
@ -270,7 +271,7 @@ static char *url_normalize_1(const char *url, struct url_info *out_info, char al
url = slash_ptr; url = slash_ptr;
} }
if (host_off) if (host_off)
host_len = norm.len - host_off; host_len = norm.len - host_off - (port_len ? port_len + 1 : 0);
/* /*
@ -378,6 +379,7 @@ static char *url_normalize_1(const char *url, struct url_info *out_info, char al
out_info->passwd_len = passwd_len; out_info->passwd_len = passwd_len;
out_info->host_off = host_off; out_info->host_off = host_off;
out_info->host_len = host_len; out_info->host_len = host_len;
out_info->port_off = port_off;
out_info->port_len = port_len; out_info->port_len = port_len;
out_info->path_off = path_off; out_info->path_off = path_off;
out_info->path_len = path_len; out_info->path_len = path_len;
@ -464,11 +466,17 @@ static int match_urls(const struct url_info *url,
usermatched = 1; usermatched = 1;
} }
/* check the host and port */ /* check the host */
if (url_prefix->host_len != url->host_len || if (url_prefix->host_len != url->host_len ||
strncmp(url->url + url->host_off, strncmp(url->url + url->host_off,
url_prefix->url + url_prefix->host_off, url->host_len)) url_prefix->url + url_prefix->host_off, url->host_len))
return 0; /* host names and/or ports do not match */ return 0; /* host names do not match */
/* check the port */
if (url_prefix->port_len != url->port_len ||
strncmp(url->url + url->port_off,
url_prefix->url + url_prefix->port_off, url->port_len))
return 0; /* ports do not match */
/* check the path */ /* check the path */
pathmatchlen = url_match_prefix( pathmatchlen = url_match_prefix(

Просмотреть файл

@ -18,11 +18,12 @@ struct url_info {
size_t passwd_len; /* length of passwd; if passwd_off != 0 but size_t passwd_len; /* length of passwd; if passwd_off != 0 but
passwd_len == 0, an empty passwd was given */ passwd_len == 0, an empty passwd was given */
size_t host_off; /* offset into url to start of host name (0 => none) */ size_t host_off; /* offset into url to start of host name (0 => none) */
size_t host_len; /* length of host name; this INCLUDES any ':portnum'; size_t host_len; /* length of host name;
* file urls may have host_len == 0 */ * file urls may have host_len == 0 */
size_t port_len; /* if a portnum is present (port_len != 0), it has size_t port_off; /* offset into url to start of port number (0 => none) */
* this length (excluding the leading ':') at the size_t port_len; /* if a portnum is present (port_off != 0), it has
* end of the host name (always 0 for file urls) */ * this length (excluding the leading ':') starting
* from port_off (always 0 for file urls) */
size_t path_off; /* offset into url to the start of the url path; size_t path_off; /* offset into url to the start of the url path;
* this will always point to a '/' character * this will always point to a '/' character
* after the url has been normalized */ * after the url has been normalized */