From a96ec4f169f82d40595fbb0d8f40ccc90c1074e9 Mon Sep 17 00:00:00 2001 From: LukePulverenti Date: Sun, 10 Mar 2013 23:17:36 -0400 Subject: [PATCH] better source stream disposal --- .../HttpServer/RangeRequestWriter.cs | 41 +++++++++++-------- 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/MediaBrowser.Server.Implementations/HttpServer/RangeRequestWriter.cs b/MediaBrowser.Server.Implementations/HttpServer/RangeRequestWriter.cs index 77e14362d..e5c4c9796 100644 --- a/MediaBrowser.Server.Implementations/HttpServer/RangeRequestWriter.cs +++ b/MediaBrowser.Server.Implementations/HttpServer/RangeRequestWriter.cs @@ -94,31 +94,35 @@ namespace MediaBrowser.Server.Implementations.HttpServer /// /// The response stream. /// Task. - private Task WriteToAsync(Stream responseStream) + private async Task WriteToAsync(Stream responseStream) { - var requestedRange = RequestedRanges.First(); - - var totalLength = SourceStream.Length; - - // If the requested range is "0-", we can optimize by just doing a stream copy - if (!requestedRange.Value.HasValue) + using (var source = SourceStream) { - return ServeCompleteRangeRequest(requestedRange, responseStream, totalLength); - } + var requestedRange = RequestedRanges.First(); - // This will have to buffer a portion of the content into memory - return ServePartialRangeRequest(requestedRange.Key, requestedRange.Value.Value, responseStream, totalLength); + var totalLength = SourceStream.Length; + + // 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); + } + + // This will have to buffer a portion of the content into memory + await ServePartialRangeRequest(source, requestedRange.Key, requestedRange.Value.Value, responseStream, totalLength).ConfigureAwait(false); + } } /// /// Handles a range request of "bytes=0-" /// This will serve the complete content and add the content-range header /// + /// The source stream. /// The requested range. /// The response stream. /// Total length of the content. /// Task. - private Task ServeCompleteRangeRequest(KeyValuePair requestedRange, Stream responseStream, long totalContentLength) + private Task ServeCompleteRangeRequest(Stream sourceStream, KeyValuePair requestedRange, Stream responseStream, long totalContentLength) { var rangeStart = requestedRange.Key; var rangeEnd = totalContentLength - 1; @@ -130,21 +134,22 @@ namespace MediaBrowser.Server.Implementations.HttpServer if (rangeStart > 0) { - SourceStream.Position = rangeStart; + sourceStream.Position = rangeStart; } - return SourceStream.CopyToAsync(responseStream); + return sourceStream.CopyToAsync(responseStream); } /// /// Serves a partial range request /// + /// The source stream. /// The range start. /// The range end. /// The response stream. /// Total length of the content. /// Task. - private async Task ServePartialRangeRequest(long rangeStart, long rangeEnd, Stream responseStream, long totalContentLength) + private async Task ServePartialRangeRequest(Stream sourceStream, long rangeStart, long rangeEnd, Stream responseStream, long totalContentLength) { var rangeLength = 1 + rangeEnd - rangeStart; @@ -152,18 +157,18 @@ namespace MediaBrowser.Server.Implementations.HttpServer Response.ContentLength64 = rangeLength; Response.Headers["Content-Range"] = string.Format("bytes {0}-{1}/{2}", rangeStart, rangeEnd, totalContentLength); - SourceStream.Position = rangeStart; + sourceStream.Position = rangeStart; // Fast track to just copy the stream to the end if (rangeEnd == totalContentLength - 1) { - await SourceStream.CopyToAsync(responseStream).ConfigureAwait(false); + await sourceStream.CopyToAsync(responseStream).ConfigureAwait(false); } else { // Read the bytes we need var buffer = new byte[Convert.ToInt32(rangeLength)]; - await SourceStream.ReadAsync(buffer, 0, buffer.Length).ConfigureAwait(false); + await sourceStream.ReadAsync(buffer, 0, buffer.Length).ConfigureAwait(false); await responseStream.WriteAsync(buffer, 0, Convert.ToInt32(rangeLength)).ConfigureAwait(false); }