2013-03-10 20:12:21 -07:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2013-03-23 19:45:00 -07:00
|
|
|
using System.Globalization;
|
2013-03-10 20:12:21 -07:00
|
|
|
using System.IO;
|
|
|
|
using System.Net;
|
2016-10-25 12:02:04 -07:00
|
|
|
using System.Threading;
|
2016-07-15 10:13:55 -07:00
|
|
|
using System.Threading.Tasks;
|
2016-10-25 12:02:04 -07:00
|
|
|
using MediaBrowser.Model.Services;
|
2019-01-13 12:20:41 -07:00
|
|
|
using Microsoft.Extensions.Logging;
|
2019-03-05 11:20:28 -07:00
|
|
|
using Microsoft.Net.Http.Headers;
|
2013-03-10 20:12:21 -07:00
|
|
|
|
2016-11-08 11:44:23 -07:00
|
|
|
namespace Emby.Server.Implementations.HttpServer
|
2013-03-10 20:12:21 -07:00
|
|
|
{
|
2016-10-25 12:02:04 -07:00
|
|
|
public class RangeRequestWriter : IAsyncStreamWriter, IHttpResult
|
2013-03-10 20:12:21 -07:00
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Gets or sets the source stream.
|
|
|
|
/// </summary>
|
|
|
|
/// <value>The source stream.</value>
|
2013-03-12 20:57:54 -07:00
|
|
|
private Stream SourceStream { get; set; }
|
2013-03-22 21:04:36 -07:00
|
|
|
private string RangeHeader { get; set; }
|
2013-03-12 20:57:54 -07:00
|
|
|
private bool IsHeadRequest { get; set; }
|
2013-03-10 20:12:21 -07:00
|
|
|
|
2013-03-23 19:45:00 -07:00
|
|
|
private long RangeStart { get; set; }
|
|
|
|
private long RangeEnd { get; set; }
|
|
|
|
private long RangeLength { get; set; }
|
|
|
|
private long TotalContentLength { get; set; }
|
|
|
|
|
2014-09-02 19:30:24 -07:00
|
|
|
public Action OnComplete { get; set; }
|
2015-07-18 11:07:03 -07:00
|
|
|
private readonly ILogger _logger;
|
2014-08-29 05:14:41 -07:00
|
|
|
|
2016-07-27 10:11:25 -07:00
|
|
|
private const int BufferSize = 81920;
|
2015-07-31 13:38:08 -07:00
|
|
|
|
2013-03-23 19:45:00 -07: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>
|
2019-01-06 13:50:43 -07:00
|
|
|
public IDictionary<string, string> Headers => _options;
|
2013-03-23 19:45:00 -07:00
|
|
|
|
2013-03-10 20:12:21 -07:00
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the <see cref="StreamWriter" /> class.
|
|
|
|
/// </summary>
|
2013-03-22 21:04:36 -07:00
|
|
|
/// <param name="rangeHeader">The range header.</param>
|
2013-03-10 20:12:21 -07:00
|
|
|
/// <param name="source">The source.</param>
|
2013-03-23 19:45:00 -07:00
|
|
|
/// <param name="contentType">Type of the content.</param>
|
2013-03-12 20:57:54 -07:00
|
|
|
/// <param name="isHeadRequest">if set to <c>true</c> [is head request].</param>
|
2018-09-12 10:26:21 -07:00
|
|
|
public RangeRequestWriter(string rangeHeader, long contentLength, Stream source, string contentType, bool isHeadRequest, ILogger logger)
|
2013-03-10 20:12:21 -07:00
|
|
|
{
|
2013-03-23 19:45:00 -07:00
|
|
|
if (string.IsNullOrEmpty(contentType))
|
|
|
|
{
|
2019-01-06 13:50:43 -07:00
|
|
|
throw new ArgumentNullException(nameof(contentType));
|
2013-03-23 19:45:00 -07:00
|
|
|
}
|
2014-04-07 03:29:37 -07:00
|
|
|
|
2013-03-22 21:04:36 -07:00
|
|
|
RangeHeader = rangeHeader;
|
2013-03-10 20:12:21 -07:00
|
|
|
SourceStream = source;
|
2013-03-12 20:57:54 -07:00
|
|
|
IsHeadRequest = isHeadRequest;
|
2015-07-18 11:07:03 -07:00
|
|
|
this._logger = logger;
|
2013-03-23 19:45:00 -07:00
|
|
|
|
|
|
|
ContentType = contentType;
|
2019-03-05 11:20:28 -07:00
|
|
|
Headers[HeaderNames.ContentType] = contentType;
|
|
|
|
Headers[HeaderNames.AcceptRanges] = "bytes";
|
2013-03-23 19:45:00 -07:00
|
|
|
StatusCode = HttpStatusCode.PartialContent;
|
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
SetRangeValues(contentLength);
|
2013-03-23 19:45:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Sets the range values.
|
|
|
|
/// </summary>
|
2018-09-12 10:26:21 -07:00
|
|
|
private void SetRangeValues(long contentLength)
|
2013-03-23 19:45:00 -07:00
|
|
|
{
|
2013-09-17 19:43:34 -07:00
|
|
|
var requestedRange = RequestedRanges[0];
|
2013-03-23 19:45:00 -07:00
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
TotalContentLength = contentLength;
|
2013-03-23 19:45:00 -07:00
|
|
|
|
|
|
|
// 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 03:29:37 -07:00
|
|
|
|
2019-04-24 05:06:54 -07:00
|
|
|
Headers[HeaderNames.ContentLength] = RangeLength.ToString(CultureInfo.InvariantCulture);
|
2019-03-05 11:20:28 -07:00
|
|
|
Headers[HeaderNames.ContentRange] = $"bytes {RangeStart}-{RangeEnd}/{TotalContentLength}";
|
2014-04-07 03:29:37 -07:00
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
if (RangeStart > 0 && SourceStream.CanSeek)
|
2013-03-23 19:45:00 -07:00
|
|
|
{
|
|
|
|
SourceStream.Position = RangeStart;
|
|
|
|
}
|
2013-03-10 20:12:21 -07: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-23 19:45:00 -07:00
|
|
|
protected List<KeyValuePair<long, long?>> RequestedRanges
|
2013-03-10 20:12:21 -07:00
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
if (_requestedRanges == null)
|
|
|
|
{
|
|
|
|
_requestedRanges = new List<KeyValuePair<long, long?>>();
|
|
|
|
|
|
|
|
// Example: bytes=0-,32-63
|
2013-03-22 21:04:36 -07:00
|
|
|
var ranges = RangeHeader.Split('=')[1].Split(',');
|
2013-03-10 20:12:21 -07:00
|
|
|
|
|
|
|
foreach (var range in ranges)
|
|
|
|
{
|
|
|
|
var vals = range.Split('-');
|
|
|
|
|
|
|
|
long start = 0;
|
|
|
|
long? end = null;
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(vals[0]))
|
|
|
|
{
|
2013-03-31 10:39:28 -07:00
|
|
|
start = long.Parse(vals[0], UsCulture);
|
2013-03-10 20:12:21 -07:00
|
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(vals[1]))
|
|
|
|
{
|
2013-03-31 10:39:28 -07:00
|
|
|
end = long.Parse(vals[1], UsCulture);
|
2013-03-10 20:12:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
_requestedRanges.Add(new KeyValuePair<long, long?>(start, end));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return _requestedRanges;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-25 12:02:04 -07:00
|
|
|
public async Task WriteToAsync(Stream responseStream, CancellationToken cancellationToken)
|
2016-07-15 10:13:55 -07:00
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
await source.CopyToAsync(responseStream, BufferSize).ConfigureAwait(false);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
await CopyToInternalAsync(source, responseStream, RangeLength).ConfigureAwait(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
if (OnComplete != null)
|
|
|
|
{
|
|
|
|
OnComplete();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-06 13:50:43 -07:00
|
|
|
private static async Task CopyToInternalAsync(Stream source, Stream destination, long copyLength)
|
2016-07-15 10:13:55 -07:00
|
|
|
{
|
|
|
|
var array = new byte[BufferSize];
|
2017-05-05 10:55:38 -07:00
|
|
|
int bytesRead;
|
|
|
|
while ((bytesRead = await source.ReadAsync(array, 0, array.Length).ConfigureAwait(false)) != 0)
|
2016-07-15 10:13:55 -07:00
|
|
|
{
|
2017-05-05 10:55:38 -07:00
|
|
|
if (bytesRead == 0)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
var bytesToCopy = Math.Min(bytesRead, copyLength);
|
2016-07-15 10:13:55 -07:00
|
|
|
|
|
|
|
await destination.WriteAsync(array, 0, Convert.ToInt32(bytesToCopy)).ConfigureAwait(false);
|
|
|
|
|
|
|
|
copyLength -= bytesToCopy;
|
|
|
|
|
|
|
|
if (copyLength <= 0)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-23 19:45:00 -07:00
|
|
|
public string ContentType { get; set; }
|
2013-03-10 20:12:21 -07:00
|
|
|
|
2013-12-07 08:52:38 -07:00
|
|
|
public IRequest RequestContext { get; set; }
|
2013-03-10 20:12:21 -07:00
|
|
|
|
2013-03-23 19:45:00 -07:00
|
|
|
public object Response { get; set; }
|
2013-03-12 20:57:54 -07:00
|
|
|
|
2013-03-23 19:45:00 -07:00
|
|
|
public int Status { get; set; }
|
2013-03-10 20:12:21 -07:00
|
|
|
|
2013-03-23 19:45:00 -07:00
|
|
|
public HttpStatusCode StatusCode
|
2013-03-10 20:12:21 -07:00
|
|
|
{
|
2019-01-06 13:50:43 -07:00
|
|
|
get => (HttpStatusCode)Status;
|
|
|
|
set => Status = (int)value;
|
2013-03-10 20:12:21 -07:00
|
|
|
}
|
|
|
|
}
|
2018-12-13 06:18:25 -07:00
|
|
|
}
|