Search in sources :

Example 76 with LastHttpContent

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.LastHttpContent in project ambry by linkedin.

the class ChannelWriteCallback method responsesWithTransferEncodingChunkedTest.

/**
 * Tests the common workflow of the {@link NettyResponseChannel} i.e., add some content to response body via
 * {@link NettyResponseChannel#write(ByteBuffer, Callback)} and then complete the response.
 * <p/>
 * These responses have the header Transfer-Encoding set to chunked.
 * @throws Exception
 */
@Test
public void responsesWithTransferEncodingChunkedTest() throws Exception {
    String content = "@@randomContent@@@";
    String lastContent = "@@randomLastContent@@@";
    EmbeddedChannel channel = createEmbeddedChannel();
    MockNettyMessageProcessor processor = channel.pipeline().get(MockNettyMessageProcessor.class);
    AtomicLong contentIdGenerator = new AtomicLong(0);
    final int ITERATIONS = 10;
    for (int i = 0; i < ITERATIONS; i++) {
        boolean isKeepAlive = i != (ITERATIONS - 1);
        HttpRequest httpRequest = RestTestUtils.createRequest(HttpMethod.POST, "/", null);
        HttpUtil.setKeepAlive(httpRequest, isKeepAlive);
        channel.writeInbound(httpRequest);
        ArrayList<String> contents = new ArrayList<>();
        for (int j = 0; j <= i; j++) {
            String contentToSend = content + contentIdGenerator.getAndIncrement();
            channel.writeInbound(createContent(contentToSend, false));
            contents.add(contentToSend);
        }
        channel.writeInbound(createContent(lastContent, true));
        verifyCallbacks(processor);
        // first outbound has to be response.
        HttpResponse response = (HttpResponse) channel.readOutbound();
        assertEquals("Unexpected response status", HttpResponseStatus.OK, response.status());
        assertTrue("Response must say 'Transfer-Encoding : chunked'", HttpUtil.isTransferEncodingChunked(response));
        // content echoed back.
        for (String srcOfTruth : contents) {
            String returnedContent = RestTestUtils.getContentString((HttpContent) channel.readOutbound());
            assertEquals("Content does not match with expected content", srcOfTruth, returnedContent);
        }
        // last content echoed back.
        String returnedContent = RestTestUtils.getContentString((HttpContent) channel.readOutbound());
        assertEquals("Content does not match with expected content", lastContent, returnedContent);
        assertTrue("Did not receive end marker", channel.readOutbound() instanceof LastHttpContent);
        assertEquals("Unexpected channel state on the server", isKeepAlive, channel.isActive());
    }
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) AtomicLong(java.util.concurrent.atomic.AtomicLong) ArrayList(java.util.ArrayList) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) HttpResponse(io.netty.handler.codec.http.HttpResponse) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) UtilsTest(com.github.ambry.utils.UtilsTest) Test(org.junit.Test)

Example 77 with LastHttpContent

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.LastHttpContent in project ambry by linkedin.

the class ChannelWriteCallback method handleContent.

/**
 * Handles a {@link HttpContent}. Checks state and echoes back the content.
 * @param httpContent the {@link HttpContent} that needs to be handled.
 * @throws Exception
 */
private void handleContent(HttpContent httpContent) throws Exception {
    if (request != null) {
        boolean isLast = httpContent instanceof LastHttpContent;
        ByteBuffer content = ByteBuffer.wrap(httpContent.content().array());
        ChannelWriteCallback callback = new ChannelWriteCallback();
        callback.setFuture(restResponseChannel.write(content, callback));
        writeCallbacksToVerify.add(callback);
        if (isLast) {
            restResponseChannel.onResponseComplete(null);
            assertFalse("Request channel is not closed", request.isOpen());
        }
    } else {
        throw new RestServiceException("Received data without a request", RestServiceErrorCode.InvalidRequestState);
    }
}
Also used : LastHttpContent(io.netty.handler.codec.http.LastHttpContent) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) ByteBuffer(java.nio.ByteBuffer)

Example 78 with LastHttpContent

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.LastHttpContent in project ambry by linkedin.

the class ChannelWriteCallback method responsesWithContentLengthTest.

/**
 * Tests the common workflow of the {@link NettyResponseChannel} i.e., add some content to response body via
 * {@link NettyResponseChannel#write(ByteBuffer, Callback)} and then complete the response.
 * <p/>
 * These responses have the header Content-Length set.
 * @throws Exception
 */
