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