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