-
Notifications
You must be signed in to change notification settings - Fork 672
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add customization to disable auto decode gzip for s3. Moves customiza…
…tions from dynamodb to reuse middleware
- Loading branch information
1 parent
4a1d0a5
commit 6b963de
Showing
8 changed files
with
204 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
...c/main/java/software/amazon/smithy/aws/go/codegen/customization/S3AcceptEncodingGzip.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package software.amazon.smithy.aws.go.codegen.customization; | ||
|
||
import java.util.List; | ||
import java.util.logging.Logger; | ||
import software.amazon.smithy.aws.go.codegen.AddAwsConfigFields; | ||
import software.amazon.smithy.aws.traits.ServiceTrait; | ||
import software.amazon.smithy.codegen.core.SymbolProvider; | ||
import software.amazon.smithy.go.codegen.GoDelegator; | ||
import software.amazon.smithy.go.codegen.GoSettings; | ||
import software.amazon.smithy.go.codegen.GoWriter; | ||
import software.amazon.smithy.go.codegen.SymbolUtils; | ||
import software.amazon.smithy.go.codegen.integration.ConfigField; | ||
import software.amazon.smithy.go.codegen.integration.GoIntegration; | ||
import software.amazon.smithy.go.codegen.integration.MiddlewareRegistrar; | ||
import software.amazon.smithy.go.codegen.integration.RuntimeClientPlugin; | ||
import software.amazon.smithy.model.Model; | ||
import software.amazon.smithy.model.shapes.ServiceShape; | ||
import software.amazon.smithy.utils.ListUtils; | ||
|
||
/** | ||
* S3AcceptEncodingGzip adds a customization for s3 client to disable | ||
* auto decoding of GZip content by Golang HTTP Client. | ||
* | ||
* This customization provides an option on the S3 client options to enable | ||
* AcceptEncoding for GZIP. The flag if set, will enable auto decompression of | ||
* GZIP by the S3 Client. | ||
* | ||
* By default, the client's auto decompression of GZIP content is turned off. | ||
*/ | ||
public class S3AcceptEncodingGzip implements GoIntegration { | ||
private static final Logger LOGGER = Logger.getLogger(AddAwsConfigFields.class.getName()); | ||
|
||
private static final String GZIP_DISABLE = "disableAcceptEncodingGzip"; | ||
private static final String GZIP_INTERNAL_ADDER = "AddAcceptEncodingGzip"; | ||
|
||
/** | ||
* Gets the sort order of the customization from -128 to 127, with lowest | ||
* executed first. | ||
* | ||
* @return Returns the sort order, defaults to -50. | ||
*/ | ||
@Override | ||
public byte getOrder() { | ||
return 127; | ||
} | ||
|
||
@Override | ||
public void writeAdditionalFiles( | ||
GoSettings settings, | ||
Model model, | ||
SymbolProvider symbolProvider, | ||
GoDelegator goDelegator | ||
) { | ||
if (!isServiceS3(model, settings.getService(model))) { | ||
return; | ||
} | ||
|
||
goDelegator.useShapeWriter(settings.getService(model), this::writeMiddlewareHelper); | ||
} | ||
|
||
private void writeMiddlewareHelper(GoWriter writer) { | ||
writer.openBlock("func $L(stack *middleware.Stack) {", "}", GZIP_DISABLE, () -> { | ||
writer.write("$T(stack, $T{})", | ||
SymbolUtils.createValueSymbolBuilder(GZIP_INTERNAL_ADDER, | ||
AwsCustomGoDependency.ACCEPT_ENCODING_CUSTOMIZATION).build(), | ||
SymbolUtils.createValueSymbolBuilder(GZIP_INTERNAL_ADDER + "Options", | ||
AwsCustomGoDependency.ACCEPT_ENCODING_CUSTOMIZATION).build() | ||
); | ||
}); | ||
writer.insertTrailingNewline(); | ||
} | ||
|
||
@Override | ||
public List<RuntimeClientPlugin> getClientPlugins() { | ||
return ListUtils.of( | ||
// register disableAcceptEncodingGzip middleware | ||
RuntimeClientPlugin.builder() | ||
.servicePredicate(S3AcceptEncodingGzip::isServiceS3) | ||
.registerMiddleware(MiddlewareRegistrar.builder() | ||
.resolvedFunction(SymbolUtils.createValueSymbolBuilder(GZIP_DISABLE) | ||
.build()) | ||
.build() | ||
) | ||
.build() | ||
); | ||
} | ||
|
||
/** | ||
* Return true if service is S3. | ||
* | ||
* @param model the model used for generation. | ||
* @param service the service shape for which default HTTP Client is generated. | ||
* @return true if service is S3 | ||
*/ | ||
private static boolean isServiceS3(Model model, ServiceShape service) { | ||
return service.expectTrait(ServiceTrait.class).getSdkId().equalsIgnoreCase("S3"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,41 @@ | ||
// Package customizations provides customizations for the Amazon DynamoDB API client. | ||
// | ||
// The DynamoDB API client uses two customizations, response checksum validation, | ||
// and manual content-encoding: gzip support. | ||
// | ||
// Middleware layering | ||
// | ||
// Checksum validation needs to be performed first in deserialization chain | ||
// on top of gzip decompression. Since the behavior of Deserialization is | ||
// in reverse order to the other stack steps its easier to consider that | ||
// "after" means "before". | ||
// | ||
// HTTP Response -> Checksum -> gzip decompress -> deserialize | ||
// | ||
// Response checksum validation | ||
// | ||
// DynamoDB responses can include a X-Amz-Crc32 header with the CRC32 checksum | ||
// value of the response body. If the response body is content-encoding: gzip, the | ||
// checksum is of the gzipped response content. | ||
// | ||
// If the header is present, the SDK should validate that the response payload | ||
// computed CRC32 checksum matches the value provided in the header. The checksum | ||
// header is based on the original payload provided returned by the service. Which | ||
// means that if the response is gzipped the checksum is of the gzipped response, | ||
// not the decompressed response bytes. | ||
// | ||
// Customization option: | ||
// DisableValidateResponseChecksum (Enabled by Default) | ||
// | ||
// Accept encoding gzip | ||
// | ||
// The Go HTTP client automatically supports accept-encoding and content-encoding | ||
// gzip by default. This default behavior is not desired by the SDK, and prevents | ||
// validating the response body's checksum. To prevent this the SDK must manually | ||
// control usage of content-encoding gzip. | ||
// | ||
// To control content-encoding, the SDK must always set the `Accept-Encoding` | ||
// header to a value. This prevents the HTTP client from using gzip automatically. | ||
// When gzip is enabled on the API client, the SDK's customization will control | ||
// decompressing the gzip data in order to not break the checksum validation. When | ||
// gzip is disabled, the API client will disable gzip, preventing the HTTP | ||
// client's default behavior. | ||
// | ||
// Customization option: | ||
// EnableAcceptEncodingGzip (Disabled by Default) | ||
// | ||
/* | ||
Package customizations provides customizations for the Amazon DynamoDB API client. | ||
The DynamoDB API client uses two customizations, response checksum validation, | ||
and manual content-encoding: gzip support. | ||
Middleware layering | ||
Checksum validation needs to be performed first in deserialization chain | ||
on top of gzip decompression. Since the behavior of Deserialization is | ||
in reverse order to the other stack steps its easier to consider that | ||
"after" means "before". | ||
HTTP Response -> Checksum -> gzip decompress -> deserialize | ||
Response checksum validation | ||
DynamoDB responses can include a X-Amz-Crc32 header with the CRC32 checksum | ||
value of the response body. If the response body is content-encoding: gzip, the | ||
checksum is of the gzipped response content. | ||
If the header is present, the SDK should validate that the response payload | ||
computed CRC32 checksum matches the value provided in the header. The checksum | ||
header is based on the original payload provided returned by the service. Which | ||
means that if the response is gzipped the checksum is of the gzipped response, | ||
not the decompressed response bytes. | ||
Customization option: | ||
DisableValidateResponseChecksum (Enabled by Default) | ||
Accept encoding gzip | ||
For customization around accept encoding, dynamodb client uses the middlewares | ||
defined at service/internal/accept-encoding. Please refer to the documentation for | ||
`accept-encoding` package for more details. | ||
Customization option: | ||
EnableAcceptEncodingGzip (Disabled by Default) | ||
*/ | ||
package customizations |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* | ||
Package customizations provides customizations associated with Accept Encoding Header. | ||
Accept encoding gzip | ||
The Go HTTP client automatically supports accept-encoding and content-encoding | ||
gzip by default. This default behavior is not desired by the SDK, and prevents | ||
validating the response body's checksum. To prevent this the SDK must manually | ||
control usage of content-encoding gzip. | ||
To control content-encoding, the SDK must always set the `Accept-Encoding` | ||
header to a value. This prevents the HTTP client from using gzip automatically. | ||
When gzip is enabled on the API client, the SDK's customization will control | ||
decompressing the gzip data in order to not break the checksum validation. When | ||
gzip is disabled, the API client will disable gzip, preventing the HTTP | ||
client's default behavior. | ||
An `EnableAcceptEncodingGzip` option may or may not be present depending on the client using | ||
the below middleware. The option if present can be used to enable auto decompressing | ||
gzip by the SDK. | ||
*/ | ||
package acceptencoding |