Search in sources :

Example 6 with StreamSinkConduit

use of org.xnio.conduits.StreamSinkConduit in project undertow by undertow-io.

the class ByteRangeHandler method handleRequest.

@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
    //range requests are only support for GET requests as per the RFC
    if (!Methods.GET.equals(exchange.getRequestMethod()) && !Methods.HEAD.equals(exchange.getRequestMethod())) {
        next.handleRequest(exchange);
        return;
    }
    if (sendAcceptRanges) {
        exchange.addResponseCommitListener(ACCEPT_RANGE_LISTENER);
    }
    final ByteRange range = ByteRange.parse(exchange.getRequestHeaders().getFirst(Headers.RANGE));
    if (range != null && range.getRanges() == 1) {
        exchange.addResponseWrapper(new ConduitWrapper<StreamSinkConduit>() {

            @Override
            public StreamSinkConduit wrap(ConduitFactory<StreamSinkConduit> factory, HttpServerExchange exchange) {
                if (exchange.getStatusCode() != StatusCodes.OK) {
                    return factory.create();
                }
                String length = exchange.getResponseHeaders().getFirst(Headers.CONTENT_LENGTH);
                if (length == null) {
                    return factory.create();
                }
                long responseLength = Long.parseLong(length);
                ByteRange.RangeResponseResult rangeResponse = range.getResponseResult(responseLength, exchange.getRequestHeaders().getFirst(Headers.IF_RANGE), DateUtils.parseDate(exchange.getResponseHeaders().getFirst(Headers.LAST_MODIFIED)), exchange.getResponseHeaders().getFirst(Headers.ETAG));
                if (rangeResponse != null) {
                    long start = rangeResponse.getStart();
                    long end = rangeResponse.getEnd();
                    exchange.setStatusCode(rangeResponse.getStatusCode());
                    exchange.getResponseHeaders().put(Headers.CONTENT_RANGE, rangeResponse.getContentRange());
                    exchange.setResponseContentLength(rangeResponse.getContentLength());
                    if (rangeResponse.getStatusCode() == StatusCodes.REQUEST_RANGE_NOT_SATISFIABLE) {
                        return new HeadStreamSinkConduit(factory.create(), null, true);
                    }
                    return new RangeStreamSinkConduit(factory.create(), start, end, responseLength);
                } else {
                    return factory.create();
                }
            }
        });
    }
    next.handleRequest(exchange);
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) RangeStreamSinkConduit(io.undertow.conduits.RangeStreamSinkConduit) ByteRange(io.undertow.util.ByteRange) HeadStreamSinkConduit(io.undertow.conduits.HeadStreamSinkConduit) RangeStreamSinkConduit(io.undertow.conduits.RangeStreamSinkConduit) StreamSinkConduit(org.xnio.conduits.StreamSinkConduit) HeadStreamSinkConduit(io.undertow.conduits.HeadStreamSinkConduit)

Example 7 with StreamSinkConduit

use of org.xnio.conduits.StreamSinkConduit in project undertow by undertow-io.

the class HttpTransferEncoding method createSinkConduit.

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;
            }
        }
    }
    return handleResponseConduit(exchange, headRequest, channel, responseHeaders, terminateResponseListener(exchange), transferEncodingHeader);
}
Also used : HeaderMap(io.undertow.util.HeaderMap) HeadStreamSinkConduit(io.undertow.conduits.HeadStreamSinkConduit) FinishableStreamSinkConduit(io.undertow.conduits.FinishableStreamSinkConduit) StreamSinkConduit(org.xnio.conduits.StreamSinkConduit) PreChunkedStreamSinkConduit(io.undertow.conduits.PreChunkedStreamSinkConduit) ChunkedStreamSinkConduit(io.undertow.conduits.ChunkedStreamSinkConduit) HttpString(io.undertow.util.HttpString) HeadStreamSinkConduit(io.undertow.conduits.HeadStreamSinkConduit)

Aggregations

StreamSinkConduit (org.xnio.conduits.StreamSinkConduit)7 HttpServerExchange (io.undertow.server.HttpServerExchange)4 HttpString (io.undertow.util.HttpString)4 ChunkedStreamSinkConduit (io.undertow.conduits.ChunkedStreamSinkConduit)2 HeadStreamSinkConduit (io.undertow.conduits.HeadStreamSinkConduit)2 HeaderMap (io.undertow.util.HeaderMap)2 ConduitStreamSinkChannel (org.xnio.conduits.ConduitStreamSinkChannel)2 ClientRequest (io.undertow.client.ClientRequest)1 BytesSentStreamSinkConduit (io.undertow.conduits.BytesSentStreamSinkConduit)1 FinishableStreamSinkConduit (io.undertow.conduits.FinishableStreamSinkConduit)1 PreChunkedStreamSinkConduit (io.undertow.conduits.PreChunkedStreamSinkConduit)1 RangeStreamSinkConduit (io.undertow.conduits.RangeStreamSinkConduit)1 StoredResponseStreamSinkConduit (io.undertow.conduits.StoredResponseStreamSinkConduit)1 Http2HeadersStreamSinkChannel (io.undertow.protocols.http2.Http2HeadersStreamSinkChannel)1 ExchangeCompletionListener (io.undertow.server.ExchangeCompletionListener)1 Resource (io.undertow.server.handlers.resource.Resource)1 ByteRange (io.undertow.util.ByteRange)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 FileChannel (java.nio.channels.FileChannel)1