From 17d01636ae8a8054dc1fc043315f4fb2f4d53187 Mon Sep 17 00:00:00 2001 From: LukePulverenti Date: Tue, 12 Mar 2013 23:57:54 -0400 Subject: #41 - Support Http Head requests --- .../HttpServer/BaseRestService.cs | 19 ++++++++++-------- .../HttpServer/RangeRequestWriter.cs | 23 ++++++++++++++++++---- 2 files changed, 30 insertions(+), 12 deletions(-) (limited to 'MediaBrowser.Server.Implementations/HttpServer') diff --git a/MediaBrowser.Server.Implementations/HttpServer/BaseRestService.cs b/MediaBrowser.Server.Implementations/HttpServer/BaseRestService.cs index 0445cd863..baa5b0888 100644 --- a/MediaBrowser.Server.Implementations/HttpServer/BaseRestService.cs +++ b/MediaBrowser.Server.Implementations/HttpServer/BaseRestService.cs @@ -132,14 +132,15 @@ namespace MediaBrowser.Server.Implementations.HttpServer return factoryFn(); } - + /// /// To the static file result. /// /// The path. + /// if set to true [headers only]. /// System.Object. /// path - 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); } /// @@ -162,7 +163,7 @@ namespace MediaBrowser.Server.Implementations.HttpServer { return new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, StreamDefaults.DefaultFileStreamBufferSize, FileOptions.Asynchronous); } - + /// /// To the static result. /// @@ -171,9 +172,10 @@ namespace MediaBrowser.Server.Implementations.HttpServer /// Duration of the cache. /// Type of the content. /// The factory fn. + /// if set to true [headers only]. /// System.Object. /// cacheKey - protected object ToStaticResult(Guid cacheKey, DateTime? lastDateModified, TimeSpan? cacheDuration, string contentType, Func> factoryFn) + protected object ToStaticResult(Guid cacheKey, DateTime? lastDateModified, TimeSpan? cacheDuration, string contentType, Func> 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; } /// @@ -249,8 +251,9 @@ namespace MediaBrowser.Server.Implementations.HttpServer /// Type of the content. /// The factory fn. /// if set to true [compress]. + /// if set to true [headers only]. /// System.Object. - private async Task ToStaticResult(string contentType, Func> factoryFn, bool compress) + private async Task ToStaticResult(string contentType, Func> 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; diff --git a/MediaBrowser.Server.Implementations/HttpServer/RangeRequestWriter.cs b/MediaBrowser.Server.Implementations/HttpServer/RangeRequestWriter.cs index e5c4c9796..9981e5fe1 100644 --- a/MediaBrowser.Server.Implementations/HttpServer/RangeRequestWriter.cs +++ b/MediaBrowser.Server.Implementations/HttpServer/RangeRequestWriter.cs @@ -15,9 +15,10 @@ namespace MediaBrowser.Server.Implementations.HttpServer /// Gets or sets the source stream. /// /// The source stream. - 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; } /// /// Initializes a new instance of the class. @@ -25,11 +26,13 @@ namespace MediaBrowser.Server.Implementations.HttpServer /// The request headers. /// The response. /// The source. - public RangeRequestWriter(NameValueCollection requestHeaders, HttpListenerResponse response, Stream source) + /// if set to true [is head request]. + public RangeRequestWriter(NameValueCollection requestHeaders, HttpListenerResponse response, Stream source, bool isHeadRequest) { RequestHeaders = requestHeaders; Response = response; SourceStream = source; + IsHeadRequest = isHeadRequest; } /// @@ -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 -- cgit v1.2.3