@@ -45,13 +45,11 @@ public final class S3CrtResponseHandlerAdapter implements S3MetaRequestResponseH
45
45
46
46
private final SimplePublisher <ByteBuffer > responsePublisher = new SimplePublisher <>();
47
47
48
- private final SdkHttpResponse .Builder initialHeadersResponse = SdkHttpResponse .builder ();
48
+ private final SdkHttpResponse .Builder respBuilder = SdkHttpResponse .builder ();
49
49
private volatile S3MetaRequest metaRequest ;
50
50
51
51
private final PublisherListener <S3MetaRequestProgress > progressListener ;
52
52
53
- private volatile boolean responseHandlingInitiated ;
54
-
55
53
public S3CrtResponseHandlerAdapter (CompletableFuture <Void > executeFuture ,
56
54
SdkAsyncHttpResponseHandler responseHandler ,
57
55
PublisherListener <S3MetaRequestProgress > progressListener ) {
@@ -62,17 +60,17 @@ public S3CrtResponseHandlerAdapter(CompletableFuture<Void> executeFuture,
62
60
63
61
@ Override
64
62
public void onResponseHeaders (int statusCode , HttpHeader [] headers ) {
65
- // Note, we cannot call responseHandler.onHeaders() here because the response status code and headers may not represent
66
- // whether the request has succeeded or not (e.g. if this is for a HeadObject call that CRT calls under the hood). We
67
- // need to rely on onResponseBody/onFinished being called to determine this.
68
- populateSdkHttpResponse (initialHeadersResponse , statusCode , headers );
63
+ for (HttpHeader h : headers ) {
64
+ respBuilder .appendHeader (h .getName (), h .getValue ());
65
+ }
66
+
67
+ respBuilder .statusCode (statusCode );
68
+ responseHandler .onHeaders (respBuilder .build ());
69
+ responseHandler .onStream (responsePublisher );
69
70
}
70
71
71
72
@ Override
72
73
public int onResponseBody (ByteBuffer bodyBytesIn , long objectRangeStart , long objectRangeEnd ) {
73
- // See reasoning in onResponseHeaders for why we call this here and not there.
74
- initiateResponseHandling (initialHeadersResponse .build ());
75
-
76
74
if (bodyBytesIn == null ) {
77
75
failResponseHandlerAndFuture (new IllegalStateException ("ByteBuffer delivered is null" ));
78
76
return 0 ;
@@ -100,10 +98,6 @@ public void onFinished(S3FinishedResponseContext context) {
100
98
if (crtCode != CRT .AWS_CRT_SUCCESS ) {
101
99
handleError (context );
102
100
} else {
103
- // onResponseBody() is not invoked for responses with no content, so we may not have invoked
104
- // SdkAsyncHttpResponseHandler#onHeaders yet.
105
- // See also reasoning in onResponseHeaders for why we call this here and not there.
106
- initiateResponseHandling (initialHeadersResponse .build ());
107
101
onSuccessfulResponseComplete ();
108
102
}
109
103
}
@@ -133,14 +127,10 @@ public void cancelRequest() {
133
127
134
128
private void handleError (S3FinishedResponseContext context ) {
135
129
int crtCode = context .getErrorCode ();
136
- HttpHeader [] headers = context .getErrorHeaders ();
137
130
int responseStatus = context .getResponseStatus ();
138
131
byte [] errorPayload = context .getErrorPayload ();
139
132
140
133
if (isErrorResponse (responseStatus ) && errorPayload != null ) {
141
- SdkHttpResponse .Builder errorResponse = populateSdkHttpResponse (SdkHttpResponse .builder (),
142
- responseStatus , headers );
143
- initiateResponseHandling (errorResponse .build ());
144
134
onErrorResponseComplete (errorPayload );
145
135
} else {
146
136
Throwable cause = context .getCause ();
@@ -152,14 +142,6 @@ private void handleError(S3FinishedResponseContext context) {
152
142
}
153
143
}
154
144
155
- private void initiateResponseHandling (SdkHttpResponse response ) {
156
- if (!responseHandlingInitiated ) {
157
- responseHandlingInitiated = true ;
158
- responseHandler .onHeaders (response );
159
- responseHandler .onStream (responsePublisher );
160
- }
161
- }
162
-
163
145
private void onErrorResponseComplete (byte [] errorPayload ) {
164
146
responsePublisher .send (ByteBuffer .wrap (errorPayload ))
165
147
.thenRun (responsePublisher ::complete )
@@ -194,17 +176,6 @@ public void onProgress(S3MetaRequestProgress progress) {
194
176
this .progressListener .subscriberOnNext (progress );
195
177
}
196
178
197
- private static SdkHttpResponse .Builder populateSdkHttpResponse (SdkHttpResponse .Builder respBuilder ,
198
- int statusCode , HttpHeader [] headers ) {
199
- if (headers != null ) {
200
- for (HttpHeader h : headers ) {
201
- respBuilder .appendHeader (h .getName (), h .getValue ());
202
- }
203
- }
204
- respBuilder .statusCode (statusCode );
205
- return respBuilder ;
206
- }
207
-
208
179
private static class NoOpPublisherListener implements PublisherListener <S3MetaRequestProgress > {
209
180
}
210
181
}
0 commit comments