Search in sources :

Example 1 with ChunkedStreamSourceConduit

use of io.undertow.conduits.ChunkedStreamSourceConduit in project undertow by undertow-io.

the class HttpTransferEncoding method handleRequestEncoding.

private static boolean handleRequestEncoding(final HttpServerExchange exchange, String transferEncodingHeader, String contentLengthHeader, HttpServerConnection connection, PipeliningBufferingStreamSinkConduit pipeliningBuffer, boolean persistentConnection) {
    HttpString transferEncoding = Headers.IDENTITY;
    if (transferEncodingHeader != null) {
        transferEncoding = new HttpString(transferEncodingHeader);
    }
    if (transferEncodingHeader != null && !transferEncoding.equals(Headers.IDENTITY)) {
        ConduitStreamSourceChannel sourceChannel = ((HttpServerConnection) exchange.getConnection()).getChannel().getSourceChannel();
        sourceChannel.setConduit(new ChunkedStreamSourceConduit(sourceChannel.getConduit(), exchange, chunkedDrainListener(exchange)));
    } else if (contentLengthHeader != null) {
        final long contentLength;
        contentLength = parsePositiveLong(contentLengthHeader);
        if (contentLength == 0L) {
            log.trace("No content, starting next request");
            // no content - immediately start the next request, returning an empty stream for this one
            Connectors.terminateRequest(exchange);
        } else {
            // fixed-length content - add a wrapper for a fixed-length stream
            ConduitStreamSourceChannel sourceChannel = ((HttpServerConnection) exchange.getConnection()).getChannel().getSourceChannel();
            sourceChannel.setConduit(fixedLengthStreamSourceConduitWrapper(contentLength, sourceChannel.getConduit(), exchange));
        }
    } else if (transferEncodingHeader != null) {
        //identity transfer encoding
        log.trace("Connection not persistent (no content length and identity transfer encoding)");
        // make it not persistent
        persistentConnection = false;
    } else if (persistentConnection) {
        //performance
        if (connection.getExtraBytes() != null && pipeliningBuffer == null && connection.getUndertowOptions().get(UndertowOptions.BUFFER_PIPELINED_DATA, false)) {
            pipeliningBuffer = new PipeliningBufferingStreamSinkConduit(connection.getOriginalSinkConduit(), connection.getByteBufferPool());
            connection.setPipelineBuffer(pipeliningBuffer);
            pipeliningBuffer.setupPipelineBuffer(exchange);
        }
        // no content - immediately start the next request, returning an empty stream for this one
        Connectors.terminateRequest(exchange);
    } else {
        //assume there is no content
        //we still know there is no content
        Connectors.terminateRequest(exchange);
    }
    return persistentConnection;
}
Also used : ConduitStreamSourceChannel(org.xnio.conduits.ConduitStreamSourceChannel) ChunkedStreamSourceConduit(io.undertow.conduits.ChunkedStreamSourceConduit) HttpString(io.undertow.util.HttpString)

Example 2 with ChunkedStreamSourceConduit

use of io.undertow.conduits.ChunkedStreamSourceConduit in project undertow by undertow-io.

the class HttpClientConnection method prepareResponseChannel.

private void prepareResponseChannel(ClientResponse response, ClientExchange exchange) {
    String encoding = response.getResponseHeaders().getLast(TRANSFER_ENCODING);
    boolean chunked = encoding != null && Headers.CHUNKED.equals(new HttpString(encoding));
    String length = response.getResponseHeaders().getFirst(CONTENT_LENGTH);
    if (exchange.getRequest().getMethod().equals(Methods.HEAD)) {
        connection.getSourceChannel().setConduit(new FixedLengthStreamSourceConduit(connection.getSourceChannel().getConduit(), 0, responseFinishedListener));
    } else if (chunked) {
        connection.getSourceChannel().setConduit(new ChunkedStreamSourceConduit(connection.getSourceChannel().getConduit(), pushBackStreamSourceConduit, bufferPool, responseFinishedListener, exchange));
    } else if (length != null) {
        try {
            long contentLength = Long.parseLong(length);
            connection.getSourceChannel().setConduit(new FixedLengthStreamSourceConduit(connection.getSourceChannel().getConduit(), contentLength, responseFinishedListener));
        } catch (NumberFormatException e) {
            handleError(new IOException(e));
            throw e;
        }
    } else if (response.getProtocol().equals(Protocols.HTTP_1_1) && !Connectors.isEntityBodyAllowed(response.getResponseCode())) {
        connection.getSourceChannel().setConduit(new FixedLengthStreamSourceConduit(connection.getSourceChannel().getConduit(), 0, responseFinishedListener));
    } else {
        state |= CLOSE_REQ;
    }
}
Also used : HttpString(io.undertow.util.HttpString) IOException(java.io.IOException) FixedLengthStreamSourceConduit(io.undertow.conduits.FixedLengthStreamSourceConduit) ChunkedStreamSourceConduit(io.undertow.conduits.ChunkedStreamSourceConduit) HttpString(io.undertow.util.HttpString)

Aggregations

ChunkedStreamSourceConduit (io.undertow.conduits.ChunkedStreamSourceConduit)2 HttpString (io.undertow.util.HttpString)2 FixedLengthStreamSourceConduit (io.undertow.conduits.FixedLengthStreamSourceConduit)1 IOException (java.io.IOException)1 ConduitStreamSourceChannel (org.xnio.conduits.ConduitStreamSourceChannel)1