{"thread":{"id":"44966","subject":"[PATCH v3 2/4] urlmatch: enable normalization of URLs with globs","startedAt":"2017-01-25T09:56:59Z","lastAt":"2017-01-27T17:47:13Z","messageCount":10,"participants":["Patrick Steinhardt","Junio C Hamano"],"isPatch":true,"patchVersion":3,"patchTotal":4},"messages":[{"id":"310144","messageId":"20170125095648.4116-3-patrick.steinhardt@elego.de","threadId":"44966","inReplyTo":"20170125095648.4116-1-patrick.steinhardt@elego.de","subject":"[PATCH v3 2/4] urlmatch: enable normalization of URLs with globs","fromName":"Patrick Steinhardt","fromEmail":"patrick.steinhardt@elego.de","sentAt":"2017-01-25T09:56:46Z","receivedAt":"2017-01-25T09:56:59Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"The `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":"310145","messageId":"20170125095648.4116-1-patrick.steinhardt@elego.de","threadId":"44966","inReplyTo":"20170123130635.29577-1-patrick.steinhardt@elego.de","subject":"[PATCH v3 0/4] urlmatch: allow regexp-based matches","fromName":"Patrick Steinhardt","fromEmail":"patrick.steinhardt@elego.de","sentAt":"2017-01-25T09:56:44Z","receivedAt":"2017-01-25T09:57:02Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"Hi,\n\nThis is version three of my patch series. The previous version\ncan be found at [1]. The use case is to be able to configure an\nHTTP proxy for all subdomains of a domain where there are\nhundreds of subdomains.\n\nThis version addresses a comment by Philip Oakley regarding the\ndocumentation. You can find the interdiff below.\n\nRegards\nPatrick\n\n[1]: http://public-inbox.org/git/20170124170031.18069-1-patrick.steinhardt@elego.de/T/#u\n\nPatrick Steinhardt (4):\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: allow globbing for the URL host part\n\n .mailmap                 |  1 +\n Documentation/config.txt |  5 +++-\n t/t1300-repo-config.sh   | 36 +++++++++++++++++++++++++\n urlmatch.c               | 68 +++++++++++++++++++++++++++++++++++++++++-------\n urlmatch.h               |  9 ++++---\n 5 files changed, 104 insertions(+), 15 deletions(-)\n\ndiff --git a/Documentation/config.txt b/Documentation/config.txt\nindex a78921c2b..078e9b490 100644\n--- a/Documentation/config.txt\n+++ b/Documentation/config.txt\n@@ -1915,9 +1915,9 @@ http.<url>.*::\n \n . Host/domain name (e.g., `example.com` in `https://example.com/`).\n   This field must match between the config key and the URL. It is\n-  possible to use globs in the config key to match all subdomains, e.g.\n-  `https://*.example.com/` to match all subdomains of `example.com`. Note\n-  that a glob only every matches a single part of the hostname.\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.\n-- \n2.11.0\n"},{"id":"310146","messageId":"20170125095648.4116-5-patrick.steinhardt@elego.de","threadId":"44966","inReplyTo":"20170125095648.4116-1-patrick.steinhardt@elego.de","subject":"[PATCH v3 4/4] urlmatch: allow globbing for the URL host part","fromName":"Patrick Steinhardt","fromEmail":"patrick.steinhardt@elego.de","sentAt":"2017-01-25T09:56:48Z","receivedAt":"2017-01-25T09:57:05Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"The 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\nThis commit introduces the ability to use globbing in the host-part of\nthe URLs. A user can simply specify a `*` as part of the host name to\nmatch all subdomains at this level. For example adding a configuration\nkey `http.https://*.example.com.proxy` will match all subdomains of\n`https://example.com`.\n\nSigned-off-by: Patrick Steinhardt <patrick.steinhardt@elego.de>\n---\n Documentation/config.txt |  5 ++++-\n t/t1300-repo-config.sh   | 36 ++++++++++++++++++++++++++++++++++++\n urlmatch.c               | 38 ++++++++++++++++++++++++++++++++++----\n 3 files changed, 74 insertions(+), 5 deletions(-)\n\ndiff --git a/Documentation/config.txt b/Documentation/config.txt\nindex 506431267..078e9b490 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 923bfc5a2..ec545e092 100755\n--- a/t/t1300-repo-config.sh\n+++ b/t/t1300-repo-config.sh\n@@ -1177,6 +1177,42 @@ test_expect_success 'urlmatch' '\n \ttest_cmp expect actual\n '\n \n+test_expect_success 'glob-based urlmatch' '\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+\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..53ff972a6 100644\n--- a/urlmatch.c\n+++ b/urlmatch.c\n@@ -63,6 +63,38 @@ static int append_normalized_escapes(struct strbuf *buf,\n \treturn 1;\n }\n \n+static int match_host(const struct url_info *url_info,\n+\t\t      const struct url_info *pattern_info)\n+{\n+\tchar *url = xmemdupz(url_info->url + url_info->host_off, url_info->host_len);\n+\tchar *pat = xmemdupz(pattern_info->url + pattern_info->host_off, pattern_info->host_len);\n+\tchar *url_tok, *pat_tok, *url_save, *pat_save;\n+\tint matching;\n+\n+\turl_tok = strtok_r(url, \".\", &url_save);\n+\tpat_tok = strtok_r(pat, \".\", &pat_save);\n+\n+\tfor (; url_tok && pat_tok; url_tok = strtok_r(NULL, \".\", &url_save),\n+\t\t\t\t   pat_tok = strtok_r(NULL, \".\", &pat_save)) {\n+\t\tif (!strcmp(pat_tok, \"*\"))\n+\t\t\tcontinue; /* a simple glob matches everything */\n+\n+\t\tif (strcmp(url_tok, pat_tok)) {\n+\t\t\t/* subdomains do not match */\n+\t\t\tmatching = 0;\n+\t\t\tbreak;\n+\t\t}\n+\t}\n+\n+\t/* matching if both URL and pattern are at their ends */\n+\tmatching = (url_tok == NULL && pat_tok == NULL);\n+\n+\tfree(url);\n+\tfree(pat);\n+\n+\treturn matching;\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 +499,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@@ -512,7 +542,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":"310147","messageId":"20170125095648.4116-2-patrick.steinhardt@elego.de","threadId":"44966","inReplyTo":"20170125095648.4116-1-patrick.steinhardt@elego.de","subject":"[PATCH v3 1/4] mailmap: add Patrick Steinhardt's work address","fromName":"Patrick Steinhardt","fromEmail":"patrick.steinhardt@elego.de","sentAt":"2017-01-25T09:56:45Z","receivedAt":"2017-01-25T09:57:09Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"Signed-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":"310148","messageId":"20170125095648.4116-4-patrick.steinhardt@elego.de","threadId":"44966","inReplyTo":"20170125095648.4116-1-patrick.steinhardt@elego.de","subject":"[PATCH v3 3/4] urlmatch: split host and port fields in `struct url_info`","fromName":"Patrick Steinhardt","fromEmail":"patrick.steinhardt@elego.de","sentAt":"2017-01-25T09:56:47Z","receivedAt":"2017-01-25T09:57:12Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"The `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":"310355","messageId":"xmqq7f5h4kng.fsf@gitster.mtv.corp.google.com","threadId":"44966","inReplyTo":"20170125095648.4116-5-patrick.steinhardt@elego.de","subject":"Re: [PATCH v3 4/4] urlmatch: allow globbing for the URL host part","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2017-01-26T20:43:31Z","receivedAt":"2017-01-26T20:47:44Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Patrick Steinhardt <patrick.steinhardt@elego.de> writes:\n\n> The URL matching function computes for two URLs whether they match not.\n> The match is performed by splitting up the URL into different parts and\n> then doing an exact comparison with the to-be-matched URL.\n>\n> The main user of `urlmatch` is the configuration subsystem. It allows to\n> set certain configurations based on the URL which is being connected to\n> via keys like `http.<url>.*`. A common use case for this is to set\n> proxies for only some remotes which match the given URL. Unfortunately,\n> having exact matches for all parts of the URL can become quite tedious\n> in some setups. Imagine for example a corporate network where there are\n> dozens or even hundreds of subdomains, which would have to be configured\n> individually.\n>\n> This commit introduces the ability to use globbing in the host-part of\n> the URLs. A user can simply specify a `*` as part of the host name to\n> match all subdomains at this level. For example adding a configuration\n> key `http.https://*.example.com.proxy` will match all subdomains of\n> `https://example.com`.\n\nThis is probably a useful improvement.\n\nHaving said that, when I mentioned \"glob\", I meant to also support\nsomething like this:\n\n\thttps://www[1-4].ibm.com/\n\nAnd when people read \"glob\", that is what they expect.\n\nSo calling this \"the ability to use globbing\" is misleading.\nThe last paragraph in the log message above needs a bit of\ntweaking, perhaps like this:\n\n\tAllow users to write an asterisk '*' in place of any 'host'\n\tor 'subdomain' label as part of the host name.  For example,\n\t\"http.https://*.example.com.proxy\" sets \"http.proxy\" for all\n\tdirect subdomains of \"https://example.com\",\n\te.g. \"https://foo.example.com\", but not\n\t\"https://foo.bar.example.com\".\n\nFortunately, your update to config.txt, which is facing the end\nusers, does not misuse the word and instead is explicit that the\nonly thing the matcher does is to match '*' to a single hierarchy.\nIt is clear that even http://www*.ibm.com/ is not supported from\nthe description, which is good.\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\nThis is good as-is.\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.\n> diff --git a/t/t1300-repo-config.sh b/t/t1300-repo-config.sh\n> index 923bfc5a2..ec545e092 100755\n> --- a/t/t1300-repo-config.sh\n> +++ b/t/t1300-repo-config.sh\n> @@ -1177,6 +1177,42 @@ test_expect_success 'urlmatch' '\n>  \ttest_cmp expect actual\n>  '\n>  \n> +test_expect_success 'glob-based urlmatch' '\n\nThis is not \"glob\".  A more generic term \"wildcard\" is OK.\n\n> +\tcat >.git/config <<-\\EOF &&\n> +\t[http]\n> +\t\tsslVerify\n> ...\n> +static int match_host(const struct url_info *url_info,\n> +\t\t      const struct url_info *pattern_info)\n> +{\n> +\tchar *url = xmemdupz(url_info->url + url_info->host_off, url_info->host_len);\n> +\tchar *pat = xmemdupz(pattern_info->url + pattern_info->host_off, pattern_info->host_len);\n> +\tchar *url_tok, *pat_tok, *url_save, *pat_save;\n> +\tint matching;\n> +\n> +\turl_tok = strtok_r(url, \".\", &url_save);\n> +\tpat_tok = strtok_r(pat, \".\", &pat_save);\n\nHmph, this will be the first use of strtok_r() in our codebase.\nDoes everybody have it?\n\nFor a use like this where your delimiter set is a singleton, it may\nbe simpler to do the usual strchrnul() or memchr() based loop.  The\nattached is my attempt to do so on top of this patch.\n\n> +\n> +\tfor (; url_tok && pat_tok; url_tok = strtok_r(NULL, \".\", &url_save),\n> +\t\t\t\t   pat_tok = strtok_r(NULL, \".\", &pat_save)) {\n> +\t\tif (!strcmp(pat_tok, \"*\"))\n> +\t\t\tcontinue; /* a simple glob matches everything */\n\ns/glob/asterisk/\n\nOther than that, the patch looks OK.\n\n\n\ndiff --git a/urlmatch.c b/urlmatch.c\nindex 53ff972a60..8dfc7fd28a 100644\n--- a/urlmatch.c\n+++ b/urlmatch.c\n@@ -63,36 +63,47 @@ 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-\tchar *url = xmemdupz(url_info->url + url_info->host_off, url_info->host_len);\n-\tchar *pat = xmemdupz(pattern_info->url + pattern_info->host_off, pattern_info->host_len);\n-\tchar *url_tok, *pat_tok, *url_save, *pat_save;\n-\tint matching;\n-\n-\turl_tok = strtok_r(url, \".\", &url_save);\n-\tpat_tok = strtok_r(pat, \".\", &pat_save);\n-\n-\tfor (; url_tok && pat_tok; url_tok = strtok_r(NULL, \".\", &url_save),\n-\t\t\t\t   pat_tok = strtok_r(NULL, \".\", &pat_save)) {\n-\t\tif (!strcmp(pat_tok, \"*\"))\n-\t\t\tcontinue; /* a simple glob matches everything */\n-\n-\t\tif (strcmp(url_tok, pat_tok)) {\n-\t\t\t/* subdomains do not match */\n-\t\t\tmatching = 0;\n-\t\t\tbreak;\n-\t\t}\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-\t/* matching if both URL and pattern are at their ends */\n-\tmatching = (url_tok == NULL && pat_tok == NULL);\n-\n-\tfree(url);\n-\tfree(pat);\n-\n-\treturn matching;\n+\treturn 1;\n }\n \n static char *url_normalize_1(const char *url, struct url_info *out_info, char allow_globs)\n"},{"id":"310356","messageId":"xmqq37g54kdz.fsf@gitster.mtv.corp.google.com","threadId":"44966","inReplyTo":"xmqq7f5h4kng.fsf@gitster.mtv.corp.google.com","subject":"Re: [PATCH v3 4/4] urlmatch: allow globbing for the URL host part","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2017-01-26T20:49:12Z","receivedAt":"2017-01-26T20:50:18Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Junio C Hamano <gitster@pobox.com> writes:\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}\n>  \n> +\treturn 1;\n\nEmbarrassing.  The last one must be \"have they both run out?\" i.e.\n\n\treturn (!url_len && !pat_len);\n\n"},{"id":"310360","messageId":"xmqqo9yt34qw.fsf@gitster.mtv.corp.google.com","threadId":"44966","inReplyTo":"xmqq37g54kdz.fsf@gitster.mtv.corp.google.com","subject":"Re: [PATCH v3 4/4] urlmatch: allow globbing for the URL host part","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2017-01-26T21:12:23Z","receivedAt":"2017-01-26T21:12:31Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Junio C Hamano <gitster@pobox.com> writes:\n\n> Junio C Hamano <gitster@pobox.com> writes:\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}\n>>  \n>> +\treturn 1;\n>\n> Embarrassing.  The last one must be \"have they both run out?\" i.e.\n>\n> \treturn (!url_len && !pat_len);\n\nOK, here is my second try.  The added test piece is to catch the\nsilly mistake I made above.\n\ndiff --git a/t/t1300-repo-config.sh b/t/t1300-repo-config.sh\nindex ec545e0929..33fd59fbb3 100755\n--- a/t/t1300-repo-config.sh\n+++ b/t/t1300-repo-config.sh\n@@ -1177,7 +1177,7 @@ test_expect_success 'urlmatch' '\n \ttest_cmp expect actual\n '\n \n-test_expect_success 'glob-based urlmatch' '\n+test_expect_success 'urlmatch with wildcard' '\n \tcat >.git/config <<-\\EOF &&\n \t[http]\n \t\tsslVerify\n@@ -1210,6 +1210,10 @@ test_expect_success 'glob-based urlmatch' '\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 53ff972a60..0e007a3e07 100644\n--- a/urlmatch.c\n+++ b/urlmatch.c\n@@ -63,36 +63,47 @@ 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-\tchar *url = xmemdupz(url_info->url + url_info->host_off, url_info->host_len);\n-\tchar *pat = xmemdupz(pattern_info->url + pattern_info->host_off, pattern_info->host_len);\n-\tchar *url_tok, *pat_tok, *url_save, *pat_save;\n-\tint matching;\n-\n-\turl_tok = strtok_r(url, \".\", &url_save);\n-\tpat_tok = strtok_r(pat, \".\", &pat_save);\n-\n-\tfor (; url_tok && pat_tok; url_tok = strtok_r(NULL, \".\", &url_save),\n-\t\t\t\t   pat_tok = strtok_r(NULL, \".\", &pat_save)) {\n-\t\tif (!strcmp(pat_tok, \"*\"))\n-\t\t\tcontinue; /* a simple glob matches everything */\n-\n-\t\tif (strcmp(url_tok, pat_tok)) {\n-\t\t\t/* subdomains do not match */\n-\t\t\tmatching = 0;\n-\t\t\tbreak;\n-\t\t}\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-\t/* matching if both URL and pattern are at their ends */\n-\tmatching = (url_tok == NULL && pat_tok == NULL);\n-\n-\tfree(url);\n-\tfree(pat);\n-\n-\treturn matching;\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"},{"id":"310386","messageId":"20170127062142.GA760@pks-pc","threadId":"44966","inReplyTo":"20170125095648.4116-5-patrick.steinhardt@elego.de","subject":"Re: [PATCH v3 4/4] urlmatch: allow globbing for the URL host part","fromName":"Patrick Steinhardt","fromEmail":"patrick.steinhardt@elego.de","sentAt":"2017-01-27T06:21:42Z","receivedAt":"2017-01-27T06:21:56Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"On Thu, Jan 26, 2017 at 12:43:31PM -0800, Junio C Hamano wrote:\n> Patrick Steinhardt <patrick.steinhardt@elego.de> writes:\n> \n> > The URL matching function computes for two URLs whether they match not.\n> > The match is performed by splitting up the URL into different parts and\n> > then doing an exact comparison with the to-be-matched URL.\n> >\n> > The main user of `urlmatch` is the configuration subsystem. It allows to\n> > set certain configurations based on the URL which is being connected to\n> > via keys like `http.<url>.*`. A common use case for this is to set\n> > proxies for only some remotes which match the given URL. Unfortunately,\n> > having exact matches for all parts of the URL can become quite tedious\n> > in some setups. Imagine for example a corporate network where there are\n> > dozens or even hundreds of subdomains, which would have to be configured\n> > individually.\n> >\n> > This commit introduces the ability to use globbing in the host-part of\n> > the URLs. A user can simply specify a `*` as part of the host name to\n> > match all subdomains at this level. For example adding a configuration\n> > key `http.https://*.example.com.proxy` will match all subdomains of\n> > `https://example.com`.\n> \n> This is probably a useful improvement.\n> \n> Having said that, when I mentioned \"glob\", I meant to also support\n> something like this:\n> \n> \thttps://www[1-4].ibm.com/\n\nThe problem with additional extended syntax like proposed by you\nis that we would indeed need an escaping mechanism here. '[]' are\nalready allowed inside the host part to enable IPv6 hosts of the\nform 'https://[2001:0db8:]/', so the syntax is now ambiguous. So\nwe have to be cautios which characters to enable for globbing\nsyntax. As of now, I think we can only safely include '*' and '?'\nhere without escaping mechanisms.\n\nIf additional use cases come up we might still extend the syntax\nlater on to allow for more special syntax.\n\n> And when people read \"glob\", that is what they expect.\n> \n> So calling this \"the ability to use globbing\" is misleading.\n> The last paragraph in the log message above needs a bit of\n> tweaking, perhaps like this:\n> \n> \tAllow users to write an asterisk '*' in place of any 'host'\n> \tor 'subdomain' label as part of the host name.  For example,\n> \t\"http.https://*.example.com.proxy\" sets \"http.proxy\" for all\n> \tdirect subdomains of \"https://example.com\",\n> \te.g. \"https://foo.example.com\", but not\n> \t\"https://foo.bar.example.com\".\n> \n> Fortunately, your update to config.txt, which is facing the end\n> users, does not misuse the word and instead is explicit that the\n> only thing the matcher does is to match '*' to a single hierarchy.\n> It is clear that even http://www*.ibm.com/ is not supported from\n> the description, which is good.\n\nI agree that globbing is the wrong word here. I'll swap in\n\"wildcard\" where applicable.\n\nI'll send a version 4 later on. Thanks again for your feedback\nand improvements.\n\nRegards\nPatrick\n"},{"id":"310419","messageId":"xmqq1svo1jne.fsf@gitster.mtv.corp.google.com","threadId":"44966","inReplyTo":"20170127062142.GA760@pks-pc","subject":"Re: [PATCH v3 4/4] urlmatch: allow globbing for the URL host part","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2017-01-27T17:45:41Z","receivedAt":"2017-01-27T17:47:13Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Patrick Steinhardt <patrick.steinhardt@elego.de> writes:\n\n>> This is probably a useful improvement.\n>> \n>> Having said that, when I mentioned \"glob\", I meant to also support\n>> something like this:\n>> \n>> \thttps://www[1-4].ibm.com/\n>\n> The problem with additional extended syntax like proposed by you\n> is that we would indeed need an escaping mechanism here.\n\nTrue.  I think a true shell globbing is overkill (so is regexp) and\njust a simple wildcarding with '*' would be a good first step that\nis easy to explain and later extend as needed.\n\nThanks.\n"}]}