Skip to content

Commit 3ec6e6e

Browse files
Patrick Steinhardtgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3e6a0e6 commit 3ec6e6e

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

urlmatch.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ static char *url_normalize_1(const char *url, struct url_info *out_info, char al
104104
struct strbuf norm;
105105
size_t spanned;
106106
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;
108108
const char *slash_ptr, *at_ptr, *colon_ptr, *path_start;
109109
char *result;
110110

@@ -263,14 +263,15 @@ static char *url_normalize_1(const char *url, struct url_info *out_info, char al
263263
return NULL;
264264
}
265265
strbuf_addch(&norm, ':');
266+
port_off = norm.len;
266267
strbuf_add(&norm, url, slash_ptr - url);
267268
port_len = slash_ptr - url;
268269
}
269270
url_len -= slash_ptr - colon_ptr;
270271
url = slash_ptr;
271272
}
272273
if (host_off)
273-
host_len = norm.len - host_off;
274+
host_len = norm.len - host_off - (port_len ? port_len + 1 : 0);
274275

275276

276277
/*
@@ -378,6 +379,7 @@ static char *url_normalize_1(const char *url, struct url_info *out_info, char al
378379
out_info->passwd_len = passwd_len;
379380
out_info->host_off = host_off;
380381
out_info->host_len = host_len;
382+
out_info->port_off = port_off;
381383
out_info->port_len = port_len;
382384
out_info->path_off = path_off;
383385
out_info->path_len = path_len;
@@ -464,11 +466,17 @@ static int match_urls(const struct url_info *url,
464466
usermatched = 1;
465467
}
466468

467-
/* check the host and port */
469+
/* check the host */
468470
if (url_prefix->host_len != url->host_len ||
469471
strncmp(url->url + url->host_off,
470472
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 */
472480

473481
/* check the path */
474482
pathmatchlen = url_match_prefix(

urlmatch.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@ struct url_info {
1818
size_t passwd_len; /* length of passwd; if passwd_off != 0 but
1919
passwd_len == 0, an empty passwd was given */
2020
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;
2222
* 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) */
2627
size_t path_off; /* offset into url to the start of the url path;
2728
* this will always point to a '/' character
2829
* after the url has been normalized */

0 commit comments

Comments
 (0)