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 HttpStream represents a single HTTP/1.1 specific Http Request/Response. 16 */ 17 public class HttpStream extends HttpStreamBase { 18 19 /* 20 * Native code will call this constructor during 21 * HttpClientConnection.makeRequest() 22 */ HttpStream(long ptr)23 protected HttpStream(long ptr) { 24 super(ptr); 25 } 26 27 /** 28 * Completion interface for writing chunks to an http stream 29 */ 30 public interface HttpStreamWriteChunkCompletionCallback { onChunkCompleted(int errorCode)31 void onChunkCompleted(int errorCode); 32 } 33 34 /** 35 * Use only for Http 1.1 Chunked Encoding. 36 * You must call activate() before using this function. 37 * 38 * @param chunkData chunk of data to send. 39 * @param isFinalChunk if set to true, this will terminate the 40 * request stream. 41 * @param chunkCompletionCallback Invoked upon the data being flushed to the 42 * wire or an error occurring. 43 */ writeChunk(final byte[] chunkData, boolean isFinalChunk, final HttpStreamWriteChunkCompletionCallback chunkCompletionCallback)44 public void writeChunk(final byte[] chunkData, boolean isFinalChunk, 45 final HttpStreamWriteChunkCompletionCallback chunkCompletionCallback) { 46 if (isNull()) { 47 throw new IllegalStateException("HttpStream has been closed."); 48 } 49 50 if (chunkCompletionCallback == null) { 51 throw new IllegalArgumentException("You must supply a chunkCompletionCallback"); 52 } 53 54 if (chunkData == null) { 55 throw new IllegalArgumentException("You must provide a non-null chunkData"); 56 } 57 58 int error = httpStreamWriteChunk(getNativeHandle(), chunkData, isFinalChunk, chunkCompletionCallback); 59 60 if (error != 0) { 61 int lastError = CRT.awsLastError(); 62 throw new CrtRuntimeException(lastError); 63 } 64 } 65 66 /** 67 * Use only for Http 1.1 Chunked Encoding. 68 * You must call activate() before using this function. 69 * 70 * @param chunkData chunk of data to send. 71 * @param isFinalChunk if set to true, this will terminate the request stream. 72 * @return completable future which will complete upon the data being flushed to 73 * the wire or an error occurring. 74 */ writeChunk(final byte[] chunkData, boolean isFinalChunk)75 public CompletableFuture<Void> writeChunk(final byte[] chunkData, boolean isFinalChunk) { 76 CompletableFuture<Void> completionFuture = new CompletableFuture<>(); 77 78 HttpStreamWriteChunkCompletionCallback completionCallback = new HttpStreamWriteChunkCompletionCallback() { 79 @Override 80 public void onChunkCompleted(int errorCode) { 81 if (errorCode == 0) { 82 completionFuture.complete(null); 83 } else { 84 completionFuture.completeExceptionally(new CrtRuntimeException(errorCode)); 85 } 86 } 87 }; 88 89 writeChunk(chunkData, isFinalChunk, completionCallback); 90 return completionFuture; 91 } 92 93 /******************************************************************************* 94 * Native methods 95 ******************************************************************************/ 96 httpStreamWriteChunk(long http_stream, byte[] chunkData, boolean isFinalChunk, HttpStreamWriteChunkCompletionCallback completionCallback)97 private static native int httpStreamWriteChunk(long http_stream, byte[] chunkData, boolean isFinalChunk, 98 HttpStreamWriteChunkCompletionCallback completionCallback); 99 } 100