2012-09-08 14:52:13 +00:00
|
|
|
|
using MediaBrowser.Common.Logging;
|
|
|
|
|
using System;
|
2012-08-10 13:07:58 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Common.Net.Handlers
|
|
|
|
|
{
|
|
|
|
|
public class StaticFileHandler : BaseHandler
|
|
|
|
|
{
|
2012-09-08 14:52:13 +00:00
|
|
|
|
public override bool HandlesRequest(HttpListenerRequest request)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-11 03:06:08 +00:00
|
|
|
|
private string _Path;
|
2012-08-10 13:18:30 +00:00
|
|
|
|
public virtual string Path
|
2012-08-10 13:07:58 +00:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2012-08-11 03:06:08 +00:00
|
|
|
|
if (!string.IsNullOrWhiteSpace(_Path))
|
|
|
|
|
{
|
|
|
|
|
return _Path;
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-10 13:07:58 +00:00
|
|
|
|
return QueryString["path"];
|
|
|
|
|
}
|
2012-08-11 03:06:08 +00:00
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_Path = value;
|
|
|
|
|
}
|
2012-08-10 13:07:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-09-11 18:20:12 +00:00
|
|
|
|
private bool _SourceStreamEnsured;
|
|
|
|
|
private Stream _SourceStream;
|
2012-08-15 13:20:29 +00:00
|
|
|
|
private Stream SourceStream
|
2012-08-10 13:07:58 +00:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2012-08-15 13:20:29 +00:00
|
|
|
|
EnsureSourceStream();
|
|
|
|
|
return _SourceStream;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void EnsureSourceStream()
|
|
|
|
|
{
|
|
|
|
|
if (!_SourceStreamEnsured)
|
|
|
|
|
{
|
|
|
|
|
try
|
2012-08-10 13:07:58 +00:00
|
|
|
|
{
|
2012-08-15 13:20:29 +00:00
|
|
|
|
_SourceStream = File.OpenRead(Path);
|
|
|
|
|
}
|
|
|
|
|
catch (FileNotFoundException ex)
|
|
|
|
|
{
|
|
|
|
|
StatusCode = 404;
|
|
|
|
|
Logger.LogException(ex);
|
|
|
|
|
}
|
|
|
|
|
catch (DirectoryNotFoundException ex)
|
|
|
|
|
{
|
|
|
|
|
StatusCode = 404;
|
|
|
|
|
Logger.LogException(ex);
|
|
|
|
|
}
|
|
|
|
|
catch (UnauthorizedAccessException ex)
|
|
|
|
|
{
|
|
|
|
|
StatusCode = 403;
|
|
|
|
|
Logger.LogException(ex);
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
_SourceStreamEnsured = true;
|
2012-08-10 13:07:58 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override bool SupportsByteRangeRequests
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-19 20:38:31 +00:00
|
|
|
|
public override bool ShouldCompressResponse(string contentType)
|
2012-08-10 13:07:58 +00:00
|
|
|
|
{
|
2012-08-19 20:38:31 +00:00
|
|
|
|
// Can't compress these
|
|
|
|
|
if (IsRangeRequest)
|
2012-08-10 13:07:58 +00:00
|
|
|
|
{
|
2012-08-19 20:38:31 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2012-08-10 13:07:58 +00:00
|
|
|
|
|
2012-08-19 20:38:31 +00:00
|
|
|
|
// Don't compress media
|
|
|
|
|
if (contentType.StartsWith("audio/", StringComparison.OrdinalIgnoreCase) || contentType.StartsWith("video/", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
2012-08-10 13:07:58 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2012-08-19 20:38:31 +00:00
|
|
|
|
|
|
|
|
|
// It will take some work to support compression within this handler
|
|
|
|
|
return false;
|
2012-08-10 13:07:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override long? GetTotalContentLength()
|
|
|
|
|
{
|
2012-08-15 13:20:29 +00:00
|
|
|
|
return SourceStream.Length;
|
2012-08-10 13:07:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-08-19 20:38:31 +00:00
|
|
|
|
protected override Task<DateTime?> GetLastDateModified()
|
2012-08-10 13:07:58 +00:00
|
|
|
|
{
|
2012-08-22 02:50:59 +00:00
|
|
|
|
DateTime? value = null;
|
2012-08-19 20:38:31 +00:00
|
|
|
|
|
2012-08-22 02:50:59 +00:00
|
|
|
|
EnsureSourceStream();
|
2012-08-15 13:20:29 +00:00
|
|
|
|
|
2012-08-22 02:50:59 +00:00
|
|
|
|
if (SourceStream != null)
|
|
|
|
|
{
|
2012-09-04 19:23:15 +00:00
|
|
|
|
value = File.GetLastWriteTimeUtc(Path);
|
2012-08-22 02:50:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-09-11 18:20:12 +00:00
|
|
|
|
return Task.FromResult(value);
|
2012-08-10 13:07:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-08-19 20:38:31 +00:00
|
|
|
|
public override Task<string> GetContentType()
|
2012-08-10 13:07:58 +00:00
|
|
|
|
{
|
2012-09-11 18:20:12 +00:00
|
|
|
|
return Task.FromResult(MimeTypes.GetMimeType(Path));
|
2012-08-10 13:07:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-08-19 20:38:31 +00:00
|
|
|
|
protected override Task PrepareResponse()
|
2012-08-15 13:20:29 +00:00
|
|
|
|
{
|
2012-08-22 02:50:59 +00:00
|
|
|
|
EnsureSourceStream();
|
|
|
|
|
return Task.FromResult<object>(null);
|
2012-08-15 13:20:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-08-22 02:50:59 +00:00
|
|
|
|
protected override Task WriteResponseToOutputStream(Stream stream)
|
2012-08-10 13:07:58 +00:00
|
|
|
|
{
|
2012-08-15 13:20:29 +00:00
|
|
|
|
if (IsRangeRequest)
|
2012-08-10 13:07:58 +00:00
|
|
|
|
{
|
2012-08-15 13:20:29 +00:00
|
|
|
|
KeyValuePair<long, long?> requestedRange = RequestedRanges.First();
|
|
|
|
|
|
|
|
|
|
// If the requested range is "0-" and we know the total length, we can optimize by avoiding having to buffer the content into memory
|
|
|
|
|
if (requestedRange.Value == null && TotalContentLength != null)
|
2012-08-10 13:07:58 +00:00
|
|
|
|
{
|
2012-08-22 02:50:59 +00:00
|
|
|
|
return ServeCompleteRangeRequest(requestedRange, stream);
|
2012-08-15 13:20:29 +00:00
|
|
|
|
}
|
2012-09-11 18:20:12 +00:00
|
|
|
|
if (TotalContentLength.HasValue)
|
2012-08-15 13:20:29 +00:00
|
|
|
|
{
|
|
|
|
|
// This will have to buffer a portion of the content into memory
|
2012-08-22 02:50:59 +00:00
|
|
|
|
return ServePartialRangeRequestWithKnownTotalContentLength(requestedRange, stream);
|
2012-08-15 13:20:29 +00:00
|
|
|
|
}
|
2012-09-11 18:20:12 +00:00
|
|
|
|
|
|
|
|
|
// This will have to buffer the entire content into memory
|
|
|
|
|
return ServePartialRangeRequestWithUnknownTotalContentLength(requestedRange, stream);
|
2012-08-10 13:07:58 +00:00
|
|
|
|
}
|
2012-09-11 18:20:12 +00:00
|
|
|
|
|
|
|
|
|
return SourceStream.CopyToAsync(stream);
|
2012-08-15 13:20:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void DisposeResponseStream()
|
|
|
|
|
{
|
|
|
|
|
base.DisposeResponseStream();
|
|
|
|
|
|
|
|
|
|
if (SourceStream != null)
|
2012-08-10 13:07:58 +00:00
|
|
|
|
{
|
2012-08-15 13:20:29 +00:00
|
|
|
|
SourceStream.Dispose();
|
2012-08-10 13:07:58 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Handles a range request of "bytes=0-"
|
|
|
|
|
/// This will serve the complete content and add the content-range header
|
|
|
|
|
/// </summary>
|
2012-08-22 02:50:59 +00:00
|
|
|
|
private Task ServeCompleteRangeRequest(KeyValuePair<long, long?> requestedRange, Stream responseStream)
|
2012-08-10 13:07:58 +00:00
|
|
|
|
{
|
|
|
|
|
long totalContentLength = TotalContentLength.Value;
|
|
|
|
|
|
|
|
|
|
long rangeStart = requestedRange.Key;
|
|
|
|
|
long rangeEnd = totalContentLength - 1;
|
|
|
|
|
long rangeLength = 1 + rangeEnd - rangeStart;
|
|
|
|
|
|
|
|
|
|
// Content-Length is the length of what we're serving, not the original content
|
|
|
|
|
HttpListenerContext.Response.ContentLength64 = rangeLength;
|
|
|
|
|
HttpListenerContext.Response.Headers["Content-Range"] = string.Format("bytes {0}-{1}/{2}", rangeStart, rangeEnd, totalContentLength);
|
|
|
|
|
|
|
|
|
|
if (rangeStart > 0)
|
|
|
|
|
{
|
2012-08-15 13:20:29 +00:00
|
|
|
|
SourceStream.Position = rangeStart;
|
2012-08-10 13:07:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-08-22 02:50:59 +00:00
|
|
|
|
return SourceStream.CopyToAsync(responseStream);
|
2012-08-10 13:07:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Serves a partial range request where the total content length is not known
|
|
|
|
|
/// </summary>
|
|
|
|
|
private async Task ServePartialRangeRequestWithUnknownTotalContentLength(KeyValuePair<long, long?> requestedRange, Stream responseStream)
|
|
|
|
|
{
|
|
|
|
|
// Read the entire stream so that we can determine the length
|
2012-08-22 02:50:59 +00:00
|
|
|
|
byte[] bytes = await ReadBytes(SourceStream, 0, null).ConfigureAwait(false);
|
2012-08-10 13:07:58 +00:00
|
|
|
|
|
|
|
|
|
long totalContentLength = bytes.LongLength;
|
|
|
|
|
|
|
|
|
|
long rangeStart = requestedRange.Key;
|
|
|
|
|
long rangeEnd = requestedRange.Value ?? (totalContentLength - 1);
|
|
|
|
|
long rangeLength = 1 + rangeEnd - rangeStart;
|
|
|
|
|
|
|
|
|
|
// Content-Length is the length of what we're serving, not the original content
|
|
|
|
|
HttpListenerContext.Response.ContentLength64 = rangeLength;
|
|
|
|
|
HttpListenerContext.Response.Headers["Content-Range"] = string.Format("bytes {0}-{1}/{2}", rangeStart, rangeEnd, totalContentLength);
|
|
|
|
|
|
2012-08-22 02:50:59 +00:00
|
|
|
|
await responseStream.WriteAsync(bytes, Convert.ToInt32(rangeStart), Convert.ToInt32(rangeLength)).ConfigureAwait(false);
|
2012-08-10 13:07:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Serves a partial range request where the total content length is already known
|
|
|
|
|
/// </summary>
|
|
|
|
|
private async Task ServePartialRangeRequestWithKnownTotalContentLength(KeyValuePair<long, long?> requestedRange, Stream responseStream)
|
|
|
|
|
{
|
|
|
|
|
long totalContentLength = TotalContentLength.Value;
|
|
|
|
|
long rangeStart = requestedRange.Key;
|
|
|
|
|
long rangeEnd = requestedRange.Value ?? (totalContentLength - 1);
|
|
|
|
|
long rangeLength = 1 + rangeEnd - rangeStart;
|
|
|
|
|
|
|
|
|
|
// Only read the bytes we need
|
2012-08-22 02:50:59 +00:00
|
|
|
|
byte[] bytes = await ReadBytes(SourceStream, Convert.ToInt32(rangeStart), Convert.ToInt32(rangeLength)).ConfigureAwait(false);
|
2012-08-10 13:07:58 +00:00
|
|
|
|
|
|
|
|
|
// Content-Length is the length of what we're serving, not the original content
|
|
|
|
|
HttpListenerContext.Response.ContentLength64 = rangeLength;
|
|
|
|
|
|
|
|
|
|
HttpListenerContext.Response.Headers["Content-Range"] = string.Format("bytes {0}-{1}/{2}", rangeStart, rangeEnd, totalContentLength);
|
|
|
|
|
|
2012-08-22 02:50:59 +00:00
|
|
|
|
await responseStream.WriteAsync(bytes, 0, Convert.ToInt32(rangeLength)).ConfigureAwait(false);
|
2012-08-10 13:07:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Reads bytes from a stream
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="input">The input stream</param>
|
|
|
|
|
/// <param name="start">The starting position</param>
|
|
|
|
|
/// <param name="count">The number of bytes to read, or null to read to the end.</param>
|
|
|
|
|
private async Task<byte[]> ReadBytes(Stream input, int start, int? count)
|
|
|
|
|
{
|
|
|
|
|
if (start > 0)
|
|
|
|
|
{
|
|
|
|
|
input.Position = start;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (count == null)
|
|
|
|
|
{
|
|
|
|
|
byte[] buffer = new byte[16 * 1024];
|
|
|
|
|
|
|
|
|
|
using (MemoryStream ms = new MemoryStream())
|
|
|
|
|
{
|
|
|
|
|
int read;
|
2012-08-22 02:50:59 +00:00
|
|
|
|
while ((read = await input.ReadAsync(buffer, 0, buffer.Length).ConfigureAwait(false)) > 0)
|
2012-08-10 13:07:58 +00:00
|
|
|
|
{
|
2012-08-22 02:50:59 +00:00
|
|
|
|
await ms.WriteAsync(buffer, 0, read).ConfigureAwait(false);
|
2012-08-10 13:07:58 +00:00
|
|
|
|
}
|
|
|
|
|
return ms.ToArray();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
byte[] buffer = new byte[count.Value];
|
|
|
|
|
|
|
|
|
|
using (MemoryStream ms = new MemoryStream())
|
|
|
|
|
{
|
2012-08-22 02:50:59 +00:00
|
|
|
|
int read = await input.ReadAsync(buffer, 0, buffer.Length).ConfigureAwait(false);
|
2012-08-10 13:07:58 +00:00
|
|
|
|
|
2012-08-22 02:50:59 +00:00
|
|
|
|
await ms.WriteAsync(buffer, 0, read).ConfigureAwait(false);
|
2012-08-10 13:07:58 +00:00
|
|
|
|
|
|
|
|
|
return ms.ToArray();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|