27
27
import software .amazon .awssdk .annotations .SdkInternalApi ;
28
28
import software .amazon .awssdk .annotations .SdkTestInternalApi ;
29
29
import software .amazon .awssdk .auth .credentials .AwsCredentialsProvider ;
30
- import software .amazon .awssdk .core .checksums .Algorithm ;
31
30
import software .amazon .awssdk .core .interceptor .trait .HttpChecksum ;
32
31
import software .amazon .awssdk .crt .http .HttpHeader ;
33
32
import software .amazon .awssdk .crt .http .HttpRequest ;
42
41
import software .amazon .awssdk .http .async .SdkAsyncHttpClient ;
43
42
import software .amazon .awssdk .regions .Region ;
44
43
import software .amazon .awssdk .utils .AttributeMap ;
44
+ import software .amazon .awssdk .utils .CollectionUtils ;
45
45
import software .amazon .awssdk .utils .Logger ;
46
46
47
47
/**
@@ -80,10 +80,12 @@ private S3CrtAsyncHttpClient(Builder builder) {
80
80
}
81
81
82
82
@ SdkTestInternalApi
83
- S3CrtAsyncHttpClient (S3Client crtS3Client , S3NativeClientConfiguration nativeClientConfiguration ) {
83
+ S3CrtAsyncHttpClient (S3Client crtS3Client ,
84
+ S3NativeClientConfiguration nativeClientConfiguration ,
85
+ boolean checksumValidationEnabled ) {
84
86
this .crtS3Client = crtS3Client ;
85
87
this .s3NativeClientConfiguration = nativeClientConfiguration ;
86
- this .checksumValidationEnabled = true ;
88
+ this .checksumValidationEnabled = checksumValidationEnabled ;
87
89
}
88
90
89
91
@ Override
@@ -95,13 +97,17 @@ public CompletableFuture<Void> execute(AsyncExecuteRequest asyncRequest) {
95
97
new S3CrtResponseHandlerAdapter (executeFuture , asyncRequest .responseHandler ());
96
98
97
99
S3MetaRequestOptions .MetaRequestType requestType = requestType (asyncRequest );
98
- ChecksumAlgorithm checksumAlgorithm = crtChecksumAlgorithm (asyncRequest );
100
+
101
+ HttpChecksum httpChecksum = asyncRequest .httpExecutionAttributes ().getAttribute (HTTP_CHECKSUM );
102
+ ChecksumAlgorithm checksumAlgorithm = crtChecksumAlgorithm (httpChecksum );
103
+
104
+ boolean validateChecksum = validateResponseChecksum (httpChecksum );
99
105
100
106
S3MetaRequestOptions requestOptions = new S3MetaRequestOptions ()
101
107
.withHttpRequest (httpRequest )
102
108
.withMetaRequestType (requestType )
103
109
.withChecksumAlgorithm (checksumAlgorithm )
104
- .withValidateChecksum (checksumValidationEnabled )
110
+ .withValidateChecksum (validateChecksum )
105
111
.withResponseHandler (responseHandler )
106
112
.withEndpoint (getEndpoint (uri ));
107
113
@@ -112,6 +118,19 @@ public CompletableFuture<Void> execute(AsyncExecuteRequest asyncRequest) {
112
118
return executeFuture ;
113
119
}
114
120
121
+ /**
122
+ * Only validate response checksum if this operation supports checksum validation AND either of the following applies
123
+ * 1. checksum validation is enabled at request level via request validation mode OR
124
+ * 2. checksum validation is enabled at client level
125
+ */
126
+ private boolean validateResponseChecksum (HttpChecksum httpChecksum ) {
127
+ if (httpChecksum == null || CollectionUtils .isNullOrEmpty (httpChecksum .responseAlgorithms ())) {
128
+ return false ;
129
+ }
130
+
131
+ return checksumValidationEnabled || httpChecksum .requestValidationMode () != null ;
132
+ }
133
+
115
134
private static URI getEndpoint (URI uri ) {
116
135
return invokeSafely (() -> new URI (uri .getScheme (), null , uri .getHost (), uri .getPort (), null , null , null ));
117
136
}
@@ -138,28 +157,31 @@ private static S3MetaRequestOptions.MetaRequestType requestType(AsyncExecuteRequ
138
157
return S3MetaRequestOptions .MetaRequestType .DEFAULT ;
139
158
}
140
159
141
- private ChecksumAlgorithm crtChecksumAlgorithm (AsyncExecuteRequest asyncRequest ) {
142
- HttpChecksum httpChecksum = asyncRequest .httpExecutionAttributes ().getAttribute (HTTP_CHECKSUM );
143
-
144
- if (checksumNotApplicable (httpChecksum )) {
160
+ private ChecksumAlgorithm crtChecksumAlgorithm (HttpChecksum httpChecksum ) {
161
+ if (requestChecksumAlgoNotApplicable (httpChecksum )) {
145
162
return null ;
146
163
}
147
164
148
- // TODO: revisit default checksum
149
- Algorithm algorithm = httpChecksum .requestAlgorithm () == null ? Algorithm .CRC32 :
150
- Algorithm .fromValue (httpChecksum .requestAlgorithm ());
151
- return ChecksumAlgorithm .valueOf (algorithm .toString ().toUpperCase ());
152
- }
165
+ if (httpChecksum .requestAlgorithm () == null ) {
166
+ // Only set checksum algorithm by default for streaming operations and operations that require checksum
167
+ if (!(httpChecksum .isRequestStreaming () || httpChecksum .isRequestChecksumRequired ())) {
168
+ return null ;
169
+ }
170
+
171
+ // TODO: revisit default checksum
172
+ return ChecksumAlgorithm .CRC32 ;
173
+ }
153
174
175
+ return ChecksumAlgorithm .valueOf (httpChecksum .requestAlgorithm ().toUpperCase ());
176
+ }
154
177
155
178
/**
156
179
* Checksum algorithm is not applicable to the following situations:
157
- * 1. checksum validation is disabled OR
180
+ * 1. Checksum validation is disabled OR
158
181
* 2. No HttpChecksum Trait for this operation OR
159
- * 3. It's a GET operation.
160
- * 4. It's not a streaming operation
182
+ * 3. It's a GET operation
161
183
*/
162
- private boolean checksumNotApplicable (HttpChecksum httpChecksum ) {
184
+ private boolean requestChecksumAlgoNotApplicable (HttpChecksum httpChecksum ) {
163
185
return !checksumValidationEnabled ||
164
186
httpChecksum == null ||
165
187
httpChecksum .responseAlgorithms () != null ;
0 commit comments