@@ -12,6 +12,7 @@ import aws.smithy.kotlin.runtime.client.config.HttpChecksumConfigOption
1212import aws.smithy.kotlin.runtime.collections.AttributeKey
1313import aws.smithy.kotlin.runtime.hashing.toHashFunction
1414import aws.smithy.kotlin.runtime.http.HttpBody
15+ import aws.smithy.kotlin.runtime.http.readAll
1516import aws.smithy.kotlin.runtime.http.request.HttpRequest
1617import aws.smithy.kotlin.runtime.http.response.HttpResponse
1718import aws.smithy.kotlin.runtime.http.response.copy
@@ -82,14 +83,33 @@ public class FlexibleChecksumsResponseInterceptor<I>(
8283 .removePrefix(" x-amz-checksum-" )
8384 .toHashFunction() ? : throw ClientException (" Could not parse checksum algorithm from header $checksumHeader " )
8485
85- logger.debug { " Validating checksum during deserialization from $checksumHeader " }
86-
87- // Wrap the response body in a hashing body
88- return context.protocolResponse.copy(
89- body = context.protocolResponse.body
90- .toHashingBody(checksumAlgorithm, context.protocolResponse.body.contentLength)
91- .toChecksumValidatingBody(serviceChecksumValue),
92- )
86+ when (val bodyType = context.protocolResponse.body) {
87+ is HttpBody .Bytes -> {
88+ logger.debug { " Validating checksum before deserialization from $checksumHeader " }
89+
90+ checksumAlgorithm.update(
91+ context.protocolResponse.body.readAll() ? : byteArrayOf(),
92+ )
93+ val sdkChecksumValue = checksumAlgorithm.digest().encodeBase64String()
94+
95+ validateAndThrow(
96+ serviceChecksumValue,
97+ sdkChecksumValue,
98+ )
99+
100+ return context.protocolResponse
101+ }
102+ is HttpBody .SourceContent , is HttpBody .ChannelContent -> {
103+ logger.debug { " Validating checksum during deserialization from $checksumHeader " }
104+
105+ return context.protocolResponse.copy(
106+ body = context.protocolResponse.body
107+ .toHashingBody(checksumAlgorithm, context.protocolResponse.body.contentLength)
108+ .toChecksumValidatingBody(serviceChecksumValue),
109+ )
110+ }
111+ else -> throw IllegalStateException (" Http body type '$bodyType ' is not supported for flexible checksums." )
112+ }
93113 }
94114}
95115
0 commit comments