#41 - Support Http Head requests

This commit is contained in:
LukePulverenti 2013-03-12 23:57:54 -04:00
parent f61a812c0a
commit 17d01636ae
6 changed files with 79 additions and 26 deletions

View File

@ -15,6 +15,12 @@ namespace MediaBrowser.Api.Playback.Progressive
[Route("/Audio/{Id}/stream.flac", "GET")]
[Route("/Audio/{Id}/stream.ogg", "GET")]
[Route("/Audio/{Id}/stream", "GET")]
[Route("/Audio/{Id}/stream.mp3", "HEAD")]
[Route("/Audio/{Id}/stream.wma", "HEAD")]
[Route("/Audio/{Id}/stream.aac", "HEAD")]
[Route("/Audio/{Id}/stream.flac", "HEAD")]
[Route("/Audio/{Id}/stream.ogg", "HEAD")]
[Route("/Audio/{Id}/stream", "HEAD")]
[ServiceStack.ServiceHost.Api(Description = "Gets an audio stream")]
public class GetAudioStream : StreamRequest
{
@ -38,7 +44,17 @@ namespace MediaBrowser.Api.Playback.Progressive
/// <returns>System.Object.</returns>
public object Get(GetAudioStream request)
{
return ProcessRequest(request);
return ProcessRequest(request, false);
}
/// <summary>
/// Gets the specified request.
/// </summary>
/// <param name="request">The request.</param>
/// <returns>System.Object.</returns>
public object Head(GetAudioStream request)
{
return ProcessRequest(request, true);
}
/// <summary>

View File

@ -94,38 +94,46 @@ namespace MediaBrowser.Api.Playback.Progressive
/// Processes the request.
/// </summary>
/// <param name="request">The request.</param>
/// <param name="isHeadRequest">if set to <c>true</c> [is head request].</param>
/// <returns>Task.</returns>
protected object ProcessRequest(StreamRequest request)
protected object ProcessRequest(StreamRequest request, bool isHeadRequest)
{
var state = GetState(request);
if (request.Static)
{
return ToStaticFileResult(state.Item.Path);
return ToStaticFileResult(state.Item.Path, isHeadRequest);
}
var outputPath = GetOutputFilePath(state);
if (File.Exists(outputPath) && !ApiEntryPoint.Instance.HasActiveTranscodingJob(outputPath, TranscodingJobType.Progressive))
{
return ToStaticFileResult(outputPath);
return ToStaticFileResult(outputPath, isHeadRequest);
}
return GetStreamResult(state).Result;
return GetStreamResult(state, isHeadRequest).Result;
}
/// <summary>
/// Gets the stream result.
/// </summary>
/// <param name="state">The state.</param>
/// <param name="isHeadRequest">if set to <c>true</c> [is head request].</param>
/// <returns>Task{System.Object}.</returns>
private async Task<ProgressiveStreamWriter> GetStreamResult(StreamState state)
private async Task<ProgressiveStreamWriter> GetStreamResult(StreamState state, bool isHeadRequest)
{
// Use the command line args with a dummy playlist path
var outputPath = GetOutputFilePath(state);
Response.ContentType = MimeTypes.GetMimeType(outputPath);
// Headers only
if (isHeadRequest)
{
return null;
}
if (!File.Exists(outputPath))
{
await StartFFMpeg(state, outputPath).ConfigureAwait(false);

View File

@ -23,6 +23,18 @@ namespace MediaBrowser.Api.Playback.Progressive
[Route("/Videos/{Id}/stream.avi", "GET")]
[Route("/Videos/{Id}/stream.m2ts", "GET")]
[Route("/Videos/{Id}/stream", "GET")]
[Route("/Videos/{Id}/stream.ts", "HEAD")]
[Route("/Videos/{Id}/stream.webm", "HEAD")]
[Route("/Videos/{Id}/stream.asf", "HEAD")]
[Route("/Videos/{Id}/stream.wmv", "HEAD")]
[Route("/Videos/{Id}/stream.ogv", "HEAD")]
[Route("/Videos/{Id}/stream.mp4", "HEAD")]
[Route("/Videos/{Id}/stream.m4v", "HEAD")]
[Route("/Videos/{Id}/stream.mkv", "HEAD")]
[Route("/Videos/{Id}/stream.mpeg", "HEAD")]
[Route("/Videos/{Id}/stream.avi", "HEAD")]
[Route("/Videos/{Id}/stream.m2ts", "HEAD")]
[Route("/Videos/{Id}/stream", "HEAD")]
[ServiceStack.ServiceHost.Api(Description = "Gets a video stream")]
public class GetVideoStream : VideoStreamRequest
{
@ -46,9 +58,14 @@ namespace MediaBrowser.Api.Playback.Progressive
/// <returns>System.Object.</returns>
public object Get(GetVideoStream request)
{
return ProcessRequest(request);
return ProcessRequest(request, false);
}
public object Head(GetVideoStream request)
{
return ProcessRequest(request, true);
}
/// <summary>
/// Gets the command line arguments.
/// </summary>

View File

@ -132,14 +132,15 @@ namespace MediaBrowser.Server.Implementations.HttpServer
return factoryFn();
}
/// <summary>
/// To the static file result.
/// </summary>
/// <param name="path">The path.</param>
/// <param name="headersOnly">if set to <c>true</c> [headers only].</param>
/// <returns>System.Object.</returns>
/// <exception cref="System.ArgumentNullException">path</exception>
protected object ToStaticFileResult(string path)
protected object ToStaticFileResult(string path, bool headersOnly = false)
{
if (string.IsNullOrEmpty(path))
{
@ -150,7 +151,7 @@ namespace MediaBrowser.Server.Implementations.HttpServer
var cacheKey = path + dateModified.Ticks;
return ToStaticResult(cacheKey.GetMD5(), dateModified, null, MimeTypes.GetMimeType(path), () => Task.FromResult(GetFileStream(path)));
return ToStaticResult(cacheKey.GetMD5(), dateModified, null, MimeTypes.GetMimeType(path), () => Task.FromResult(GetFileStream(path)), headersOnly);
}
/// <summary>
@ -162,7 +163,7 @@ namespace MediaBrowser.Server.Implementations.HttpServer
{
return new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, StreamDefaults.DefaultFileStreamBufferSize, FileOptions.Asynchronous);
}
/// <summary>
/// To the static result.
/// </summary>
@ -171,9 +172,10 @@ namespace MediaBrowser.Server.Implementations.HttpServer
/// <param name="cacheDuration">Duration of the cache.</param>
/// <param name="contentType">Type of the content.</param>
/// <param name="factoryFn">The factory fn.</param>
/// <param name="headersOnly">if set to <c>true</c> [headers only].</param>
/// <returns>System.Object.</returns>
/// <exception cref="System.ArgumentNullException">cacheKey</exception>
protected object ToStaticResult(Guid cacheKey, DateTime? lastDateModified, TimeSpan? cacheDuration, string contentType, Func<Task<Stream>> factoryFn)
protected object ToStaticResult(Guid cacheKey, DateTime? lastDateModified, TimeSpan? cacheDuration, string contentType, Func<Task<Stream>> factoryFn, bool headersOnly = false)
{
if (cacheKey == Guid.Empty)
{
@ -203,7 +205,7 @@ namespace MediaBrowser.Server.Implementations.HttpServer
Response.AddHeader("Vary", "Accept-Encoding");
}
return ToStaticResult(contentType, factoryFn, compress).Result;
return ToStaticResult(contentType, factoryFn, compress, headersOnly).Result;
}
/// <summary>
@ -249,8 +251,9 @@ namespace MediaBrowser.Server.Implementations.HttpServer
/// <param name="contentType">Type of the content.</param>
/// <param name="factoryFn">The factory fn.</param>
/// <param name="compress">if set to <c>true</c> [compress].</param>
/// <param name="headersOnly">if set to <c>true</c> [headers only].</param>
/// <returns>System.Object.</returns>
private async Task<object> ToStaticResult(string contentType, Func<Task<Stream>> factoryFn, bool compress)
private async Task<object> ToStaticResult(string contentType, Func<Task<Stream>> factoryFn, bool compress, bool headersOnly = false)
{
if (!compress || string.IsNullOrEmpty(RequestContext.CompressionType))
{
@ -263,7 +266,7 @@ namespace MediaBrowser.Server.Implementations.HttpServer
if (IsRangeRequest)
{
return new RangeRequestWriter(Request.Headers, httpListenerResponse, stream);
return new RangeRequestWriter(Request.Headers, httpListenerResponse, stream, headersOnly);
}
httpListenerResponse.ContentLength64 = stream.Length;

View File

@ -15,9 +15,10 @@ namespace MediaBrowser.Server.Implementations.HttpServer
/// Gets or sets the source stream.
/// </summary>
/// <value>The source stream.</value>
public Stream SourceStream { get; set; }
public HttpListenerResponse Response { get; set; }
public NameValueCollection RequestHeaders { get; set; }
private Stream SourceStream { get; set; }
private HttpListenerResponse Response { get; set; }
private NameValueCollection RequestHeaders { get; set; }
private bool IsHeadRequest { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="StreamWriter" /> class.
@ -25,11 +26,13 @@ namespace MediaBrowser.Server.Implementations.HttpServer
/// <param name="requestHeaders">The request headers.</param>
/// <param name="response">The response.</param>
/// <param name="source">The source.</param>
public RangeRequestWriter(NameValueCollection requestHeaders, HttpListenerResponse response, Stream source)
/// <param name="isHeadRequest">if set to <c>true</c> [is head request].</param>
public RangeRequestWriter(NameValueCollection requestHeaders, HttpListenerResponse response, Stream source, bool isHeadRequest)
{
RequestHeaders = requestHeaders;
Response = response;
SourceStream = source;
IsHeadRequest = isHeadRequest;
}
/// <summary>
@ -132,6 +135,12 @@ namespace MediaBrowser.Server.Implementations.HttpServer
Response.ContentLength64 = rangeLength;
Response.Headers["Content-Range"] = string.Format("bytes {0}-{1}/{2}", rangeStart, rangeEnd, totalContentLength);
// Headers only
if (IsHeadRequest)
{
return Task.FromResult(true);
}
if (rangeStart > 0)
{
sourceStream.Position = rangeStart;
@ -157,6 +166,12 @@ namespace MediaBrowser.Server.Implementations.HttpServer
Response.ContentLength64 = rangeLength;
Response.Headers["Content-Range"] = string.Format("bytes {0}-{1}/{2}", rangeStart, rangeEnd, totalContentLength);
// Headers only
if (IsHeadRequest)
{
return;
}
sourceStream.Position = rangeStart;
// Fast track to just copy the stream to the end

View File

@ -108,6 +108,7 @@
var item = items[0];
// Account for screen rotation. Use the larger dimension as the width.
var screenWidth = Math.max(screen.height, screen.width);
var screenHeight = Math.min(screen.height, screen.width);
@ -152,13 +153,6 @@
html += '<source type="application/x-mpegURL" src="' + hlsVideoUrl + '" />';
html += '<source type="video/ogg" src="' + ogvVideoUrl + '" />';
//html += '<object type="application/x-shockwave-flash" data="http://releases.flowplayer.org/swf/flowplayer-3.2.1.swf" width="640" height="360">';
//html += '<param name="movie" value="http://releases.flowplayer.org/swf/flowplayer-3.2.1.swf" />';
//html += '<param name="allowFullScreen" value="true" />';
//html += '<param name="wmode" value="transparent" />';
////html += '<param name="flashVars" value="config={'playlist':['http%3A%2F%2Fsandbox.thewikies.com%2Fvfe-generator%2Fimages%2Fbig-buck-bunny_poster.jpg',{'url':'http%3A%2F%2Fclips.vorwaerts-gmbh.de%2Fbig_buck_bunny.mp4','autoPlay':false}]}" />';
//html += '</object>';
html += '</video';
var nowPlayingBar = $('#nowPlayingBar').show();