Skip to content

Commit d9f0659

Browse files
committed
Make it compile
1 parent 1157f26 commit d9f0659

File tree

11 files changed

+94
-45
lines changed

11 files changed

+94
-45
lines changed

codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/checksums/HttpChecksumRequiredIntegration.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ private val httpChecksumRequiredMiddleware = object : ProtocolMiddleware {
6060
override fun render(ctx: ProtocolGenerator.GenerationContext, op: OperationShape, writer: KotlinWriter) {
6161
writer.write(
6262
"op.interceptors.add(#T())",
63-
RuntimeTypes.HttpClient.Interceptors.HttpChecksumRequiredInterceptor
63+
RuntimeTypes.HttpClient.Interceptors.HttpChecksumRequiredInterceptor,
6464
)
6565
}
6666
}

runtime/protocol/http-client/api/http-client.api

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -332,20 +332,16 @@ public final class aws/smithy/kotlin/runtime/http/interceptors/DiscoveredEndpoin
332332
}
333333

334334
public final class aws/smithy/kotlin/runtime/http/interceptors/FlexibleChecksumsRequestInterceptor : aws/smithy/kotlin/runtime/http/interceptors/AbstractChecksumInterceptor {
335-
public fun <init> ()V
336-
public fun <init> (Lkotlin/jvm/functions/Function1;)V
337-
public synthetic fun <init> (Lkotlin/jvm/functions/Function1;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
338335
public fun <init> (ZLaws/smithy/kotlin/runtime/client/config/HttpChecksumConfigOption;Ljava/lang/String;)V
339336
public fun applyChecksum (Laws/smithy/kotlin/runtime/client/ProtocolRequestInterceptorContext;Ljava/lang/String;)Laws/smithy/kotlin/runtime/http/request/HttpRequest;
340337
public fun calculateChecksum (Laws/smithy/kotlin/runtime/client/ProtocolRequestInterceptorContext;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
341338
public fun modifyBeforeSigning (Laws/smithy/kotlin/runtime/client/ProtocolRequestInterceptorContext;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
342-
public fun readAfterSerialization (Laws/smithy/kotlin/runtime/client/ProtocolRequestInterceptorContext;)V
343339
}
344340

345-
public final class aws/smithy/kotlin/runtime/http/interceptors/FlexibleChecksumsResponseInterceptor : aws/smithy/kotlin/runtime/client/Interceptor {
341+
public class aws/smithy/kotlin/runtime/http/interceptors/FlexibleChecksumsResponseInterceptor : aws/smithy/kotlin/runtime/client/Interceptor {
346342
public static final field Companion Laws/smithy/kotlin/runtime/http/interceptors/FlexibleChecksumsResponseInterceptor$Companion;
347-
public fun <init> (Lkotlin/jvm/functions/Function1;)V
348343
public fun <init> (ZLaws/smithy/kotlin/runtime/client/config/HttpChecksumConfigOption;)V
344+
public fun ignoreChecksum (Ljava/lang/String;)Z
349345
public fun modifyBeforeAttemptCompletion-gIAlu-s (Laws/smithy/kotlin/runtime/client/ResponseInterceptorContext;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
350346
public fun modifyBeforeCompletion-gIAlu-s (Laws/smithy/kotlin/runtime/client/ResponseInterceptorContext;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
351347
public fun modifyBeforeDeserialization (Laws/smithy/kotlin/runtime/client/ProtocolResponseInterceptorContext;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
@@ -371,10 +367,8 @@ public final class aws/smithy/kotlin/runtime/http/interceptors/FlexibleChecksums
371367
public final fun getChecksumHeaderValidated ()Laws/smithy/kotlin/runtime/collections/AttributeKey;
372368
}
373369

374-
public final class aws/smithy/kotlin/runtime/http/interceptors/Md5ChecksumInterceptor : aws/smithy/kotlin/runtime/http/interceptors/AbstractChecksumInterceptor {
370+
public final class aws/smithy/kotlin/runtime/http/interceptors/HttpChecksumRequiredInterceptor : aws/smithy/kotlin/runtime/http/interceptors/AbstractChecksumInterceptor {
375371
public fun <init> ()V
376-
public fun <init> (Lkotlin/jvm/functions/Function1;)V
377-
public synthetic fun <init> (Lkotlin/jvm/functions/Function1;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
378372
public fun applyChecksum (Laws/smithy/kotlin/runtime/client/ProtocolRequestInterceptorContext;Ljava/lang/String;)Laws/smithy/kotlin/runtime/http/request/HttpRequest;
379373
public fun calculateChecksum (Laws/smithy/kotlin/runtime/client/ProtocolRequestInterceptorContext;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
380374
public fun modifyBeforeSigning (Laws/smithy/kotlin/runtime/client/ProtocolRequestInterceptorContext;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
@@ -518,9 +512,9 @@ public abstract interface class aws/smithy/kotlin/runtime/http/operation/HttpDes
518512

519513
public final class aws/smithy/kotlin/runtime/http/operation/HttpOperationContext {
520514
public static final field INSTANCE Laws/smithy/kotlin/runtime/http/operation/HttpOperationContext;
521-
public final fun getChecksumAlgorithm ()Laws/smithy/kotlin/runtime/collections/AttributeKey;
522515
public final fun getClockSkew ()Laws/smithy/kotlin/runtime/collections/AttributeKey;
523516
public final fun getClockSkewApproximateSigningTime ()Laws/smithy/kotlin/runtime/collections/AttributeKey;
517+
public final fun getDefaultChecksumAlgorithm ()Laws/smithy/kotlin/runtime/collections/AttributeKey;
524518
public final fun getHostPrefix ()Laws/smithy/kotlin/runtime/collections/AttributeKey;
525519
public final fun getHttpCallList ()Laws/smithy/kotlin/runtime/collections/AttributeKey;
526520
public final fun getOperationAttributes ()Laws/smithy/kotlin/runtime/collections/AttributeKey;

runtime/protocol/http-client/common/src/aws/smithy/kotlin/runtime/http/interceptors/FlexibleChecksumsRequestInterceptor.kt

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,17 @@ public class FlexibleChecksumsRequestInterceptor(
5858

5959
context.protocolRequest.userProvidedChecksumHeader(logger)?.let {
6060
logger.debug { "Checksum was supplied via header: skipping checksum calculation" }
61+
62+
val request = context.protocolRequest.toBuilder()
63+
request.headers.removeAllChecksumHeadersExcept(it)
6164
return context.protocolRequest
6265
}
6366

6467
checksumAlgorithm(
6568
requestChecksumRequired,
6669
requestChecksumCalculation,
6770
requestChecksumAlgorithm,
68-
context
71+
context,
6972
)?.let { checksumAlgorithm ->
7073
if (context.protocolRequest.body.isEligibleForAwsChunkedStreaming) { // Handle checksum calculation here
7174
logger.debug { "Calculating checksum during transmission using: ${checksumAlgorithm::class.simpleName}" }
@@ -97,7 +100,7 @@ public class FlexibleChecksumsRequestInterceptor(
97100
requestChecksumRequired: Boolean,
98101
requestChecksumCalculation: HttpChecksumConfigOption?,
99102
requestChecksumAlgorithm: String?,
100-
context: ProtocolRequestInterceptorContext<Any, HttpRequest>
103+
context: ProtocolRequestInterceptorContext<Any, HttpRequest>,
101104
): HashFunction? =
102105
requestChecksumAlgorithm
103106
?.toHashFunctionOrThrow()
@@ -116,7 +119,7 @@ public class FlexibleChecksumsRequestInterceptor(
116119
requestChecksumRequired,
117120
requestChecksumCalculation,
118121
requestChecksumAlgorithm,
119-
context
122+
context,
120123
)!!
121124

122125
return when {
@@ -135,18 +138,19 @@ public class FlexibleChecksumsRequestInterceptor(
135138
// Handles applying checksum for non-aws-chunked requests
136139
override fun applyChecksum(
137140
context: ProtocolRequestInterceptorContext<Any, HttpRequest>,
138-
checksum: String
141+
checksum: String,
139142
): HttpRequest {
140143
val request = context.protocolRequest.toBuilder()
141144
val checksumAlgorithm = checksumAlgorithm(
142145
requestChecksumRequired,
143146
requestChecksumCalculation,
144147
requestChecksumAlgorithm,
145-
context
148+
context,
146149
)!!
147150
val checksumHeader = checksumAlgorithmHeader(checksumAlgorithm)
148151

149152
request.headers[checksumHeader] = checksum
153+
request.headers.removeAllChecksumHeadersExcept(checksumHeader)
150154
context.executionContext.emitBusinessMetric(checksumAlgorithm.toBusinessMetric())
151155

152156
return request.build()
@@ -197,4 +201,12 @@ public class FlexibleChecksumsRequestInterceptor(
197201
is Sha256 -> SmithyBusinessMetric.FLEXIBLE_CHECKSUMS_REQ_SHA256
198202
else -> throw IllegalStateException("Checksum was calculated using an unsupported hash function: ${this::class.simpleName}")
199203
}
204+
205+
/**
206+
* Removes all checksum headers except specified header
207+
*/
208+
private fun HeadersBuilder.removeAllChecksumHeadersExcept(checksumHeader: String) =
209+
names()
210+
.filter { it.startsWith("x-amz-checksum-", ignoreCase = true) && !it.equals(checksumHeader, ignoreCase = true) }
211+
.forEach { remove(it) }
200212
}

runtime/protocol/http-client/common/src/aws/smithy/kotlin/runtime/http/interceptors/FlexibleChecksumsResponseInterceptor.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@ public open class FlexibleChecksumsResponseInterceptor(
6262

6363
val checksumHeader = CHECKSUM_HEADER_VALIDATION_PRIORITY_LIST
6464
.firstOrNull { context.protocolResponse.headers.contains(it) } ?: run {
65-
logger.warn { "Checksum validation was requested but the response headers didn't contain a valid checksum." }
66-
return context.protocolResponse
67-
}
65+
logger.warn { "Checksum validation was requested but the response headers didn't contain a valid checksum." }
66+
return context.protocolResponse
67+
}
6868

6969
val serviceChecksumValue = context.protocolResponse.headers[checksumHeader]!!
7070
if (ignoreChecksum(serviceChecksumValue)) {

runtime/protocol/http-client/common/src/aws/smithy/kotlin/runtime/http/interceptors/HttpChecksumRequiredInterceptor.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,3 @@ public class HttpChecksumRequiredInterceptor : AbstractChecksumInterceptor() {
5252
return request.build()
5353
}
5454
}
55-

runtime/protocol/http-client/common/test/aws/smithy/kotlin/runtime/http/interceptors/FlexibleChecksumsRequestInterceptorTest.kt

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class FlexibleChecksumsRequestInterceptorTest {
4242
val op = newTestOperation<Unit, Unit>(req, Unit)
4343

4444
op.interceptors.add(
45-
FlexibleChecksumsRequestInterceptor<Unit>(
45+
FlexibleChecksumsRequestInterceptor(
4646
requestChecksumAlgorithm = checksumAlgorithmName,
4747
requestChecksumRequired = true,
4848
requestChecksumCalculation = HttpChecksumConfigOption.WHEN_SUPPORTED,
@@ -68,8 +68,9 @@ class FlexibleChecksumsRequestInterceptorTest {
6868

6969
val op = newTestOperation<Unit, Unit>(req, Unit)
7070

71+
op.context.attributes[HttpOperationContext.DefaultChecksumAlgorithm] = "CRC32"
7172
op.interceptors.add(
72-
FlexibleChecksumsRequestInterceptor<Unit>(
73+
FlexibleChecksumsRequestInterceptor(
7374
requestChecksumAlgorithm = checksumAlgorithmName,
7475
requestChecksumRequired = true,
7576
requestChecksumCalculation = HttpChecksumConfigOption.WHEN_SUPPORTED,
@@ -94,12 +95,13 @@ class FlexibleChecksumsRequestInterceptorTest {
9495

9596
assertFailsWith<ClientException> {
9697
op.interceptors.add(
97-
FlexibleChecksumsRequestInterceptor<Unit>(
98+
FlexibleChecksumsRequestInterceptor(
9899
requestChecksumAlgorithm = unsupportedChecksumAlgorithmName,
99100
requestChecksumRequired = true,
100101
requestChecksumCalculation = HttpChecksumConfigOption.WHEN_SUPPORTED,
101102
),
102103
)
104+
op.roundTrip(client, Unit)
103105
}
104106
}
105107

@@ -120,7 +122,7 @@ class FlexibleChecksumsRequestInterceptorTest {
120122
val op = newTestOperation<Unit, Unit>(req, Unit)
121123

122124
op.interceptors.add(
123-
FlexibleChecksumsRequestInterceptor<Unit>(
125+
FlexibleChecksumsRequestInterceptor(
124126
requestChecksumAlgorithm = checksumAlgorithmName,
125127
requestChecksumRequired = true,
126128
requestChecksumCalculation = HttpChecksumConfigOption.WHEN_SUPPORTED,
@@ -141,7 +143,7 @@ class FlexibleChecksumsRequestInterceptorTest {
141143
val source = byteArray.source()
142144
val completableDeferred = CompletableDeferred<String>()
143145
val hashingSource = HashingSource(hashFunctionName.toHashFunction()!!, source)
144-
val completingSource = FlexibleChecksumsRequestInterceptor.CompletingSource(completableDeferred, hashingSource)
146+
val completingSource = CompletingSource(completableDeferred, hashingSource)
145147

146148
completingSource.read(SdkBuffer(), 1L)
147149
assertFalse(completableDeferred.isCompleted) // deferred value should not be completed because the source is not exhausted
@@ -162,7 +164,7 @@ class FlexibleChecksumsRequestInterceptorTest {
162164
val channel = SdkByteReadChannel(byteArray)
163165
val completableDeferred = CompletableDeferred<String>()
164166
val hashingChannel = HashingByteReadChannel(hashFunctionName.toHashFunction()!!, channel)
165-
val completingChannel = FlexibleChecksumsRequestInterceptor.CompletingByteReadChannel(completableDeferred, hashingChannel)
167+
val completingChannel = CompletingByteReadChannel(completableDeferred, hashingChannel)
166168

167169
completingChannel.read(SdkBuffer(), 1L)
168170
assertFalse(completableDeferred.isCompleted)
@@ -188,7 +190,7 @@ class FlexibleChecksumsRequestInterceptorTest {
188190
val op = newTestOperation<Unit, Unit>(req, Unit)
189191

190192
op.interceptors.add(
191-
FlexibleChecksumsRequestInterceptor<Unit>(
193+
FlexibleChecksumsRequestInterceptor(
192194
requestChecksumAlgorithm = checksumAlgorithmName,
193195
requestChecksumRequired = true,
194196
requestChecksumCalculation = HttpChecksumConfigOption.WHEN_SUPPORTED,
@@ -232,8 +234,9 @@ class FlexibleChecksumsRequestInterceptorTest {
232234

233235
val op = newTestOperation<Unit, Unit>(req, Unit)
234236

237+
op.context.attributes[HttpOperationContext.DefaultChecksumAlgorithm] = "CRC32"
235238
op.interceptors.add(
236-
FlexibleChecksumsRequestInterceptor<Unit>(
239+
FlexibleChecksumsRequestInterceptor(
237240
requestChecksumAlgorithm = null, // See if default checksum is applied
238241
requestChecksumRequired = testCase.requestChecksumRequired,
239242
requestChecksumCalculation = testCase.requestChecksumCalculation,

runtime/protocol/http-client/common/test/aws/smithy/kotlin/runtime/http/interceptors/FlexibleChecksumsResponseInterceptorTest.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ class FlexibleChecksumsResponseInterceptorTest {
7474
val op = newTestOperation<TestInput>(req)
7575

7676
op.interceptors.add(
77-
FlexibleChecksumsResponseInterceptor<TestInput>(
77+
FlexibleChecksumsResponseInterceptor(
7878
responseValidationRequired = true,
7979
responseChecksumValidation = HttpChecksumConfigOption.WHEN_SUPPORTED,
8080
),
@@ -101,7 +101,7 @@ class FlexibleChecksumsResponseInterceptorTest {
101101
val op = newTestOperation<TestInput>(req)
102102

103103
op.interceptors.add(
104-
FlexibleChecksumsResponseInterceptor<TestInput>(
104+
FlexibleChecksumsResponseInterceptor(
105105
responseValidationRequired = true,
106106
responseChecksumValidation = HttpChecksumConfigOption.WHEN_SUPPORTED,
107107
),
@@ -129,7 +129,7 @@ class FlexibleChecksumsResponseInterceptorTest {
129129
val op = newTestOperation<TestInput>(req)
130130

131131
op.interceptors.add(
132-
FlexibleChecksumsResponseInterceptor<TestInput>(
132+
FlexibleChecksumsResponseInterceptor(
133133
responseValidationRequired = true,
134134
responseChecksumValidation = HttpChecksumConfigOption.WHEN_SUPPORTED,
135135
),
@@ -154,7 +154,7 @@ class FlexibleChecksumsResponseInterceptorTest {
154154
val op = newTestOperation<TestInput>(req)
155155

156156
op.interceptors.add(
157-
FlexibleChecksumsResponseInterceptor<TestInput>(
157+
FlexibleChecksumsResponseInterceptor(
158158
responseValidationRequired = true,
159159
responseChecksumValidation = HttpChecksumConfigOption.WHEN_SUPPORTED,
160160
),
@@ -175,7 +175,7 @@ class FlexibleChecksumsResponseInterceptorTest {
175175
val op = newTestOperation<TestInput>(req)
176176

177177
op.interceptors.add(
178-
FlexibleChecksumsResponseInterceptor<TestInput>(
178+
FlexibleChecksumsResponseInterceptor(
179179
responseValidationRequired = false,
180180
responseChecksumValidation = HttpChecksumConfigOption.WHEN_REQUIRED,
181181
),
@@ -219,7 +219,7 @@ class FlexibleChecksumsResponseInterceptorTest {
219219
val op = newTestOperation<TestInput>(req)
220220

221221
op.interceptors.add(
222-
FlexibleChecksumsResponseInterceptor<TestInput>(
222+
FlexibleChecksumsResponseInterceptor(
223223
responseValidationRequired = testCase.responseValidationRequired,
224224
responseChecksumValidation = testCase.responseChecksumValidation,
225225
),

runtime/protocol/http-client/common/test/aws/smithy/kotlin/runtime/http/interceptors/HttpChecksumRequiredInterceptorTest.kt

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package aws.smithy.kotlin.runtime.http.interceptors
77

88
import aws.smithy.kotlin.runtime.collections.get
9+
import aws.smithy.kotlin.runtime.hashing.Crc32
910
import aws.smithy.kotlin.runtime.http.HttpBody
1011
import aws.smithy.kotlin.runtime.http.SdkHttpClient
1112
import aws.smithy.kotlin.runtime.http.operation.HttpOperationContext
@@ -14,6 +15,7 @@ import aws.smithy.kotlin.runtime.http.operation.roundTrip
1415
import aws.smithy.kotlin.runtime.http.request.HttpRequestBuilder
1516
import aws.smithy.kotlin.runtime.httptest.TestEngine
1617
import aws.smithy.kotlin.runtime.io.SdkByteReadChannel
18+
import aws.smithy.kotlin.runtime.text.encoding.encodeBase64String
1719
import kotlinx.coroutines.test.runTest
1820
import kotlin.test.Test
1921
import kotlin.test.assertEquals
@@ -29,10 +31,9 @@ class HttpChecksumRequiredInterceptorTest {
2931
}
3032
val op = newTestOperation<Unit, Unit>(req, Unit)
3133

34+
op.context.attributes[HttpOperationContext.DefaultChecksumAlgorithm] = "MD5"
3235
op.interceptors.add(
33-
HttpChecksumRequiredInterceptor<Unit> {
34-
true
35-
},
36+
HttpChecksumRequiredInterceptor(),
3637
)
3738

3839
val expected = "RG22oBSZFmabBbkzVGRi4w=="
@@ -41,6 +42,29 @@ class HttpChecksumRequiredInterceptorTest {
4142
assertEquals(expected, call.request.headers["Content-MD5"])
4243
}
4344

45+
@Test
46+
fun itSetsContentCrc32Header() = runTest {
47+
val testBody = "<Foo>bar</Foo>".encodeToByteArray()
48+
49+
val req = HttpRequestBuilder().apply {
50+
body = HttpBody.fromBytes(testBody)
51+
}
52+
val op = newTestOperation<Unit, Unit>(req, Unit)
53+
54+
op.context.attributes[HttpOperationContext.DefaultChecksumAlgorithm] = "CRC32"
55+
op.interceptors.add(
56+
HttpChecksumRequiredInterceptor(),
57+
)
58+
59+
val crc32 = Crc32()
60+
crc32.update(testBody)
61+
val expected = crc32.digest().encodeBase64String()
62+
63+
op.roundTrip(client, Unit)
64+
val call = op.context.attributes[HttpOperationContext.HttpCallList].first()
65+
assertEquals(expected, call.request.headers["x-amz-checksum-crc32"])
66+
}
67+
4468
@Test
4569
fun itOnlySetsHeaderForBytesContent() = runTest {
4670
val req = HttpRequestBuilder().apply {
@@ -50,10 +74,9 @@ class HttpChecksumRequiredInterceptorTest {
5074
}
5175
val op = newTestOperation<Unit, Unit>(req, Unit)
5276

77+
op.context.attributes[HttpOperationContext.DefaultChecksumAlgorithm] = "MD5"
5378
op.interceptors.add(
54-
HttpChecksumRequiredInterceptor<Unit> {
55-
true
56-
},
79+
HttpChecksumRequiredInterceptor(),
5780
)
5881

5982
op.roundTrip(client, Unit)
@@ -69,9 +92,7 @@ class HttpChecksumRequiredInterceptorTest {
6992
val op = newTestOperation<Unit, Unit>(req, Unit)
7093

7194
op.interceptors.add(
72-
HttpChecksumRequiredInterceptor<Unit> {
73-
false // interceptor disabled
74-
},
95+
HttpChecksumRequiredInterceptor(),
7596
)
7697

7798
op.roundTrip(client, Unit)

0 commit comments

Comments
 (0)