Search in sources :

Example 76 with LastHttpContent

use of 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 77 with LastHttpContent

use of 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 78 with LastHttpContent

use of 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 79 with LastHttpContent

use of 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)

Example 80 with LastHttpContent

use of io.netty.handler.codec.http.LastHttpContent in project jocean-http by isdom.

the class DefaultSignalClient method assembleOutgoing.

private Outgoing assembleOutgoing(final HttpRequest request, final BodyForm body, final Attachment[] attachments) throws Exception {
    if (0 == attachments.length) {
        final LastHttpContent lastContent = buildLastContent(request, body);
        return new Outgoing(Observable.<Object>just(request, lastContent), lastContent.content().readableBytes(), new Action0() {

            @Override
            public void call() {
                ReferenceCountUtil.release(request);
                ReferenceCountUtil.release(lastContent);
            }
        });
    } else {
        // Use the PostBody encoder
        final HttpPostRequestEncoder postRequestEncoder = new HttpPostRequestEncoder(_DATA_FACTORY, request, true, CharsetUtil.UTF_8, // true => multipart
        EncoderMode.HTML5);
        final long signalSize = addSignalToMultipart(postRequestEncoder, body);
        final long attachmentSize = addAttachmentsToMultipart(postRequestEncoder, attachments);
        final long total = signalSize + attachmentSize;
        // finalize request
        final HttpRequest request4send = postRequestEncoder.finalizeRequest();
        final Action0 toRelease = new Action0() {

            @Override
            public void call() {
                ReferenceCountUtil.release(request4send);
                RxNettys.releaseObjects(postRequestEncoder.getBodyListAttributes());
            }
        };
        return postRequestEncoder.isChunked() ? new Outgoing(Observable.<Object>just(request4send, postRequestEncoder), total, toRelease) : new Outgoing(Observable.<Object>just(request4send), total, toRelease);
    }
}
Also used : DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) HttpRequest(io.netty.handler.codec.http.HttpRequest) Action0(rx.functions.Action0) HttpPostRequestEncoder(io.netty.handler.codec.http.multipart.HttpPostRequestEncoder) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent)

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 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 IOException (java.io.IOException)10 HttpProcessingState (com.nike.riposte.server.http.HttpProcessingState)9 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)9