github.com/aavshr/aws-sdk-go@v1.41.3/private/protocol/host_prefix.go (about)

     1  package protocol
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/aavshr/aws-sdk-go/aws"
     7  	"github.com/aavshr/aws-sdk-go/aws/request"
     8  )
     9  
    10  // HostPrefixHandlerName is the handler name for the host prefix request
    11  // handler.
    12  const HostPrefixHandlerName = "awssdk.endpoint.HostPrefixHandler"
    13  
    14  // NewHostPrefixHandler constructs a build handler
    15  func NewHostPrefixHandler(prefix string, labelsFn func() map[string]string) request.NamedHandler {
    16  	builder := HostPrefixBuilder{
    17  		Prefix:   prefix,
    18  		LabelsFn: labelsFn,
    19  	}
    20  
    21  	return request.NamedHandler{
    22  		Name: HostPrefixHandlerName,
    23  		Fn:   builder.Build,
    24  	}
    25  }
    26  
    27  // HostPrefixBuilder provides the request handler to expand and prepend
    28  // the host prefix into the operation's request endpoint host.
    29  type HostPrefixBuilder struct {
    30  	Prefix   string
    31  	LabelsFn func() map[string]string
    32  }
    33  
    34  // Build updates the passed in Request with the HostPrefix template expanded.
    35  func (h HostPrefixBuilder) Build(r *request.Request) {
    36  	if aws.BoolValue(r.Config.DisableEndpointHostPrefix) {
    37  		return
    38  	}
    39  
    40  	var labels map[string]string
    41  	if h.LabelsFn != nil {
    42  		labels = h.LabelsFn()
    43  	}
    44  
    45  	prefix := h.Prefix
    46  	for name, value := range labels {
    47  		prefix = strings.Replace(prefix, "{"+name+"}", value, -1)
    48  	}
    49  
    50  	r.HTTPRequest.URL.Host = prefix + r.HTTPRequest.URL.Host
    51  	if len(r.HTTPRequest.Host) > 0 {
    52  		r.HTTPRequest.Host = prefix + r.HTTPRequest.Host
    53  	}
    54  }