Skip to content

Commit 70fb85d

Browse files
authored
opt: use net/url for validating network addresses (#728)
1 parent 2a31b19 commit 70fb85d

File tree

6 files changed

+23
-18
lines changed

6 files changed

+23
-18
lines changed

.github/workflows/cross-compile-bsd.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ jobs:
3131
strategy:
3232
fail-fast: false
3333
matrix:
34-
go: ['1.20', '1.24']
34+
go: ['1.20', '1.25']
3535
os:
3636
- ubuntu-latest
3737
name: Go ${{ matrix.go }} @ ${{ matrix.os }}

.github/workflows/test.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@ jobs:
5656
strategy:
5757
fail-fast: false
5858
matrix:
59-
go: ['1.20', '1.24']
59+
go: ['1.20', '1.25']
6060
os:
6161
- ubuntu-latest
62-
- macos-latest
62+
- macos-14 # check out this issue: https://github.com/actions/runner-images/issues/10924
6363
- windows-latest
6464
name: Go ${{ matrix.go }} @ ${{ matrix.os }}
6565
runs-on: ${{ matrix.os }}

.github/workflows/test_gc_opt.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@ jobs:
5656
strategy:
5757
fail-fast: false
5858
matrix:
59-
go: ['1.20', '1.24']
59+
go: ['1.20', '1.25']
6060
os:
6161
- ubuntu-latest
62-
- macos-latest
62+
- macos-14 # check out this issue: https://github.com/actions/runner-images/issues/10924
6363
- windows-latest
6464
name: Go ${{ matrix.go }} @ ${{ matrix.os }}
6565
runs-on: ${{ matrix.os }}

.github/workflows/test_poll_opt.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ jobs:
5555
strategy:
5656
fail-fast: false
5757
matrix:
58-
go: ['1.20', '1.24']
59-
os: [ubuntu-latest, macos-latest]
58+
go: ['1.20', '1.25']
59+
os: [ubuntu-latest, macos-14] # check out this issue: https://github.com/actions/runner-images/issues/10924
6060
name: Go ${{ matrix.go }} @ ${{ matrix.os }}
6161
runs-on: ${{ matrix.os }}
6262
steps:

.github/workflows/test_poll_opt_gc_opt.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ jobs:
5555
strategy:
5656
fail-fast: false
5757
matrix:
58-
go: ['1.20', '1.24']
59-
os: [ubuntu-latest, macos-latest]
58+
go: ['1.20', '1.25']
59+
os: [ubuntu-latest, macos-14] # check out this issue: https://github.com/actions/runner-images/issues/10924
6060
name: Go ${{ matrix.go }} @ ${{ matrix.os }}
6161
runs-on: ${{ matrix.os }}
6262
steps:

gnet.go

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"context"
2323
"io"
2424
"net"
25+
"net/url"
2526
"runtime"
2627
"strings"
2728
"sync"
@@ -746,21 +747,25 @@ func Stop(ctx context.Context, protoAddr string) error {
746747
}
747748

748749
func parseProtoAddr(protoAddr string) (string, string, error) {
749-
protoAddr = strings.ToLower(protoAddr)
750-
if strings.Count(protoAddr, "://") != 1 {
750+
// Percent-encode "%" in the address to avoid url.Parse error.
751+
// For example: udp://[ff02::3%lo0]:9991
752+
protoAddr = strings.ReplaceAll(protoAddr, "%", "%25")
753+
754+
u, err := url.Parse(protoAddr)
755+
if err != nil {
756+
return "", "", err
757+
}
758+
759+
if u.Scheme == "" || u.Host == "" || u.Path != "" {
751760
return "", "", errorx.ErrInvalidNetworkAddress
752761
}
753-
pair := strings.SplitN(protoAddr, "://", 2)
754-
proto, addr := pair[0], pair[1]
755-
switch proto {
762+
switch u.Scheme {
756763
case "tcp", "tcp4", "tcp6", "udp", "udp4", "udp6", "unix":
757764
default:
758765
return "", "", errorx.ErrUnsupportedProtocol
759766
}
760-
if addr == "" {
761-
return "", "", errorx.ErrInvalidNetworkAddress
762-
}
763-
return proto, addr, nil
767+
768+
return u.Scheme, u.Host, nil
764769
}
765770

766771
func determineEventLoops(opts *Options) int {

0 commit comments

Comments
 (0)