2015-07-18 18:07:03 +00:00
|
|
|
|
using MediaBrowser.Model.Logging;
|
|
|
|
|
using ServiceStack.Web;
|
2013-03-11 03:12:21 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2013-03-24 02:45:00 +00:00
|
|
|
|
using System.Globalization;
|
2013-03-11 03:12:21 +00:00
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Net;
|
2014-08-29 04:06:30 +00:00
|
|
|
|
using System.Threading;
|
2013-03-11 03:12:21 +00:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Server.Implementations.HttpServer
|
|
|
|
|
{
|
2013-03-24 02:45:00 +00:00
|
|
|
|
public class RangeRequestWriter : IStreamWriter, IHttpResult
|
2013-03-11 03:12:21 +00:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the source stream.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The source stream.</value>
|
2013-03-13 03:57:54 +00:00
|
|
|
|
private Stream SourceStream { get; set; }
|
2013-03-23 04:04:36 +00:00
|
|
|
|
private string RangeHeader { get; set; }
|
2013-03-13 03:57:54 +00:00
|
|
|
|
private bool IsHeadRequest { get; set; }
|
2013-03-11 03:12:21 +00:00
|
|
|
|
|
2013-03-24 02:45:00 +00:00
|
|
|
|
private long RangeStart { get; set; }
|
|
|
|
|
private long RangeEnd { get; set; }
|
|
|
|
|
private long RangeLength { get; set; }
|
|
|
|
|
private long TotalContentLength { get; set; }
|
|
|
|
|
|
2014-09-03 02:30:24 +00:00
|
|
|
|
public Action OnComplete { get; set; }
|
2015-07-18 18:07:03 +00:00
|
|
|
|
private readonly ILogger _logger;
|
2014-08-29 12:14:41 +00:00
|
|
|
|
|
2015-07-31 20:38:08 +00:00
|
|
|
|
// 256k
|
|
|
|
|
private const int BufferSize = 262144;
|
|
|
|
|
|
2013-03-24 02:45:00 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The _options
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly Dictionary<string, string> _options = new Dictionary<string, string>();
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The us culture
|
|
|
|
|
/// </summary>
|
|
|
|
|
private static readonly CultureInfo UsCulture = new CultureInfo("en-US");
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Additional HTTP Headers
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The headers.</value>
|
|
|
|
|
public Dictionary<string, string> Headers
|
|
|
|
|
{
|
|
|
|
|
get { return _options; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the options.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The options.</value>
|
|
|
|
|
public IDictionary<string, string> Options
|
|
|
|
|
{
|
|
|
|
|
get { return Headers; }
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-11 03:12:21 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Initializes a new instance of the <see cref="StreamWriter" /> class.
|
|
|
|
|
/// </summary>
|
2013-03-23 04:04:36 +00:00
|
|
|
|
/// <param name="rangeHeader">The range header.</param>
|
2013-03-11 03:12:21 +00:00
|
|
|
|
/// <param name="source">The source.</param>
|
2013-03-24 02:45:00 +00:00
|
|
|
|
/// <param name="contentType">Type of the content.</param>
|
2013-03-13 03:57:54 +00:00
|
|
|
|
/// <param name="isHeadRequest">if set to <c>true</c> [is head request].</param>
|
2015-07-18 18:07:03 +00:00
|
|
|
|
public RangeRequestWriter(string rangeHeader, Stream source, string contentType, bool isHeadRequest, ILogger logger)
|
2013-03-11 03:12:21 +00:00
|
|
|
|
{
|
2013-03-24 02:45:00 +00:00
|
|
|
|
if (string.IsNullOrEmpty(contentType))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("contentType");
|
|
|
|
|
}
|
2014-04-07 10:29:37 +00:00
|
|
|
|
|
2013-03-23 04:04:36 +00:00
|
|
|
|
RangeHeader = rangeHeader;
|
2013-03-11 03:12:21 +00:00
|
|
|
|
SourceStream = source;
|
2013-03-13 03:57:54 +00:00
|
|
|
|
IsHeadRequest = isHeadRequest;
|
2015-07-18 18:07:03 +00:00
|
|
|
|
this._logger = logger;
|
2013-03-24 02:45:00 +00:00
|
|
|
|
|
|
|
|
|
ContentType = contentType;
|
|
|
|
|
Options["Content-Type"] = contentType;
|
|
|
|
|
Options["Accept-Ranges"] = "bytes";
|
|
|
|
|
StatusCode = HttpStatusCode.PartialContent;
|
|
|
|
|
|
|
|
|
|
SetRangeValues();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Sets the range values.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private void SetRangeValues()
|
|
|
|
|
{
|
2013-09-18 02:43:34 +00:00
|
|
|
|
var requestedRange = RequestedRanges[0];
|
2013-03-24 02:45:00 +00:00
|
|
|
|
|
|
|
|
|
TotalContentLength = SourceStream.Length;
|
|
|
|
|
|
|
|
|
|
// If the requested range is "0-", we can optimize by just doing a stream copy
|
|
|
|
|
if (!requestedRange.Value.HasValue)
|
|
|
|
|
{
|
|
|
|
|
RangeEnd = TotalContentLength - 1;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
RangeEnd = requestedRange.Value.Value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RangeStart = requestedRange.Key;
|
|
|
|
|
RangeLength = 1 + RangeEnd - RangeStart;
|
2014-04-07 10:29:37 +00:00
|
|
|
|
|
2013-03-24 02:45:00 +00:00
|
|
|
|
// Content-Length is the length of what we're serving, not the original content
|
|
|
|
|
Options["Content-Length"] = RangeLength.ToString(UsCulture);
|
|
|
|
|
Options["Content-Range"] = string.Format("bytes {0}-{1}/{2}", RangeStart, RangeEnd, TotalContentLength);
|
2014-04-07 10:29:37 +00:00
|
|
|
|
|
2013-03-24 02:45:00 +00:00
|
|
|
|
if (RangeStart > 0)
|
|
|
|
|
{
|
|
|
|
|
SourceStream.Position = RangeStart;
|
|
|
|
|
}
|
2013-03-11 03:12:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The _requested ranges
|
|
|
|
|
/// </summary>
|
|
|
|
|
private List<KeyValuePair<long, long?>> _requestedRanges;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the requested ranges.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The requested ranges.</value>
|
2013-03-24 02:45:00 +00:00
|
|
|
|
protected List<KeyValuePair<long, long?>> RequestedRanges
|
2013-03-11 03:12:21 +00:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_requestedRanges == null)
|
|
|
|
|
{
|
|
|
|
|
_requestedRanges = new List<KeyValuePair<long, long?>>();
|
|
|
|
|
|
|
|
|
|
// Example: bytes=0-,32-63
|
2013-03-23 04:04:36 +00:00
|
|
|
|
var ranges = RangeHeader.Split('=')[1].Split(',');
|
2013-03-11 03:12:21 +00:00
|
|
|
|
|
|
|
|
|
foreach (var range in ranges)
|
|
|
|
|
{
|
|
|
|
|
var vals = range.Split('-');
|
|
|
|
|
|
|
|
|
|
long start = 0;
|
|
|
|
|
long? end = null;
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(vals[0]))
|
|
|
|
|
{
|
2013-03-31 17:39:28 +00:00
|
|
|
|
start = long.Parse(vals[0], UsCulture);
|
2013-03-11 03:12:21 +00:00
|
|
|
|
}
|
|
|
|
|
if (!string.IsNullOrEmpty(vals[1]))
|
|
|
|
|
{
|
2013-03-31 17:39:28 +00:00
|
|
|
|
end = long.Parse(vals[1], UsCulture);
|
2013-03-11 03:12:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_requestedRanges.Add(new KeyValuePair<long, long?>(start, end));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return _requestedRanges;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Writes to.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="responseStream">The response stream.</param>
|
|
|
|
|
public void WriteTo(Stream responseStream)
|
|
|
|
|
{
|
2014-10-22 05:06:19 +00:00
|
|
|
|
WriteToInternal(responseStream);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Writes to async.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="responseStream">The response stream.</param>
|
|
|
|
|
/// <returns>Task.</returns>
|
|
|
|
|
private void WriteToInternal(Stream responseStream)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
// Headers only
|
|
|
|
|
if (IsHeadRequest)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
using (var source = SourceStream)
|
|
|
|
|
{
|
|
|
|
|
// If the requested range is "0-", we can optimize by just doing a stream copy
|
|
|
|
|
if (RangeEnd >= TotalContentLength - 1)
|
|
|
|
|
{
|
2015-07-31 20:38:08 +00:00
|
|
|
|
source.CopyTo(responseStream, BufferSize);
|
2014-10-22 05:06:19 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2015-07-18 18:07:03 +00:00
|
|
|
|
CopyToInternal(source, responseStream, RangeLength);
|
2014-10-22 05:06:19 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-07-18 18:07:03 +00:00
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error in range request writer", ex);
|
|
|
|
|
throw;
|
|
|
|
|
}
|
2014-10-22 05:06:19 +00:00
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
if (OnComplete != null)
|
|
|
|
|
{
|
|
|
|
|
OnComplete();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-18 18:07:03 +00:00
|
|
|
|
private void CopyToInternal(Stream source, Stream destination, long copyLength)
|
2014-10-22 05:06:19 +00:00
|
|
|
|
{
|
2015-07-31 20:38:08 +00:00
|
|
|
|
var array = new byte[BufferSize];
|
2014-10-22 05:06:19 +00:00
|
|
|
|
int count;
|
|
|
|
|
while ((count = source.Read(array, 0, array.Length)) != 0)
|
|
|
|
|
{
|
|
|
|
|
var bytesToCopy = Math.Min(count, copyLength);
|
|
|
|
|
|
2015-07-18 18:07:03 +00:00
|
|
|
|
destination.Write(array, 0, Convert.ToInt32(bytesToCopy));
|
2013-03-11 03:12:21 +00:00
|
|
|
|
|
2014-10-22 05:06:19 +00:00
|
|
|
|
copyLength -= bytesToCopy;
|
|
|
|
|
|
|
|
|
|
if (copyLength <= 0)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-03-11 03:12:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Writes to async.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="responseStream">The response stream.</param>
|
|
|
|
|
/// <returns>Task.</returns>
|
2013-03-11 03:17:36 +00:00
|
|
|
|
private async Task WriteToAsync(Stream responseStream)
|
2013-03-11 03:12:21 +00:00
|
|
|
|
{
|
2014-09-03 02:30:24 +00:00
|
|
|
|
try
|
2013-03-11 03:17:36 +00:00
|
|
|
|
{
|
2014-09-03 02:30:24 +00:00
|
|
|
|
// Headers only
|
|
|
|
|
if (IsHeadRequest)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2013-03-11 03:12:21 +00:00
|
|
|
|
|
2014-09-03 02:30:24 +00:00
|
|
|
|
using (var source = SourceStream)
|
2014-04-07 16:48:57 +00:00
|
|
|
|
{
|
2014-09-03 02:30:24 +00:00
|
|
|
|
// If the requested range is "0-", we can optimize by just doing a stream copy
|
|
|
|
|
if (RangeEnd >= TotalContentLength - 1)
|
|
|
|
|
{
|
2015-07-31 20:38:08 +00:00
|
|
|
|
await source.CopyToAsync(responseStream, BufferSize).ConfigureAwait(false);
|
2014-09-03 02:30:24 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
await CopyToAsyncInternal(source, responseStream, Convert.ToInt32(RangeLength), CancellationToken.None).ConfigureAwait(false);
|
|
|
|
|
}
|
2014-04-07 16:48:57 +00:00
|
|
|
|
}
|
2014-09-03 02:30:24 +00:00
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
if (OnComplete != null)
|
2014-04-07 16:48:57 +00:00
|
|
|
|
{
|
2014-09-03 02:30:24 +00:00
|
|
|
|
OnComplete();
|
2014-04-07 16:48:57 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task CopyToAsyncInternal(Stream source, Stream destination, int copyLength, CancellationToken cancellationToken)
|
|
|
|
|
{
|
2015-07-31 20:38:08 +00:00
|
|
|
|
var array = new byte[BufferSize];
|
2014-04-07 16:48:57 +00:00
|
|
|
|
int count;
|
|
|
|
|
while ((count = await source.ReadAsync(array, 0, array.Length, cancellationToken).ConfigureAwait(false)) != 0)
|
|
|
|
|
{
|
|
|
|
|
var bytesToCopy = Math.Min(count, copyLength);
|
|
|
|
|
|
|
|
|
|
await destination.WriteAsync(array, 0, bytesToCopy, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
copyLength -= bytesToCopy;
|
|
|
|
|
|
|
|
|
|
if (copyLength <= 0)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
2013-03-11 03:17:36 +00:00
|
|
|
|
}
|
2013-03-11 03:12:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-03-24 02:45:00 +00:00
|
|
|
|
public string ContentType { get; set; }
|
2013-03-11 03:12:21 +00:00
|
|
|
|
|
2013-12-07 15:52:38 +00:00
|
|
|
|
public IRequest RequestContext { get; set; }
|
2013-03-11 03:12:21 +00:00
|
|
|
|
|
2013-03-24 02:45:00 +00:00
|
|
|
|
public object Response { get; set; }
|
2013-03-13 03:57:54 +00:00
|
|
|
|
|
2013-03-24 02:45:00 +00:00
|
|
|
|
public IContentTypeWriter ResponseFilter { get; set; }
|
2013-03-11 03:12:21 +00:00
|
|
|
|
|
2013-03-24 02:45:00 +00:00
|
|
|
|
public int Status { get; set; }
|
2013-03-11 03:12:21 +00:00
|
|
|
|
|
2013-03-24 02:45:00 +00:00
|
|
|
|
public HttpStatusCode StatusCode
|
2013-03-11 03:12:21 +00:00
|
|
|
|
{
|
2013-03-24 02:45:00 +00:00
|
|
|
|
get { return (HttpStatusCode)Status; }
|
|
|
|
|
set { Status = (int)value; }
|
2013-03-11 03:12:21 +00:00
|
|
|
|
}
|
2013-03-24 02:45:00 +00:00
|
|
|
|
|
|
|
|
|
public string StatusDescription { get; set; }
|
2014-07-02 04:57:18 +00:00
|
|
|
|
|
|
|
|
|
public int PaddingLength { get; set; }
|
2013-03-11 03:12:21 +00:00
|
|
|
|
}
|
2014-04-07 16:48:57 +00:00
|
|
|
|
}
|