]> git.immae.eu Git - github/fretlink/terraform-provider-statuscake.git/blob - vendor/github.com/aws/aws-sdk-go/private/protocol/host.go
Upgrade to 0.12
[github/fretlink/terraform-provider-statuscake.git] / vendor / github.com / aws / aws-sdk-go / private / protocol / host.go
1 package protocol
2
3 import (
4 "strings"
5
6 "github.com/aws/aws-sdk-go/aws/request"
7 )
8
9 // ValidateEndpointHostHandler is a request handler that will validate the
10 // request endpoint's hosts is a valid RFC 3986 host.
11 var ValidateEndpointHostHandler = request.NamedHandler{
12 Name: "awssdk.protocol.ValidateEndpointHostHandler",
13 Fn: func(r *request.Request) {
14 err := ValidateEndpointHost(r.Operation.Name, r.HTTPRequest.URL.Host)
15 if err != nil {
16 r.Error = err
17 }
18 },
19 }
20
21 // ValidateEndpointHost validates that the host string passed in is a valid RFC
22 // 3986 host. Returns error if the host is not valid.
23 func ValidateEndpointHost(opName, host string) error {
24 paramErrs := request.ErrInvalidParams{Context: opName}
25 labels := strings.Split(host, ".")
26
27 for i, label := range labels {
28 if i == len(labels)-1 && len(label) == 0 {
29 // Allow trailing dot for FQDN hosts.
30 continue
31 }
32
33 if !ValidHostLabel(label) {
34 paramErrs.Add(request.NewErrParamFormat(
35 "endpoint host label", "[a-zA-Z0-9-]{1,63}", label))
36 }
37 }
38
39 if len(host) > 255 {
40 paramErrs.Add(request.NewErrParamMaxLen(
41 "endpoint host", 255, host,
42 ))
43 }
44
45 if paramErrs.Len() > 0 {
46 return paramErrs
47 }
48 return nil
49 }
50
51 // ValidHostLabel returns if the label is a valid RFC 3986 host label.
52 func ValidHostLabel(label string) bool {
53 if l := len(label); l == 0 || l > 63 {
54 return false
55 }
56 for _, r := range label {
57 switch {
58 case r >= '0' && r <= '9':
59 case r >= 'A' && r <= 'Z':
60 case r >= 'a' && r <= 'z':
61 case r == '-':
62 default:
63 return false
64 }
65 }
66
67 return true
68 }