Example usage for io.netty.handler.codec.http HttpUtil setKeepAlive

List of usage examples for io.netty.handler.codec.http HttpUtil setKeepAlive

Introduction

In this page you can find the example usage for io.netty.handler.codec.http HttpUtil setKeepAlive.

Prototype

public static void setKeepAlive(HttpMessage message, boolean keepAlive) 

Source Link

Document

Sets the value of the "Connection" header depending on the protocol version of the specified message.

Usage

From source file:io.netty.example.http.websocketx.benchmarkserver.WebSocketServerHandler.java

License:Apache License

private static void sendHttpResponse(ChannelHandlerContext ctx, FullHttpRequest req, FullHttpResponse res) {
    // Generate an error page if response getStatus code is not OK (200).
    HttpResponseStatus responseStatus = res.status();
    if (responseStatus.code() != 200) {
        ByteBufUtil.writeUtf8(res.content(), responseStatus.toString());
        HttpUtil.setContentLength(res, res.content().readableBytes());
    }//from  w w  w .j  a  v  a  2 s  . c o m
    // Send the response and close the connection if necessary.
    boolean keepAlive = HttpUtil.isKeepAlive(req) && responseStatus.code() == 200;
    HttpUtil.setKeepAlive(res, keepAlive);
    ChannelFuture future = ctx.write(res); // Flushed in channelReadComplete()
    if (!keepAlive) {
        future.addListener(ChannelFutureListener.CLOSE);
    }
}

From source file:io.netty.example.http.websocketx.server.WebSocketIndexPageHandler.java

License:Apache License

private static void sendHttpResponse(ChannelHandlerContext ctx, FullHttpRequest req, FullHttpResponse res) {
    // Generate an error page if response getStatus code is not OK (200).
    HttpResponseStatus responseStatus = res.status();
    if (responseStatus.code() != 200) {
        ByteBufUtil.writeUtf8(res.content(), responseStatus.toString());
        HttpUtil.setContentLength(res, res.content().readableBytes());
    }/*  w ww  . j  a v  a2  s .c o  m*/
    // Send the response and close the connection if necessary.
    boolean keepAlive = HttpUtil.isKeepAlive(req) && responseStatus.code() == 200;
    HttpUtil.setKeepAlive(res, keepAlive);
    ChannelFuture future = ctx.writeAndFlush(res);
    if (!keepAlive) {
        future.addListener(ChannelFutureListener.CLOSE);
    }
}

From source file:org.elasticsearch.http.nio.HttpReadWriteHandlerTests.java

License:Apache License

public void testDecodeHttpRequestContentLengthToLongGeneratesOutboundMessage() throws IOException {
    String uri = "localhost:9090/" + randomAlphaOfLength(8);
    io.netty.handler.codec.http.HttpRequest httpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1,
            HttpMethod.POST, uri, false);
    HttpUtil.setContentLength(httpRequest, 1025);
    HttpUtil.setKeepAlive(httpRequest, false);

    ByteBuf buf = requestEncoder.encode(httpRequest);
    try {//from  ww w . java 2s . co m
        handler.consumeReads(toChannelBuffer(buf));
    } finally {
        buf.release();
    }
    verify(transport, times(0)).incomingRequestError(any(), any(), any());
    verify(transport, times(0)).incomingRequest(any(), any());

    List<FlushOperation> flushOperations = handler.pollFlushOperations();
    assertFalse(flushOperations.isEmpty());

    FlushOperation flushOperation = flushOperations.get(0);
    FullHttpResponse response = responseDecoder
            .decode(Unpooled.wrappedBuffer(flushOperation.getBuffersToWrite()));
    try {
        assertEquals(HttpVersion.HTTP_1_1, response.protocolVersion());
        assertEquals(HttpResponseStatus.REQUEST_ENTITY_TOO_LARGE, response.status());

        flushOperation.getListener().accept(null, null);
        // Since we have keep-alive set to false, we should close the channel after the response has been
        // flushed
        verify(nioHttpChannel).close();
    } finally {
        response.release();
    }
}

From source file:org.restnext.server.ServerHandler.java

License:Apache License

private void createOutboutHeaders(HttpResponse resp, Response response, boolean keepAlive) {
    // Copy the outbound response headers.
    for (Map.Entry<String, List<String>> entries : response.getHeaders().entrySet()) {
        resp.headers().add(entries.getKey(), entries.getValue());
    }//from w  w  w .  j av  a  2 s . c o m
    // Check and set keep alive header to decide
    // whether to close the connection or not.
    HttpUtil.setKeepAlive(resp, keepAlive);
}

From source file:reactor.ipc.netty.http.client.HttpClientOperations.java

License:Open Source License

@Override
public HttpClientRequest keepAlive(boolean keepAlive) {
    HttpUtil.setKeepAlive(nettyRequest, keepAlive);
    return this;
}

From source file:reactor.ipc.netty.http.server.HttpServerOperations.java

License:Open Source License

@Override
public HttpServerResponse keepAlive(boolean keepAlive) {
    HttpUtil.setKeepAlive(nettyResponse, keepAlive);
    return this;
}