Repository: cloudwego/hertz
Pull request #203 (2022-08-18 07:52:42)

add v6 support for AddMissingPort function
#### What type of PR is this?

fix

#### What this PR does / why we need it (English/Chinese):

EN: add support for bare v6 address in AddMissingPort function
ZH：AddMissingPort函数增加对裸v6地址的处理


codecov[bot]: # [Codecov](https://codecov.io/gh/cloudwego/hertz/pull/203?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cloudwego) Report
> Merging [#203](https://codecov.io/gh/cloudwego/hertz/pull/203?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cloudwego) (94bebf8) into [develop](https://codecov.io/gh/cloudwego/hertz/commit/86974815dd1f60b67761fa6a21b704fc50a67a19?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cloudwego) (8697481) will **decrease** coverage by `0.04%`.
> The diff coverage is `100.00%`.

```diff
@@             Coverage Diff             @@
##           develop     #203      +/-   ##
===========================================
- Coverage    59.03%   58.98%   -0.05%     
===========================================
  Files           79       79              
  Lines         7997     7998       +1     
===========================================
- Hits          4721     4718       -3     
- Misses        2928     2932       +4     
  Partials       348      348              
```


| [Impacted Files](https://codecov.io/gh/cloudwego/hertz/pull/203?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cloudwego) | Coverage Δ | |
|---|---|---|
| [pkg/common/utils/path.go](https://codecov.io/gh/cloudwego/hertz/pull/203/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cloudwego#diff-cGtnL2NvbW1vbi91dGlscy9wYXRoLmdv) | `82.60% <100.00%> (+0.51%)` | :arrow_up: |
| [pkg/common/timer/timer.go](https://codecov.io/gh/cloudwego/hertz/pull/203/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cloudwego#diff-cGtnL2NvbW1vbi90aW1lci90aW1lci5nbw==) | `61.90% <0.00%> (-19.05%)` | :arrow_down: |
| [pkg/network/standard/buffer.go](https://codecov.io/gh/cloudwego/hertz/pull/203/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cloudwego#diff-cGtnL25ldHdvcmsvc3RhbmRhcmQvYnVmZmVyLmdv) | `81.08% <0.00%> (-0.50%)` | :arrow_down: |

Help us with your feedback. Take ten seconds to tell us [how you rate us](https://about.codecov.io/nps?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cloudwego). Have a feature suggestion? [Share it here.](https://app.codecov.io/gh/feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cloudwego)


Patch:
diff --git a/pkg/common/utils/path.go b/pkg/common/utils/path.go
index a162f11..fa2b176 100644
--- a/pkg/common/utils/path.go
+++ b/pkg/common/utils/path.go
@@ -186,9 +186,16 @@ func bufApp(buf *[]byte, s string, w int, c byte) {
 	b[w] = c
 }
 
+// AddMissingPort adds a port to a host if it is missing.
+// A literal IPv6 address in hostport must be enclosed in square
+// brackets, as in "[::1]:80", "[::1%lo0]:80".
 func AddMissingPort(addr string, isTLS bool) string {
 	if strings.IndexByte(addr, ':') >= 0 {
-		return addr
+		endOfV6 := strings.IndexByte(addr, ']')
+		// we do not care about the validity of the address, just check if it has more bytes after ']'
+		if endOfV6 < len(addr)-1 {
+			return addr
+		}
 	}
 	if !isTLS {
 		return addr + ":80"


Test patch:
diff --git a/pkg/common/utils/path_test.go b/pkg/common/utils/path_test.go
index 5c0d539..475e439 100644
--- a/pkg/common/utils/path_test.go
+++ b/pkg/common/utils/path_test.go
@@ -98,26 +98,20 @@ func TestPathCleanPath(t *testing.T) {
 
 // The Function AddMissingPort can only add the missed port, don't consider the other error case.
 func TestPathAddMissingPort(t *testing.T) {
-	isTLS := true
-	notTLS := false
-	ipAddress := "127.0.0.1"
-	expectedNormalHTTPAddress := "127.0.0.1:80"
-	expectedNormalHTTPsAddress := "127.0.0.1:443"
-	httpAddress := AddMissingPort(ipAddress, notTLS)
-	httpsAddress := AddMissingPort(ipAddress, isTLS)
-
-	if httpAddress != expectedNormalHTTPAddress {
-		t.Fatalf("Unexpected address: %s. Expecting address: %s", httpAddress, expectedNormalHTTPAddress)
-	}
-	if httpsAddress != expectedNormalHTTPsAddress {
-		t.Fatalf("Unexpected address: %s. Expecting address: %s", httpsAddress, expectedNormalHTTPsAddress)
-	}
-
-	normalAddress := "127.0.0.1:80"
-	expectedNormalAddress := "127.0.0.1:80"
-	addAddress := AddMissingPort(normalAddress, notTLS)
-
-	if addAddress != expectedNormalAddress {
-		t.Fatalf("Unexpected address: %s. Expecting address: %s", addAddress, expectedNormalAddress)
+	ipList := []string{"127.0.0.1", "111.111.1.1", "[0:0:0:0:0:ffff:192.1.56.10]", "[0:0:0:0:0:ffff:c0a8:101]", "www.foobar.com"}
+	for _, ip := range ipList {
+		if AddMissingPort(ip, true) != ip+":443" {
+			t.Fatalf("Unexpected address: %s. Expecting address: %s", AddMissingPort(ip, true), ip+":443")
+		}
+		if AddMissingPort(ip, false) != ip+":80" {
+			t.Fatalf("Unexpected address: %s. Expecting address: %s", AddMissingPort(ip, false), ip+":80")
+		}
+		customizedPort := ":8080"
+		if AddMissingPort(ip+customizedPort, true) != ip+customizedPort {
+			t.Fatalf("Unexpected address: %s. Expecting address: %s", AddMissingPort(ip+customizedPort, false), ip+customizedPort)
+		}
+		if AddMissingPort(ip+customizedPort, false) != ip+customizedPort {
+			t.Fatalf("Unexpected address: %s. Expecting address: %s", AddMissingPort(ip+customizedPort, true), ip+customizedPort)
+		}
 	}
 }