@Test
public void responsesWithContentLengthTest() throws Exception {
    EmbeddedChannel channel = createEmbeddedChannel();
    MockNettyMessageProcessor processor = channel.pipeline().get(MockNettyMessageProcessor.class);
    final int ITERATIONS = 10;
    for (int i = 0; i < ITERATIONS; i++) {
        boolean isKeepAlive = i != (ITERATIONS - 1);
        HttpHeaders httpHeaders = new DefaultHttpHeaders();
        httpHeaders.set(MockNettyMessageProcessor.CHUNK_COUNT_HEADER_NAME, i);
        HttpRequest httpRequest = RestTestUtils.createRequest(HttpMethod.POST, TestingUri.ResponseWithContentLength.toString(), httpHeaders);
        HttpUtil.setKeepAlive(httpRequest, isKeepAlive);
        channel.writeInbound(httpRequest);
        verifyCallbacks(processor);
        // first outbound has to be response.
        HttpResponse response = (HttpResponse) channel.readOutbound();
        assertEquals("Unexpected response status", HttpResponseStatus.OK, response.status());
        long contentLength = HttpUtil.getContentLength(response, -1);
        assertEquals("Unexpected Content-Length", MockNettyMessageProcessor.CHUNK.length * i, contentLength);
        if (contentLength == 0) {
            // special case. Since Content-Length is set, the response should be an instance of FullHttpResponse.
            assertTrue("Response not instance of FullHttpResponse", response instanceof FullHttpResponse);
        } else {
            HttpContent httpContent = null;
            for (int j = 0; j < i; j++) {
                httpContent = (HttpContent) channel.readOutbound();
                byte[] returnedContent = httpContent.content().array();
                assertArrayEquals("Content does not match with expected content", MockNettyMessageProcessor.CHUNK, returnedContent);
            }
            // the last HttpContent should also be an instance of LastHttpContent
            assertTrue("The last part of the content is not LastHttpContent", httpContent instanceof LastHttpContent);
        }
        assertEquals("Unexpected channel state on the server", isKeepAlive, channel.isActive());
    }
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) HttpResponse(io.netty.handler.codec.http.HttpResponse) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) HttpContent(io.netty.handler.codec.http.HttpContent) DefaultHttpContent(io.netty.handler.codec.http.DefaultHttpContent) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) UtilsTest(com.github.ambry.utils.UtilsTest) Test(org.junit.Test)

Example 79 with LastHttpContent

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.LastHttpContent in project ambry by linkedin.

the class FrontendIntegrationTest method assertNoContent.

/**
 * Verifies that no content has been sent as part of the response or readable bytes is equivalent to 0
 * @param contents the content of the response.
 */
private void assertNoContent(Queue<HttpObject> contents) {
    boolean endMarkerFound = false;
    for (HttpObject object : contents) {
        assertFalse("There should have been no more data after the end marker was found", endMarkerFound);
        HttpContent content = (HttpContent) object;
        assertEquals("No content expected ", 0, content.content().readableBytes());
        endMarkerFound = object instanceof LastHttpContent;
        ReferenceCountUtil.release(content);
    }
    assertTrue("There should have been an end marker", endMarkerFound);
}
Also used : HttpObject(io.netty.handler.codec.http.HttpObject) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) HttpContent(io.netty.handler.codec.http.HttpContent)

Example 80 with LastHttpContent

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.LastHttpContent in project jocean-http by isdom.

the class Nettys method httpobjs2fullreq.

// retain when build fullreq
public static FullHttpRequest httpobjs2fullreq(final List<HttpObject> httpobjs) {
    if (httpobjs.size() > 0 && (httpobjs.get(0) instanceof HttpRequest) && (httpobjs.get(httpobjs.size() - 1) instanceof LastHttpContent)) {
        if (httpobjs.get(0) instanceof FullHttpRequest) {
            return ((FullHttpRequest) httpobjs.get(0)).retainedDuplicate();
        }
        final HttpRequest req = (HttpRequest) httpobjs.get(0);
        final DefaultFullHttpRequest fullreq = new DefaultFullHttpRequest(req.protocolVersion(), req.method(), req.uri(), tobuf(httpobjs));
        fullreq.headers().add(req.headers());
        // ? need update Content-Length header field ?
        return fullreq;
    } else {
        throw new RuntimeException("invalid HttpObjects");
    }
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) HttpRequest(io.netty.handler.codec.http.HttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) LastHttpContent(io.netty.handler.codec.http.LastHttpContent)

Aggregations

LastHttpContent (io.netty.handler.codec.http.LastHttpContent)137 DefaultLastHttpContent (io.netty.handler.codec.http.DefaultLastHttpContent)63 HttpContent (io.netty.handler.codec.http.HttpContent)55 ByteBuf (io.netty.buffer.ByteBuf)49 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)43 HttpResponse (io.netty.handler.codec.http.HttpResponse)42 HttpRequest (io.netty.handler.codec.http.HttpRequest)34 Test (org.junit.Test)27 Test (org.junit.jupiter.api.Test)24 DefaultHttpContent (io.netty.handler.codec.http.DefaultHttpContent)23 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)20 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)18 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)13 IOException (java.io.IOException)12 ChannelFuture (io.netty.channel.ChannelFuture)11 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)10 HttpObject (io.netty.handler.codec.http.HttpObject)10 JsonObjectDecoder (io.netty.handler.codec.json.JsonObjectDecoder)10 HttpProcessingState (com.nike.riposte.server.http.HttpProcessingState)9 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)9