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;
}
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);
}
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);
}
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;
}
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());
}
Aggregations