Search in sources :

Example 86 with HttpContent

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

the class ChannelWriteCallback method fillWriteBufferTest.

/**
 * Tests handling of content that is larger than write buffer size. In this test case, the write buffer low and high
 * watermarks are requested to be set to 1 and 2 respectively so the content will be written byte by byte into the
 * {@link NettyResponseChannel}. This does <b><i>not</i></b> test for the same situation in a async scenario since
 * {@link EmbeddedChannel} only provides blocking semantics.
 * @throws IOException
 */
@Test
public void fillWriteBufferTest() throws IOException {
    String content = "@@randomContent@@@";
    String lastContent = "@@randomLastContent@@@";
    EmbeddedChannel channel = createEmbeddedChannel();
    HttpRequest httpRequest = RestTestUtils.createRequest(HttpMethod.GET, TestingUri.FillWriteBuffer.toString(), null);
    HttpUtil.setKeepAlive(httpRequest, false);
    channel.writeInbound(httpRequest);
    channel.writeInbound(createContent(content, false));
    channel.writeInbound(createContent(lastContent, true));
    // first outbound has to be response.
    HttpResponse response = (HttpResponse) channel.readOutbound();
    assertEquals("Unexpected response status", HttpResponseStatus.OK, response.status());
    // content echoed back.
    StringBuilder returnedContent = new StringBuilder();
    while (returnedContent.length() < content.length()) {
        returnedContent.append(RestTestUtils.getContentString((HttpContent) channel.readOutbound()));
    }
    assertEquals("Content does not match with expected content", content, returnedContent.toString());
    // last content echoed back.
    StringBuilder returnedLastContent = new StringBuilder();
    while (returnedLastContent.length() < lastContent.length()) {
        returnedLastContent.append(RestTestUtils.getContentString((HttpContent) channel.readOutbound()));
    }
    assertEquals("Content does not match with expected content", lastContent, returnedLastContent.toString());
    assertFalse("Channel not closed on the server", channel.isActive());
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) 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) 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 87 with HttpContent

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpContent 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 88 with HttpContent

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpContent 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 89 with HttpContent

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpContent in project BRFS by zhangnianli.

the class NettyMessageServerHandler method channelRead.

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    HttpContent httpRequest = (HttpContent) msg;
    ByteBuf bBuf = httpRequest.content();
    byte[] bytes = new byte[bBuf.readableBytes()];
    bBuf.readBytes(bytes);
    NettyMessageReqRes req = NettyMessageReqRes.parseFrom(bytes);
    NettyMessageReqRes.Builder builder = NettyMessageReqRes.newBuilder();
    builder.setSessionId(req.getSessionId()).setOptType(req.getOptType());
    if (NettyMessageReqRes.OptType.STORATE_NAME == req.getOptType()) {
        StorageNameRequest snRequest = req.getSnReqRes().getRequest();
        StorageNameResponse.Builder snBuilder = StorageNameResponse.newBuilder();
        if (StorageNameRequest.StorageNameOptType.CREATE == snRequest.getStorageNameOptType()) {
            System.out.println("create sn:" + snRequest);
            snBuilder.setCode(StorageNameResponse.ResultCode.SUCCESS);
            snBuilder.setDesc("create sn successfully!");
        } else if (StorageNameRequest.StorageNameOptType.UPDATE == snRequest.getStorageNameOptType()) {
            System.out.println("update sn:" + snRequest);
            snBuilder.setCode(StorageNameResponse.ResultCode.SUCCESS);
            snBuilder.setDesc("update sn successfully!");
        } else if (StorageNameRequest.StorageNameOptType.DELETE == snRequest.getStorageNameOptType()) {
            System.out.println("delete sn:" + snRequest);
            snBuilder.setCode(StorageNameResponse.ResultCode.SUCCESS);
            snBuilder.setDesc("delete sn successfully!");
        }
        StorageNameReqRes snResponse = StorageNameReqRes.newBuilder().setResponse(snBuilder.build()).build();
        builder.setSnReqRes(snResponse);
    } else if (NettyMessageReqRes.OptType.DATA_FILE == req.getOptType()) {
        FileDataReqRes dataRequest = req.getDataReqRes();
        if (FileDataReqRes.DataOptType.WRITE == dataRequest.getDataOptType()) {
            System.out.println("write data:");
            for (int i = 0; i < dataRequest.getDataCount(); i++) {
                System.out.println(dataRequest.getData(i));
            }
        } else if (FileDataReqRes.DataOptType.READ == dataRequest.getDataOptType()) {
            System.out.println("read data:");
            for (int i = 0; i < dataRequest.getFidCount(); i++) {
                System.out.println(dataRequest.getFid(i));
            }
        } else if (FileDataReqRes.DataOptType.DELETE == dataRequest.getDataOptType()) {
            System.out.println("删除方式:" + dataRequest.getDeleteDataType().name());
            System.out.println("删除时间段:" + dataRequest.getBeginTime() + "--" + dataRequest.getEndTime());
        }
    }
    NettyMessageReqRes responseContent = builder.build();
    ByteBuf byteBuf = Unpooled.wrappedBuffer(responseContent.toByteArray());
    FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, byteBuf);
    System.out.println("return result!");
    System.out.println(new String(responseContent.toByteArray()));
    ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) StorageNameResponse(com.bonree.brfs.common.proto.StorageNameProtos.StorageNameResponse) StorageNameReqRes(com.bonree.brfs.common.proto.StorageNameProtos.StorageNameReqRes) ByteBuf(io.netty.buffer.ByteBuf) NettyMessageReqRes(com.bonree.brfs.common.proto.NettyMessageProto.NettyMessageReqRes) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) StorageNameRequest(com.bonree.brfs.common.proto.StorageNameProtos.StorageNameRequest) FileDataReqRes(com.bonree.brfs.common.proto.FileDataProtos.FileDataReqRes) HttpContent(io.netty.handler.codec.http.HttpContent)

