using System.Net;
namespace EmbedIO
{
///
/// When thrown, breaks the request handling control flow
/// and sends a redirection response to the client.
///
#pragma warning disable CA1032 // Implement standard exception constructors - they have no meaning here.
public class HttpRangeNotSatisfiableException : HttpException
#pragma warning restore CA1032
{
///
/// Initializes a new instance of the class.
/// without specifying a value for the response's Content-Range header.
///
public HttpRangeNotSatisfiableException()
: this(null)
{
}
///
/// Initializes a new instance of the class.
///
/// The total length of the requested resource, expressed in bytes,
/// or to omit the Content-Range header in the response.
public HttpRangeNotSatisfiableException(long? contentLength)
: base((int)HttpStatusCode.RequestedRangeNotSatisfiable)
{
ContentLength = contentLength;
}
///
/// Gets the total content length to be specified
/// on the response's Content-Range header.
///
public long? ContentLength { get; }
///
public override void PrepareResponse(IHttpContext context)
{
// RFC 7233, Section 3.1: "When this status code is generated in response
// to a byte-range request, the sender
// SHOULD generate a Content-Range header field specifying
// the current length of the selected representation."
if (ContentLength.HasValue)
context.Response.Headers.Set(HttpHeaderNames.ContentRange, $"bytes */{ContentLength.Value}");
}
}
}