Skip to content

Commit 5326cdc

Browse files
committed
stream cacnel
1 parent 9807b97 commit 5326cdc

11 files changed

Lines changed: 78 additions & 58 deletions

File tree

crt/aws-c-auth

crt/aws-lc

src/main/java/software/amazon/awssdk/crt/http/HttpStreamBase.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,23 @@ public int getResponseStatusCode() {
9797
}
9898

9999
/**
100-
* Cancels the stream.
100+
* Cancels the stream with the default error code (AWS_ERROR_HTTP_STREAM_CANCELLED).
101+
* <p>
102+
* For HTTP/1.1 streams, this is equivalent to closing the connection.
103+
* For HTTP/2 streams, this sends a RST_STREAM frame with AWS_HTTP2_ERR_CANCEL.
104+
* <p>
105+
* The stream will complete with AWS_ERROR_HTTP_STREAM_CANCELLED, unless the stream is
106+
* already completing for other reasons, or the stream is not activated,
107+
* in which case this call will have no effect.
108+
*/
109+
public void cancel() {
110+
if (!isNull()) {
111+
httpStreamBaseCancelDefaultError(getNativeHandle());
112+
}
113+
}
114+
115+
/**
116+
* Cancels the stream with a specific error code.
101117
* <p>
102118
* For HTTP/1.1 streams, this is equivalent to closing the connection.
103119
* For HTTP/2 streams, this sends a RST_STREAM frame with AWS_HTTP2_ERR_CANCEL.
@@ -107,7 +123,6 @@ public int getResponseStatusCode() {
107123
* in which case this call will have no effect.
108124
*
109125
* @param errorCode The CRT error code to use when completing the stream.
110-
* Use CRT.awsErrorCode() to convert AWS error codes.
111126
*/
112127
public void cancel(int errorCode) {
113128
if (!isNull()) {
@@ -127,5 +142,7 @@ public void cancel(int errorCode) {
127142

128143
private static native int httpStreamBaseGetResponseStatusCode(long http_stream);
129144

145+
private static native void httpStreamBaseCancelDefaultError(long http_stream);
146+
130147
private static native void httpStreamBaseCancel(long http_stream, int error_code);
131148
}

src/native/http_request_response.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -706,11 +706,10 @@ JNIEXPORT void JNICALL Java_software_amazon_awssdk_crt_http_HttpStreamBase_httpS
706706
aws_http_stream_update_window(stream, window_update);
707707
}
708708

709-
JNIEXPORT void JNICALL Java_software_amazon_awssdk_crt_http_HttpStreamBase_httpStreamBaseCancel(
709+
JNIEXPORT void JNICALL Java_software_amazon_awssdk_crt_http_HttpStreamBase_httpStreamBaseCancelDefaultError(
710710
JNIEnv *env,
711711
jclass jni_class,
712-
jlong jni_binding,
713-
jint error_code) {
712+
jlong jni_binding) {
714713

715714
(void)jni_class;
716715
aws_cache_jni_ids(env);
@@ -723,8 +722,8 @@ JNIEXPORT void JNICALL Java_software_amazon_awssdk_crt_http_HttpStreamBase_httpS
723722
return;
724723
}
725724

726-
AWS_LOGF_TRACE(AWS_LS_HTTP_STREAM, "Cancelling Stream. stream: %p, error_code: %d", (void *)stream, error_code);
727-
aws_http_stream_cancel(stream, error_code);
725+
AWS_LOGF_TRACE(AWS_LS_HTTP_STREAM, "Cancelling Stream with default error. stream: %p", (void *)stream);
726+
aws_http_stream_cancel_default_error(stream);
728727
}
729728

730729
JNIEXPORT void JNICALL Java_software_amazon_awssdk_crt_http_Http2Stream_http2StreamResetStream(

src/test/java/software/amazon/awssdk/crt/test/Http2ClientLocalHostTest.java

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -341,44 +341,4 @@ public void onResponseComplete(HttpStreamBase stream, int errorCode) {
341341
CrtResource.waitForNoResources();
342342
}
343343

344-
@Test
345-
public void testHttp2StreamCancel() throws Exception {
346-
skipIfAndroid();
347-
skipIfLocalhostUnavailable();
348-
URI uri = new URI(String.format("https://localhost:%d/echo", LOCAL_HTTPS_PORT));
349-
try (Http2StreamManager streamManager = createStreamManager(uri, 100)) {
350-
long bodyLength = 250000L;
351-
352-
Http2Request request = createHttp2Request("GET", uri, 0);
353-
request.addHeader(new HttpHeader("x-repeat-data", String.valueOf(bodyLength)));
354-
/* Get a slow response to make sure we cancel before finishes */
355-
request.addHeader(new HttpHeader("x-slow-response", "true"));
356-
357-
final CompletableFuture<Void> requestCompleteFuture = new CompletableFuture<Void>();
358-
final int expectedErrorCode = 0x0832; // Some Random error code
359-
CompletableFuture<Http2Stream> acquireCompleteFuture = streamManager.acquireStream(request,
360-
new HttpStreamBaseResponseHandler() {
361-
@Override
362-
public void onResponseHeaders(HttpStreamBase stream, int responseStatusCode, int blockType,
363-
HttpHeader[] nextHeaders) {
364-
// Cancel the HTTP/2 stream immediately upon receiving headers with specific
365-
// error code
366-
stream.cancel(expectedErrorCode);
367-
}
368-
369-
@Override
370-
public void onResponseComplete(HttpStreamBase stream, int errorCode) {
371-
372-
Assert.assertTrue(errorCode == expectedErrorCode);
373-
stream.close();
374-
requestCompleteFuture.complete(null);
375-
}
376-
});
377-
378-
acquireCompleteFuture.get(3, TimeUnit.SECONDS);
379-
requestCompleteFuture.get(10, TimeUnit.SECONDS);
380-
}
381-
CrtResource.logNativeResources();
382-
CrtResource.waitForNoResources();
383-
}
384344
}

src/test/java/software/amazon/awssdk/crt/test/Http2StreamManagerTest.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,4 +293,48 @@ public void onResponseComplete(HttpStreamBase stream, int errorCode) {
293293
CrtResource.logNativeResources();
294294
CrtResource.waitForNoResources();
295295
}
296+
297+
@Test
298+
public void testHttp2StreamCancel() throws Exception {
299+
skipIfAndroid();
300+
skipIfLocalhostUnavailable();
301+
302+
URI uri = new URI(endpoint);
303+
String large_file_path = "/crt-canary-obj.txt";
304+
int maxConcurrentStreams = 20;
305+
try (Http2StreamManager streamManager = createStreamManager(uri, 100, maxConcurrentStreams)) {
306+
307+
Http2Request request = createHttp2Request("GET", endpoint, large_file_path, EMPTY_BODY);
308+
309+
final CompletableFuture<Integer> requestCompleteFuture = new CompletableFuture<>();
310+
311+
streamManager.acquireStream(request, new HttpStreamBaseResponseHandler() {
312+
@Override
313+
public void onResponseHeaders(HttpStreamBase stream, int responseStatusCode, int blockType,
314+
HttpHeader[] nextHeaders) {
315+
// Cancel the HTTP/2 stream using the default error code (AWS_ERROR_HTTP_STREAM_CANCELLED)
316+
stream.cancel();
317+
}
318+
319+
@Override
320+
public void onResponseComplete(HttpStreamBase stream, int errorCode) {
321+
requestCompleteFuture.complete(errorCode);
322+
stream.close();
323+
}
324+
}).whenComplete((stream, throwable) -> {
325+
if (throwable != null) {
326+
requestCompleteFuture.completeExceptionally(throwable);
327+
}
328+
});
329+
330+
331+
int actualErrorCode = requestCompleteFuture.get(60, TimeUnit.SECONDS);
332+
333+
// The HTTP/2 stream should complete with AWS_ERROR_HTTP_STREAM_CANCELLED
334+
Assert.assertEquals("Error code should be AWS_ERROR_HTTP_STREAM_CANCELLED",
335+
"AWS_ERROR_HTTP_STREAM_CANCELLED", CRT.awsErrorName(actualErrorCode));
336+
}
337+
CrtResource.logNativeResources();
338+
CrtResource.waitForNoResources();
339+
}
296340
}

0 commit comments

Comments
 (0)