Example 90 with HttpContent

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpContent in project tesla by linking12.

the class ProxyConnection method readHTTP.

@SuppressWarnings("unchecked")
private void readHTTP(HttpObject httpObject) {
    ConnectionState nextState = getCurrentState();
    switch(getCurrentState()) {
        case AWAITING_INITIAL:
            if (httpObject instanceof HttpMessage) {
                nextState = readHTTPInitial((I) httpObject);
            } else {
                LOG.debug("Dropping message because HTTP object was not an HttpMessage. HTTP object may be orphaned content from a short-circuited response. Message: {}", httpObject);
            }
            break;
        case AWAITING_CHUNK:
            HttpContent chunk = (HttpContent) httpObject;
            readHTTPChunk(chunk);
            nextState = ProxyUtils.isLastChunk(chunk) ? AWAITING_INITIAL : AWAITING_CHUNK;
            break;
        case AWAITING_PROXY_AUTHENTICATION:
            if (httpObject instanceof HttpRequest) {
                nextState = readHTTPInitial((I) httpObject);
            } else {
            }
            break;
        case CONNECTING:
            LOG.warn("Attempted to read from connection that's in the process of connecting.  This shouldn't happen.");
            break;
        case NEGOTIATING_CONNECT:
            LOG.debug("Attempted to read from connection that's in the process of negotiating an HTTP CONNECT.  This is probably the LastHttpContent of a chunked CONNECT.");
            break;
        case AWAITING_CONNECT_OK:
            LOG.warn("AWAITING_CONNECT_OK should have been handled by ProxyToServerConnection.read()");
            break;
        case HANDSHAKING:
            LOG.warn("Attempted to read from connection that's in the process of handshaking.  This shouldn't happen.", channel);
            break;
        case DISCONNECT_REQUESTED:
        case DISCONNECTED:
            LOG.info("Ignoring message since the connection is closed or about to close");
            break;
    }
    become(nextState);
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) ConnectionState(io.github.tesla.gateway.netty.transmit.ConnectionState) HttpMessage(io.netty.handler.codec.http.HttpMessage) HttpContent(io.netty.handler.codec.http.HttpContent)

Aggregations

HttpContent (io.netty.handler.codec.http.HttpContent)158 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)122 DefaultLastHttpContent (io.netty.handler.codec.http.DefaultLastHttpContent)62 DefaultHttpContent (io.netty.handler.codec.http.DefaultHttpContent)59 Test (org.junit.Test)59 ByteBuf (io.netty.buffer.ByteBuf)40 HttpResponse (io.netty.handler.codec.http.HttpResponse)37 HttpRequest (io.netty.handler.codec.http.HttpRequest)35 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)34 ArrayList (java.util.ArrayList)30 HttpObject (io.netty.handler.codec.http.HttpObject)28 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)24 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)20 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)17 DefaultHttpRequest (io.netty.handler.codec.http.DefaultHttpRequest)17 Channel (io.netty.channel.Channel)16 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)15 DefaultHttpResponse (io.netty.handler.codec.http.DefaultHttpResponse)14 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)13 IOException (java.io.IOException)13