github.com/mdaxf/iac@v0.0.0-20240519030858-58a061660378/vendor_skip/go.mongodb.org/mongo-driver/internal/aws/signer/v4/request.go (about) 1 // Copyright (C) MongoDB, Inc. 2017-present. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); you may 4 // not use this file except in compliance with the License. You may obtain 5 // a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 6 // 7 // Based on github.com/aws/aws-sdk-go by Amazon.com, Inc. with code from: 8 // - github.com/aws/aws-sdk-go/blob/v1.44.225/aws/request/request.go 9 // See THIRD-PARTY-NOTICES for original license terms 10 11 package v4 12 13 import ( 14 "net/http" 15 "strings" 16 ) 17 18 // Returns host from request 19 func getHost(r *http.Request) string { 20 if r.Host != "" { 21 return r.Host 22 } 23 24 if r.URL == nil { 25 return "" 26 } 27 28 return r.URL.Host 29 } 30 31 // Hostname returns u.Host, without any port number. 32 // 33 // If Host is an IPv6 literal with a port number, Hostname returns the 34 // IPv6 literal without the square brackets. IPv6 literals may include 35 // a zone identifier. 36 // 37 // Copied from the Go 1.8 standard library (net/url) 38 func stripPort(hostport string) string { 39 colon := strings.IndexByte(hostport, ':') 40 if colon == -1 { 41 return hostport 42 } 43 if i := strings.IndexByte(hostport, ']'); i != -1 { 44 return strings.TrimPrefix(hostport[:i], "[") 45 } 46 return hostport[:colon] 47 } 48 49 // Port returns the port part of u.Host, without the leading colon. 50 // If u.Host doesn't contain a port, Port returns an empty string. 51 // 52 // Copied from the Go 1.8 standard library (net/url) 53 func portOnly(hostport string) string { 54 colon := strings.IndexByte(hostport, ':') 55 if colon == -1 { 56 return "" 57 } 58 if i := strings.Index(hostport, "]:"); i != -1 { 59 return hostport[i+len("]:"):] 60 } 61 if strings.Contains(hostport, "]") { 62 return "" 63 } 64 return hostport[colon+len(":"):] 65 } 66 67 // Returns true if the specified URI is using the standard port 68 // (i.e. port 80 for HTTP URIs or 443 for HTTPS URIs) 69 func isDefaultPort(scheme, port string) bool { 70 if port == "" { 71 return true 72 } 73 74 lowerCaseScheme := strings.ToLower(scheme) 75 if (lowerCaseScheme == "http" && port == "80") || (lowerCaseScheme == "https" && port == "443") { 76 return true 77 } 78 79 return false 80 }