Search in sources :

Example 26 with HttpHeader

use of software.amazon.awssdk.crt.http.HttpHeader in project aws-crt-java by awslabs.

the class SigningTest method createChunkedTrailerTestRequest.

private HttpRequest createChunkedTrailerTestRequest() throws Exception {
    URI uri = new URI("https://s3.amazonaws.com/examplebucket/chunkObject.txt");
    HttpHeader[] requestHeaders = new HttpHeader[] { new HttpHeader("Host", uri.getHost()), new HttpHeader("x-amz-storage-class", "REDUCED_REDUNDANCY"), new HttpHeader("Content-Encoding", "aws-chunked"), new HttpHeader("x-amz-decoded-content-length", "66560"), new HttpHeader("Content-Length", "66824"), new HttpHeader("x-amz-trailer", "first,second,third") };
    return new HttpRequest("PUT", uri.getPath(), requestHeaders, null);
}
Also used : HttpRequest(software.amazon.awssdk.crt.http.HttpRequest) HttpHeader(software.amazon.awssdk.crt.http.HttpHeader) URI(java.net.URI)

Example 27 with HttpHeader

use of software.amazon.awssdk.crt.http.HttpHeader in project aws-crt-java by awslabs.

the class SigningTest method createBadBodyStreamRequest.

private HttpRequest createBadBodyStreamRequest(String method, String path) throws Exception {
    HttpHeader[] requestHeaders = new HttpHeader[] { new HttpHeader("Something", "else") };
    HttpRequestBodyStream badBodyStream = new HttpRequestBodyStream() {

        @Override
        public boolean sendRequestBody(ByteBuffer bodyBytesOut) {
            throw new RuntimeException("Doh");
        }

        @Override
        public boolean resetPosition() {
            return true;
        }
    };
    return new HttpRequest(method, path, requestHeaders, badBodyStream);
}
Also used : HttpRequest(software.amazon.awssdk.crt.http.HttpRequest) HttpRequestBodyStream(software.amazon.awssdk.crt.http.HttpRequestBodyStream) HttpHeader(software.amazon.awssdk.crt.http.HttpHeader) ByteBuffer(java.nio.ByteBuffer)

Example 28 with HttpHeader

use of software.amazon.awssdk.crt.http.HttpHeader in project aws-crt-java by awslabs.

the class S3NativeClientTest method validateCustomHeaders.

private void validateCustomHeaders(List<HttpHeader> generatedHeaders, HttpHeader[] customHeaders) {
    if (customHeaders == null || customHeaders.length == 0) {
        return;
    }
    for (HttpHeader customHeader : customHeaders) {
        int numTimesFound = 0;
        for (HttpHeader generatedHeader : generatedHeaders) {
            if (generatedHeader.getName().equals(customHeader.getName())) {
                assertEquals(generatedHeader.getValue(), customHeader.getValue());
                ++numTimesFound;
            }
        }
        assertEquals(1, numTimesFound);
    }
}
Also used : HttpHeader(software.amazon.awssdk.crt.http.HttpHeader)

Example 29 with HttpHeader

use of software.amazon.awssdk.crt.http.HttpHeader in project aws-crt-java by awslabs.

the class HttpRequestResponseTest method testHttpRequestUnActivated.

