mirror of
https://github.com/jellyfin/jellyfin.git
synced 2026-03-06 18:26:33 +00:00
#41 - Support Http Head requests
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user