{"thread":{"id":"45008","subject":"[PATCH v5 5/5] urlmatch: allow globbing for the URL host part","startedAt":"2017-01-31T09:05:02Z","lastAt":"2017-01-31T09:09:55Z","messageCount":6,"participants":["Patrick Steinhardt"],"isPatch":true,"patchVersion":5,"patchTotal":5},"messages":[{"id":"310592","messageId":"542b3b76f6c23a2b0e1e1306b6e58ce522977bf3.1485853153.git.ps@pks.im","threadId":"45008","inReplyTo":"cover.1485853153.git.ps@pks.im","subject":"[PATCH v5 5/5] urlmatch: allow globbing for the URL host part","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2017-01-31T09:01:47Z","receivedAt":"2017-01-31T09:05:02Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"From: Patrick Steinhardt <patrick.steinhardt@elego.de>\n\nThe URL matching function computes for two URLs whether they match not.\nThe match is performed by splitting up the URL into different parts and\nthen doing an exact comparison with the to-be-matched URL.\n\nThe main user of `urlmatch` is the configuration subsystem. It allows to\nset certain configurations based on the URL which is being connected to\nvia keys like `http.<url>.*`. A common use case for this is to set\nproxies for only some remotes which match the given URL. Unfortunately,\nhaving exact matches for all parts of the URL can become quite tedious\nin some setups. Imagine for example a corporate network where there are\ndozens or even hundreds of subdomains, which would have to be configured\nindividually.\n\nAllow users to write an asterisk '*' in place of any 'host' or\n'subdomain' label as part of the host name.  For example,\n\"http.https://*.example.com.proxy\" sets \"http.proxy\" for all direct\nsubdomains of \"https://example.com\", e.g. \"https://foo.example.com\", but\nnot \"https://foo.bar.example.com\".\n\nSigned-off-by: Patrick Steinhardt <patrick.steinhardt@elego.de>\nHelped-by: Junio C Hamano <gitster@pobox.com>\n---\n Documentation/config.txt |  5 +++-\n t/t1300-repo-config.sh   | 72 ++++++++++++++++++++++++++++++++++++++++++++++++\n urlmatch.c               | 49 +++++++++++++++++++++++++++++---\n 3 files changed, 121 insertions(+), 5 deletions(-)\n\ndiff --git a/Documentation/config.txt b/Documentation/config.txt\nindex af2ae4cc0..ee155d8a6 100644\n--- a/Documentation/config.txt\n+++ b/Documentation/config.txt\n@@ -1914,7 +1914,10 @@ http.<url>.*::\n   must match exactly between the config key and the URL.\n \n . Host/domain name (e.g., `example.com` in `https://example.com/`).\n-  This field must match exactly between the config key and the URL.\n+  This field must match between the config key and the URL. It is\n+  possible to specify a `*` as part of the host name to match all subdomains\n+  at this level. `https://*.example.com/` for example would match\n+  `https://foo.example.com/`, but not `https://foo.bar.example.com/`.\n \n . Port number (e.g., `8080` in `http://example.com:8080/`).\n   This field must match exactly between the config key and the URL.\ndiff --git a/t/t1300-repo-config.sh b/t/t1300-repo-config.sh\nindex 6c844d519..052f12021 100755\n--- a/t/t1300-repo-config.sh\n+++ b/t/t1300-repo-config.sh\n@@ -1187,6 +1187,18 @@ test_expect_success 'urlmatch favors more specific URLs' '\n \t\tcookieFile = /tmp/user.txt\n \t[http \"https://averylonguser@example.com/\"]\n \t\tcookieFile = /tmp/averylonguser.txt\n+\t[http \"https://preceding.example.com\"]\n+\t\tcookieFile = /tmp/preceding.txt\n+\t[http \"https://*.example.com\"]\n+\t\tcookieFile = /tmp/wildcard.txt\n+\t[http \"https://*.example.com/wildcardwithsubdomain\"]\n+\t\tcookieFile = /tmp/wildcardwithsubdomain.txt\n+\t[http \"https://trailing.example.com\"]\n+\t\tcookieFile = /tmp/trailing.txt\n+\t[http \"https://user@*.example.com/\"]\n+\t\tcookieFile = /tmp/wildcardwithuser.txt\n+\t[http \"https://sub.example.com/\"]\n+\t\tcookieFile = /tmp/sub.txt\n \tEOF\n \n \techo http.cookiefile /tmp/root.txt >expect &&\n@@ -1207,6 +1219,66 @@ test_expect_success 'urlmatch favors more specific URLs' '\n \n \techo http.cookiefile /tmp/subdirectory.txt >expect &&\n \tgit config --get-urlmatch HTTP https://averylonguser@example.com/subdirectory >actual &&\n+\ttest_cmp expect actual &&\n+\n+\techo http.cookiefile /tmp/preceding.txt >expect &&\n+\tgit config --get-urlmatch HTTP https://preceding.example.com >actual &&\n+\ttest_cmp expect actual &&\n+\n+\techo http.cookiefile /tmp/wildcard.txt >expect &&\n+\tgit config --get-urlmatch HTTP https://wildcard.example.com >actual &&\n+\ttest_cmp expect actual &&\n+\n+\techo http.cookiefile /tmp/sub.txt >expect &&\n+\tgit config --get-urlmatch HTTP https://sub.example.com/wildcardwithsubdomain >actual &&\n+\ttest_cmp expect actual &&\n+\n+\techo http.cookiefile /tmp/trailing.txt >expect &&\n+\tgit config --get-urlmatch HTTP https://trailing.example.com >actual &&\n+\ttest_cmp expect actual &&\n+\n+\techo http.cookiefile /tmp/sub.txt >expect &&\n+\tgit config --get-urlmatch HTTP https://user@sub.example.com >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'urlmatch with wildcard' '\n+\tcat >.git/config <<-\\EOF &&\n+\t[http]\n+\t\tsslVerify\n+\t[http \"https://*.example.com\"]\n+\t\tsslVerify = false\n+\t\tcookieFile = /tmp/cookie.txt\n+\tEOF\n+\n+\ttest_expect_code 1 git config --bool --get-urlmatch doesnt.exist https://good.example.com >actual &&\n+\ttest_must_be_empty actual &&\n+\n+\techo true >expect &&\n+\tgit config --bool --get-urlmatch http.SSLverify https://example.com >actual &&\n+\ttest_cmp expect actual &&\n+\n+\techo true >expect &&\n+\tgit config --bool --get-urlmatch http.SSLverify https://good-example.com >actual &&\n+\ttest_cmp expect actual &&\n+\n+\techo true >expect &&\n+\tgit config --bool --get-urlmatch http.sslverify https://deep.nested.example.com >actual &&\n+\ttest_cmp expect actual &&\n+\n+\techo false >expect &&\n+\tgit config --bool --get-urlmatch http.sslverify https://good.example.com >actual &&\n+\ttest_cmp expect actual &&\n+\n+\t{\n+\t\techo http.cookiefile /tmp/cookie.txt &&\n+\t\techo http.sslverify false\n+\t} >expect &&\n+\tgit config --get-urlmatch HTTP https://good.example.com >actual &&\n+\ttest_cmp expect actual &&\n+\n+\techo http.sslverify >expect &&\n+\tgit config --get-urlmatch HTTP https://more.example.com.au >actual &&\n \ttest_cmp expect actual\n '\n \ndiff --git a/urlmatch.c b/urlmatch.c\nindex f79887825..6c12f1a48 100644\n--- a/urlmatch.c\n+++ b/urlmatch.c\n@@ -63,6 +63,49 @@ static int append_normalized_escapes(struct strbuf *buf,\n \treturn 1;\n }\n \n+static const char *end_of_token(const char *s, int c, size_t n)\n+{\n+\tconst char *next = memchr(s, c, n);\n+\tif (!next)\n+\t\tnext = s + n;\n+\treturn next;\n+}\n+\n+static int match_host(const struct url_info *url_info,\n+\t\t      const struct url_info *pattern_info)\n+{\n+\tconst char *url = url_info->url + url_info->host_off;\n+\tconst char *pat = pattern_info->url + pattern_info->host_off;\n+\tint url_len = url_info->host_len;\n+\tint pat_len = pattern_info->host_len;\n+\n+\twhile (url_len && pat_len) {\n+\t\tconst char *url_next = end_of_token(url, '.', url_len);\n+\t\tconst char *pat_next = end_of_token(pat, '.', pat_len);\n+\n+\t\tif (pat_next == pat + 1 && pat[0] == '*')\n+\t\t\t/* wildcard matches anything */\n+\t\t\t;\n+\t\telse if ((pat_next - pat) == (url_next - url) &&\n+\t\t\t !memcmp(url, pat, url_next - url))\n+\t\t\t/* the components are the same */\n+\t\t\t;\n+\t\telse\n+\t\t\treturn 0; /* found an unmatch */\n+\n+\t\tif (url_next < url + url_len)\n+\t\t\turl_next++;\n+\t\turl_len -= url_next - url;\n+\t\turl = url_next;\n+\t\tif (pat_next < pat + pat_len)\n+\t\t\tpat_next++;\n+\t\tpat_len -= pat_next - pat;\n+\t\tpat = pat_next;\n+\t}\n+\n+\treturn (!url_len && !pat_len);\n+}\n+\n static char *url_normalize_1(const char *url, struct url_info *out_info, char allow_globs)\n {\n \t/*\n@@ -467,9 +510,7 @@ static int match_urls(const struct url_info *url,\n \t}\n \n \t/* check the host */\n-\tif (url_prefix->host_len != url->host_len ||\n-\t    strncmp(url->url + url->host_off,\n-\t\t    url_prefix->url + url_prefix->host_off, url->host_len))\n+\tif (!match_host(url, url_prefix))\n \t\treturn 0; /* host names do not match */\n \n \t/* check the port */\n@@ -528,7 +569,7 @@ int urlmatch_config_entry(const char *var, const char *value, void *cb)\n \t\tstruct url_info norm_info;\n \n \t\tconfig_url = xmemdupz(key, dot - key);\n-\t\tnorm_url = url_normalize(config_url, &norm_info);\n+\t\tnorm_url = url_normalize_1(config_url, &norm_info, 1);\n \t\tfree(config_url);\n \t\tif (!norm_url)\n \t\t\treturn 0;\n-- \n2.11.0\n\n"},{"id":"310593","messageId":"dec34b078e3019d7078be6799d3d8c0e28d912ba.1485853153.git.ps@pks.im","threadId":"45008","inReplyTo":"cover.1485853153.git.ps@pks.im","subject":"[PATCH v5 2/5] urlmatch: enable normalization of URLs with globs","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2017-01-31T09:01:44Z","receivedAt":"2017-01-31T09:09:41Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"From: Patrick Steinhardt <patrick.steinhardt@elego.de>\n\nThe `url_normalize` function is used to validate and normalize URLs. As\nsuch, it does not allow for some special characters to be part of the\nURLs that are to be normalized. As we want to allow using globs in some\nconfiguration keys making use of URLs, namely `http.<url>.<key>`, but\nstill normalize them, we need to somehow enable some additional allowed\ncharacters.\n\nTo do this without having to change all callers of `url_normalize`,\nwhere most do not actually want globbing at all, we split off another\nfunction `url_normalize_1`. This function accepts an additional\nparameter `allow_globs`, which is subsequently called by `url_normalize`\nwith `allow_globs=0`.\n\nAs of now, this function is not used with globbing enabled. A caller\nwill be added in the following commit.\n\nSigned-off-by: Patrick Steinhardt <patrick.steinhardt@elego.de>\n---\n urlmatch.c | 14 ++++++++++++--\n 1 file changed, 12 insertions(+), 2 deletions(-)\n\ndiff --git a/urlmatch.c b/urlmatch.c\nindex 132d342bc..d350478c0 100644\n--- a/urlmatch.c\n+++ b/urlmatch.c\n@@ -63,7 +63,7 @@ static int append_normalized_escapes(struct strbuf *buf,\n \treturn 1;\n }\n \n-char *url_normalize(const char *url, struct url_info *out_info)\n+static char *url_normalize_1(const char *url, struct url_info *out_info, char allow_globs)\n {\n \t/*\n \t * Normalize NUL-terminated url using the following rules:\n@@ -191,7 +191,12 @@ char *url_normalize(const char *url, struct url_info *out_info)\n \t\tstrbuf_release(&norm);\n \t\treturn NULL;\n \t}\n-\tspanned = strspn(url, URL_HOST_CHARS);\n+\n+\tif (allow_globs)\n+\t\tspanned = strspn(url, URL_HOST_CHARS \"*\");\n+\telse\n+\t\tspanned = strspn(url, URL_HOST_CHARS);\n+\n \tif (spanned < colon_ptr - url) {\n \t\t/* Host name has invalid characters */\n \t\tif (out_info) {\n@@ -380,6 +385,11 @@ char *url_normalize(const char *url, struct url_info *out_info)\n \treturn result;\n }\n \n+char *url_normalize(const char *url, struct url_info *out_info)\n+{\n+\treturn url_normalize_1(url, out_info, 0);\n+}\n+\n static size_t url_match_prefix(const char *url,\n \t\t\t       const char *url_prefix,\n \t\t\t       size_t url_prefix_len)\n-- \n2.11.0\n\n"},{"id":"310594","messageId":"371385ec381c937e87e15de50c6663cd1e36e984.1485853153.git.ps@pks.im","threadId":"45008","inReplyTo":"cover.1485853153.git.ps@pks.im","subject":"[PATCH v5 1/5] mailmap: add Patrick Steinhardt's work address","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2017-01-31T09:01:43Z","receivedAt":"2017-01-31T09:09:47Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"From: Patrick Steinhardt <patrick.steinhardt@elego.de>\n\nSigned-off-by: Patrick Steinhardt <patrick.steinhardt@elego.de>\n---\n .mailmap | 1 +\n 1 file changed, 1 insertion(+)\n\ndiff --git a/.mailmap b/.mailmap\nindex 9c87a3840..ea59205b9 100644\n--- a/.mailmap\n+++ b/.mailmap\n@@ -177,6 +177,7 @@ Paolo Bonzini <bonzini@gnu.org> <paolo.bonzini@lu.unisi.ch>\n Pascal Obry <pascal@obry.net> <pascal.obry@gmail.com>\n Pascal Obry <pascal@obry.net> <pascal.obry@wanadoo.fr>\n Pat Notz <patnotz@gmail.com> <pknotz@sandia.gov>\n+Patrick Steinhardt <ps@pks.im> <patrick.steinhardt@elego.de>\n Paul Mackerras <paulus@samba.org> <paulus@dorrigo.(none)>\n Paul Mackerras <paulus@samba.org> <paulus@pogo.(none)>\n Peter Baumann <waste.manager@gmx.de> <Peter.B.Baumann@stud.informatik.uni-erlangen.de>\n-- \n2.11.0\n\n"},{"id":"310595","messageId":"cover.1485853153.git.ps@pks.im","threadId":"45008","inReplyTo":"20170123130635.29577-1-patrick.steinhardt@elego.de","subject":"[PATCH v5 0/5] urlmatch: allow wildcard-based matches","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2017-01-31T09:01:42Z","receivedAt":"2017-01-31T09:09:50Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"Hi,\n\nthis is version 5 of my patch series. The previous version can\nbe found at [1]. The use case is to be able to configure an HTTP\nproxy for all subdomains of a domain where there are hundreds of\nsubdomains.\n\nThis includes only a single change, interdiff is included below.\nThe previous version had an embarassing bug because a variable\nwas not properly initialized in all cases, leading to undefined\nbehavior. I also verified that the patches work on top of\n4e59582ff (Seventh batch for 2.12, 2017-01-23), where Junio\nreported the test failures.\n\nRegards\nPatrick\n\nPatrick Steinhardt (5):\n  mailmap: add Patrick Steinhardt's work address\n  urlmatch: enable normalization of URLs with globs\n  urlmatch: split host and port fields in `struct url_info`\n  urlmatch: include host in urlmatch ranking\n  urlmatch: allow globbing for the URL host part\n\n .mailmap                 |   1 +\n Documentation/config.txt |   5 +-\n t/t1300-repo-config.sh   | 105 ++++++++++++++++++++++++++++++++++++\n urlmatch.c               | 138 +++++++++++++++++++++++++++++++++++------------\n urlmatch.h               |  12 +++--\n 5 files changed, 220 insertions(+), 41 deletions(-)\n\n-- \n2.11.0\n\ndiff --git a/urlmatch.c b/urlmatch.c\nindex bb5267732..6c12f1a48 100644\n--- a/urlmatch.c\n+++ b/urlmatch.c\n@@ -552,7 +552,7 @@ int urlmatch_config_entry(const char *var, const char *value, void *cb)\n {\n \tstruct string_list_item *item;\n \tstruct urlmatch_config *collect = cb;\n-\tstruct urlmatch_item matched;\n+\tstruct urlmatch_item matched = {0};\n \tstruct url_info *url = &collect->url;\n \tconst char *key, *dot;\n \tstruct strbuf synthkey = STRBUF_INIT;\n"},{"id":"310596","messageId":"19a49e4b091d0acf7dbd8733605b8d7db23b7039.1485853153.git.ps@pks.im","threadId":"45008","inReplyTo":"cover.1485853153.git.ps@pks.im","subject":"[PATCH v5 3/5] urlmatch: split host and port fields in `struct url_info`","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2017-01-31T09:01:45Z","receivedAt":"2017-01-31T09:09:52Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"From: Patrick Steinhardt <patrick.steinhardt@elego.de>\n\nThe `url_info` structure contains information about a normalized URL\nwith the URL's components being represented by different fields. The\nhost and port part though are to be accessed by the same `host` field,\nso that getting the host and/or port separately becomes more involved\nthan really necessary.\n\nTo make the port more readily accessible, split up the host and port\nfields. Namely, the `host_len` will not include the port length anymore\nand a new `port_off` field has been added which includes the offset to\nthe port, if available.\n\nThe only user of these fields is `url_normalize_1`. This change makes it\neasier later on to treat host and port differently when introducing\nglobs for domains.\n\nSigned-off-by: Patrick Steinhardt <patrick.steinhardt@elego.de>\n---\n urlmatch.c | 16 ++++++++++++----\n urlmatch.h |  9 +++++----\n 2 files changed, 17 insertions(+), 8 deletions(-)\n\ndiff --git a/urlmatch.c b/urlmatch.c\nindex d350478c0..e328905eb 100644\n--- a/urlmatch.c\n+++ b/urlmatch.c\n@@ -104,7 +104,7 @@ static char *url_normalize_1(const char *url, struct url_info *out_info, char al\n \tstruct strbuf norm;\n \tsize_t spanned;\n \tsize_t scheme_len, user_off=0, user_len=0, passwd_off=0, passwd_len=0;\n-\tsize_t host_off=0, host_len=0, port_len=0, path_off, path_len, result_len;\n+\tsize_t host_off=0, host_len=0, port_off=0, port_len=0, path_off, path_len, result_len;\n \tconst char *slash_ptr, *at_ptr, *colon_ptr, *path_start;\n \tchar *result;\n \n@@ -263,6 +263,7 @@ static char *url_normalize_1(const char *url, struct url_info *out_info, char al\n \t\t\t\treturn NULL;\n \t\t\t}\n \t\t\tstrbuf_addch(&norm, ':');\n+\t\t\tport_off = norm.len;\n \t\t\tstrbuf_add(&norm, url, slash_ptr - url);\n \t\t\tport_len = slash_ptr - url;\n \t\t}\n@@ -270,7 +271,7 @@ static char *url_normalize_1(const char *url, struct url_info *out_info, char al\n \t\turl = slash_ptr;\n \t}\n \tif (host_off)\n-\t\thost_len = norm.len - host_off;\n+\t\thost_len = norm.len - host_off - (port_len ? port_len + 1 : 0);\n \n \n \t/*\n@@ -378,6 +379,7 @@ static char *url_normalize_1(const char *url, struct url_info *out_info, char al\n \t\tout_info->passwd_len = passwd_len;\n \t\tout_info->host_off = host_off;\n \t\tout_info->host_len = host_len;\n+\t\tout_info->port_off = port_off;\n \t\tout_info->port_len = port_len;\n \t\tout_info->path_off = path_off;\n \t\tout_info->path_len = path_len;\n@@ -464,11 +466,17 @@ static int match_urls(const struct url_info *url,\n \t\tusermatched = 1;\n \t}\n \n-\t/* check the host and port */\n+\t/* check the host */\n \tif (url_prefix->host_len != url->host_len ||\n \t    strncmp(url->url + url->host_off,\n \t\t    url_prefix->url + url_prefix->host_off, url->host_len))\n-\t\treturn 0; /* host names and/or ports do not match */\n+\t\treturn 0; /* host names do not match */\n+\n+\t/* check the port */\n+\tif (url_prefix->port_len != url->port_len ||\n+\t    strncmp(url->url + url->port_off,\n+\t\t    url_prefix->url + url_prefix->port_off, url->port_len))\n+\t\treturn 0; /* ports do not match */\n \n \t/* check the path */\n \tpathmatchlen = url_match_prefix(\ndiff --git a/urlmatch.h b/urlmatch.h\nindex 528862adc..0ea812b03 100644\n--- a/urlmatch.h\n+++ b/urlmatch.h\n@@ -18,11 +18,12 @@ struct url_info {\n \tsize_t passwd_len;\t/* length of passwd; if passwd_off != 0 but\n \t\t\t\t   passwd_len == 0, an empty passwd was given */\n \tsize_t host_off;\t/* offset into url to start of host name (0 => none) */\n-\tsize_t host_len;\t/* length of host name; this INCLUDES any ':portnum';\n+\tsize_t host_len;\t/* length of host name;\n \t\t\t\t * file urls may have host_len == 0 */\n-\tsize_t port_len;\t/* if a portnum is present (port_len != 0), it has\n-\t\t\t\t * this length (excluding the leading ':') at the\n-\t\t\t\t * end of the host name (always 0 for file urls) */\n+\tsize_t port_off;\t/* offset into url to start of port number (0 => none) */\n+\tsize_t port_len;\t/* if a portnum is present (port_off != 0), it has\n+\t\t\t\t * this length (excluding the leading ':') starting\n+\t\t\t\t * from port_off (always 0 for file urls) */\n \tsize_t path_off;\t/* offset into url to the start of the url path;\n \t\t\t\t * this will always point to a '/' character\n \t\t\t\t * after the url has been normalized */\n-- \n2.11.0\n\n"},{"id":"310597","messageId":"3280b309860266dd68622f8362e265c7f9834cba.1485853153.git.ps@pks.im","threadId":"45008","inReplyTo":"cover.1485853153.git.ps@pks.im","subject":"[PATCH v5 4/5] urlmatch: include host in urlmatch ranking","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2017-01-31T09:01:46Z","receivedAt":"2017-01-31T09:09:55Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"From: Patrick Steinhardt <patrick.steinhardt@elego.de>\n\nIn order to be able to rank positive matches by `urlmatch`, we inspect\nthe path length and user part to decide whether a match is better than\nanother match. As all other parts are matched exactly between both URLs,\nthis is the correct thing to do right now.\n\nIn the future, though, we want to introduce wild cards for the domain\npart. When doing this, it does not make sense anymore to only compare\nthe path lengths. Instead, we also want to compare the domain lengths to\ndetermine which of both URLs matches the host part more closely.\n\nSigned-off-by: Patrick Steinhardt <patrick.steinhardt@elego.de>\n---\n t/t1300-repo-config.sh | 33 ++++++++++++++++++++++++++++\n urlmatch.c             | 59 +++++++++++++++++++++++++++++---------------------\n urlmatch.h             |  3 ++-\n 3 files changed, 69 insertions(+), 26 deletions(-)\n\ndiff --git a/t/t1300-repo-config.sh b/t/t1300-repo-config.sh\nindex 923bfc5a2..6c844d519 100755\n--- a/t/t1300-repo-config.sh\n+++ b/t/t1300-repo-config.sh\n@@ -1177,6 +1177,39 @@ test_expect_success 'urlmatch' '\n \ttest_cmp expect actual\n '\n \n+test_expect_success 'urlmatch favors more specific URLs' '\n+\tcat >.git/config <<-\\EOF &&\n+\t[http \"https://example.com/\"]\n+\t\tcookieFile = /tmp/root.txt\n+\t[http \"https://example.com/subdirectory\"]\n+\t\tcookieFile = /tmp/subdirectory.txt\n+\t[http \"https://user@example.com/\"]\n+\t\tcookieFile = /tmp/user.txt\n+\t[http \"https://averylonguser@example.com/\"]\n+\t\tcookieFile = /tmp/averylonguser.txt\n+\tEOF\n+\n+\techo http.cookiefile /tmp/root.txt >expect &&\n+\tgit config --get-urlmatch HTTP https://example.com >actual &&\n+\ttest_cmp expect actual &&\n+\n+\techo http.cookiefile /tmp/subdirectory.txt >expect &&\n+\tgit config --get-urlmatch HTTP https://example.com/subdirectory >actual &&\n+\ttest_cmp expect actual &&\n+\n+\techo http.cookiefile /tmp/subdirectory.txt >expect &&\n+\tgit config --get-urlmatch HTTP https://example.com/subdirectory/nested >actual &&\n+\ttest_cmp expect actual &&\n+\n+\techo http.cookiefile /tmp/user.txt >expect &&\n+\tgit config --get-urlmatch HTTP https://user@example.com/ >actual &&\n+\ttest_cmp expect actual &&\n+\n+\techo http.cookiefile /tmp/subdirectory.txt >expect &&\n+\tgit config --get-urlmatch HTTP https://averylonguser@example.com/subdirectory >actual &&\n+\ttest_cmp expect actual\n+'\n+\n # good section hygiene\n test_expect_failure 'unsetting the last key in a section removes header' '\n \tcat >.git/config <<-\\EOF &&\ndiff --git a/urlmatch.c b/urlmatch.c\nindex e328905eb..f79887825 100644\n--- a/urlmatch.c\n+++ b/urlmatch.c\n@@ -426,7 +426,7 @@ static size_t url_match_prefix(const char *url,\n \n static int match_urls(const struct url_info *url,\n \t\t      const struct url_info *url_prefix,\n-\t\t      int *exactusermatch)\n+\t\t      struct urlmatch_item *match)\n {\n \t/*\n \t * url_prefix matches url if the scheme, host and port of url_prefix\n@@ -445,8 +445,8 @@ static int match_urls(const struct url_info *url,\n \t * contained a user name or false if url_prefix did not have a\n \t * user name.  If there is no match *exactusermatch is left untouched.\n \t */\n-\tint usermatched = 0;\n-\tint pathmatchlen;\n+\tchar usermatched = 0;\n+\tsize_t pathmatchlen;\n \n \tif (!url || !url_prefix || !url->url || !url_prefix->url)\n \t\treturn 0;\n@@ -483,22 +483,38 @@ static int match_urls(const struct url_info *url,\n \t\turl->url + url->path_off,\n \t\turl_prefix->url + url_prefix->path_off,\n \t\turl_prefix->url_len - url_prefix->path_off);\n+\tif (!pathmatchlen)\n+\t\treturn 0; /* paths do not match */\n \n-\tif (pathmatchlen && exactusermatch)\n-\t\t*exactusermatch = usermatched;\n-\treturn pathmatchlen;\n+\tif (match) {\n+\t\tmatch->hostmatch_len = url_prefix->host_len;\n+\t\tmatch->pathmatch_len = pathmatchlen;\n+\t\tmatch->user_matched = usermatched;\n+\t}\n+\n+\treturn 1;\n+}\n+\n+static int cmp_matches(const struct urlmatch_item *a,\n+\t\t       const struct urlmatch_item *b)\n+{\n+\tif (a->hostmatch_len != b->hostmatch_len)\n+\t\treturn a->hostmatch_len < b->hostmatch_len ? -1 : 1;\n+\tif (a->pathmatch_len != b->pathmatch_len)\n+\t\treturn a->pathmatch_len < b->pathmatch_len ? -1 : 1;\n+\tif (a->user_matched != b->user_matched)\n+\t\treturn b->user_matched ? -1 : 1;\n+\treturn 0;\n }\n \n int urlmatch_config_entry(const char *var, const char *value, void *cb)\n {\n \tstruct string_list_item *item;\n \tstruct urlmatch_config *collect = cb;\n-\tstruct urlmatch_item *matched;\n+\tstruct urlmatch_item matched = {0};\n \tstruct url_info *url = &collect->url;\n \tconst char *key, *dot;\n \tstruct strbuf synthkey = STRBUF_INIT;\n-\tsize_t matched_len = 0;\n-\tint user_matched = 0;\n \tint retval;\n \n \tif (!skip_prefix(var, collect->section, &key) || *(key++) != '.') {\n@@ -516,9 +532,9 @@ int urlmatch_config_entry(const char *var, const char *value, void *cb)\n \t\tfree(config_url);\n \t\tif (!norm_url)\n \t\t\treturn 0;\n-\t\tmatched_len = match_urls(url, &norm_info, &user_matched);\n+\t\tretval = match_urls(url, &norm_info, &matched);\n \t\tfree(norm_url);\n-\t\tif (!matched_len)\n+\t\tif (!retval)\n \t\t\treturn 0;\n \t\tkey = dot + 1;\n \t}\n@@ -528,24 +544,17 @@ int urlmatch_config_entry(const char *var, const char *value, void *cb)\n \n \titem = string_list_insert(&collect->vars, key);\n \tif (!item->util) {\n-\t\tmatched = xcalloc(1, sizeof(*matched));\n-\t\titem->util = matched;\n+\t\titem->util = xcalloc(1, sizeof(matched));\n \t} else {\n-\t\tmatched = item->util;\n-\t\t/*\n-\t\t * Is our match shorter?  Is our match the same\n-\t\t * length, and without user while the current\n-\t\t * candidate is with user?  Then we cannot use it.\n-\t\t */\n-\t\tif (matched_len < matched->matched_len ||\n-\t\t    ((matched_len == matched->matched_len) &&\n-\t\t     (!user_matched && matched->user_matched)))\n+\t\tif (cmp_matches(&matched, item->util) <= 0)\n+\t\t\t /*\n+\t\t\t  * Our match is worse than the old one,\n+\t\t\t  * we cannot use it.\n+\t\t\t  */\n \t\t\treturn 0;\n-\t\t/* Otherwise, replace it with this one. */\n \t}\n \n-\tmatched->matched_len = matched_len;\n-\tmatched->user_matched = user_matched;\n+\tmemcpy(item->util, &matched, sizeof(matched));\n \tstrbuf_addstr(&synthkey, collect->section);\n \tstrbuf_addch(&synthkey, '.');\n \tstrbuf_addstr(&synthkey, key);\ndiff --git a/urlmatch.h b/urlmatch.h\nindex 0ea812b03..37ee5da85 100644\n--- a/urlmatch.h\n+++ b/urlmatch.h\n@@ -34,7 +34,8 @@ struct url_info {\n extern char *url_normalize(const char *, struct url_info *);\n \n struct urlmatch_item {\n-\tsize_t matched_len;\n+\tsize_t hostmatch_len;\n+\tsize_t pathmatch_len;\n \tchar user_matched;\n };\n \n-- \n2.11.0\n\n"}]}