Search in sources :

Example 96 with LastHttpContent

use of io.netty.handler.codec.http.LastHttpContent in project riposte by Nike-Inc.

the class RequestInfoImpl method addContentChunk.

/**
 * {@inheritDoc}
 */
@Override
public int addContentChunk(@NotNull HttpContent chunk) {
    if (isCompleteRequestWithAllChunks) {
        throw new IllegalStateException("Cannot add new content chunk - this RequestInfo is already marked as " + "representing the complete request with all chunks");
    }
    chunk.retain();
    rawContentLengthInBytes += chunk.content().readableBytes();
    // If content chunks will be released externally then there's no point in us holding on to them
    if (!contentChunksWillBeReleasedExternally)
        contentChunks.add(chunk);
    if (chunk instanceof LastHttpContent) {
        // to be set to true if content chunks are released externally.
        if (!contentChunksWillBeReleasedExternally)
            isCompleteRequestWithAllChunks = true;
        HttpHeaders chunkTrailingHeaders = ((LastHttpContent) chunk).trailingHeaders();
        // noinspection StatementWithEmptyBody
        if (trailingHeaders == chunkTrailingHeaders) {
        // Can happen during the constructor. We've already set the trailing headers to the chunk's trailing headers, so nothing to do here.
        } else {
            if (!trailingHeaders.isEmpty()) {
                throw new IllegalStateException("Received the final chunk, but trailingHeaders was already " + "populated. This should not be possible.");
            }
            trailingHeaders.add(chunkTrailingHeaders);
        }
    }
    return rawContentLengthInBytes;
}
Also used : HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) LastHttpContent(io.netty.handler.codec.http.LastHttpContent)

Example 97 with LastHttpContent

use of io.netty.handler.codec.http.LastHttpContent in project ambry by linkedin.

the class FrontendIntegrationTest method assertNoContent.

// helpers
// general
/**
 * Discards all the content in {@code contents} and checks none of the chunks have actual content
 * @param contents the content to discard.
 * @param expectedDiscardCount the number of {@link HttpObject}s that are expected to discarded.
 */
void assertNoContent(Queue<HttpObject> contents, int expectedDiscardCount) {
    assertEquals("Objects that will be discarded differ from expected", expectedDiscardCount, contents.size());
    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(object);
    }
    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 98 with LastHttpContent

use of io.netty.handler.codec.http.LastHttpContent in project ambry by linkedin.

the class FrontendIntegrationTestBase method assertNoContent.

// BeforeClass helpers
/**
 * Discards all the content in {@code contents} and checks none of the chunks have actual content
 * @param contents the content to discard.
 * @param expectedDiscardCount the number of {@link HttpObject}s that are expected to discarded.
 */
void assertNoContent(Queue<HttpObject> contents, int expectedDiscardCount) {
    assertEquals("Objects that will be discarded differ from expected", expectedDiscardCount, contents.size());
    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(object);
    }
    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 99 with LastHttpContent

use of io.netty.handler.codec.http.LastHttpContent in project ambry by linkedin.

the class FrontendIntegrationTestBase method getContent.

/**
 * Combines all the parts in {@code contents} into one {@link ByteBuffer}.
 * @param contents the content of the response.
 * @param expectedContentLength the length of the contents in bytes.
 * @return a {@link ByteBuffer} that contains all the data in {@code contents}.
 */
ByteBuffer getContent(Queue<HttpObject> contents, long expectedContentLength) {
    ByteBuffer buffer = ByteBuffer.allocate((int) expectedContentLength);
    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;
        buffer.put(content.content().nioBuffer());
        endMarkerFound = object instanceof LastHttpContent;
        content.release();
    }
    assertEquals("Content length did not match expected", expectedContentLength, buffer.position());
    assertTrue("End marker was not found", endMarkerFound);
    buffer.flip();
    return buffer;
}
Also used : HttpObject(io.netty.handler.codec.http.HttpObject) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) ByteBuffer(java.nio.ByteBuffer) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) HttpContent(io.netty.handler.codec.http.HttpContent)

Example 100 with LastHttpContent

use of io.netty.handler.codec.http.LastHttpContent in project ambry by linkedin.

the class MockChannelHandlerContext method noResponseBodyTest.

/**
 * Checks the case where no body needs to be returned but just a
 * {@link RestResponseChannel#onResponseComplete(Exception)} is called on the server. This should return just
 * response metadata.
 */
@Test
public void noResponseBodyTest() {
    EmbeddedChannel channel = createEmbeddedChannel();
    // with Transfer-Encoding:Chunked
    HttpRequest httpRequest = RestTestUtils.createRequest(HttpMethod.GET, TestingUri.ImmediateResponseComplete.toString(), null);
    channel.writeInbound(httpRequest);
    // There should be a response.
    HttpResponse response = channel.readOutbound();
    assertEquals("Unexpected response status", HttpResponseStatus.OK, response.status());
    assertTrue("Response must say 'Transfer-Encoding : chunked'", HttpUtil.isTransferEncodingChunked(response));
    // since this is Transfer-Encoding:chunked, there should be a LastHttpContent
    assertTrue("Did not receive end marker", channel.readOutbound() instanceof LastHttpContent);
    assertTrue("Channel should be alive", channel.isActive());
    // with Content-Length set
    HttpHeaders headers = new DefaultHttpHeaders();
    headers.set(MockNettyMessageProcessor.CHUNK_COUNT_HEADER_NAME, 0);
    httpRequest = RestTestUtils.createRequest(HttpMethod.GET, TestingUri.ImmediateResponseComplete.toString(), headers);
    HttpUtil.setKeepAlive(httpRequest, false);
    channel.writeInbound(httpRequest);
    // There should be a response.
    response = channel.readOutbound();
    assertEquals("Response must have Content-Length set to 0", 0, HttpUtil.getContentLength(response, NettyRequest.UNKNOWN_CONTENT_LENGTH));
    assertEquals("Unexpected response status", HttpResponseStatus.OK, response.status());
    // since Content-Length is set, the response should be an instance of FullHttpResponse.
    assertTrue("Response not instance of FullHttpResponse", response instanceof FullHttpResponse);
    assertFalse("Channel should not be alive", 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) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) HttpResponse(io.netty.handler.codec.http.HttpResponse) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) Test(org.junit.Test)

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