Search in sources :

Example 26 with HttpRequest

use of software.amazon.awssdk.crt.http.HttpRequest in project aws-sdk-java-v2 by aws.

the class CrtHttpRequestConverterTest method request_withHeaders_isConvertedToCrtFormat.

@Test
public void request_withHeaders_isConvertedToCrtFormat() {
    String data = "data";
    SdkHttpFullRequest request = SdkHttpFullRequest.builder().method(SdkHttpMethod.POST).contentStreamProvider(() -> new ByteArrayInputStream(data.getBytes(StandardCharsets.UTF_8))).putHeader("x-amz-archive-description", "test  test").putHeader("Host", "demo.us-east-1.amazonaws.com").encodedPath("/").uri(URI.create("https://demo.us-east-1.amazonaws.com")).build();
    HttpRequest crtHttpRequest = converter.requestToCrt(request);
    assertThat(crtHttpRequest.getMethod()).isEqualTo("POST");
    assertThat(crtHttpRequest.getEncodedPath()).isEqualTo("/");
    List<HttpHeader> headers = crtHttpRequest.getHeaders();
    HttpHeader[] headersAsArray = crtHttpRequest.getHeadersAsArray();
    assertThat(headers.size()).isEqualTo(2);
    assertThat(headersAsArray.length).isEqualTo(2);
    assertThat(headers.get(0).getName()).isEqualTo("Host");
    assertThat(headers.get(0).getValue()).isEqualTo("demo.us-east-1.amazonaws.com");
    assertThat(headersAsArray[1].getName()).isEqualTo("x-amz-archive-description");
    assertThat(headersAsArray[1].getValue()).isEqualTo("test  test");
    assertStream(data, crtHttpRequest.getBodyStream());
    assertHttpRequestSame(request, crtHttpRequest);
}
Also used : HttpRequest(software.amazon.awssdk.crt.http.HttpRequest) HttpHeader(software.amazon.awssdk.crt.http.HttpHeader) SdkHttpFullRequest(software.amazon.awssdk.http.SdkHttpFullRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.junit.jupiter.api.Test)

Example 27 with HttpRequest

use of software.amazon.awssdk.crt.http.HttpRequest in project aws-sdk-java-v2 by aws.

the class SignerTestUtils method verifyEcdsaSignature.

public static boolean verifyEcdsaSignature(SdkHttpFullRequest request, String expectedCanonicalRequest, AwsSigningConfig signingConfig, String signatureValue) {
    CrtHttpRequestConverter requestConverter = new CrtHttpRequestConverter();
    HttpRequest crtRequest = requestConverter.requestToCrt(SigningUtils.sanitizeSdkRequestForCrtSigning(request));
    return AwsSigningUtils.verifySigv4aEcdsaSignature(crtRequest, expectedCanonicalRequest, signingConfig, signatureValue.getBytes(), TEST_VERIFICATION_PUB_X, TEST_VERIFICATION_PUB_Y);
}
Also used : HttpRequest(software.amazon.awssdk.crt.http.HttpRequest) CrtHttpRequestConverter(software.amazon.awssdk.authcrt.signer.internal.CrtHttpRequestConverter)

Example 28 with HttpRequest

use of software.amazon.awssdk.crt.http.HttpRequest in project aws-sdk-java-v2 by aws.

the class AwsCrt4aSigningAdapter method signRequest.

public SdkHttpFullRequest signRequest(SdkHttpFullRequest request, AwsSigningConfig signingConfig) {
    HttpRequest crtRequest = requestConverter.requestToCrt(SigningUtils.sanitizeSdkRequestForCrtSigning(request));
    CompletableFuture<HttpRequest> future = AwsSigner.signRequest(crtRequest, signingConfig);
    try {
        HttpRequest signedRequest = future.get();
        return requestConverter.crtRequestToHttp(request, signedRequest);
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        throw SdkClientException.create("The thread got interrupted while attempting to sign request: " + e.getMessage(), e);
    } catch (Exception e) {
        throw SdkClientException.create("Unable to sign request: " + e.getMessage(), e);
    }
}
Also used : HttpRequest(software.amazon.awssdk.crt.http.HttpRequest) SdkClientException(software.amazon.awssdk.core.exception.SdkClientException)

Example 29 with HttpRequest

use of software.amazon.awssdk.crt.http.HttpRequest in project aws-sdk-java-v2 by aws.

the class CrtRequestExecutor method toCrtRequest.

