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);
}
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);
}
Aggregations