host_prefix.go 1.3 KB

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