diff --git a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/SmithyGoDependency.java b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/SmithyGoDependency.java index 4c427e7b4..e20255cd9 100644 --- a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/SmithyGoDependency.java +++ b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/SmithyGoDependency.java @@ -110,6 +110,6 @@ private static GoDependency relativePackage( private static final class Versions { private static final String GO_STDLIB = "1.14"; private static final String GO_CMP = "v0.4.1"; - private static final String SMITHY_GO = "v0.3.1-0.20201104233911-38864709e183"; + private static final String SMITHY_GO = "v0.3.1-0.20201108010311-62c2a93810b4"; } } diff --git a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/integration/EndpointHostPrefixMiddleware.java b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/integration/EndpointHostPrefixMiddleware.java index 3ef0a8bb7..d4ef86c69 100644 --- a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/integration/EndpointHostPrefixMiddleware.java +++ b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/integration/EndpointHostPrefixMiddleware.java @@ -35,6 +35,10 @@ import software.amazon.smithy.model.shapes.StructureShape; import software.amazon.smithy.model.traits.EndpointTrait; +/** + * EndpointHostPrefixMiddleware adds middlewares to identify + * a host prefix and mutate the request URL host if permitted. +**/ public class EndpointHostPrefixMiddleware implements GoIntegration { private static final MiddlewareIdentifier MIDDLEWARE_ID = MiddlewareIdentifier.string("EndpointHostPrefix"); @@ -84,10 +88,9 @@ public void writeAdditionalFiles( middlewareHelperName, () -> { writer.write( - "return stack.Serialize.Insert(&$L{}, `OperationSerializer`, middleware.Before)", + "return stack.Serialize.Insert(&$L{}, `OperationSerializer`, middleware.After)", middlewareName); }); - }); }); } @@ -109,7 +112,8 @@ private static void writeMiddleware( writer.addUseImports(SmithyGoDependency.SMITHY_HTTP_TRANSPORT); writer.addUseImports(SmithyGoDependency.FMT); - w.openBlock("if smithyhttp.GetHostnameImmutable(ctx) {", "}", () -> { + w.openBlock("if smithyhttp.GetHostnameImmutable(ctx) || " + + "smithyhttp.IsEndpointHostPrefixDisabled(ctx) {", "}", () -> { w.write("return next.$L(ctx, in)", generator.getHandleMethodName()); }).write(""); @@ -119,8 +123,7 @@ private static void writeMiddleware( }).write(""); if (pattern.getLabels().isEmpty()) { - w.write("req.HostPrefix = $S", pattern.toString()); - + w.write("req.URL.Host = $S + req.URL.Host", pattern.toString()); } else { // If the pattern has labels, we need to build up the host prefix using a string builder. writer.addUseImports(SmithyGoDependency.STRINGS); @@ -156,8 +159,7 @@ private static void writeMiddleware( }); } } - - w.write("req.HostPrefix = prefix.String()"); + w.write("req.URL.Host = prefix.String() + req.URL.Host"); } w.write(""); diff --git a/transport/http/middleware_metadata.go b/transport/http/middleware_metadata.go index b4203ab0a..e0d386262 100644 --- a/transport/http/middleware_metadata.go +++ b/transport/http/middleware_metadata.go @@ -4,6 +4,7 @@ import "context" type ( hostnameImmutableKey struct{} + hostPrefixDisableKey struct{} ) // GetHostnameImmutable retrieves if the endpoint hostname should be considered @@ -18,3 +19,17 @@ func GetHostnameImmutable(ctx context.Context) (v bool) { func SetHostnameImmutable(ctx context.Context, value bool) context.Context { return context.WithValue(ctx, hostnameImmutableKey{}, value) } + +// DisableEndpointHostPrefix sets or modifies if the request's endpoint host +// prefixing to be disabled. If value is set to true, endpoint host prefixing +// will be disabled. +func DisableEndpointHostPrefix(ctx context.Context, value bool) context.Context { + return context.WithValue(ctx, hostPrefixDisableKey{}, value) +} + +// IsEndpointHostPrefixDisabled retrieves if the hostname prefixing +// is disabled. +func IsEndpointHostPrefixDisabled(ctx context.Context) (v bool) { + v, _ = ctx.Value(hostPrefixDisableKey{}).(bool) + return v +} diff --git a/transport/http/request.go b/transport/http/request.go index 71475b17c..ee84fc521 100644 --- a/transport/http/request.go +++ b/transport/http/request.go @@ -18,7 +18,6 @@ type Request struct { stream io.Reader isStreamSeekable bool streamStartPos int64 - HostPrefix string } // NewStackRequest returns an initialized request ready to populated with the @@ -127,7 +126,7 @@ func (r *Request) SetStream(reader io.Reader) (rc *Request, err error) { // Build returns a build standard HTTP request value from the Smithy request. // The request's stream is wrapped in a safe container that allows it to be -// reused for subsiquent attempts. +// reused for subsequent attempts. func (r *Request) Build(ctx context.Context) *http.Request { req := r.Request.Clone(ctx) @@ -139,11 +138,6 @@ func (r *Request) Build(ctx context.Context) *http.Request { req.ContentLength = 0 } - // Add the host prefix - if len(r.HostPrefix) != 0 { - req.URL.Host = r.HostPrefix + req.URL.Host - } - return req }