Search in sources :

Example 26 with HttpContent

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpContent in project jocean-http by isdom.

the class RxNettysTestCase method test_BUILD_FULL_REQUEST_ForFullRequestAsArray.

@Test
public final void test_BUILD_FULL_REQUEST_ForFullRequestAsArray() throws Exception {
    final DefaultHttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/");
    final HttpContent[] req_contents = Nettys4Test.buildContentArray(REQ_CONTENT.getBytes(Charsets.UTF_8), 1);
    final List<HttpObject> reqs = new ArrayList<HttpObject>() {

        private static final long serialVersionUID = 1L;

        {
            this.add(request);
            this.addAll(Arrays.asList(req_contents));
            this.add(LastHttpContent.EMPTY_LAST_CONTENT);
        }
    };
    RxActions.applyArrayBy(req_contents, new Action1<HttpContent>() {

        @Override
        public void call(final HttpContent c) {
            assertEquals(1, c.refCnt());
        }
    });
    final FullHttpRequest fullreq = RxNettys.BUILD_FULL_REQUEST.call(reqs.toArray(new HttpObject[0]));
    assertNotNull(fullreq);
    RxActions.applyArrayBy(req_contents, new Action1<HttpContent>() {

        @Override
        public void call(final HttpContent c) {
            assertEquals(2, c.refCnt());
        }
    });
    assertEquals(REQ_CONTENT, new String(Nettys.dumpByteBufAsBytes(fullreq.content()), Charsets.UTF_8));
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) HttpObject(io.netty.handler.codec.http.HttpObject) ArrayList(java.util.ArrayList) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) HttpContent(io.netty.handler.codec.http.HttpContent) Test(org.junit.Test)

Example 27 with HttpContent

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpContent in project jocean-http by isdom.

the class RxNettysTestCase method test_BUILD_FULL_RESPONSE_ForFullResponseAsArray.

@Test
public final void test_BUILD_FULL_RESPONSE_ForFullResponseAsArray() throws Exception {
    final DefaultHttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
    final HttpContent[] resp_contents = Nettys4Test.buildContentArray(REQ_CONTENT.getBytes(Charsets.UTF_8), 1);
    final List<HttpObject> reqs = new ArrayList<HttpObject>() {

        private static final long serialVersionUID = 1L;

        {
            this.add(response);
            this.addAll(Arrays.asList(resp_contents));
            this.add(LastHttpContent.EMPTY_LAST_CONTENT);
        }
    };
    RxActions.applyArrayBy(resp_contents, new Action1<HttpContent>() {

        @Override
        public void call(final HttpContent c) {
            assertEquals(1, c.refCnt());
        }
    });
    final FullHttpResponse fullresp = RxNettys.BUILD_FULL_RESPONSE.call(reqs.toArray(new HttpObject[0]));
    assertNotNull(fullresp);
    RxActions.applyArrayBy(resp_contents, new Action1<HttpContent>() {

        @Override
        public void call(final HttpContent c) {
            assertEquals(2, c.refCnt());
        }
    });
    assertEquals(REQ_CONTENT, new String(Nettys.dumpByteBufAsBytes(fullresp.content()), Charsets.UTF_8));
}
Also used : DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) HttpObject(io.netty.handler.codec.http.HttpObject) ArrayList(java.util.ArrayList) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) HttpContent(io.netty.handler.codec.http.HttpContent) Test(org.junit.Test)

Example 28 with HttpContent

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpContent in project jocean-http by isdom.

the class HttpContentTestCase method testHttpContentEquals.

@Test
public final void testHttpContentEquals() {
    final ByteBuf buf1 = Unpooled.buffer(100);
    buf1.writeBytes("hello".getBytes(CharsetUtil.UTF_8));
    final HttpContent c1 = new DefaultHttpContent(buf1);
    final ByteBuf buf2 = Unpooled.buffer(100);
    buf2.writeBytes("world".getBytes(CharsetUtil.UTF_8));
    final HttpContent c2 = new DefaultHttpContent(buf2);
    assertEquals(c1, c2);
    assertNotEquals(c1.content(), c2.content());
    final HttpContent c3 = c1.duplicate();
    assertEquals(c1.content(), c3.content());
    assertTrue(c1.content() == c3.content().unwrap());
}
Also used : DefaultHttpContent(io.netty.handler.codec.http.DefaultHttpContent) ByteBuf(io.netty.buffer.ByteBuf) DefaultHttpContent(io.netty.handler.codec.http.DefaultHttpContent) HttpContent(io.netty.handler.codec.http.HttpContent) Test(org.junit.Test)

Example 29 with HttpContent

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpContent in project jocean-http by isdom.

the class Nettys4Test method buildContentArray.

public static HttpContent[] buildContentArray(final byte[] srcBytes, final int bytesPerContent) {
    final List<HttpContent> contents = new ArrayList<>();
    int startInBytes = 0;
    while (startInBytes < srcBytes.length) {
        final ByteBuf content = Unpooled.buffer(bytesPerContent);
        final int len = Math.min(bytesPerContent, srcBytes.length - startInBytes);
        content.writeBytes(srcBytes, startInBytes, len);
        startInBytes += len;
        if (LOG.isDebugEnabled()) {
            LOG.debug("build content: {}@{}", content, UnsafeOp.toAddress(content));
        }
        contents.add(new DefaultHttpContent(content));
    }
    return contents.toArray(new HttpContent[0]);
}
Also used : ArrayList(java.util.ArrayList) DefaultHttpContent(io.netty.handler.codec.http.DefaultHttpContent) ByteBuf(io.netty.buffer.ByteBuf) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) HttpContent(io.netty.handler.codec.http.HttpContent) DefaultHttpContent(io.netty.handler.codec.http.DefaultHttpContent)

Example 30 with HttpContent

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

the class AuditLogHandler method channelRead.

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    // When a request is forwarded to the internal CDAP service
    if (msg instanceof HttpRequest) {
        HttpRequest request = (HttpRequest) msg;
        // Extra configurations for audit log
        AuditLogConfig logConfig = AUDIT_LOG_LOOKUP_METHOD.contains(request.method()) ? RouterAuditLookUp.getInstance().getAuditLogContent(request.uri(), request.method()) : null;
        if (logConfig == null) {
            logEntry = new AuditLogEntry(request, Networks.getIP(ctx.channel().remoteAddress()));
        } else {
            logEntry = new AuditLogEntry(request, Networks.getIP(ctx.channel().remoteAddress()), logConfig.getHeaderNames());
            logRequestBody = logConfig.isLogRequestBody();
            logResponseBody = logConfig.isLogResponseBody();
        }
    } else if (msg instanceof HttpContent && logEntry != null) {
        ByteBuf content = ((HttpContent) msg).content();
        if (logRequestBody && content.isReadable()) {
            logEntry.appendRequestBody(content.toString(StandardCharsets.UTF_8));
        }
    }
    ctx.fireChannelRead(msg);
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) AuditLogEntry(co.cask.cdap.common.logging.AuditLogEntry) ByteBuf(io.netty.buffer.ByteBuf) AuditLogConfig(co.cask.cdap.common.logging.AuditLogConfig) HttpContent(io.netty.handler.codec.http.HttpContent) LastHttpContent(io.netty.handler.codec.http.LastHttpContent)

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