[PATCH 1/3] urlmatch: normalize ssh and ftp default ports

Fabian Pottbäcker <[email protected]>
Newsgroups org.kernel.vger.git
Message-ID <[email protected]>
These protocols are still supported by git and have well known
default ports. This leaves FTPS, which does not have one default
port.

Signed-off-by: Fabian Pottbäcker <[email protected]>
---
 t/unit-tests/u-urlmatch-normalization.c |  9 +++++++++
 urlmatch.c                              | 16 ++++++++++++----
 2 files changed, 21 insertions(+), 4 deletions(-)

diff --git a/t/unit-tests/u-urlmatch-normalization.c b/t/unit-tests/u-urlmatch-normalization.c
index 3595d893a2..1808e5e51f 100644
--- a/t/unit-tests/u-urlmatch-normalization.c
+++ b/t/unit-tests/u-urlmatch-normalization.c
@@ -141,9 +141,18 @@ void test_urlmatch_normalization__port_normalization(void)
 	check_normalized_url("http://x:80", "http://x/");
 	check_normalized_url("http://x:080", "http://x/");
 	check_normalized_url("http://x:000000080", "http://x/");
+	check_normalized_url("https://x:8443", "https://x:8443/");
 	check_normalized_url("https://x:443", "https://x/");
 	check_normalized_url("https://x:0443", "https://x/");
 	check_normalized_url("https://x:000000443", "https://x/");
+	check_normalized_url("ftp://x:2121", "ftp://x:2121/");
+	check_normalized_url("ftp://x:21", "ftp://x/");
+	check_normalized_url("ftp://x:021", "ftp://x/");
+	check_normalized_url("ftp://x:00000021", "ftp://x/");
+	check_normalized_url("ssh://x:2222", "ssh://x:2222/");
+	check_normalized_url("ssh://x:22", "ssh://x/");
+	check_normalized_url("ssh://x:022", "ssh://x/");
+	check_normalized_url("ssh://x:00000022", "ssh://x/");
 }
 
 void test_urlmatch_normalization__general_escape(void)
diff --git a/urlmatch.c b/urlmatch.c
index 20bc2d009c..0c2ddf2e40 100644
--- a/urlmatch.c
+++ b/urlmatch.c
@@ -274,12 +274,20 @@ static char *url_normalize_1(const char *url, struct url_info *out_info, bool al
 		if (url == slash_ptr) {
 			/* Skip ":" port with no number, it's same as default */
 		} else if (slash_ptr - url == 2 &&
-			   starts_with(norm.buf, "http:") &&
-			   !strncmp(url, "80", 2)) {
+			    starts_with(norm.buf, "ftp:") &&
+			    !strncmp(url, "21", 2)) {
+			/* Skip http :21 as it's the default */
+		} else if (slash_ptr - url == 2 &&
+			    starts_with(norm.buf, "ssh:") &&
+			    !strncmp(url, "22", 2)) {
+			/* Skip http :22 as it's the default */
+		} else if (slash_ptr - url == 2 &&
+			    starts_with(norm.buf, "http:") &&
+			    !strncmp(url, "80", 2)) {
 			/* Skip http :80 as it's the default */
 		} else if (slash_ptr - url == 3 &&
-			   starts_with(norm.buf, "https:") &&
-			   !strncmp(url, "443", 3)) {
+			    starts_with(norm.buf, "https:") &&
+			    !strncmp(url, "443", 3)) {
 			/* Skip https :443 as it's the default */
 		} else {
 			/*
-- 
2.50.1 (Apple Git-155)
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.