-
Notifications
You must be signed in to change notification settings - Fork 12
/
url.go
65 lines (57 loc) · 1.49 KB
/
url.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright 2020 The searKing Author. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package url
import (
"fmt"
"net/url"
"strconv"
)
func ParseStandardURL(rawUrl string) (*url.URL, error) {
u, err := url.Parse(rawUrl) // Just url_.Parse (url_ is shadowed for godoc).
if err != nil {
return nil, err
}
// The host's colon:port should be normalized. See Issue 14836.
u.Host = removeEmptyPort(u.Host)
return u, nil
}
var portMap = map[string]string{
"http": "80",
"https": "443",
"socks5": "1080",
"stun": "3478",
"turn": "3478",
"stuns": "5349",
"turns": "5349",
}
func ParseURL(rawUrl string, getDefaultPort func(schema string) (string, error)) (u *url.URL, host string, port int, err error) {
if getDefaultPort == nil {
getDefaultPort = func(schema string) (string, error) {
port, ok := portMap[schema]
if ok {
return port, nil
}
return "", fmt.Errorf("malformed schema:%s", schema)
}
}
standUrl, err := ParseStandardURL(rawUrl)
if err != nil {
return nil, "", -1, err
}
var hostport string
if standUrl.Opaque == "" {
hostport = standUrl.Host
} else {
hostport = standUrl.Opaque
}
rawHost, rawPort, err := ParseHostPort(standUrl.Scheme, hostport, getDefaultPort)
if err != nil {
return nil, "", -1, err
}
host = rawHost
if port, err = strconv.Atoi(rawPort); err != nil {
return nil, "", -1, fmt.Errorf("malformed port:%s", rawPort)
}
return standUrl, host, port, nil
}