Search in sources :

Example 31 with DefaultFullHttpResponse

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.DefaultFullHttpResponse in project wso2-synapse by wso2.

the class HttpResponseProcessor method write404NotFoundResponse.

private boolean write404NotFoundResponse(HttpRequestContext requestContext, ChannelHandlerContext ctx) {
    boolean keepAlive = requestContext.isKeepAlive();
    HttpVersion httpVersion = consumerContext.getHttpVersion();
    FullHttpResponse response = new DefaultFullHttpResponse(httpVersion, NOT_FOUND);
    response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "text/plain; charset=UTF-8");
    if (!keepAlive) {
        ctx.write(response).addListener(ChannelFutureListener.CLOSE);
    } else {
        response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, response.content().readableBytes());
        response.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
        ctx.write(response);
    }
    return keepAlive;
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) HttpVersion(io.netty.handler.codec.http.HttpVersion)

Example 32 with DefaultFullHttpResponse

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

the class AbstractHttpServlet method createResponse.

protected static FullHttpResponse createResponse(HttpVersion version, HttpResponseStatus status, String contentType, byte[] content) {
    FullHttpResponse resp;
    ByteBuf buf = null;
    int contentLen = (content == null) ? 0 : content.length;
    if (contentLen != 0) {
        buf = Unpooled.wrappedBuffer(content);
        resp = new DefaultFullHttpResponse(version, status, buf);
    } else {
        resp = new DefaultFullHttpResponse(version, status);
    }
    resp.headers().addInt("Content-Length", contentLen);
    if (contentType != null && !contentType.isEmpty()) {
        resp.headers().add("Content-Type", contentType);
    }
    return resp;
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) ByteBuf(io.netty.buffer.ByteBuf)

Example 33 with DefaultFullHttpResponse

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

the class AbstractHttpServlet method createErrorResponse.

protected static FullHttpResponse createErrorResponse(HttpVersion version, HttpResponseStatus status) {
    FullHttpResponse resp = new DefaultFullHttpResponse(version, status);
    resp.headers().addInt("Content-Length", 0);
    return resp;
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse)

Example 34 with DefaultFullHttpResponse

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

the class DefaultHttpClientTestCase method testInitiatorInteractionSuccessAsHttps10ConnectionClose.

@Test(timeout = 5000)
public void testInitiatorInteractionSuccessAsHttps10ConnectionClose() 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, enableSSL4ServerWithSelfSigned(), Feature.ENABLE_LOGGING_OVER_SSL);
    final DefaultHttpClient client = new DefaultHttpClient(new TestChannelCreator(), enableSSL4Client(), Feature.ENABLE_LOGGING_OVER_SSL);
    try {
        final HttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_0, HttpMethod.GET, "/");
        request.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE);
        startInteraction(client.initiator().remoteAddress(new LocalAddress(addr)), Observable.just(request), new Interaction() {

            @Override
            public void interact(final HttpInitiator initiator, final Observable<DisposableWrapper<FullHttpResponse>> getresp) throws Exception {
                final Observable<DisposableWrapper<FullHttpResponse>> cached = getresp.cache();
                cached.subscribe();
                // server side recv req
                final HttpTrade trade = trades.take();
                // recv all request
                trade.inbound().toCompletable().await();
                final ByteBuf svrRespContent = allocator.buffer(CONTENT.length).writeBytes(CONTENT);
                // for HTTP 1.0 Connection: Close response behavior
                final FullHttpResponse fullrespfromsvr = new DefaultFullHttpResponse(HttpVersion.HTTP_1_0, HttpResponseStatus.OK, svrRespContent);
                fullrespfromsvr.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain");
                // missing Content-Length
                // response.headers().set(CONTENT_LENGTH, response.content().readableBytes());
                fullrespfromsvr.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE);
                trade.outbound(Observable.just(fullrespfromsvr));
                // wait for recv all resp at client side
                cached.toCompletable().await();
                svrRespContent.release();
                assertTrue(Arrays.equals(dumpResponseContentAsBytes(cached), CONTENT));
            }
        });
    } finally {
        assertEquals(0, allActiveAllocationsCount(allocator));
        client.close();
        server.unsubscribe();
    }
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) HttpRequest(io.netty.handler.codec.http.HttpRequest) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) LocalAddress(io.netty.channel.local.LocalAddress) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) DisposableWrapper(org.jocean.idiom.DisposableWrapper) ByteBuf(io.netty.buffer.ByteBuf) SSLException(javax.net.ssl.SSLException) TransportException(org.jocean.http.TransportException) ConnectException(java.net.ConnectException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) ConnectableObservable(rx.observables.ConnectableObservable) Observable(rx.Observable) PooledByteBufAllocator(io.netty.buffer.PooledByteBufAllocator) HttpInitiator(org.jocean.http.client.HttpClient.HttpInitiator) HttpTrade(org.jocean.http.server.HttpServerBuilder.HttpTrade) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) Subscription(rx.Subscription) Test(org.junit.Test)

Example 35 with DefaultFullHttpResponse

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

the class DefaultSignalClientTestCase method buildResponse.

private static Observable<HttpObject> buildResponse(final Object responseBean, final Action1<Action0> onTerminate) {
    final byte[] responseBytes = JSON.toJSONBytes(responseBean);
    final FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, OK, Unpooled.wrappedBuffer(responseBytes));
    onTerminate.call(new Action0() {

        @Override
        public void call() {
            final boolean released = response.release();
            if (LOG.isDebugEnabled()) {
                LOG.debug("buildBytesResponse release {} released({})", response, released);
            }
        }
    });
    response.headers().set(HttpHeaderNames.CONTENT_TYPE, "application/json");
    response.headers().set(HttpHeaderNames.CONTENT_LENGTH, response.content().readableBytes());
    return Observable.<HttpObject>just(response);
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) Action0(rx.functions.Action0) HttpObject(io.netty.handler.codec.http.HttpObject) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse)

Aggregations

DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)223 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)145 ByteBuf (io.netty.buffer.ByteBuf)68 HttpResponse (io.netty.handler.codec.http.HttpResponse)39 Test (org.junit.Test)25 HttpRequest (io.netty.handler.codec.http.HttpRequest)24 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)23 HttpResponseStatus (io.netty.handler.codec.http.HttpResponseStatus)22 ChannelFuture (io.netty.channel.ChannelFuture)19 IOException (java.io.IOException)19 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)16 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)16 DefaultHttpResponse (io.netty.handler.codec.http.DefaultHttpResponse)15 HttpObject (io.netty.handler.codec.http.HttpObject)15 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)12 HttpVersion (io.netty.handler.codec.http.HttpVersion)12 InetSocketAddress (java.net.InetSocketAddress)12 SSLException (javax.net.ssl.SSLException)12 Test (org.junit.jupiter.api.Test)12 Map (java.util.Map)11