aboutsummaryrefslogtreecommitdiff
path: root/vendor/github.com/aws/aws-sdk-go/private/endpoints/endpoints.go
blob: 19d97562fee7f20166a6e7fdf5ddb5e118d4c76d (plain)
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
66
67
68
69
70
// Package endpoints validates regional endpoints for services.
package endpoints

//go:generate go run -tags codegen ../model/cli/gen-endpoints/main.go endpoints.json endpoints_map.go
//go:generate gofmt -s -w endpoints_map.go

import (
	"fmt"
	"regexp"
	"strings"
)

// NormalizeEndpoint takes and endpoint and service API information to return a
// normalized endpoint and signing region.  If the endpoint is not an empty string
// the service name and region will be used to look up the service's API endpoint.
// If the endpoint is provided the scheme will be added if it is not present.
func NormalizeEndpoint(endpoint, serviceName, region string, disableSSL, useDualStack bool) (normEndpoint, signingRegion string) {
	if endpoint == "" {
		return EndpointForRegion(serviceName, region, disableSSL, useDualStack)
	}

	return AddScheme(endpoint, disableSSL), ""
}

// EndpointForRegion returns an endpoint and its signing region for a service and region.
// if the service and region pair are not found endpoint and signingRegion will be empty.
func EndpointForRegion(svcName, region string, disableSSL, useDualStack bool) (endpoint, signingRegion string) {
	dualStackField := ""
	if useDualStack {
		dualStackField = "/dualstack"
	}

	derivedKeys := []string{
		region + "/" + svcName + dualStackField,
		region + "/*" + dualStackField,
		"*/" + svcName + dualStackField,
		"*/*" + dualStackField,
	}

	for _, key := range derivedKeys {
		if val, ok := endpointsMap.Endpoints[key]; ok {
			ep := val.Endpoint
			ep = strings.Replace(ep, "{region}", region, -1)
			ep = strings.Replace(ep, "{service}", svcName, -1)

			endpoint = ep
			signingRegion = val.SigningRegion
			break
		}
	}

	return AddScheme(endpoint, disableSSL), signingRegion
}

// Regular expression to determine if the endpoint string is prefixed with a scheme.
var schemeRE = regexp.MustCompile("^([^:]+)://")

// AddScheme adds the HTTP or HTTPS schemes to a endpoint URL if there is no
// scheme. If disableSSL is true HTTP will be added instead of the default HTTPS.
func AddScheme(endpoint string, disableSSL bool) string {
	if endpoint != "" && !schemeRE.MatchString(endpoint) {
		scheme := "https"
		if disableSSL {
			scheme = "http"
		}
		endpoint = fmt.Sprintf("%s://%s", scheme, endpoint)
	}

	return endpoint
}