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>

Patrick Steinhardt committed Jan 31, 2017 at 10:01 UTC 3ec6e6e8a0870a32357689e2179d845700539623
2 files changed +17 -8
urlmatch.c
+12 -4
@@ -104,7 +104,7 @@ static char *url_normalize_1(const char *url, struct url_info *out_info, char al
104 struct strbuf norm;
105 size_t spanned;
106 size_t scheme_len, user_off=0, user_len=0, passwd_off=0, passwd_len=0;
107 - size_t host_off=0, host_len=0, port_len=0, path_off, path_len, result_len;
107 + size_t host_off=0, host_len=0, port_off=0, port_len=0, path_off, path_len, result_len;
108 const char *slash_ptr, *at_ptr, *colon_ptr, *path_start;
109 char *result;
110
@@ -263,6 +263,7 @@ static char *url_normalize_1(const char *url, struct url_info *out_info, char al
263 return NULL;
264 }
265 strbuf_addch(&norm, ':');
266 + port_off = norm.len;
267 strbuf_add(&norm, url, slash_ptr - url);
268 port_len = slash_ptr - url;
269 }
@@ -270,7 +271,7 @@ static char *url_normalize_1(const char *url, struct url_info *out_info, char al
271 url = slash_ptr;
272 }
273 if (host_off)
273 - host_len = norm.len - host_off;
274 + host_len = norm.len - host_off - (port_len ? port_len + 1 : 0);
275
276
277 /*
@@ -378,6 +379,7 @@ static char *url_normalize_1(const char *url, struct url_info *out_info, char al
379 out_info->passwd_len = passwd_len;
380 out_info->host_off = host_off;
381 out_info->host_len = host_len;
382 + out_info->port_off = port_off;
383 out_info->port_len = port_len;
384 out_info->path_off = path_off;
385 out_info->path_len = path_len;
@@ -464,11 +466,17 @@ static int match_urls(const struct url_info *url,
466 usermatched = 1;
467 }
468
467 - /* check the host and port */
469 + /* check the host */
470 if (url_prefix->host_len != url->host_len ||
471 strncmp(url->url + url->host_off,
472 url_prefix->url + url_prefix->host_off, url->host_len))
471 - return 0; /* host names and/or ports do not match */
473 + return 0; /* host names do not match */
474 +
475 + /* check the port */
476 + if (url_prefix->port_len != url->port_len ||
477 + strncmp(url->url + url->port_off,
478 + url_prefix->url + url_prefix->port_off, url->port_len))
479 + return 0; /* ports do not match */
480
481 /* check the path */
482 pathmatchlen = url_match_prefix(
urlmatch.h
+5 -4
@@ -18,11 +18,12 @@ struct url_info {
18 size_t passwd_len; /* length of passwd; if passwd_off != 0 but
19 passwd_len == 0, an empty passwd was given */
20 size_t host_off; /* offset into url to start of host name (0 => none) */
21 - size_t host_len; /* length of host name; this INCLUDES any ':portnum';
21 + size_t host_len; /* length of host name;
22 * file urls may have host_len == 0 */
23 - size_t port_len; /* if a portnum is present (port_len != 0), it has
24 - * this length (excluding the leading ':') at the
25 - * end of the host name (always 0 for file urls) */
23 + size_t port_off; /* offset into url to start of port number (0 => none) */
24 + size_t port_len; /* if a portnum is present (port_off != 0), it has
25 + * this length (excluding the leading ':') starting
26 + * from port_off (always 0 for file urls) */
27 size_t path_off; /* offset into url to the start of the url path;
28 * this will always point to a '/' character
29 * after the url has been normalized */