• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  * SPDX-License-Identifier: Apache-2.0.
4  */
5 
6 package software.amazon.awssdk.crt.http;
7 
8 import software.amazon.awssdk.crt.CRT;
9 import software.amazon.awssdk.crt.CrtResource;
10 import software.amazon.awssdk.crt.CrtRuntimeException;
11 
12 import java.util.concurrent.CompletableFuture;
13 
14 /**
15  * An base class represents a single Http Request/Response for both HTTP/1.1 and
16  * HTTP/2 and wraps the native resources from the aws-c-http library.
17  *
18  * Can be used to update the Window size, or to abort the stream early in the
19  * middle of sending/receiving Http Bodies.
20  */
21 public class HttpStreamBase extends CrtResource {
22 
23     /*
24      * Native code will call this constructor during
25      * HttpClientConnection.makeRequest()
26      */
HttpStreamBase(long ptr)27     protected HttpStreamBase(long ptr) {
28         acquireNativeHandle(ptr);
29     }
30 
31     /**
32      * Determines whether a resource releases its dependencies at the same time the
33      * native handle is released or if it waits.
34      * Resources that wait are responsible for calling releaseReferences() manually.
35      */
36     @Override
canReleaseReferencesImmediately()37     protected boolean canReleaseReferencesImmediately() {
38         return true;
39     }
40 
41     /**
42      * Cleans up the stream's associated native handle
43      */
44     @Override
releaseNativeHandle()45     protected void releaseNativeHandle() {
46         if (!isNull()) {
47             httpStreamBaseRelease(getNativeHandle());
48         }
49     }
50 
51     /*******************************************************************************
52      * Shared method
53      ******************************************************************************/
54 
55     /**
56      * Increment the flow-control window, so that response data continues downloading.
57      * <p>
58      * If {@link HttpClientConnectionManagerOptions#withManualWindowManagement} was set true,
59      * each HTTP stream has a flow-control window that shrinks as response
60      * body data is downloaded (headers do not affect the size of the window).
61      * {@link HttpClientConnectionManagerOptions#withWindowSize} sets the starting size for each HTTP stream's window.
62      * Whenever the window reaches zero, data stops downloading.
63      * Increment the window to keep data flowing.
64      * Maintain a larger window to keep up a high download throughput,
65      * parts cannot download in parallel unless the window is large enough to hold multiple parts.
66      * Maintain a smaller window to limit the amount of data buffered in memory.
67      * <p>
68      * If manual window management is disabled this call has no effect.
69      *
70      * @param windowSize How many bytes to increment the sliding window by.
71      * @see HttpClientConnectionManagerOptions#withManualWindowManagement
72      */
incrementWindow(int windowSize)73     public void incrementWindow(int windowSize) {
74         if (windowSize < 0) {
75             throw new IllegalArgumentException("windowSize must be >= 0. Actual value: " + windowSize);
76         }
77         if (!isNull()) {
78             httpStreamBaseIncrementWindow(getNativeHandle(), windowSize);
79         }
80     }
81 
82     /**
83      * Activates the client stream.
84      */
activate()85     public void activate() {
86         if (!isNull()) {
87             httpStreamBaseActivate(getNativeHandle(), this);
88         }
89     }
90 
91     /**
92      * Retrieves the Http Response Status Code
93      *
94      * @return The Http Response Status Code
95      */
getResponseStatusCode()96     public int getResponseStatusCode() {
97         if (!isNull()) {
98             return httpStreamBaseGetResponseStatusCode(getNativeHandle());
99         }
100         throw new IllegalStateException("Can't get Status Code on Closed Stream");
101     }
102 
103     /*******************************************************************************
104      * Native methods
105      ******************************************************************************/
106 
httpStreamBaseRelease(long http_stream)107     private static native void httpStreamBaseRelease(long http_stream);
108 
httpStreamBaseIncrementWindow(long http_stream, int window_size)109     private static native void httpStreamBaseIncrementWindow(long http_stream, int window_size);
110 
httpStreamBaseActivate(long http_stream, HttpStreamBase streamObj)111     private static native void httpStreamBaseActivate(long http_stream, HttpStreamBase streamObj);
112 
httpStreamBaseGetResponseStatusCode(long http_stream)113     private static native int httpStreamBaseGetResponseStatusCode(long http_stream);
114 }
115