2013-03-11 03:12:21 +00:00
|
|
|
|
using ServiceStack.Service;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Server.Implementations.HttpServer
|
|
|
|
|
{
|
|
|
|
|
public class RangeRequestWriter : IStreamWriter
|
|
|
|
|
{
|
|
|
|
|
/// <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; }
|
|
|
|
|
private HttpListenerResponse Response { 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
|
|
|
|
|
|
|
|
|
/// <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="response">The response.</param>
|
|
|
|
|
/// <param name="source">The source.</param>
|
2013-03-13 03:57:54 +00:00
|
|
|
|
/// <param name="isHeadRequest">if set to <c>true</c> [is head request].</param>
|
2013-03-23 04:04:36 +00:00
|
|
|
|
public RangeRequestWriter(string rangeHeader, HttpListenerResponse response, Stream source, bool isHeadRequest)
|
2013-03-11 03:12:21 +00:00
|
|
|
|
{
|
2013-03-23 04:04:36 +00:00
|
|
|
|
RangeHeader = rangeHeader;
|
2013-03-11 03:12:21 +00:00
|
|
|
|
Response = response;
|
|
|
|
|
SourceStream = source;
|
2013-03-13 03:57:54 +00:00
|
|
|
|
IsHeadRequest = isHeadRequest;
|
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>
|
|
|
|
|
protected IEnumerable<KeyValuePair<long, long?>> RequestedRanges
|
|
|
|
|
{
|
|
|
|
|
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]))
|
|
|
|
|
{
|
|
|
|
|
start = long.Parse(vals[0]);
|
|
|
|
|
}
|
|
|
|
|
if (!string.IsNullOrEmpty(vals[1]))
|
|
|
|
|
{
|
|
|
|
|
end = long.Parse(vals[1]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_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)
|
|
|
|
|
{
|
|
|
|
|
Response.Headers["Accept-Ranges"] = "bytes";
|
|
|
|
|
Response.StatusCode = 206;
|
|
|
|
|
|
|
|
|
|
var task = WriteToAsync(responseStream);
|
|
|
|
|
|
|
|
|
|
Task.WaitAll(task);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <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
|
|
|
|
{
|
2013-03-11 03:17:36 +00:00
|
|
|
|
using (var source = SourceStream)
|
|
|
|
|
{
|
|
|
|
|
var requestedRange = RequestedRanges.First();
|
2013-03-11 03:12:21 +00:00
|
|
|
|
|
2013-03-11 03:17:36 +00:00
|
|
|
|
var totalLength = SourceStream.Length;
|
2013-03-11 03:12:21 +00:00
|
|
|
|
|
2013-03-11 03:17:36 +00:00
|
|
|
|
// If the requested range is "0-", we can optimize by just doing a stream copy
|
|
|
|
|
if (!requestedRange.Value.HasValue)
|
|
|
|
|
{
|
|
|
|
|
await ServeCompleteRangeRequest(source, requestedRange, responseStream, totalLength).ConfigureAwait(false);
|
|
|
|
|
}
|
2013-03-11 03:12:21 +00:00
|
|
|
|
|
2013-03-11 03:17:36 +00:00
|
|
|
|
// This will have to buffer a portion of the content into memory
|
|
|
|
|
await ServePartialRangeRequest(source, requestedRange.Key, requestedRange.Value.Value, responseStream, totalLength).ConfigureAwait(false);
|
|
|
|
|
}
|
2013-03-11 03:12:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Handles a range request of "bytes=0-"
|
|
|
|
|
/// This will serve the complete content and add the content-range header
|
|
|
|
|
/// </summary>
|
2013-03-11 03:17:36 +00:00
|
|
|
|
/// <param name="sourceStream">The source stream.</param>
|
2013-03-11 03:12:21 +00:00
|
|
|
|
/// <param name="requestedRange">The requested range.</param>
|
|
|
|
|
/// <param name="responseStream">The response stream.</param>
|
|
|
|
|
/// <param name="totalContentLength">Total length of the content.</param>
|
|
|
|
|
/// <returns>Task.</returns>
|
2013-03-11 03:17:36 +00:00
|
|
|
|
private Task ServeCompleteRangeRequest(Stream sourceStream, KeyValuePair<long, long?> requestedRange, Stream responseStream, long totalContentLength)
|
2013-03-11 03:12:21 +00:00
|
|
|
|
{
|
|
|
|
|
var rangeStart = requestedRange.Key;
|
|
|
|
|
var rangeEnd = totalContentLength - 1;
|
|
|
|
|
var rangeLength = 1 + rangeEnd - rangeStart;
|
|
|
|
|
|
|
|
|
|
// Content-Length is the length of what we're serving, not the original content
|
|
|
|
|
Response.ContentLength64 = rangeLength;
|
|
|
|
|
Response.Headers["Content-Range"] = string.Format("bytes {0}-{1}/{2}", rangeStart, rangeEnd, totalContentLength);
|
|
|
|
|
|
2013-03-13 03:57:54 +00:00
|
|
|
|
// Headers only
|
|
|
|
|
if (IsHeadRequest)
|
|
|
|
|
{
|
|
|
|
|
return Task.FromResult(true);
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-11 03:12:21 +00:00
|
|
|
|
if (rangeStart > 0)
|
|
|
|
|
{
|
2013-03-11 03:17:36 +00:00
|
|
|
|
sourceStream.Position = rangeStart;
|
2013-03-11 03:12:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-03-11 03:17:36 +00:00
|
|
|
|
return sourceStream.CopyToAsync(responseStream);
|
2013-03-11 03:12:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Serves a partial range request
|
|
|
|
|
/// </summary>
|
2013-03-11 03:17:36 +00:00
|
|
|
|
/// <param name="sourceStream">The source stream.</param>
|
2013-03-11 03:12:21 +00:00
|
|
|
|
/// <param name="rangeStart">The range start.</param>
|
|
|
|
|
/// <param name="rangeEnd">The range end.</param>
|
|
|
|
|
/// <param name="responseStream">The response stream.</param>
|
|
|
|
|
/// <param name="totalContentLength">Total length of the content.</param>
|
|
|
|
|
/// <returns>Task.</returns>
|
2013-03-11 03:17:36 +00:00
|
|
|
|
private async Task ServePartialRangeRequest(Stream sourceStream, long rangeStart, long rangeEnd, Stream responseStream, long totalContentLength)
|
2013-03-11 03:12:21 +00:00
|
|
|
|
{
|
|
|
|
|
var rangeLength = 1 + rangeEnd - rangeStart;
|
|
|
|
|
|
|
|
|
|
// Content-Length is the length of what we're serving, not the original content
|
|
|
|
|
Response.ContentLength64 = rangeLength;
|
|
|
|
|
Response.Headers["Content-Range"] = string.Format("bytes {0}-{1}/{2}", rangeStart, rangeEnd, totalContentLength);
|
|
|
|
|
|
2013-03-13 03:57:54 +00:00
|
|
|
|
// Headers only
|
|
|
|
|
if (IsHeadRequest)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-11 03:17:36 +00:00
|
|
|
|
sourceStream.Position = rangeStart;
|
2013-03-11 03:12:21 +00:00
|
|
|
|
|
|
|
|
|
// Fast track to just copy the stream to the end
|
|
|
|
|
if (rangeEnd == totalContentLength - 1)
|
|
|
|
|
{
|
2013-03-11 03:17:36 +00:00
|
|
|
|
await sourceStream.CopyToAsync(responseStream).ConfigureAwait(false);
|
2013-03-11 03:12:21 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// Read the bytes we need
|
|
|
|
|
var buffer = new byte[Convert.ToInt32(rangeLength)];
|
2013-03-11 03:17:36 +00:00
|
|
|
|
await sourceStream.ReadAsync(buffer, 0, buffer.Length).ConfigureAwait(false);
|
2013-03-11 03:12:21 +00:00
|
|
|
|
|
|
|
|
|
await responseStream.WriteAsync(buffer, 0, Convert.ToInt32(rangeLength)).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|