Skip to content

Commit d0dede7

Browse files
authored
Refactor RequestChecksumInterceptor into distinct interceptors (#4384)
## Motivation and Context This PR breaks up `RequestChecksumInterceptor` into two interceptors: `RequestChecksumInterceptor` and `AwsChunkedContentEncodingInterceptor` with additional code restructuring to support the upcoming aws-chunked content encoding enhancement. ## Description `RequestChecksumInterceptor` previously handled two distinct responsibilities: 1. Adding checksums (body or trailer) 2. Applying aws-chunked content encoding This refactor extracts the second responsibility (aws-chunked logic) into `AwsChunkedContentEncodingInterceptor`, aiming to enable it via the dedicated trait when available in Smithy. In addition, both interceptors now wrap bodies in `modify_before_transmit` instead of `modify_before_signing` per design requirements. ## Tips for merging to `feature/http-1.x` This refactor reluctantly adds `http_0x` constructs that will conflict with `feature/http-1.x`. Update these files when merging: - `aws-inlineable/src/http_request_checksum.rs` - Apply same 1.x updates as done [in branch](https://github.com/smithy-lang/smithy-rs/compare/feature/http-1.x?expand=1#diff-7c4006e142da69a7a442cd1d152bf5d0ae05a18c93a02c9754776ef29cef5dcd) (note: `test_checksum_body_is_retryable` was removed as it tested a utility only used in streaming cases against non-streaming bodies) - `aws-inlineable/src/aws_chunked.rs` - Use 1.x for header names, values, and body trait methods in unit tests, as in `http_request_checksum.rs` - `aws/rust-runtime/aws-runtime/src/content_encoding.rs` - Needs 1.x updates ``` fn size_hint(&self) -> http_body_1x::SizeHint { http_body_1x::SizeHint::with_exact(self.encoded_length()) } ``` to ``` fn size_hint(&self) -> http_body_1x::SizeHint { http_body_1x::SizeHint::with_exact(self.options.encoded_length()) } ``` because the `.encoded_length()` method has been moved from `AwsChunkedBody` to `AwsChunkedBodyOptions` (due to the `content-length` header needing encoded length during signing but body creation deferred to `modify_before_transmit`) ## Testing - CI ---- _By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice._
1 parent 41ea15d commit d0dede7

File tree

11 files changed

+740
-316
lines changed

11 files changed

+740
-316
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package software.amazon.smithy.rustsdk
7+
8+
import software.amazon.smithy.aws.traits.HttpChecksumTrait
9+
import software.amazon.smithy.model.shapes.OperationShape
10+
import software.amazon.smithy.model.shapes.StructureShape
11+
import software.amazon.smithy.model.traits.HttpHeaderTrait
12+
import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenContext
13+
import software.amazon.smithy.rust.codegen.client.smithy.customize.ClientCodegenDecorator
14+
import software.amazon.smithy.rust.codegen.client.smithy.generators.OperationCustomization
15+
import software.amazon.smithy.rust.codegen.client.smithy.generators.OperationSection
16+
import software.amazon.smithy.rust.codegen.core.rustlang.CargoDependency
17+
import software.amazon.smithy.rust.codegen.core.rustlang.Visibility
18+
import software.amazon.smithy.rust.codegen.core.rustlang.rustTemplate
19+
import software.amazon.smithy.rust.codegen.core.rustlang.writable
20+
import software.amazon.smithy.rust.codegen.core.smithy.RuntimeConfig
21+
import software.amazon.smithy.rust.codegen.core.smithy.RuntimeType
22+
import software.amazon.smithy.rust.codegen.core.util.getTrait
23+
import software.amazon.smithy.rust.codegen.core.util.hasStreamingMember
24+
25+
class AwsChunkedContentEncodingDecorator : ClientCodegenDecorator {
26+
override val name: String = "AwsChunkedContentEncoding"
27+
28+
// This decorator must decorate after any of the following:
29+
// - HttpRequestChecksumDecorator
30+
// - HttpRequestCompressionDecorator
31+
override val order: Byte = (minOf(HttpRequestChecksumDecorator.ORDER, HttpRequestCompressionDecorator.ORDER) - 1).toByte()
32+
33+
override fun operationCustomizations(
34+
codegenContext: ClientCodegenContext,
35+
operation: OperationShape,
36+
baseCustomizations: List<OperationCustomization>,
37+
) = baseCustomizations + AwsChunkedOparationCustomization(codegenContext, operation)
38+
}
39+
40+
private class AwsChunkedOparationCustomization(
41+
private val codegenContext: ClientCodegenContext,
42+
private val operation: OperationShape,
43+
) : OperationCustomization() {
44+
private val model = codegenContext.model
45+
private val runtimeConfig = codegenContext.runtimeConfig
46+
47+
override fun section(section: OperationSection) =
48+
writable {
49+
when (section) {
50+
is OperationSection.AdditionalInterceptors -> {
51+
// TODO(https://github.com/smithy-lang/smithy-rs/issues/4382): Remove all of these early returns
52+
// once we have the dedicated trait available in Smithy.
53+
val checksumTrait = operation.getTrait<HttpChecksumTrait>() ?: return@writable
54+
val requestAlgorithmMember =
55+
checksumTrait.requestAlgorithmMemberShape(codegenContext, operation) ?: return@writable
56+
requestAlgorithmMember.getTrait<HttpHeaderTrait>()?.value ?: return@writable
57+
val input = model.expectShape(operation.inputShape, StructureShape::class.java)
58+
if (!input.hasStreamingMember(model)) {
59+
return@writable
60+
}
61+
62+
section.registerInterceptor(runtimeConfig, this) {
63+
rustTemplate(
64+
"""
65+
#{AwsChunkedContentEncodingInterceptor}
66+
""",
67+
"AwsChunkedContentEncodingInterceptor" to
68+
runtimeConfig.awsChunked()
69+
.resolve("AwsChunkedContentEncodingInterceptor"),
70+
)
71+
}
72+
}
73+
74+
else -> emptySection
75+
}
76+
}
77+
}
78+
79+
private fun RuntimeConfig.awsChunked() =
80+
RuntimeType.forInlineDependency(
81+
InlineAwsDependency.forRustFile(
82+
"aws_chunked", visibility = Visibility.PUBCRATE,
83+
CargoDependency.Bytes,
84+
CargoDependency.Http,
85+
CargoDependency.HttpBody,
86+
CargoDependency.Tracing,
87+
AwsCargoDependency.awsRuntime(this).withFeature("http-02x"),
88+
CargoDependency.smithyRuntimeApiClient(this),
89+
CargoDependency.smithyTypes(this),
90+
AwsCargoDependency.awsSigv4(this),
91+
CargoDependency.TempFile.toDevDependency(),
92+
),
93+
)

aws/codegen-aws-sdk/src/main/kotlin/software/amazon/smithy/rustsdk/AwsCodegenDecorator.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ val DECORATORS: List<ClientCodegenDecorator> =
5656
SdkConfigDecorator(),
5757
ServiceConfigDecorator(),
5858
AwsPresigningDecorator(),
59+
AwsChunkedContentEncodingDecorator(),
5960
AwsCrateDocsDecorator(),
6061
AwsEndpointsStdLib(),
6162
*PromotedBuiltInsDecorators,

aws/codegen-aws-sdk/src/main/kotlin/software/amazon/smithy/rustsdk/HttpRequestChecksumDecorator.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,12 @@ internal fun RuntimeConfig.awsInlineableHttpRequestChecksum() =
5555
)
5656

5757
class HttpRequestChecksumDecorator : ClientCodegenDecorator {
58+
companion object {
59+
const val ORDER: Byte = 0
60+
}
61+
5862
override val name: String = "HttpRequestChecksum"
59-
override val order: Byte = 0
63+
override val order: Byte = ORDER
6064

6165
override fun operationCustomizations(
6266
codegenContext: ClientCodegenContext,

aws/codegen-aws-sdk/src/main/kotlin/software/amazon/smithy/rustsdk/HttpRequestCompressionDecorator.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,12 @@ import software.amazon.smithy.rust.codegen.core.smithy.customize.adhocCustomizat
2121
import software.amazon.smithy.rust.codegen.core.util.thenSingletonListOf
2222

2323
class HttpRequestCompressionDecorator : ClientCodegenDecorator {
24+
companion object {
25+
const val ORDER: Byte = 0
26+
}
27+
2428
override val name: String = "HttpRequestCompression"
25-
override val order: Byte = 0
29+
override val order: Byte = ORDER
2630

2731
private fun usesRequestCompression(codegenContext: ClientCodegenContext): Boolean {
2832
val index = TopDownIndex.of(codegenContext.model)

aws/rust-runtime/Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)