private static HttpRequest toCrtRequest(AsyncExecuteRequest asyncRequest, AwsCrtAsyncHttpStreamAdapter crtToSdkAdapter) {
    URI uri = asyncRequest.request().getUri();
    SdkHttpRequest sdkRequest = asyncRequest.request();
    String method = sdkRequest.method().name();
    String encodedPath = sdkRequest.encodedPath();
    if (encodedPath == null || encodedPath.length() == 0) {
        encodedPath = "/";
    }
    String encodedQueryString = sdkRequest.encodedQueryParameters().map(value -> "?" + value).orElse("");
    HttpHeader[] crtHeaderArray = asArray(createHttpHeaderList(uri, asyncRequest));
    return new HttpRequest(method, encodedPath + encodedQueryString, crtHeaderArray, crtToSdkAdapter);
}
Also used : SdkHttpRequest(software.amazon.awssdk.http.SdkHttpRequest) Logger(software.amazon.awssdk.utils.Logger) IOException(java.io.IOException) CompletableFuture(java.util.concurrent.CompletableFuture) HttpRequest(software.amazon.awssdk.crt.http.HttpRequest) FunctionalUtils.invokeSafely(software.amazon.awssdk.utils.FunctionalUtils.invokeSafely) ArrayList(java.util.ArrayList) HttpClientConnection(software.amazon.awssdk.crt.http.HttpClientConnection) AsyncExecuteRequest(software.amazon.awssdk.http.async.AsyncExecuteRequest) CrtRuntimeException(software.amazon.awssdk.crt.CrtRuntimeException) List(java.util.List) Header(software.amazon.awssdk.http.Header) SdkCancellationException(software.amazon.awssdk.http.SdkCancellationException) Optional(java.util.Optional) URI(java.net.URI) SdkInternalApi(software.amazon.awssdk.annotations.SdkInternalApi) SdkAsyncHttpResponseHandler(software.amazon.awssdk.http.async.SdkAsyncHttpResponseHandler) HttpHeader(software.amazon.awssdk.crt.http.HttpHeader) SdkHttpRequest(software.amazon.awssdk.http.SdkHttpRequest) HttpRequest(software.amazon.awssdk.crt.http.HttpRequest) SdkHttpRequest(software.amazon.awssdk.http.SdkHttpRequest) HttpHeader(software.amazon.awssdk.crt.http.HttpHeader) URI(java.net.URI)

Example 30 with HttpRequest

use of software.amazon.awssdk.crt.http.HttpRequest in project aws-sdk-java-v2 by aws.

the class CrtRequestExecutor method execute.

public CompletableFuture<Void> execute(CrtRequestContext executionContext) {
    CompletableFuture<Void> requestFuture = createExecutionFuture(executionContext.sdkRequest());
    // When a Connection is ready from the Connection Pool, schedule the Request on the connection
    CompletableFuture<HttpClientConnection> httpClientConnectionCompletableFuture = executionContext.crtConnPool().acquireConnection();
    httpClientConnectionCompletableFuture.whenComplete((crtConn, throwable) -> {
        AsyncExecuteRequest asyncRequest = executionContext.sdkRequest();
        // If we didn't get a connection for some reason, fail the request
        if (throwable != null) {
            handleFailure(new IOException("An exception occurred when acquiring connection", throwable), requestFuture, asyncRequest.responseHandler());
            return;
        }
        AwsCrtAsyncHttpStreamAdapter crtToSdkAdapter = new AwsCrtAsyncHttpStreamAdapter(crtConn, requestFuture, asyncRequest, executionContext.readBufferSize());
        HttpRequest crtRequest = toCrtRequest(asyncRequest, crtToSdkAdapter);
        // Submit the Request on this Connection
        invokeSafely(() -> {
            try {
                crtConn.makeRequest(crtRequest, crtToSdkAdapter).activate();
            } catch (IllegalStateException | CrtRuntimeException e) {
                log.debug(() -> "An exception occurred when making the request", e);
                handleFailure(new IOException("An exception occurred when making the request", e), requestFuture, asyncRequest.responseHandler());
            }
        });
    });
    return requestFuture;
}
Also used : SdkHttpRequest(software.amazon.awssdk.http.SdkHttpRequest) HttpRequest(software.amazon.awssdk.crt.http.HttpRequest) AsyncExecuteRequest(software.amazon.awssdk.http.async.AsyncExecuteRequest) HttpClientConnection(software.amazon.awssdk.crt.http.HttpClientConnection) CrtRuntimeException(software.amazon.awssdk.crt.CrtRuntimeException) IOException(java.io.IOException)

Aggregations

HttpRequest (software.amazon.awssdk.crt.http.HttpRequest)47 HttpHeader (software.amazon.awssdk.crt.http.HttpHeader)26 Test (org.junit.Test)22 URI (java.net.URI)15 ByteBuffer (java.nio.ByteBuffer)13 HttpRequestBodyStream (software.amazon.awssdk.crt.http.HttpRequestBodyStream)13 AwsSigningConfig (software.amazon.awssdk.crt.auth.signing.AwsSigningConfig)11 CompletableFuture (java.util.concurrent.CompletableFuture)8 StaticCredentialsProvider (software.amazon.awssdk.crt.auth.credentials.StaticCredentialsProvider)8 SdkHttpRequest (software.amazon.awssdk.http.SdkHttpRequest)8 List (java.util.List)7 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)7 ArrayList (java.util.ArrayList)6 AwsSigningResult (software.amazon.awssdk.crt.auth.signing.AwsSigningResult)6 AsyncExecuteRequest (software.amazon.awssdk.http.async.AsyncExecuteRequest)6 S3MetaRequestOptions (software.amazon.awssdk.crt.s3.S3MetaRequestOptions)5 Optional (java.util.Optional)4 S3MetaRequest (software.amazon.awssdk.crt.s3.S3MetaRequest)4 StandardCharsets (java.nio.charset.StandardCharsets)3 Arrays (java.util.Arrays)3