Search in sources :

Example 71 with FullHttpResponse

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

the class TestHttpUtil method buildBytesResponse.

public static Observable<HttpObject> buildBytesResponse(final String contentType, final byte[] content) {
    final FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, OK, Unpooled.wrappedBuffer(content));
    response.headers().set(HttpHeaderNames.CONTENT_TYPE, contentType);
    response.headers().set(HttpHeaderNames.CONTENT_LENGTH, response.content().readableBytes());
    return Observable.<HttpObject>just(response);
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) HttpObject(io.netty.handler.codec.http.HttpObject) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse)

Example 72 with FullHttpResponse

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

the class DefaultHttpClientTestCase method testInitiatorInteractionClientCanceledAsHttp.

@Test(timeout = 5000)
public void testInitiatorInteractionClientCanceledAsHttp() throws Exception {
    // 配置 池化分配器 为 取消缓存,使用 Heap
    configDefaultAllocator();
    final PooledByteBufAllocator allocator = defaultAllocator();
    final BlockingQueue<HttpTrade> trades = new ArrayBlockingQueue<>(1);
    final String addr = UUID.randomUUID().toString();
    final Subscription server = TestHttpUtil.createTestServerWith(addr, trades, Feature.ENABLE_LOGGING);
    final DefaultHttpClient client = new DefaultHttpClient(new TestChannelCreator(), Feature.ENABLE_LOGGING);
    assertEquals(0, allActiveAllocationsCount(allocator));
    try {
        startInteraction(client.initiator().remoteAddress(new LocalAddress(addr)), Observable.just(fullHttpRequest()), new Interaction() {

            @Override
            public void interact(final HttpInitiator initiator, final Observable<DisposableWrapper<FullHttpResponse>> getresp) throws Exception {
                final TestSubscriber<DisposableWrapper<FullHttpResponse>> subscriber = new TestSubscriber<>();
                final Subscription subscription = getresp.subscribe(subscriber);
                // server side recv req
                final HttpTrade trade = trades.take();
                // recv request from client side
                trade.inbound().doOnNext(DISPOSE_EACH).toCompletable().await();
                // server not send response, and client cancel this interaction
                subscription.unsubscribe();
                TerminateAware.Util.awaitTerminated(trade);
                TerminateAware.Util.awaitTerminated(initiator);
                assertTrue(!initiator.isActive());
                subscriber.assertNoTerminalEvent();
                subscriber.assertNoValues();
            }
        });
        assertEquals(0, allActiveAllocationsCount(allocator));
    } finally {
        client.close();
        server.unsubscribe();
    }
}
Also used : LocalAddress(io.netty.channel.local.LocalAddress) DisposableWrapper(org.jocean.idiom.DisposableWrapper) SSLException(javax.net.ssl.SSLException) TransportException(org.jocean.http.TransportException) ConnectException(java.net.ConnectException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) PooledByteBufAllocator(io.netty.buffer.PooledByteBufAllocator) HttpInitiator(org.jocean.http.client.HttpClient.HttpInitiator) HttpTrade(org.jocean.http.server.HttpServerBuilder.HttpTrade) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) TestSubscriber(rx.observers.TestSubscriber) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) Subscription(rx.Subscription) Test(org.junit.Test)

Example 73 with FullHttpResponse

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

the class Nettys method httpobjs2fullresp.

// retain when build fullresp
public static FullHttpResponse httpobjs2fullresp(final List<HttpObject> httpobjs) {
    if (httpobjs.size() > 0 && (httpobjs.get(0) instanceof HttpResponse) && (httpobjs.get(httpobjs.size() - 1) instanceof LastHttpContent)) {
        if (httpobjs.get(0) instanceof FullHttpResponse) {
            return ((FullHttpResponse) httpobjs.get(0)).retainedDuplicate();
        }
        final HttpResponse resp = (HttpResponse) httpobjs.get(0);
        final DefaultFullHttpResponse fullresp = new DefaultFullHttpResponse(resp.protocolVersion(), resp.status(), tobuf(httpobjs));
        fullresp.headers().add(resp.headers());
        // ? need update Content-Length header field ?
        return fullresp;
    } else {
        throw new RuntimeException("invalid HttpObjects");
    }
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) HttpResponse(io.netty.handler.codec.http.HttpResponse) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) LastHttpContent(io.netty.handler.codec.http.LastHttpContent)

Example 74 with FullHttpResponse

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.FullHttpResponse in project ratpack by ratpack.

the class ContentAggregatingRequestAction method addResponseHandlers.

@Override
protected void addResponseHandlers(ChannelPipeline p, Downstream<? super ReceivedResponse> downstream) {
    p.addLast(AGGREGATOR_HANDLER_NAME, new NoContentLengthOnNoBodyHttpObjectAggregator(requestConfig.maxContentLength));
    p.addLast(RESPONSE_HANDLER_NAME, new SimpleChannelInboundHandler<FullHttpResponse>(false) {

        @Override
        protected void channelRead0(ChannelHandlerContext ctx, FullHttpResponse response) throws Exception {
            response.touch();
            dispose(ctx.pipeline(), response);
            ByteBuf content = new ByteBufRef(response.content());
            execution.onComplete(() -> {
                if (content.refCnt() > 0) {
                    content.release();
                }
            });
            success(downstream, toReceivedResponse(response, content));
        }

        @Override
        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
            cause = decorateException(cause);
            error(downstream, cause);
            forceDispose(ctx.pipeline());
        }
    });
}
Also used : FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ByteBuf(io.netty.buffer.ByteBuf) ByteBufRef(ratpack.bytebuf.ByteBufRef)

Example 75 with FullHttpResponse

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

the class HttpRequestRouter method createPipeliningNotSupported.

private HttpResponse createPipeliningNotSupported() {
    FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.NOT_IMPLEMENTED);
    response.content().writeCharSequence("HTTP pipelining is not supported", StandardCharsets.UTF_8);
    HttpUtil.setContentLength(response, response.content().readableBytes());
    return response;
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse)

Aggregations

FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)261 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)174 ByteBuf (io.netty.buffer.ByteBuf)53 Test (org.junit.Test)50 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)38 HttpRequest (io.netty.handler.codec.http.HttpRequest)34 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)24 HttpObject (io.netty.handler.codec.http.HttpObject)23 IOException (java.io.IOException)23 HttpTrade (org.jocean.http.server.HttpServerBuilder.HttpTrade)23 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)22 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)21 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)21 Test (org.junit.jupiter.api.Test)21 ChannelFuture (io.netty.channel.ChannelFuture)20 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)20 HttpResponse (io.netty.handler.codec.http.HttpResponse)20 HttpInitiator (org.jocean.http.client.HttpClient.HttpInitiator)20 Subscription (rx.Subscription)20 LocalAddress (io.netty.channel.local.LocalAddress)19