diff options
author | Sergey Ponomarev <stokito@gmail.com> | 2022-11-28 09:06:09 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-11-28 08:06:09 +0100 |
commit | c50de95952581388eec6e67a8f7a651d80c37a0b (patch) | |
tree | 5fdc17e9a98f881b823f2ae1c10489abf779f6a6 /client.go | |
parent | feat: add ShutdownWithContext (#1383) (diff) | |
download | fasthttp-c50de95952581388eec6e67a8f7a651d80c37a0b.tar.gz fasthttp-c50de95952581388eec6e67a8f7a651d80c37a0b.tar.bz2 fasthttp-c50de95952581388eec6e67a8f7a651d80c37a0b.zip |
client.go fix addMissingPort() (#1444)
* client.go Make addMissingPort() public
It's needed for those who creates the instance of the HostClient manually.
* client.go fix AddMissingPort()
Previously for IPv6 addresses the default port wasn't added.
The fix adding a test and optimization that should avoid itoa() call and reduce a memory usage
Diffstat (limited to 'client.go')
-rw-r--r-- | client.go | 34 |
1 files changed, 25 insertions, 9 deletions
@@ -9,7 +9,6 @@ import ( "fmt" "io" "net" - "strconv" "strings" "sync" "sync/atomic" @@ -495,7 +494,7 @@ func (c *Client) Do(req *Request, resp *Response) error { hc := m[string(host)] if hc == nil { hc = &HostClient{ - Addr: addMissingPort(string(host), isTLS), + Addr: AddMissingPort(string(host), isTLS), Name: c.Name, NoDefaultUserAgentHeader: c.NoDefaultUserAgentHeader, Dial: c.Dial, @@ -1968,7 +1967,7 @@ func dialAddr(addr string, dial DialFunc, dialDualStack, isTLS bool, tlsConfig * } else { dial = Dial } - addr = addMissingPort(addr, isTLS) + addr = AddMissingPort(addr, isTLS) } conn, err := dial(addr) if err != nil { @@ -2006,16 +2005,33 @@ func (c *HostClient) getClientName() []byte { return clientName } -func addMissingPort(addr string, isTLS bool) string { - n := strings.Index(addr, ":") - if n >= 0 { +// 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 { + addrLen := len(addr) + if addrLen == 0 { return addr } - port := 80 + + isIp6 := addr[0] == '[' + if isIp6 { + // if the IPv6 has opening bracket but closing bracket is the last char then it doesn't have a port + isIp6WithoutPort := addr[addrLen-1] == ']' + if !isIp6WithoutPort { + return addr + } + } else { // IPv4 + columnPos := strings.LastIndexByte(addr, ':') + if columnPos > 0 { + return addr + } + } + port := ":80" if isTLS { - port = 443 + port = ":443" } - return net.JoinHostPort(addr, strconv.Itoa(port)) + return addr + port } // A wantConn records state about a wanted connection |