-
Notifications
You must be signed in to change notification settings - Fork 43
Bind out the write data API #981
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
67a8760
f8814b1
71080d7
c019fea
3ba53e9
738be52
e72343a
f4bf53d
97257be
35b4efc
52e2841
16e1877
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,7 +5,11 @@ | |
|
|
||
| package software.amazon.awssdk.crt.http; | ||
|
|
||
| import software.amazon.awssdk.crt.CRT; | ||
| import software.amazon.awssdk.crt.CrtResource; | ||
| import software.amazon.awssdk.crt.CrtRuntimeException; | ||
|
|
||
| import java.util.concurrent.CompletableFuture; | ||
|
|
||
| /** | ||
| * An base class represents a single Http Request/Response for both HTTP/1.1 and | ||
|
|
@@ -112,6 +116,63 @@ public void cancel() { | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * Completion interface for writing data to an http stream. | ||
| */ | ||
| public interface HttpStreamWriteDataCompletionCallback { | ||
| void onWriteDataCompleted(int errorCode); | ||
| } | ||
|
|
||
| /** | ||
| * Write data to an HTTP stream. Works for both HTTP/1.1 and HTTP/2. | ||
| * The stream must have been created with {@code useManualDataWrites = true}. | ||
| * You must call activate() before using this function. | ||
| * | ||
| * @param data data to send, or null to write zero bytes. Pass null with | ||
| * endStream=true to signal end-of-body without sending additional data. | ||
| * @param endStream if true, this is the last data to be sent on this stream. | ||
| * @param completionCallback invoked when the data has been flushed or an error occurs. | ||
| */ | ||
| public void writeData(final byte[] data, boolean endStream, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I feel like we can also support taking Meanwhile mention the difference of this and the existing |
||
| final HttpStreamWriteDataCompletionCallback completionCallback) { | ||
| if (isNull()) { | ||
| throw new IllegalStateException("HttpStream has been closed."); | ||
| } | ||
| if (completionCallback == null) { | ||
| throw new IllegalArgumentException("You must supply a completionCallback"); | ||
| } | ||
|
|
||
| int error = httpStreamBaseWriteData(getNativeHandle(), data, endStream, completionCallback); | ||
| if (error != 0) { | ||
| int lastError = CRT.awsLastError(); | ||
| throw new CrtRuntimeException(lastError); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Write data to an HTTP stream. Works for both HTTP/1.1 and HTTP/2. | ||
| * The stream must have been created with {@code useManualDataWrites = true}. | ||
| * You must call activate() before using this function. | ||
| * | ||
| * @param data data to send, or null to write zero bytes. Pass null with | ||
| * endStream=true to signal end-of-body without sending additional data. | ||
| * @param endStream if true, this is the last data to be sent on this stream. | ||
| * @return completable future which completes when data is flushed or an error occurs. | ||
| */ | ||
| public CompletableFuture<Void> writeData(final byte[] data, boolean endStream) { | ||
| CompletableFuture<Void> completionFuture = new CompletableFuture<>(); | ||
|
|
||
| writeData(data, endStream, (errorCode) -> { | ||
| if (errorCode == 0) { | ||
| completionFuture.complete(null); | ||
| } else { | ||
| completionFuture.completeExceptionally(new CrtRuntimeException(errorCode)); | ||
| } | ||
| }); | ||
|
|
||
| return completionFuture; | ||
| } | ||
|
|
||
| /******************************************************************************* | ||
| * Native methods | ||
| ******************************************************************************/ | ||
|
|
@@ -125,4 +186,7 @@ public void cancel() { | |
| private static native int httpStreamBaseGetResponseStatusCode(long http_stream); | ||
|
|
||
| private static native void httpStreamBaseCancelDefaultError(long http_stream); | ||
|
|
||
| private static native int httpStreamBaseWriteData(long http_stream, byte[] data, boolean endStream, | ||
| HttpStreamWriteDataCompletionCallback completionCallback); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IIRC, we have some restriction on with manual write, the request doesn't allow to have an input stream with it. Is that true?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mean the body stream? then yes, when manual writes is on, having a body stream with h1 would fail but h2 is supposed to send them one after the other and is handled by the H2 protocol..
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, that's a very inconsistent behavior, either we document it out clearly, or try to just handle it.
And if we decided to error out for the case, I think we should error out early in the binding, so that it's more clear comparing to our C error handling with one error code and need to look deep into the log to figure out the error