Java Code Examples for io.undertow.server.HttpServerExchange#isPersistent()

The following examples show how to use io.undertow.server.HttpServerExchange#isPersistent() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HttpTransferEncoding.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
private static StreamSinkConduit handleExplicitTransferEncoding(HttpServerExchange exchange, StreamSinkConduit channel, ConduitListener<StreamSinkConduit> finishListener, HeaderMap responseHeaders, String transferEncodingHeader, boolean headRequest) {
    HttpString transferEncoding = new HttpString(transferEncodingHeader);
    if (transferEncoding.equals(Headers.CHUNKED)) {
        if (headRequest) {
            return channel;
        }
        Boolean preChunked = exchange.getAttachment(HttpAttachments.PRE_CHUNKED_RESPONSE);
        if(preChunked != null && preChunked) {
            return new PreChunkedStreamSinkConduit(channel, finishListener, exchange);
        } else {
            return new ChunkedStreamSinkConduit(channel, exchange.getConnection().getByteBufferPool(), true, !exchange.isPersistent(), responseHeaders, finishListener, exchange);
        }
    } else {

        if (headRequest) {
            return channel;
        }
        log.trace("Cancelling persistence because response is identity with no content length");
        // make it not persistent - very unfortunate for the next request handler really...
        exchange.setPersistent(false);
        responseHeaders.put(Headers.CONNECTION, Headers.CLOSE.toString());
        return new FinishableStreamSinkConduit(channel, terminateResponseListener(exchange));
    }
}
 
Example 2
Source File: AjpReadListener.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public void exchangeComplete(final HttpServerExchange exchange) {
    if (!exchange.isUpgrade() && exchange.isPersistent()) {
        startRequest();
        ConduitStreamSourceChannel channel = ((AjpServerConnection) exchange.getConnection()).getChannel().getSourceChannel();
        channel.getReadSetter().set(this);
        channel.wakeupReads();
    } else if(!exchange.isPersistent()) {
        safeClose(exchange.getConnection());
    }
}
 
Example 3
Source File: HttpTransferEncoding.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private static StreamSinkConduit handleResponseConduit(HttpServerExchange exchange, boolean headRequest, StreamSinkConduit channel, HeaderMap responseHeaders, ConduitListener<StreamSinkConduit> finishListener, String transferEncodingHeader) {

        if (transferEncodingHeader == null) {
            if (exchange.isHttp11()) {
                if (exchange.isPersistent()) {
                    responseHeaders.put(Headers.TRANSFER_ENCODING, Headers.CHUNKED.toString());

                    if (headRequest) {
                        return channel;
                    }
                    return new ChunkedStreamSinkConduit(channel, exchange.getConnection().getByteBufferPool(), true, !exchange.isPersistent(), responseHeaders, finishListener, exchange);
                } else {
                    if (headRequest) {
                        return channel;
                    }
                    return new FinishableStreamSinkConduit(channel, finishListener);
                }
            } else {
                exchange.setPersistent(false);
                responseHeaders.put(Headers.CONNECTION, Headers.CLOSE.toString());
                if (headRequest) {
                    return channel;
                }
                return new FinishableStreamSinkConduit(channel, finishListener);
            }
        } else {
            //moved outside because this is rarely used
            //and makes the method small enough to be inlined
            return handleExplicitTransferEncoding(exchange, channel, finishListener, responseHeaders, transferEncodingHeader, headRequest);
        }
    }
 
Example 4
Source File: MCMPHandler.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Handle a management+ request.
 *
 * @param method   the http method
 * @param exchange the http server exchange
 * @throws Exception
 */
