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 71 72 73 74 75 76 77 78
|
package s3shared
import (
"context"
"fmt"
"strings"
"github.com/aws/smithy-go/middleware"
smithyhttp "github.com/aws/smithy-go/transport/http"
awsmiddle "github.com/aws/aws-sdk-go-v2/aws/middleware"
)
// EnableDualstack represents middleware struct for enabling dualstack support
//
// Deprecated: See EndpointResolverOptions' UseDualStackEndpoint support
type EnableDualstack struct {
// UseDualstack indicates if dualstack endpoint resolving is to be enabled
UseDualstack bool
// DefaultServiceID is the service id prefix used in endpoint resolving
// by default service-id is 's3' and 's3-control' for service s3, s3control.
DefaultServiceID string
}
// ID returns the middleware ID.
func (*EnableDualstack) ID() string {
return "EnableDualstack"
}
// HandleSerialize handles serializer middleware behavior when middleware is executed
func (u *EnableDualstack) HandleSerialize(
ctx context.Context, in middleware.SerializeInput, next middleware.SerializeHandler,
) (
out middleware.SerializeOutput, metadata middleware.Metadata, err error,
) {
// check for host name immutable property
if smithyhttp.GetHostnameImmutable(ctx) {
return next.HandleSerialize(ctx, in)
}
serviceID := awsmiddle.GetServiceID(ctx)
// s3-control may be represented as `S3 Control` as in model
if serviceID == "S3 Control" {
serviceID = "s3-control"
}
if len(serviceID) == 0 {
// default service id
serviceID = u.DefaultServiceID
}
req, ok := in.Request.(*smithyhttp.Request)
if !ok {
return out, metadata, fmt.Errorf("unknown request type %T", req)
}
if u.UseDualstack {
parts := strings.Split(req.URL.Host, ".")
if len(parts) < 3 {
return out, metadata, fmt.Errorf("unable to update endpoint host for dualstack, hostname invalid, %s", req.URL.Host)
}
for i := 0; i+1 < len(parts); i++ {
if strings.EqualFold(parts[i], serviceID) {
parts[i] = parts[i] + ".dualstack"
break
}
}
// construct the url host
req.URL.Host = strings.Join(parts, ".")
}
return next.HandleSerialize(ctx, in)
}
|