Search in sources :

Example 46 with DefaultHttpResponse

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

the class ProxyUtils method duplicateHttpResponse.

public static HttpResponse duplicateHttpResponse(HttpResponse originalResponse) {
    DefaultHttpResponse newResponse = new DefaultHttpResponse(originalResponse.protocolVersion(), originalResponse.status());
    newResponse.headers().add(originalResponse.headers());
    return newResponse;
}
Also used : DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse)

Example 47 with DefaultHttpResponse

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

the class NettysTestCase method test_httpobjs2fullresp_firstcontentdisposed.

@Test
public final void test_httpobjs2fullresp_firstcontentdisposed() 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> resps = 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.content().refCnt());
        }
    });
    // release [0]'s content
    resp_contents[0].release();
    FullHttpResponse fullresp = null;
    thrown.expect(IllegalReferenceCountException.class);
    try {
        fullresp = Nettys.httpobjs2fullresp(resps);
    } finally {
        assertNull(fullresp);
        RxActions.applyArrayBy(Arrays.copyOfRange(resp_contents, 1, resp_contents.length), new Action1<HttpContent>() {

            @Override
            public void call(final HttpContent c) {
                assertEquals(1, c.content().refCnt());
            }
        });
    }
}
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 48 with DefaultHttpResponse

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

the class NettysTestCase method test_httpobjs2fullresp_misslastcontent.

@Test
public final void test_httpobjs2fullresp_misslastcontent() 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> resps = new ArrayList<HttpObject>() {

        private static final long serialVersionUID = 1L;

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

        @Override
        public void call(final HttpContent c) {
            assertEquals(1, c.content().refCnt());
        }
    });
    FullHttpResponse fullresp = null;
    thrown.expect(RuntimeException.class);
    try {
        fullresp = Nettys.httpobjs2fullresp(resps);
    } finally {
        assertNull(fullresp);
        RxActions.applyArrayBy(resp_contents, new Action1<HttpContent>() {

            @Override
            public void call(final HttpContent c) {
                assertEquals(1, c.content().refCnt());
            }
        });
    }
}
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 49 with DefaultHttpResponse

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.DefaultHttpResponse in project reactor-netty by reactor.

the class HttpOperationsTest method httpAndJsonDecoders.

@Test
public void httpAndJsonDecoders() {
    EmbeddedChannel channel = new EmbeddedChannel();
    NettyContext testContext = () -> channel;
    ChannelHandler handler = new JsonObjectDecoder(true);
    testContext.addHandlerLast("foo", handler);
    HttpOperations.autoAddHttpExtractor(testContext, "foo", handler);
    String json1 = "[{\"some\": 1} , {\"valu";
    String json2 = "e\": true, \"test\": 1}]";
    Object[] content = new Object[3];
    content[0] = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
    content[1] = new DefaultHttpContent(Unpooled.copiedBuffer(json1, CharsetUtil.UTF_8));
    content[2] = new DefaultLastHttpContent(Unpooled.copiedBuffer(json2, CharsetUtil.UTF_8));
    channel.writeInbound(content);
    Object t = channel.readInbound();
    assertThat(t, instanceOf(HttpResponse.class));
    assertThat(t, not(instanceOf(HttpContent.class)));
    t = channel.readInbound();
    assertThat(t, instanceOf(ByteBuf.class));
    assertThat(((ByteBuf) t).toString(CharsetUtil.UTF_8), is("{\"some\": 1}"));
    ((ByteBuf) t).release();
    t = channel.readInbound();
    assertThat(t, instanceOf(ByteBuf.class));
    assertThat(((ByteBuf) t).toString(CharsetUtil.UTF_8), is("{\"value\": true, \"test\": 1}"));
    ((ByteBuf) t).release();
    t = channel.readInbound();
    assertThat(t, is(LastHttpContent.EMPTY_LAST_CONTENT));
    ((LastHttpContent) t).release();
    t = channel.readInbound();
    assertThat(t, nullValue());
}
Also used : DefaultHttpContent(io.netty.handler.codec.http.DefaultHttpContent) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) JsonObjectDecoder(io.netty.handler.codec.json.JsonObjectDecoder) DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) HttpResponse(io.netty.handler.codec.http.HttpResponse) ChannelHandler(io.netty.channel.ChannelHandler) ByteBuf(io.netty.buffer.ByteBuf) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) NettyContext(reactor.ipc.netty.NettyContext) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) Test(org.junit.Test)

Example 50 with DefaultHttpResponse

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.DefaultHttpResponse in project moco by dreamhead.

the class ActualWebSocketServer method connectRequest.

public void connectRequest(final ChannelHandlerContext ctx, final FullHttpRequest request) {
    QueryStringDecoder decoder = new QueryStringDecoder(request.uri());
    final String actual = decoder.path();
    if (!uri.equals(actual)) {
        ctx.writeAndFlush(new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.BAD_REQUEST));
        return;
    }
    WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory(this.uri, null, false);
    WebSocketServerHandshaker handshaker = wsFactory.newHandshaker(request);
    Channel channel = ctx.channel();
    if (handshaker == null) {
        WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(channel);
        return;
    }
    handshaker.handshake(channel, request);
    connect(channel);
    sendConnected(channel);
}
Also used : QueryStringDecoder(io.netty.handler.codec.http.QueryStringDecoder) DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) WebSocketServerHandshaker(io.netty.handler.codec.http.websocketx.WebSocketServerHandshaker) Channel(io.netty.channel.Channel) WebSocketServerHandshakerFactory(io.netty.handler.codec.http.websocketx.WebSocketServerHandshakerFactory)

Aggregations

DefaultHttpResponse (io.netty.handler.codec.http.DefaultHttpResponse)64 HttpResponse (io.netty.handler.codec.http.HttpResponse)34 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)23 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)18 HttpContent (io.netty.handler.codec.http.HttpContent)14 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)11 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)11 ChannelFuture (io.netty.channel.ChannelFuture)10 HttpRequest (io.netty.handler.codec.http.HttpRequest)10 ByteBuf (io.netty.buffer.ByteBuf)9 DefaultLastHttpContent (io.netty.handler.codec.http.DefaultLastHttpContent)9 HttpObject (io.netty.handler.codec.http.HttpObject)9 RandomAccessFile (java.io.RandomAccessFile)9 Test (org.junit.Test)9 FileNotFoundException (java.io.FileNotFoundException)7 IOException (java.io.IOException)7 Channel (io.netty.channel.Channel)6 File (java.io.File)6 HttpChunkedInput (io.netty.handler.codec.http.HttpChunkedInput)5 URL (java.net.URL)5