protected void handleRequest(final HttpString method, HttpServerExchange exchange) throws Exception {
    final RequestData requestData = parseFormData(exchange);
    boolean persistent = exchange.isPersistent();
    exchange.setPersistent(false); //UNDERTOW-947 MCMP should not use persistent connections
    if (CONFIG.equals(method)) {
        processConfig(exchange, requestData);
    } else if (ENABLE_APP.equals(method)) {
        processCommand(exchange, requestData, MCMPAction.ENABLE);
    } else if (DISABLE_APP.equals(method)) {
        processCommand(exchange, requestData, MCMPAction.DISABLE);
    } else if (STOP_APP.equals(method)) {
        processCommand(exchange, requestData, MCMPAction.STOP);
    } else if (REMOVE_APP.equals(method)) {
        processCommand(exchange, requestData, MCMPAction.REMOVE);
    } else if (STATUS.equals(method)) {
        processStatus(exchange, requestData);
    } else if (INFO.equals(method)) {
        processInfo(exchange);
    } else if (DUMP.equals(method)) {
        processDump(exchange);
    } else if (PING.equals(method)) {
        processPing(exchange, requestData);
    } else {
        exchange.setPersistent(persistent);
        next.handleRequest(exchange);
    }
}
 
Example 5
Source File: HttpTransferEncoding.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
static StreamSinkConduit createSinkConduit(final HttpServerExchange exchange) {
    DateUtils.addDateHeaderIfRequired(exchange);

    boolean headRequest = exchange.getRequestMethod().equals(Methods.HEAD);
    HttpServerConnection serverConnection = (HttpServerConnection) exchange.getConnection();

    HttpResponseConduit responseConduit = serverConnection.getResponseConduit();
    responseConduit.reset(exchange);
    StreamSinkConduit channel = responseConduit;
    if (headRequest) {
        //if this is a head request we add a head channel underneath the content encoding channel
        //this will just discard the data
        //we still go through with the rest of the logic, to make sure all headers are set correctly
        channel = new HeadStreamSinkConduit(channel, terminateResponseListener(exchange));
    } else if(!Connectors.isEntityBodyAllowed(exchange)) {
        //we are not allowed to send an entity body for some requests
        exchange.getResponseHeaders().remove(Headers.CONTENT_LENGTH);
        exchange.getResponseHeaders().remove(Headers.TRANSFER_ENCODING);
        channel = new HeadStreamSinkConduit(channel, terminateResponseListener(exchange));
        return channel;
    }

    final HeaderMap responseHeaders = exchange.getResponseHeaders();
    // test to see if we're still persistent
    String connection = responseHeaders.getFirst(Headers.CONNECTION);
    if (!exchange.isPersistent()) {
        responseHeaders.put(Headers.CONNECTION, Headers.CLOSE.toString());
    } else if (exchange.isPersistent() && connection != null) {
        if (HttpString.tryFromString(connection).equals(Headers.CLOSE)) {
            exchange.setPersistent(false);
        }
    } else if (exchange.getConnection().getUndertowOptions().get(UndertowOptions.ALWAYS_SET_KEEP_ALIVE, true)) {
        responseHeaders.put(Headers.CONNECTION, Headers.KEEP_ALIVE.toString());
    }
    //according to the HTTP RFC we should ignore content length if a transfer coding is specified
    final String transferEncodingHeader = responseHeaders.getLast(Headers.TRANSFER_ENCODING);
    if(transferEncodingHeader == null) {
        final String contentLengthHeader = responseHeaders.getFirst(Headers.CONTENT_LENGTH);
        if (contentLengthHeader != null) {
            StreamSinkConduit res = handleFixedLength(exchange, headRequest, channel, responseHeaders, contentLengthHeader, serverConnection);
            if (res != null) {
                return res;
            }
        }
    } else {
        responseHeaders.remove(Headers.CONTENT_LENGTH); //if there is a transfer-encoding header we remove content length if present
    }
    return handleResponseConduit(exchange, headRequest, channel, responseHeaders, terminateResponseListener(exchange), transferEncodingHeader);
}
 
Example 6
Source File: ServerFixedLengthStreamSinkConduit.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
void reset(long contentLength, HttpServerExchange exchange) {
    this.exchange = exchange;
    super.reset(contentLength, !exchange.isPersistent());
}