@Test
public void testHttpRequestUnActivated() throws Exception {
    skipIfNetworkUnavailable();
    URI uri = new URI("https://httpbin.org");
    HttpHeader[] requestHeaders = new HttpHeader[] { new HttpHeader("Host", uri.getHost()) };
    HttpRequest request = new HttpRequest("GET", "/get", requestHeaders, null);
    CompletableFuture<Void> shutdownComplete = null;
    try (HttpClientConnectionManager connPool = createConnectionPoolManager(uri)) {
        shutdownComplete = connPool.getShutdownCompleteFuture();
        try (HttpClientConnection conn = connPool.acquireConnection().get(60, TimeUnit.SECONDS)) {
            HttpStreamResponseHandler streamHandler = new HttpStreamResponseHandler() {

                @Override
                public void onResponseHeaders(HttpStream stream, int responseStatusCode, int blockType, HttpHeader[] nextHeaders) {
                // do nothing
                }

                @Override
                public void onResponseHeadersDone(HttpStream stream, int blockType) {
                // do nothing
                }

                @Override
                public int onResponseBody(HttpStream stream, byte[] bodyBytesIn) {
                    // do nothing
                    return bodyBytesIn.length;
                }

                @Override
                public void onResponseComplete(HttpStream stream, int errorCode) {
                // do nothing.
                }
            };
            HttpStream stream = conn.makeRequest(request, streamHandler);
            stream.close();
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    if (shutdownComplete != null) {
        shutdownComplete.get();
    }
    CrtResource.waitForNoResources();
}
Also used : HttpRequest(software.amazon.awssdk.crt.http.HttpRequest) HttpHeader(software.amazon.awssdk.crt.http.HttpHeader) HttpClientConnection(software.amazon.awssdk.crt.http.HttpClientConnection) HttpStreamResponseHandler(software.amazon.awssdk.crt.http.HttpStreamResponseHandler) HttpClientConnectionManager(software.amazon.awssdk.crt.http.HttpClientConnectionManager) URI(java.net.URI) HttpStream(software.amazon.awssdk.crt.http.HttpStream) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Test(org.junit.Test)

Example 30 with HttpHeader

use of software.amazon.awssdk.crt.http.HttpHeader in project aws-crt-java by awslabs.

the class HttpRequestResponseTest method testRequest.

public TestHttpResponse testRequest(String method, String endpoint, String path, String requestBody, boolean useChunkedEncoding, int expectedStatus) throws Exception {
    URI uri = new URI(endpoint);
    HttpHeader[] requestHeaders = null;
    if (!useChunkedEncoding) {
        requestHeaders = new HttpHeader[] { new HttpHeader("Host", uri.getHost()), new HttpHeader("Content-Length", Integer.toString(requestBody.getBytes(UTF8).length)) };
    } else {
        requestHeaders = new HttpHeader[] { new HttpHeader("Host", uri.getHost()), new HttpHeader("Transfer-Encoding", "chunked") };
    }
    HttpRequestBodyStream bodyStream = null;
    if (!useChunkedEncoding) {
        final ByteBuffer bodyBytesIn = ByteBuffer.wrap(requestBody.getBytes(UTF8));
        bodyStream = new HttpRequestBodyStream() {

            @Override
            public boolean sendRequestBody(ByteBuffer bodyBytesOut) {
                transferData(bodyBytesIn, bodyBytesOut);
                return bodyBytesIn.remaining() == 0;
            }

            @Override
            public boolean resetPosition() {
                bodyBytesIn.position(0);
                return true;
            }
        };
    }
    HttpRequest request = new HttpRequest(method, path, requestHeaders, bodyStream);
    TestHttpResponse response = null;
    int numAttempts = 0;
    do {
        if (request.getBodyStream() != null) {
            request.getBodyStream().resetPosition();
        }
        numAttempts++;
        response = null;
        try {
            if (useChunkedEncoding) {
                response = getResponse(uri, request, requestBody.getBytes(UTF8));
            } else {
                response = getResponse(uri, request, null);
            }
        } catch (Exception ex) {
        // do nothing just let it retry
        }
    } while ((response == null || shouldRetry(response)) && numAttempts < 3);
    Assert.assertNotEquals(-1, response.blockType);
    boolean hasContentLengthHeader = false;
    for (HttpHeader h : response.headers) {
        if (h.getName().equals("Content-Length")) {
            hasContentLengthHeader = true;
        }
    }
    Assert.assertTrue(hasContentLengthHeader);
    if (response.statusCode < 500) {
        // if the server errored, not our fault
        Assert.assertEquals("Expected and Actual Status Codes don't match", expectedStatus, response.statusCode);
    }
    return response;
}
Also used : HttpRequest(software.amazon.awssdk.crt.http.HttpRequest) HttpRequestBodyStream(software.amazon.awssdk.crt.http.HttpRequestBodyStream) HttpHeader(software.amazon.awssdk.crt.http.HttpHeader) URI(java.net.URI) ByteBuffer(java.nio.ByteBuffer) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Aggregations

HttpHeader (software.amazon.awssdk.crt.http.HttpHeader)35 HttpRequest (software.amazon.awssdk.crt.http.HttpRequest)25 URI (java.net.URI)12 ByteBuffer (java.nio.ByteBuffer)11 Test (org.junit.Test)11 HttpRequestBodyStream (software.amazon.awssdk.crt.http.HttpRequestBodyStream)10 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 ArrayList (java.util.ArrayList)7 List (java.util.List)7 CompletableFuture (java.util.concurrent.CompletableFuture)6 SdkInternalApi (software.amazon.awssdk.annotations.SdkInternalApi)5 String (java.lang.String)4 Optional (java.util.Optional)4 AwsSigningResult (software.amazon.awssdk.crt.auth.signing.AwsSigningResult)4 SdkHttpFullRequest (software.amazon.awssdk.http.SdkHttpFullRequest)4 SdkHttpRequest (software.amazon.awssdk.http.SdkHttpRequest)4 CollectionUtils.isNullOrEmpty (software.amazon.awssdk.utils.CollectionUtils.isNullOrEmpty)4 SdkHttpUtils (software.amazon.awssdk.utils.http.SdkHttpUtils)4 FileNotFoundException (java.io.FileNotFoundException)3 IOException (java.io.IOException)3