github.com/cloudwego/kitex@v0.9.0/pkg/http/resolver.go (about) 1 /* 2 * Copyright 2021 CloudWeGo Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 // Package http is used to implement RPC over HTTP. 18 package http 19 20 import ( 21 "net" 22 "net/url" 23 "strconv" 24 ) 25 26 // Resolver resolves url to address. 27 type Resolver interface { 28 Resolve(string) (string, error) 29 } 30 31 type defaultResolver struct{} 32 33 // NewDefaultResolver creates a default resolver. 34 func NewDefaultResolver() Resolver { 35 return &defaultResolver{} 36 } 37 38 // Resolve implements the Resolver interface. 39 func (p *defaultResolver) Resolve(URL string) (string, error) { 40 pu, err := url.Parse(URL) 41 if err != nil { 42 return "", err 43 } 44 host := pu.Hostname() 45 port := pu.Port() 46 if port == "" { 47 port = "443" 48 if pu.Scheme == "http" { 49 port = "80" 50 } 51 } 52 addr, err := net.ResolveTCPAddr("tcp", net.JoinHostPort(host, port)) 53 if err != nil { 54 return "", err 55 } 56 return net.JoinHostPort(addr.IP.String(), strconv.Itoa(addr.Port)), nil 57 }