github.com/waldiirawan/apm-agent-go/v2@v2.2.2/internal/apmhttputil/remoteaddr.go (about) 1 // Licensed to Elasticsearch B.V. under one or more contributor 2 // license agreements. See the NOTICE file distributed with 3 // this work for additional information regarding copyright 4 // ownership. Elasticsearch B.V. licenses this file to you under 5 // the Apache License, Version 2.0 (the "License"); you may 6 // not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, 12 // software distributed under the License is distributed on an 13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 // KIND, either express or implied. See the License for the 15 // specific language governing permissions and limitations 16 // under the License. 17 18 package apmhttputil 19 20 import ( 21 "net/http" 22 "strconv" 23 ) 24 25 // RemoteAddr returns the remote (peer) socket address for req, 26 // a server HTTP request. 27 func RemoteAddr(req *http.Request) string { 28 remoteAddr, _ := splitHost(req.RemoteAddr) 29 return remoteAddr 30 } 31 32 // DestinationAddr returns the destination server address and port 33 // for req, a client HTTP request. 34 // 35 // If req.URL.Host contains a port it will be returned, and otherwise 36 // the default port according to req.URL.Scheme will be returned. If 37 // the included port is not a valid integer, or no port is included 38 // and the scheme is unknown, the returned port value will be zero. 39 func DestinationAddr(req *http.Request) (string, int) { 40 host, strport := splitHost(req.URL.Host) 41 var port int 42 if strport != "" { 43 port, _ = strconv.Atoi(strport) 44 } else { 45 port = SchemeDefaultPort(req.URL.Scheme) 46 } 47 return host, port 48 } 49 50 // SchemeDefaultPort returns the default port for the given URI scheme, 51 // if known, or 0 otherwise. 52 func SchemeDefaultPort(scheme string) int { 53 switch scheme { 54 case "http": 55 return 80 56 case "https": 57 return 443 58 } 59 return 0 60 }